blob: e1a33df60802830e39718085f53bac3a226b17cd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010025#define NAMESPACE_CHAR (char_u *)"abglstvw"
26
Bram Moolenaar532c7802005-01-27 14:44:31 +000027/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000028 * When recursively copying lists and dicts we need to remember which ones we
29 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000030 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000031 */
32static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020033
Bram Moolenaar071d4272004-06-13 20:20:40 +000034/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000035 * Info used by a ":for" loop.
36 */
Bram Moolenaar33570922005-01-25 22:26:29 +000037typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000038{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010039 int fi_semicolon; // TRUE if ending in '; var]'
40 int fi_varcount; // nr of variables in the list
41 listwatch_T fi_lw; // keep an eye on the item used.
42 list_T *fi_list; // list being used
43 int fi_bi; // index of blob
44 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000045} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000046
Bram Moolenaar48e697e2016-01-23 22:17:30 +010047static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
53static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020054static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000055
Bram Moolenaar48e697e2016-01-23 22:17:30 +010056static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020057static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020058
Bram Moolenaare21c1582019-03-02 11:57:09 +010059/*
60 * Return "n1" divided by "n2", taking care of dividing by zero.
61 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020062 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010063num_divide(varnumber_T n1, varnumber_T n2)
64{
65 varnumber_T result;
66
67 if (n2 == 0) // give an error message?
68 {
69 if (n1 == 0)
70 result = VARNUM_MIN; // similar to NaN
71 else if (n1 < 0)
72 result = -VARNUM_MAX;
73 else
74 result = VARNUM_MAX;
75 }
76 else
77 result = n1 / n2;
78
79 return result;
80}
81
82/*
83 * Return "n1" modulus "n2", taking care of dividing by zero.
84 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010086num_modulus(varnumber_T n1, varnumber_T n2)
87{
88 // Give an error when n2 is 0?
89 return (n2 == 0) ? 0 : (n1 % n2);
90}
91
Bram Moolenaara1fa8922017-01-12 20:06:33 +010092#if defined(EBCDIC) || defined(PROTO)
93/*
94 * Compare struct fst by function name.
95 */
96 static int
97compare_func_name(const void *s1, const void *s2)
98{
99 struct fst *p1 = (struct fst *)s1;
100 struct fst *p2 = (struct fst *)s2;
101
102 return STRCMP(p1->f_name, p2->f_name);
103}
104
105/*
106 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100107 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100108 * On machines using EBCDIC we have to sort it.
109 */
110 static void
111sortFunctions(void)
112{
113 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
114
115 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
116}
117#endif
118
Bram Moolenaar33570922005-01-25 22:26:29 +0000119/*
120 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000121 */
122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100123eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000124{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200125 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200126 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000127
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200128#ifdef EBCDIC
129 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100130 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200131 */
132 sortFunctions();
133#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000134}
135
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000136#if defined(EXITFREE) || defined(PROTO)
137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100138eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000139{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200140 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100141 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200142 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000143
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200144 // autoloaded script names
145 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000146
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200147 // unreferenced lists and dicts
148 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200149
150 // functions not garbage collected
151 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000152}
153#endif
154
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155/*
156 * Top level evaluation function, returning a boolean.
157 * Sets "error" to TRUE if there was an error.
158 * Return TRUE or FALSE.
159 */
160 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100161eval_to_bool(
162 char_u *arg,
163 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200164 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100165 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166{
Bram Moolenaar33570922005-01-25 22:26:29 +0000167 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200168 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
170 if (skip)
171 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200172 if (eval0(arg, &tv, eap, skip ? NULL : &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 else
175 {
176 *error = FALSE;
177 if (!skip)
178 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
185
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200186 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187}
188
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100189/*
190 * Call eval1() and give an error message if not done at a lower level.
191 */
192 static int
193eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
194{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100195 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100196 int ret;
197 int did_emsg_before = did_emsg;
198 int called_emsg_before = called_emsg;
199
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200200 ret = eval1(arg, rettv, evaluate ? &EVALARG_EVALUATE : NULL);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201 if (ret == FAIL)
202 {
203 // Report the invalid expression unless the expression evaluation has
204 // been cancelled due to an aborting error, an interrupt, or an
205 // exception, or we already gave a more specific error.
206 // Also check called_emsg for when using assert_fails().
207 if (!aborting() && did_emsg == did_emsg_before
208 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100209 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100210 }
211 return ret;
212}
213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200215 * Return whether a typval is a valid expression to pass to eval_expr_typval()
216 * or eval_expr_to_bool(). An empty string returns FALSE;
217 */
218 int
219eval_expr_valid_arg(typval_T *tv)
220{
221 return tv->v_type != VAR_UNKNOWN
222 && (tv->v_type != VAR_STRING
223 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
224}
225
226/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227 * Evaluate an expression, which can be a function, partial or string.
228 * Pass arguments "argv[argc]".
229 * Return the result in "rettv" and OK or FAIL.
230 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200231 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100232eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
233{
234 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100235 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200236 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100237
238 if (expr->v_type == VAR_FUNC)
239 {
240 s = expr->vval.v_string;
241 if (s == NULL || *s == NUL)
242 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200243 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200244 funcexe.evaluate = TRUE;
245 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100246 return FAIL;
247 }
248 else if (expr->v_type == VAR_PARTIAL)
249 {
250 partial_T *partial = expr->vval.v_partial;
251
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200252 if (partial == NULL)
253 return FAIL;
254
Bram Moolenaar822ba242020-05-24 23:00:18 +0200255 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200256 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200258 if (call_def_function(partial->pt_func, argc, argv,
259 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260 return FAIL;
261 }
262 else
263 {
264 s = partial_name(partial);
265 if (s == NULL || *s == NUL)
266 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200267 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 funcexe.evaluate = TRUE;
269 funcexe.partial = partial;
270 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
271 return FAIL;
272 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 }
274 else
275 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100276 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100277 if (s == NULL)
278 return FAIL;
279 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100280 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100281 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100282 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100283 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200284 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100285 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100286 return FAIL;
287 }
288 }
289 return OK;
290}
291
292/*
293 * Like eval_to_bool() but using a typval_T instead of a string.
294 * Works for string, funcref and partial.
295 */
296 int
297eval_expr_to_bool(typval_T *expr, int *error)
298{
299 typval_T rettv;
300 int res;
301
302 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
303 {
304 *error = TRUE;
305 return FALSE;
306 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100307 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 clear_tv(&rettv);
309 return res;
310}
311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312/*
313 * Top level evaluation function, returning a string. If "skip" is TRUE,
314 * only parsing to "nextcmd" is done, without reporting errors. Return
315 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
316 */
317 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100318eval_to_string_skip(
319 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200320 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100321 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322{
Bram Moolenaar33570922005-01-25 22:26:29 +0000323 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 char_u *retval;
325
326 if (skip)
327 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200328 if (eval0(arg, &tv, eap, skip ? NULL : &EVALARG_EVALUATE) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 retval = NULL;
330 else
331 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100332 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000333 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 }
335 if (skip)
336 --emsg_skip;
337
338 return retval;
339}
340
341/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000342 * Skip over an expression at "*pp".
343 * Return FAIL for an error, OK otherwise.
344 */
345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100346skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000347{
Bram Moolenaar33570922005-01-25 22:26:29 +0000348 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000349
350 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200351 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000352}
353
354/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200355 * Skip over an expression at "*pp".
356 * If in Vim9 script and line breaks are encountered, the lines are
357 * concatenated. "evalarg->eval_tofree" will be set accordingly.
358 * Return FAIL for an error, OK otherwise.
359 */
360 int
361skip_expr_concatenate(char_u **start, char_u **end, evalarg_T *evalarg)
362{
363 typval_T rettv;
364 int res;
365 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
366 garray_T *gap = &evalarg->eval_ga;
367 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
368
369 if (vim9script && evalarg->eval_cookie != NULL)
370 {
371 ga_init2(gap, sizeof(char_u *), 10);
372 if (ga_grow(gap, 1) == OK)
373 // leave room for "start"
374 ++gap->ga_len;
375 }
376
377 // Don't evaluate the expression.
378 if (evalarg != NULL)
379 evalarg->eval_flags &= ~EVAL_EVALUATE;
380 *end = skipwhite(*end);
381 res = eval1(end, &rettv, evalarg);
382 if (evalarg != NULL)
383 evalarg->eval_flags = save_flags;
384
385 if (vim9script && evalarg->eval_cookie != NULL
386 && evalarg->eval_ga.ga_len > 1)
387 {
388 char_u *p;
389 size_t endoff = STRLEN(*end);
390
391 // Line breaks encountered, concatenate all the lines.
392 *((char_u **)gap->ga_data) = *start;
393 p = ga_concat_strings(gap, "");
394 *((char_u **)gap->ga_data) = NULL;
395 ga_clear_strings(gap);
396 gap->ga_itemsize = 0;
397 if (p == NULL)
398 return FAIL;
399 *start = p;
400 vim_free(evalarg->eval_tofree);
401 evalarg->eval_tofree = p;
402 // Compute "end" relative to the end.
403 *end = *start + STRLEN(*start) - endoff;
404 }
405
406 return res;
407}
408
409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000411 * When "convert" is TRUE convert a List into a sequence of lines and convert
412 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 * Return pointer to allocated memory, or NULL for failure.
414 */
415 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100416eval_to_string(
417 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100418 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419{
Bram Moolenaar33570922005-01-25 22:26:29 +0000420 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000422 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000423#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000424 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200427 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 retval = NULL;
429 else
430 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000431 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000432 {
433 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000434 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200435 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200436 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200437 if (tv.vval.v_list->lv_len > 0)
438 ga_append(&ga, NL);
439 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000440 ga_append(&ga, NUL);
441 retval = (char_u *)ga.ga_data;
442 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000443#ifdef FEAT_FLOAT
444 else if (convert && tv.v_type == VAR_FLOAT)
445 {
446 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
447 retval = vim_strsave(numbuf);
448 }
449#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000450 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100451 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000452 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 }
454
455 return retval;
456}
457
458/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000459 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200460 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 */
462 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100463eval_to_string_safe(
464 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100465 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466{
467 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200468 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200470 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000471 if (use_sandbox)
472 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200473 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200474 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000475 if (use_sandbox)
476 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200477 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200478 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 return retval;
480}
481
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482/*
483 * Top level evaluation function, returning a number.
484 * Evaluates "expr" silently.
485 * Returns -1 for an error.
486 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200487 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100488eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
Bram Moolenaar33570922005-01-25 22:26:29 +0000490 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200491 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000492 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
494 ++emsg_off;
495
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200496 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 retval = -1;
498 else
499 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100500 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000501 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503 --emsg_off;
504
505 return retval;
506}
507
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000508/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000509 * Top level evaluation function.
510 * Returns an allocated typval_T with the result.
511 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000512 */
513 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200514eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000515{
516 typval_T *tv;
517
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200518 tv = ALLOC_ONE(typval_T);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200519 if (tv != NULL && eval0(arg, tv, eap, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100520 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000521
522 return tv;
523}
524
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100526 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200527 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
528 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000529 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100532call_vim_function(
533 char_u *func,
534 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200535 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200536 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000538 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200539 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100541 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200542 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200543 funcexe.firstline = curwin->w_cursor.lnum;
544 funcexe.lastline = curwin->w_cursor.lnum;
545 funcexe.evaluate = TRUE;
546 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000547 if (ret == FAIL)
548 clear_tv(rettv);
549
550 return ret;
551}
552
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100553/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100554 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100555 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200556 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
557 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100558 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200559 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100560call_func_retnr(
561 char_u *func,
562 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200563 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100564{
565 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200566 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100567
Bram Moolenaarded27a12018-08-01 19:06:03 +0200568 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100569 return -1;
570
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100571 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100572 clear_tv(&rettv);
573 return retval;
574}
575
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000576/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100577 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000578 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200579 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
580 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000581 */
582 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100583call_func_retstr(
584 char_u *func,
585 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200586 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000587{
588 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000589 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000590
Bram Moolenaarded27a12018-08-01 19:06:03 +0200591 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000592 return NULL;
593
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100594 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000595 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 return retval;
597}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000598
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000599/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100600 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200601 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
602 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000603 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000604 */
605 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100606call_func_retlist(
607 char_u *func,
608 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200609 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000610{
611 typval_T rettv;
612
Bram Moolenaarded27a12018-08-01 19:06:03 +0200613 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000614 return NULL;
615
616 if (rettv.v_type != VAR_LIST)
617 {
618 clear_tv(&rettv);
619 return NULL;
620 }
621
622 return rettv.vval.v_list;
623}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625#ifdef FEAT_FOLDING
626/*
627 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
628 * it in "*cp". Doesn't give error messages.
629 */
630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
Bram Moolenaar33570922005-01-25 22:26:29 +0000633 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200634 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000636 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
637 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000640 if (use_sandbox)
641 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200642 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200644 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 retval = 0;
646 else
647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100648 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000649 if (tv.v_type == VAR_NUMBER)
650 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000651 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 retval = 0;
653 else
654 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100655 // If the result is a string, check if there is a non-digit before
656 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000657 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if (!VIM_ISDIGIT(*s) && *s != '-')
659 *cp = *s++;
660 retval = atol((char *)s);
661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000662 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000665 if (use_sandbox)
666 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200667 --textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200669 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670}
671#endif
672
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000674 * Get an lval: variable, Dict item or List item that can be assigned a value
675 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
676 * "name.key", "name.key[expr]" etc.
677 * Indexing only works if "name" is an existing List or Dictionary.
678 * "name" points to the start of the name.
679 * If "rettv" is not NULL it points to the value to be assigned.
680 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
681 * wrong; must end in space or cmd separator.
682 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100683 * flags:
684 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100685 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100686 * GLV_NO_AUTOLOAD: do not use script autoloading
687 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000688 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000689 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000690 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000691 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200692 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100693get_lval(
694 char_u *name,
695 typval_T *rettv,
696 lval_T *lp,
697 int unlet,
698 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100699 int flags, // GLV_ values
700 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000701{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000702 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000703 char_u *expr_start, *expr_end;
704 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000705 dictitem_T *v;
706 typval_T var1;
707 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000708 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000709 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000710 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000711 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000712 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100713 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000714
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100715 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200716 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000717
718 if (skip)
719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100720 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000721 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000722 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000723 }
724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100725 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000726 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000728 if (expr_start != NULL)
729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100730 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100731 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000732 && *p != '[' && *p != '.')
733 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100734 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000735 return NULL;
736 }
737
738 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
739 if (lp->ll_exp_name == NULL)
740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100741 // Report an invalid expression in braces, unless the
742 // expression evaluation has been cancelled due to an
743 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000744 if (!aborting() && !quiet)
745 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000746 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100747 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000748 return NULL;
749 }
750 }
751 lp->ll_name = lp->ll_exp_name;
752 }
753 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000755 lp->ll_name = name;
756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
758 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100759 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 char_u *tp = skipwhite(p + 1);
761
762 // parse the type after the name
763 lp->ll_type = parse_type(&tp, &si->sn_type_list);
764 lp->ll_name_end = tp;
765 }
766 }
767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100768 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000769 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
770 return p;
771
772 cc = *p;
773 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100774 // Only pass &ht when we would write to the variable, it prevents autoload
775 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100776 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
777 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000778 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100779 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000780 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000781 if (v == NULL)
782 return NULL;
783
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000784 /*
785 * Loop until no more [idx] or .key is following.
786 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000787 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100788 var1.v_type = VAR_UNKNOWN;
789 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000790 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000792 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
793 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100794 && lp->ll_tv->vval.v_dict != NULL)
795 && !(lp->ll_tv->v_type == VAR_BLOB
796 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000797 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000798 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100799 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000800 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000801 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000802 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000804 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000807 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000808
Bram Moolenaar8c711452005-01-14 21:53:12 +0000809 len = -1;
810 if (*p == '.')
811 {
812 key = p + 1;
813 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
814 ;
815 if (len == 0)
816 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000817 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100818 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000820 }
821 p = key + len;
822 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000823 else
824 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100825 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000826 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000827 if (*p == ':')
828 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000829 else
830 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000831 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200832 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000833 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100834 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000835 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100836 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000837 clear_tv(&var1);
838 return NULL;
839 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000840 }
841
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100842 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000843 if (*p == ':')
844 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000845 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000846 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100848 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100849 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000851 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100852 if (rettv != NULL
853 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100854 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100855 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100856 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000857 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100859 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100860 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000861 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000862 }
863 p = skipwhite(p + 1);
864 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000866 else
867 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200869 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200870 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000871 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100872 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000874 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100875 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000876 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100877 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100878 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000879 clear_tv(&var2);
880 return NULL;
881 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000882 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000884 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000885 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000887
Bram Moolenaar8c711452005-01-14 21:53:12 +0000888 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000889 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100891 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100892 clear_tv(&var1);
893 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000895 }
896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100897 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000898 ++p;
899 }
900
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 {
903 if (len == -1)
904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 // "[key]": get key from "var1"
906 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200907 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000908 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000909 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000910 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000911 }
912 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000913 lp->ll_list = NULL;
914 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000915 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200916
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100917 // When assigning to a scope dictionary check that a function and
918 // variable name is valid (only variable name unless it is l: or
919 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200920 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200921 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200922 int prevval;
923 int wrong;
924
925 if (len != -1)
926 {
927 prevval = key[len];
928 key[len] = NUL;
929 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200930 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100931 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200932 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
933 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200934 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200935 || !valid_varname(key);
936 if (len != -1)
937 key[len] = prevval;
938 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200939 {
940 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200941 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200942 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200943 }
944
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000945 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000946 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100947 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200948 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100949 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200950 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100951 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100952 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200953 return NULL;
954 }
955
Bram Moolenaar31b81602019-02-10 22:14:27 +0100956 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000957 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000958 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100960 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100961 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000963 }
964 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000966 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100968 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 p = NULL;
971 break;
972 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100973 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100974 else if ((flags & GLV_READ_ONLY) == 0
975 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100976 {
977 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200978 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100979 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200980
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100981 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000983 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100984 else if (lp->ll_tv->v_type == VAR_BLOB)
985 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100986 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
987
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100988 /*
989 * Get the number and item for the only or first index of the List.
990 */
991 if (empty1)
992 lp->ll_n1 = 0;
993 else
994 // is number or string
995 lp->ll_n1 = (long)tv_get_number(&var1);
996 clear_tv(&var1);
997
998 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100999 || lp->ll_n1 > bloblen
1000 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001001 {
1002 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001003 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001004 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001005 return NULL;
1006 }
1007 if (lp->ll_range && !lp->ll_empty2)
1008 {
1009 lp->ll_n2 = (long)tv_get_number(&var2);
1010 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001011 if (lp->ll_n2 < 0
1012 || lp->ll_n2 >= bloblen
1013 || lp->ll_n2 < lp->ll_n1)
1014 {
1015 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001016 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001017 return NULL;
1018 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001019 }
1020 lp->ll_blob = lp->ll_tv->vval.v_blob;
1021 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001022 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001023 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 else
1025 {
1026 /*
1027 * Get the number and item for the only or first index of the List.
1028 */
1029 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001031 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001032 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001033 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001034 clear_tv(&var1);
1035
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001036 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 lp->ll_list = lp->ll_tv->vval.v_list;
1038 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1039 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001040 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001041 if (lp->ll_n1 < 0)
1042 {
1043 lp->ll_n1 = 0;
1044 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1045 }
1046 }
1047 if (lp->ll_li == NULL)
1048 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001049 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001050 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001051 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001053 }
1054
1055 /*
1056 * May need to find the item or absolute index for the second
1057 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 * When no index given: "lp->ll_empty2" is TRUE.
1059 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001060 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001063 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001064 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001065 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001066 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001067 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001068 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001070 {
1071 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001072 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001074 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001075 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001076 }
1077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001078 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001079 if (lp->ll_n1 < 0)
1080 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1081 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001082 {
1083 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001084 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001086 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001087 }
1088
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001089 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001091 }
1092
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001093 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001095 return p;
1096}
1097
1098/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001099 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001100 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001101 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001102clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001103{
1104 vim_free(lp->ll_exp_name);
1105 vim_free(lp->ll_newkey);
1106}
1107
1108/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001109 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001110 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001111 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1112 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001114 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001115set_var_lval(
1116 lval_T *lp,
1117 char_u *endp,
1118 typval_T *rettv,
1119 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001121 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122{
1123 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001124 listitem_T *ri;
1125 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001126
1127 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001128 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001129 cc = *endp;
1130 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001131 if (lp->ll_blob != NULL)
1132 {
1133 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001134
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001135 if (op != NULL && *op != '=')
1136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001137 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001138 return;
1139 }
1140
1141 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1142 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001143 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001144
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001145 if (lp->ll_empty2)
1146 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1147
1148 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001149 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001150 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001151 return;
1152 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001153 if (lp->ll_empty2)
1154 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001155
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001156 ir = 0;
1157 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1158 blob_set(lp->ll_blob, il,
1159 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001160 }
1161 else
1162 {
1163 val = (int)tv_get_number_chk(rettv, &error);
1164 if (!error)
1165 {
1166 garray_T *gap = &lp->ll_blob->bv_ga;
1167
1168 // Allow for appending a byte. Setting a byte beyond
1169 // the end is an error otherwise.
1170 if (lp->ll_n1 < gap->ga_len
1171 || (lp->ll_n1 == gap->ga_len
1172 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1173 {
1174 blob_set(lp->ll_blob, lp->ll_n1, val);
1175 if (lp->ll_n1 == gap->ga_len)
1176 ++gap->ga_len;
1177 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001178 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 }
1180 }
1181 }
1182 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001184 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001185
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001187 {
1188 emsg(_(e_cannot_mod));
1189 *endp = cc;
1190 return;
1191 }
1192
Bram Moolenaarff697e62019-02-12 22:28:33 +01001193 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001194 di = NULL;
1195 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1196 &tv, &di, TRUE, FALSE) == OK)
1197 {
1198 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001199 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1200 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001201 && tv_op(&tv, rettv, op) == OK)
1202 set_var(lp->ll_name, &tv, FALSE);
1203 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001204 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001206 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001208 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001210 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001211 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001212 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001213 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001214 else if (lp->ll_range)
1215 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001216 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001217 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001220 {
1221 emsg(_("E996: Cannot lock a range"));
1222 return;
1223 }
1224
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001225 /*
1226 * Check whether any of the list items is locked
1227 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001228 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001229 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001230 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001231 return;
1232 ri = ri->li_next;
1233 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1234 break;
1235 ll_li = ll_li->li_next;
1236 ++ll_n1;
1237 }
1238
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 /*
1240 * Assign the List values to the list items.
1241 */
1242 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001243 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001244 if (op != NULL && *op != '=')
1245 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1246 else
1247 {
1248 clear_tv(&lp->ll_li->li_tv);
1249 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1250 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 ri = ri->li_next;
1252 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1253 break;
1254 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001255 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001256 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001257 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 ri = NULL;
1260 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001261 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001262 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263 lp->ll_li = lp->ll_li->li_next;
1264 ++lp->ll_n1;
1265 }
1266 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001267 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001268 else if (lp->ll_empty2
1269 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001270 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001271 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 }
1273 else
1274 {
1275 /*
1276 * Assign to a List or Dictionary item.
1277 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001279 {
1280 emsg(_("E996: Cannot lock a list or dict"));
1281 return;
1282 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001283 if (lp->ll_newkey != NULL)
1284 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001285 if (op != NULL && *op != '=')
1286 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001287 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001288 return;
1289 }
1290
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001291 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001292 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001293 if (di == NULL)
1294 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001295 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1296 {
1297 vim_free(di);
1298 return;
1299 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001301 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 else if (op != NULL && *op != '=')
1303 {
1304 tv_op(lp->ll_tv, rettv, op);
1305 return;
1306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001307 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001309
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001310 /*
1311 * Assign the value to the variable or list item.
1312 */
1313 if (copy)
1314 copy_tv(rettv, lp->ll_tv);
1315 else
1316 {
1317 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001318 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001319 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001320 }
1321 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322}
1323
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001324/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001325 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1326 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001327 * Returns OK or FAIL.
1328 */
1329 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001330tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001331{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001332 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001333 char_u numbuf[NUMBUFLEN];
1334 char_u *s;
1335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001336 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001337 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001338 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001339 {
1340 switch (tv1->v_type)
1341 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001342 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001343 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001345 case VAR_DICT:
1346 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001347 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001348 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001349 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001350 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001351 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001352 break;
1353
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001354 case VAR_BLOB:
1355 if (*op != '+' || tv2->v_type != VAR_BLOB)
1356 break;
1357 // BLOB += BLOB
1358 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1359 {
1360 blob_T *b1 = tv1->vval.v_blob;
1361 blob_T *b2 = tv2->vval.v_blob;
1362 int i, len = blob_len(b2);
1363 for (i = 0; i < len; i++)
1364 ga_append(&b1->bv_ga, blob_get(b2, i));
1365 }
1366 return OK;
1367
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001368 case VAR_LIST:
1369 if (*op != '+' || tv2->v_type != VAR_LIST)
1370 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001371 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001372 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1373 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1374 return OK;
1375
1376 case VAR_NUMBER:
1377 case VAR_STRING:
1378 if (tv2->v_type == VAR_LIST)
1379 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001380 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001381 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001382 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001383 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001384#ifdef FEAT_FLOAT
1385 if (tv2->v_type == VAR_FLOAT)
1386 {
1387 float_T f = n;
1388
Bram Moolenaarff697e62019-02-12 22:28:33 +01001389 if (*op == '%')
1390 break;
1391 switch (*op)
1392 {
1393 case '+': f += tv2->vval.v_float; break;
1394 case '-': f -= tv2->vval.v_float; break;
1395 case '*': f *= tv2->vval.v_float; break;
1396 case '/': f /= tv2->vval.v_float; break;
1397 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001398 clear_tv(tv1);
1399 tv1->v_type = VAR_FLOAT;
1400 tv1->vval.v_float = f;
1401 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001402 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001403#endif
1404 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001405 switch (*op)
1406 {
1407 case '+': n += tv_get_number(tv2); break;
1408 case '-': n -= tv_get_number(tv2); break;
1409 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001410 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1411 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001412 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001413 clear_tv(tv1);
1414 tv1->v_type = VAR_NUMBER;
1415 tv1->vval.v_number = n;
1416 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001417 }
1418 else
1419 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001420 if (tv2->v_type == VAR_FLOAT)
1421 break;
1422
Bram Moolenaarff697e62019-02-12 22:28:33 +01001423 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001424 s = tv_get_string(tv1);
1425 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001426 clear_tv(tv1);
1427 tv1->v_type = VAR_STRING;
1428 tv1->vval.v_string = s;
1429 }
1430 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001431
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001432 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001433#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001434 {
1435 float_T f;
1436
Bram Moolenaarff697e62019-02-12 22:28:33 +01001437 if (*op == '%' || *op == '.'
1438 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001439 && tv2->v_type != VAR_NUMBER
1440 && tv2->v_type != VAR_STRING))
1441 break;
1442 if (tv2->v_type == VAR_FLOAT)
1443 f = tv2->vval.v_float;
1444 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001445 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001446 switch (*op)
1447 {
1448 case '+': tv1->vval.v_float += f; break;
1449 case '-': tv1->vval.v_float -= f; break;
1450 case '*': tv1->vval.v_float *= f; break;
1451 case '/': tv1->vval.v_float /= f; break;
1452 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001453 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001454#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001455 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001456 }
1457 }
1458
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001459 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001460 return FAIL;
1461}
1462
1463/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001464 * Evaluate the expression used in a ":for var in expr" command.
1465 * "arg" points to "var".
1466 * Set "*errp" to TRUE for an error, FALSE otherwise;
1467 * Return a pointer that holds the info. Null when there is an error.
1468 */
1469 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001470eval_for_line(
1471 char_u *arg,
1472 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001473 exarg_T *eap,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001474 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475{
Bram Moolenaar33570922005-01-25 22:26:29 +00001476 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001477 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001478 typval_T tv;
1479 list_T *l;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001480 evalarg_T evalarg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001481
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001482 CLEAR_FIELD(evalarg);
1483 evalarg.eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001484 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001485
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001486 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001487 if (fi == NULL)
1488 return NULL;
1489
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001490 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001491 if (expr == NULL)
1492 return fi;
1493
1494 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001495 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001496 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498 return fi;
1499 }
1500
1501 if (skip)
1502 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001503 if (eval0(skipwhite(expr + 2), &tv, eap, &evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001504 {
1505 *errp = FALSE;
1506 if (!skip)
1507 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001508 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001509 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001510 l = tv.vval.v_list;
1511 if (l == NULL)
1512 {
1513 // a null list is like an empty list: do nothing
1514 clear_tv(&tv);
1515 }
1516 else
1517 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001519 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001521 // No need to increment the refcount, it's already set for
1522 // the list being used in "tv".
1523 fi->fi_list = l;
1524 list_add_watch(l, &fi->fi_lw);
1525 fi->fi_lw.lw_item = l->lv_first;
1526 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001527 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001528 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001529 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001530 fi->fi_bi = 0;
1531 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001532 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001533 typval_T btv;
1534
1535 // Make a copy, so that the iteration still works when the
1536 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001538 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001539 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001540 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001541 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001542 else
1543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001544 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001545 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001546 }
1547 }
1548 }
1549 if (skip)
1550 --emsg_skip;
1551
1552 return fi;
1553}
1554
1555/*
1556 * Use the first item in a ":for" list. Advance to the next.
1557 * Assign the values to the variable (list). "arg" points to the first one.
1558 * Return TRUE when a valid item was found, FALSE when at end of list or
1559 * something wrong.
1560 */
1561 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001562next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001564 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001565 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001566 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1567 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001568 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001569
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001570 if (fi->fi_blob != NULL)
1571 {
1572 typval_T tv;
1573
1574 if (fi->fi_bi >= blob_len(fi->fi_blob))
1575 return FALSE;
1576 tv.v_type = VAR_NUMBER;
1577 tv.v_lock = VAR_FIXED;
1578 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1579 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001580 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001581 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001582 }
1583
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001584 item = fi->fi_lw.lw_item;
1585 if (item == NULL)
1586 result = FALSE;
1587 else
1588 {
1589 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001590 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001591 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001592 }
1593 return result;
1594}
1595
1596/*
1597 * Free the structure used to store info used by ":for".
1598 */
1599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001600free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001601{
Bram Moolenaar33570922005-01-25 22:26:29 +00001602 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001603
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001604 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001605 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001606 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001607 list_unref(fi->fi_list);
1608 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001609 if (fi != NULL && fi->fi_blob != NULL)
1610 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001611 vim_free(fi);
1612}
1613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001615set_context_for_expression(
1616 expand_T *xp,
1617 char_u *arg,
1618 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 int got_eq = FALSE;
1621 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001622 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001624 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001625 {
1626 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001627 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001628 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001629 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001630 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001631 {
1632 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001633 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001634 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635 break;
1636 }
1637 return;
1638 }
1639 }
1640 else
1641 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1642 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 while ((xp->xp_pattern = vim_strpbrk(arg,
1644 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1645 {
1646 c = *xp->xp_pattern;
1647 if (c == '&')
1648 {
1649 c = xp->xp_pattern[1];
1650 if (c == '&')
1651 {
1652 ++xp->xp_pattern;
1653 xp->xp_context = cmdidx != CMD_let || got_eq
1654 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1655 }
1656 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001657 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001659 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1660 xp->xp_pattern += 2;
1661
1662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 }
1664 else if (c == '$')
1665 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001666 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 xp->xp_context = EXPAND_ENV_VARS;
1668 }
1669 else if (c == '=')
1670 {
1671 got_eq = TRUE;
1672 xp->xp_context = EXPAND_EXPRESSION;
1673 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001674 else if (c == '#'
1675 && xp->xp_context == EXPAND_EXPRESSION)
1676 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001677 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001678 break;
1679 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001680 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 && xp->xp_context == EXPAND_FUNCTIONS
1682 && vim_strchr(xp->xp_pattern, '(') == NULL)
1683 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001684 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 break;
1686 }
1687 else if (cmdidx != CMD_let || got_eq)
1688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001689 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1692 if (c == '\\' && xp->xp_pattern[1] != NUL)
1693 ++xp->xp_pattern;
1694 xp->xp_context = EXPAND_NOTHING;
1695 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001696 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001698 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1700 /* skip */ ;
1701 xp->xp_context = EXPAND_NOTHING;
1702 }
1703 else if (c == '|')
1704 {
1705 if (xp->xp_pattern[1] == '|')
1706 {
1707 ++xp->xp_pattern;
1708 xp->xp_context = EXPAND_EXPRESSION;
1709 }
1710 else
1711 xp->xp_context = EXPAND_COMMANDS;
1712 }
1713 else
1714 xp->xp_context = EXPAND_EXPRESSION;
1715 }
1716 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001717 // Doesn't look like something valid, expand as an expression
1718 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 arg = xp->xp_pattern;
1721 if (*arg != NUL)
1722 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1723 /* skip */ ;
1724 }
1725 xp->xp_pattern = arg;
1726}
1727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001729 * Return TRUE if "pat" matches "text".
1730 * Does not use 'cpo' and always uses 'magic'.
1731 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001732 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001733pattern_match(char_u *pat, char_u *text, int ic)
1734{
1735 int matches = FALSE;
1736 char_u *save_cpo;
1737 regmatch_T regmatch;
1738
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001739 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001740 save_cpo = p_cpo;
1741 p_cpo = (char_u *)"";
1742 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1743 if (regmatch.regprog != NULL)
1744 {
1745 regmatch.rm_ic = ic;
1746 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1747 vim_regfree(regmatch.regprog);
1748 }
1749 p_cpo = save_cpo;
1750 return matches;
1751}
1752
1753/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001754 * Handle a name followed by "(". Both for just "name(arg)" and for
1755 * "expr->name(arg)".
1756 * Returns OK or FAIL.
1757 */
1758 static int
1759eval_func(
1760 char_u **arg, // points to "(", will be advanced
1761 char_u *name,
1762 int name_len,
1763 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001764 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001765 typval_T *basetv) // "expr" for "expr->name(arg)"
1766{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001767 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001768 char_u *s = name;
1769 int len = name_len;
1770 partial_T *partial;
1771 int ret = OK;
1772
1773 if (!evaluate)
1774 check_vars(s, len);
1775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001776 // If "s" is the name of a variable of type VAR_FUNC
1777 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001778 s = deref_func_name(s, &len, &partial, !evaluate);
1779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001780 // Need to make a copy, in case evaluating the arguments makes
1781 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001782 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001783 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001784 ret = FAIL;
1785 else
1786 {
1787 funcexe_T funcexe;
1788
1789 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001790 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001791 funcexe.firstline = curwin->w_cursor.lnum;
1792 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001793 funcexe.evaluate = evaluate;
1794 funcexe.partial = partial;
1795 funcexe.basetv = basetv;
1796 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1797 }
1798 vim_free(s);
1799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001800 // If evaluate is FALSE rettv->v_type was not set in
1801 // get_func_tv, but it's needed in handle_subscript() to parse
1802 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001803 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1804 {
1805 rettv->vval.v_string = NULL;
1806 rettv->v_type = VAR_FUNC;
1807 }
1808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001809 // Stop the expression evaluation when immediately
1810 // aborting on error, or when an interrupt occurred or
1811 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001812 if (evaluate && aborting())
1813 {
1814 if (ret == OK)
1815 clear_tv(rettv);
1816 ret = FAIL;
1817 }
1818 return ret;
1819}
1820
1821/*
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001822 * If inside Vim9 script, "arg" points to the end of a line (ignoring comments)
1823 * and there is a next line, return the next line (skipping blanks) and set
1824 * "getnext".
1825 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1826 * "arg" must point somewhere inside a line, not at the start.
1827 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001828 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001829eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1830{
1831 *getnext = FALSE;
1832 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1833 && evalarg != NULL
1834 && evalarg->eval_cookie != NULL
1835 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
1836 && (*arg == '"' || *arg == '#')))
1837 && source_nextline(evalarg->eval_cookie) != NULL)
1838 {
1839 char_u *p = source_nextline(evalarg->eval_cookie);
1840
1841 if (p != NULL)
1842 {
1843 *getnext = TRUE;
1844 return skipwhite(p);
1845 }
1846 }
1847 return arg;
1848}
1849
1850/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001851 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001852 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001853 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001854eval_next_line(evalarg_T *evalarg)
1855{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001856 garray_T *gap = &evalarg->eval_ga;
1857 char_u *line;
1858
1859 line = getsourceline(0, evalarg->eval_cookie, 0, TRUE);
1860 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1861 {
1862 // Going to concatenate the lines after parsing.
1863 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1864 ++gap->ga_len;
1865 }
1866 else
1867 {
1868 vim_free(evalarg->eval_tofree);
1869 evalarg->eval_tofree = line;
1870 }
1871 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001872}
1873
1874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1878 */
1879
1880/*
1881 * Handle zero level expression.
1882 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001884 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001885 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 * Return OK or FAIL.
1887 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001888 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001889eval0(
1890 char_u *arg,
1891 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001892 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001893 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 int ret;
1896 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001897 int did_emsg_before = did_emsg;
1898 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001899 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
1901 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001902 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001903
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001904 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 /*
1909 * Report the invalid expression unless the expression evaluation has
1910 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001911 * exception, or we already gave a more specific error.
1912 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001914 if (!aborting()
1915 && did_emsg == did_emsg_before
1916 && called_emsg == called_emsg_before
1917 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001918 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 ret = FAIL;
1920 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001921
1922 if (eap != NULL)
1923 eap->nextcmd = check_nextcmd(p);
1924
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001925 if (evalarg != NULL && eap != NULL && evalarg->eval_tofree != NULL)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001926 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001927 // We may need to keep the original command line, e.g. for
1928 // ":let" it has the variable names. But we may also need the
1929 // new one, "nextcmd" points into it. Keep both.
1930 vim_free(eap->cmdline_tofree);
1931 eap->cmdline_tofree = *eap->cmdlinep;
1932 *eap->cmdlinep = evalarg->eval_tofree;
1933 evalarg->eval_tofree = NULL;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935
1936 return ret;
1937}
1938
1939/*
1940 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001941 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 *
1943 * "arg" must point to the first non-white of the expression.
1944 * "arg" is advanced to the next non-white after the recognized expression.
1945 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001946 * Note: "rettv.v_lock" is not set.
1947 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 * Return OK or FAIL.
1949 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001950 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001951eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
Bram Moolenaar793648f2020-06-26 21:28:25 +02001953 char_u *p;
1954 int getnext;
1955
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 /*
1957 * Get the first variable.
1958 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001959 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 return FAIL;
1961
Bram Moolenaar793648f2020-06-26 21:28:25 +02001962 p = eval_next_non_blank(*arg, evalarg, &getnext);
1963 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001965 int result;
1966 typval_T var2;
1967 evalarg_T nested_evalarg;
1968 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02001969 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001970
Bram Moolenaar793648f2020-06-26 21:28:25 +02001971 if (getnext)
1972 *arg = eval_next_line(evalarg);
1973
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001974 if (evalarg == NULL)
1975 {
1976 CLEAR_FIELD(nested_evalarg);
1977 orig_flags = 0;
1978 }
1979 else
1980 {
1981 nested_evalarg = *evalarg;
1982 orig_flags = evalarg->eval_flags;
1983 }
1984
Bram Moolenaar7acde512020-06-24 23:02:40 +02001985 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001987 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001989 int error = FALSE;
1990
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001991 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001993 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001994 if (error)
1995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 }
1997
1998 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001999 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 */
2001 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002002 nested_evalarg.eval_flags = result ? orig_flags
2003 : orig_flags & ~EVAL_EVALUATE;
2004 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 return FAIL;
2006
2007 /*
2008 * Check for the ":".
2009 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002010 p = eval_next_non_blank(*arg, evalarg, &getnext);
2011 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 return FAIL;
2017 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002018 if (getnext)
2019 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002022 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 */
2024 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002025 nested_evalarg.eval_flags = !result ? orig_flags
2026 : orig_flags & ~EVAL_EVALUATE;
2027 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
2029 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 return FAIL;
2032 }
2033 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002034 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 }
2036
2037 return OK;
2038}
2039
2040/*
2041 * Handle first level expression:
2042 * expr2 || expr2 || expr2 logical OR
2043 *
2044 * "arg" must point to the first non-white of the expression.
2045 * "arg" is advanced to the next non-white after the recognized expression.
2046 *
2047 * Return OK or FAIL.
2048 */
2049 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002050eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002052 char_u *p;
2053 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002054 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 long result;
2056 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002057 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
2059 /*
2060 * Get the first variable.
2061 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002062 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 return FAIL;
2064
2065 /*
2066 * Repeat until there is no following "||".
2067 */
2068 first = TRUE;
2069 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002070 p = eval_next_non_blank(*arg, evalarg, &getnext);
2071 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002073 evalarg_T nested_evalarg;
2074 int evaluate;
2075 int orig_flags;
2076
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002077 if (getnext)
2078 *arg = eval_next_line(evalarg);
2079
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002080 if (evalarg == NULL)
2081 {
2082 CLEAR_FIELD(nested_evalarg);
2083 orig_flags = 0;
2084 evaluate = FALSE;
2085 }
2086 else
2087 {
2088 nested_evalarg = *evalarg;
2089 orig_flags = evalarg->eval_flags;
2090 evaluate = orig_flags & EVAL_EVALUATE;
2091 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002092
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 if (evaluate && first)
2094 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002095 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002098 if (error)
2099 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 first = FALSE;
2101 }
2102
2103 /*
2104 * Get the second variable.
2105 */
2106 *arg = skipwhite(*arg + 2);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002107 nested_evalarg.eval_flags = !result ? orig_flags
2108 : orig_flags & ~EVAL_EVALUATE;
2109 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 return FAIL;
2111
2112 /*
2113 * Compute the result.
2114 */
2115 if (evaluate && !result)
2116 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002117 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002119 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002120 if (error)
2121 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
2123 if (evaluate)
2124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125 rettv->v_type = VAR_NUMBER;
2126 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002128
2129 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
2131
2132 return OK;
2133}
2134
2135/*
2136 * Handle second level expression:
2137 * expr3 && expr3 && expr3 logical AND
2138 *
2139 * "arg" must point to the first non-white of the expression.
2140 * "arg" is advanced to the next non-white after the recognized expression.
2141 *
2142 * Return OK or FAIL.
2143 */
2144 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002145eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002147 char_u *p;
2148 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002149 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 long result;
2151 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002152 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 /*
2155 * Get the first variable.
2156 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002157 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 return FAIL;
2159
2160 /*
2161 * Repeat until there is no following "&&".
2162 */
2163 first = TRUE;
2164 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002165 p = eval_next_non_blank(*arg, evalarg, &getnext);
2166 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002168 evalarg_T nested_evalarg;
2169 int orig_flags;
2170 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002171
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002172 if (getnext)
2173 *arg = eval_next_line(evalarg);
2174
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175 if (evalarg == NULL)
2176 {
2177 CLEAR_FIELD(nested_evalarg);
2178 orig_flags = 0;
2179 evaluate = FALSE;
2180 }
2181 else
2182 {
2183 nested_evalarg = *evalarg;
2184 orig_flags = evalarg->eval_flags;
2185 evaluate = orig_flags & EVAL_EVALUATE;
2186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (evaluate && first)
2188 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002189 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002192 if (error)
2193 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 first = FALSE;
2195 }
2196
2197 /*
2198 * Get the second variable.
2199 */
2200 *arg = skipwhite(*arg + 2);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002201 nested_evalarg.eval_flags = result ? orig_flags
2202 : orig_flags & ~EVAL_EVALUATE;
2203 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 return FAIL;
2205
2206 /*
2207 * Compute the result.
2208 */
2209 if (evaluate && result)
2210 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002211 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002214 if (error)
2215 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217 if (evaluate)
2218 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 rettv->v_type = VAR_NUMBER;
2220 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002222
2223 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225
2226 return OK;
2227}
2228
2229/*
2230 * Handle third level expression:
2231 * var1 == var2
2232 * var1 =~ var2
2233 * var1 != var2
2234 * var1 !~ var2
2235 * var1 > var2
2236 * var1 >= var2
2237 * var1 < var2
2238 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002239 * var1 is var2
2240 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 *
2242 * "arg" must point to the first non-white of the expression.
2243 * "arg" is advanced to the next non-white after the recognized expression.
2244 *
2245 * Return OK or FAIL.
2246 */
2247 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002248eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249{
Bram Moolenaar33570922005-01-25 22:26:29 +00002250 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002252 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002254 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257
2258 /*
2259 * Get the first variable.
2260 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002261 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 return FAIL;
2263
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002264 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 switch (p[0])
2266 {
2267 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002268 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002270 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 break;
2272 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002273 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002275 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 break;
2277 case '>': if (p[1] != '=')
2278 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002279 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 len = 1;
2281 }
2282 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002283 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 break;
2285 case '<': if (p[1] != '=')
2286 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002287 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 len = 1;
2289 }
2290 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002291 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002293 case 'i': if (p[1] == 's')
2294 {
2295 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2296 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002297 i = p[len];
2298 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002299 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002300 }
2301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303
2304 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002305 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002307 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002309 if (getnext)
2310 *arg = eval_next_line(evalarg);
2311
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002312 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (p[len] == '?')
2314 {
2315 ic = TRUE;
2316 ++len;
2317 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002318 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 else if (p[len] == '#')
2320 {
2321 ic = FALSE;
2322 ++len;
2323 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002324 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 else
2326 ic = p_ic;
2327
2328 /*
2329 * Get the second variable.
2330 */
2331 *arg = skipwhite(p + len);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002332 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 return FAIL;
2336 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002337 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002338 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002339 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002340
2341 clear_tv(&var2);
2342 return ret;
2343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
2345
2346 return OK;
2347}
2348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349 void
2350eval_addblob(typval_T *tv1, typval_T *tv2)
2351{
2352 blob_T *b1 = tv1->vval.v_blob;
2353 blob_T *b2 = tv2->vval.v_blob;
2354 blob_T *b = blob_alloc();
2355 int i;
2356
2357 if (b != NULL)
2358 {
2359 for (i = 0; i < blob_len(b1); i++)
2360 ga_append(&b->bv_ga, blob_get(b1, i));
2361 for (i = 0; i < blob_len(b2); i++)
2362 ga_append(&b->bv_ga, blob_get(b2, i));
2363
2364 clear_tv(tv1);
2365 rettv_blob_set(tv1, b);
2366 }
2367}
2368
2369 int
2370eval_addlist(typval_T *tv1, typval_T *tv2)
2371{
2372 typval_T var3;
2373
2374 // concatenate Lists
2375 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2376 {
2377 clear_tv(tv1);
2378 clear_tv(tv2);
2379 return FAIL;
2380 }
2381 clear_tv(tv1);
2382 *tv1 = var3;
2383 return OK;
2384}
2385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386/*
2387 * Handle fourth level expression:
2388 * + number addition
2389 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002390 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002391 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 *
2393 * "arg" must point to the first non-white of the expression.
2394 * "arg" is advanced to the next non-white after the recognized expression.
2395 *
2396 * Return OK or FAIL.
2397 */
2398 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002399eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002401 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403 /*
2404 * Get the first variable.
2405 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002406 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 return FAIL;
2408
2409 /*
2410 * Repeat computing, until no '+', '-' or '.' is following.
2411 */
2412 for (;;)
2413 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002414 int getnext;
2415 char_u *p;
2416 int op;
2417 int concat;
2418 typval_T var2;
2419
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002420 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002421 p = eval_next_non_blank(*arg, evalarg, &getnext);
2422 op = *p;
2423 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002424 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002427 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002429 if ((op != '+' || (rettv->v_type != VAR_LIST
2430 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002431#ifdef FEAT_FLOAT
2432 && (op == '.' || rettv->v_type != VAR_FLOAT)
2433#endif
2434 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002435 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002436 // For "list + ...", an illegal use of the first operand as
2437 // a number cannot be determined before evaluating the 2nd
2438 // operand: if this is also a list, all is ok.
2439 // For "something . ...", "something - ..." or "non-list + ...",
2440 // we know that the first operand needs to be a string or number
2441 // without evaluating the 2nd operand. So check before to avoid
2442 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002443 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 {
2445 clear_tv(rettv);
2446 return FAIL;
2447 }
2448 }
2449
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 /*
2451 * Get the second variable.
2452 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002453 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2454 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002456 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 return FAIL;
2460 }
2461
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002462 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
2464 /*
2465 * Compute the result.
2466 */
2467 if (op == '.')
2468 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2470 char_u *s1 = tv_get_string_buf(rettv, buf1);
2471 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002473 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 {
2475 clear_tv(rettv);
2476 clear_tv(&var2);
2477 return FAIL;
2478 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 clear_tv(rettv);
2481 rettv->v_type = VAR_STRING;
2482 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002484 else if (op == '+' && rettv->v_type == VAR_BLOB
2485 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002487 else if (op == '+' && rettv->v_type == VAR_LIST
2488 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002489 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002491 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 else
2494 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002495 int error = FALSE;
2496 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002497#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002498 float_T f1 = 0, f2 = 0;
2499
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002500 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002502 f1 = rettv->vval.v_float;
2503 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002505 else
2506#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002507 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002508 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002509 if (error)
2510 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002511 // This can only happen for "list + non-list". For
2512 // "non-list + ..." or "something - ...", we returned
2513 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002514 clear_tv(rettv);
2515 return FAIL;
2516 }
2517#ifdef FEAT_FLOAT
2518 if (var2.v_type == VAR_FLOAT)
2519 f1 = n1;
2520#endif
2521 }
2522#ifdef FEAT_FLOAT
2523 if (var2.v_type == VAR_FLOAT)
2524 {
2525 f2 = var2.vval.v_float;
2526 n2 = 0;
2527 }
2528 else
2529#endif
2530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002531 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002532 if (error)
2533 {
2534 clear_tv(rettv);
2535 clear_tv(&var2);
2536 return FAIL;
2537 }
2538#ifdef FEAT_FLOAT
2539 if (rettv->v_type == VAR_FLOAT)
2540 f2 = n2;
2541#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002542 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002544
2545#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002546 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002547 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2548 {
2549 if (op == '+')
2550 f1 = f1 + f2;
2551 else
2552 f1 = f1 - f2;
2553 rettv->v_type = VAR_FLOAT;
2554 rettv->vval.v_float = f1;
2555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002557#endif
2558 {
2559 if (op == '+')
2560 n1 = n1 + n2;
2561 else
2562 n1 = n1 - n2;
2563 rettv->v_type = VAR_NUMBER;
2564 rettv->vval.v_number = n1;
2565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569 }
2570 return OK;
2571}
2572
2573/*
2574 * Handle fifth level expression:
2575 * * number multiplication
2576 * / number division
2577 * % number modulo
2578 *
2579 * "arg" must point to the first non-white of the expression.
2580 * "arg" is advanced to the next non-white after the recognized expression.
2581 *
2582 * Return OK or FAIL.
2583 */
2584 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002585eval6(
2586 char_u **arg,
2587 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002588 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002589 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002593 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002594#ifdef FEAT_FLOAT
2595 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002596 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002597#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002598 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600 /*
2601 * Get the first variable.
2602 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002603 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 return FAIL;
2605
2606 /*
2607 * Repeat computing, until no '*', '/' or '%' is following.
2608 */
2609 for (;;)
2610 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002611 int evaluate = evalarg == NULL ? 0
2612 : (evalarg->eval_flags & EVAL_EVALUATE);
2613 int getnext;
2614
2615 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002616 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002618 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002619 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002621 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002623#ifdef FEAT_FLOAT
2624 if (rettv->v_type == VAR_FLOAT)
2625 {
2626 f1 = rettv->vval.v_float;
2627 use_float = TRUE;
2628 n1 = 0;
2629 }
2630 else
2631#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002632 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002634 if (error)
2635 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637 else
2638 n1 = 0;
2639
2640 /*
2641 * Get the second variable.
2642 */
2643 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 return FAIL;
2646
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002647 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002649#ifdef FEAT_FLOAT
2650 if (var2.v_type == VAR_FLOAT)
2651 {
2652 if (!use_float)
2653 {
2654 f1 = n1;
2655 use_float = TRUE;
2656 }
2657 f2 = var2.vval.v_float;
2658 n2 = 0;
2659 }
2660 else
2661#endif
2662 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002663 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002664 clear_tv(&var2);
2665 if (error)
2666 return FAIL;
2667#ifdef FEAT_FLOAT
2668 if (use_float)
2669 f2 = n2;
2670#endif
2671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672
2673 /*
2674 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002675 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002677#ifdef FEAT_FLOAT
2678 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002680 if (op == '*')
2681 f1 = f1 * f2;
2682 else if (op == '/')
2683 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002684# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002685 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002686 if (f2 == 0.0)
2687 {
2688 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002689 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002690 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002691 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002692 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002693 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002694 }
2695 else
2696 f1 = f1 / f2;
2697# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002698 // We rely on the floating point library to handle divide
2699 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002700 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002701# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002704 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002706 return FAIL;
2707 }
2708 rettv->v_type = VAR_FLOAT;
2709 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
2711 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002714 if (op == '*')
2715 n1 = n1 * n2;
2716 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002717 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002719 n1 = num_modulus(n1, n2);
2720
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002721 rettv->v_type = VAR_NUMBER;
2722 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 }
2726
2727 return OK;
2728}
2729
2730/*
2731 * Handle sixth level expression:
2732 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002733 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002734 * "string" string constant
2735 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 * &option-name option value
2737 * @r register contents
2738 * identifier variable value
2739 * function() function call
2740 * $VAR environment variable
2741 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002742 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002744 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002745 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 *
2747 * Also handle:
2748 * ! in front logical NOT
2749 * - in front unary minus
2750 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 * trailing [] subscript in String or List
2752 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002753 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 *
2755 * "arg" must point to the first non-white of the expression.
2756 * "arg" is advanced to the next non-white after the recognized expression.
2757 *
2758 * Return OK or FAIL.
2759 */
2760 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002761eval7(
2762 char_u **arg,
2763 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002764 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002767 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2768 int evaluate = evalarg != NULL
2769 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 int len;
2771 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 char_u *start_leader, *end_leader;
2773 int ret = OK;
2774 char_u *alias;
2775
2776 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002777 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002778 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002780 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
2782 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002783 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 */
2785 start_leader = *arg;
2786 while (**arg == '!' || **arg == '-' || **arg == '+')
2787 *arg = skipwhite(*arg + 1);
2788 end_leader = *arg;
2789
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002790 if (**arg == '.' && (!isdigit(*(*arg + 1))
2791#ifdef FEAT_FLOAT
2792 || current_sctx.sc_version < 2
2793#endif
2794 ))
2795 {
2796 semsg(_(e_invexpr2), *arg);
2797 ++*arg;
2798 return FAIL;
2799 }
2800
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 switch (**arg)
2802 {
2803 /*
2804 * Number constant.
2805 */
2806 case '0':
2807 case '1':
2808 case '2':
2809 case '3':
2810 case '4':
2811 case '5':
2812 case '6':
2813 case '7':
2814 case '8':
2815 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002817
2818 // Apply prefixed "-" and "+" now. Matters especially when
2819 // "->" follows.
2820 if (ret == OK && evaluate && end_leader > start_leader)
2821 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 break;
2823
2824 /*
2825 * String constant: "string".
2826 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002827 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 break;
2829
2830 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002833 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002834 break;
2835
2836 /*
2837 * List: [expr, expr]
2838 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002839 case '[': ret = get_list_tv(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 break;
2841
2842 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002843 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002844 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002845 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002846 {
2847 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002848 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002849 }
2850 else
2851 ret = NOTDONE;
2852 break;
2853
2854 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002855 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002856 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002858 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002859 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002860 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 break;
2862
2863 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002864 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002866 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 break;
2868
2869 /*
2870 * Environment variable: $VAR.
2871 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002872 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 break;
2874
2875 /*
2876 * Register contents: @r.
2877 */
2878 case '@': ++*arg;
2879 if (evaluate)
2880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002881 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002882 rettv->vval.v_string = get_reg_contents(**arg,
2883 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
2885 if (**arg != NUL)
2886 ++*arg;
2887 break;
2888
2889 /*
2890 * nested expression: (expression).
2891 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002892 case '(': {
2893 *arg = skipwhite(*arg + 1);
2894 ret = eval1(arg, rettv, evalarg); // recursive!
2895 if (**arg == ')')
2896 ++*arg;
2897 else if (ret == OK)
2898 {
2899 emsg(_(e_missing_close));
2900 clear_tv(rettv);
2901 ret = FAIL;
2902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
2904 break;
2905
Bram Moolenaar8c711452005-01-14 21:53:12 +00002906 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 break;
2908 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909
2910 if (ret == NOTDONE)
2911 {
2912 /*
2913 * Must be a variable or function name.
2914 * Can also be a curly-braces kind of name: {expr}.
2915 */
2916 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002917 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918 if (alias != NULL)
2919 s = alias;
2920
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002921 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002922 ret = FAIL;
2923 else
2924 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002925 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002926 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002927 else if (flags & EVAL_CONSTANT)
2928 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002929 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002930 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002931 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002932 {
2933 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002934 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002935 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002936 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002937 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002938 }
2939
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 *arg = skipwhite(*arg);
2941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002942 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2943 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002944 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002945 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 /*
2948 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2949 */
2950 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002951 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002952 return ret;
2953}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002954
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002955/*
2956 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002957 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002958 * Adjusts "end_leaderp" until it is at "start_leader".
2959 */
2960 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002961eval7_leader(
2962 typval_T *rettv,
2963 int numeric_only,
2964 char_u *start_leader,
2965 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002966{
2967 char_u *end_leader = *end_leaderp;
2968 int ret = OK;
2969 int error = FALSE;
2970 varnumber_T val = 0;
2971#ifdef FEAT_FLOAT
2972 float_T f = 0.0;
2973
2974 if (rettv->v_type == VAR_FLOAT)
2975 f = rettv->vval.v_float;
2976 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002977#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002978 val = tv_get_number_chk(rettv, &error);
2979 if (error)
2980 {
2981 clear_tv(rettv);
2982 ret = FAIL;
2983 }
2984 else
2985 {
2986 while (end_leader > start_leader)
2987 {
2988 --end_leader;
2989 if (*end_leader == '!')
2990 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002991 if (numeric_only)
2992 {
2993 ++end_leader;
2994 break;
2995 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002996#ifdef FEAT_FLOAT
2997 if (rettv->v_type == VAR_FLOAT)
2998 f = !f;
2999 else
3000#endif
3001 val = !val;
3002 }
3003 else if (*end_leader == '-')
3004 {
3005#ifdef FEAT_FLOAT
3006 if (rettv->v_type == VAR_FLOAT)
3007 f = -f;
3008 else
3009#endif
3010 val = -val;
3011 }
3012 }
3013#ifdef FEAT_FLOAT
3014 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003016 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003017 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003019 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003020#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003021 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003022 clear_tv(rettv);
3023 rettv->v_type = VAR_NUMBER;
3024 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003027 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 return ret;
3029}
3030
3031/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003032 * Call the function referred to in "rettv".
3033 */
3034 static int
3035call_func_rettv(
3036 char_u **arg,
3037 typval_T *rettv,
3038 int evaluate,
3039 dict_T *selfdict,
3040 typval_T *basetv)
3041{
3042 partial_T *pt = NULL;
3043 funcexe_T funcexe;
3044 typval_T functv;
3045 char_u *s;
3046 int ret;
3047
3048 // need to copy the funcref so that we can clear rettv
3049 if (evaluate)
3050 {
3051 functv = *rettv;
3052 rettv->v_type = VAR_UNKNOWN;
3053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003054 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003055 if (functv.v_type == VAR_PARTIAL)
3056 {
3057 pt = functv.vval.v_partial;
3058 s = partial_name(pt);
3059 }
3060 else
3061 s = functv.vval.v_string;
3062 }
3063 else
3064 s = (char_u *)"";
3065
Bram Moolenaara80faa82020-04-12 19:37:17 +02003066 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003067 funcexe.firstline = curwin->w_cursor.lnum;
3068 funcexe.lastline = curwin->w_cursor.lnum;
3069 funcexe.evaluate = evaluate;
3070 funcexe.partial = pt;
3071 funcexe.selfdict = selfdict;
3072 funcexe.basetv = basetv;
3073 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
3074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003075 // Clear the funcref afterwards, so that deleting it while
3076 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003077 if (evaluate)
3078 clear_tv(&functv);
3079
3080 return ret;
3081}
3082
3083/*
3084 * Evaluate "->method()".
3085 * "*arg" points to the '-'.
3086 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3087 */
3088 static int
3089eval_lambda(
3090 char_u **arg,
3091 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003092 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003093 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003094{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003095 int evaluate = evalarg != NULL
3096 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003097 typval_T base = *rettv;
3098 int ret;
3099
3100 // Skip over the ->.
3101 *arg += 2;
3102 rettv->v_type = VAR_UNKNOWN;
3103
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003104 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003105 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003106 return FAIL;
3107 else if (**arg != '(')
3108 {
3109 if (verbose)
3110 {
3111 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003112 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003113 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003115 }
3116 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003117 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003118 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003119 else
3120 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
3121
3122 // Clear the funcref afterwards, so that deleting it while
3123 // evaluating the arguments is possible (see test55).
3124 if (evaluate)
3125 clear_tv(&base);
3126
3127 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003128}
3129
3130/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003131 * Evaluate "->method()".
3132 * "*arg" points to the '-'.
3133 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3134 */
3135 static int
3136eval_method(
3137 char_u **arg,
3138 typval_T *rettv,
3139 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003140 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003141{
3142 char_u *name;
3143 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003144 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003145 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003146 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003147
3148 // Skip over the ->.
3149 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003150 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003151
Bram Moolenaarac92e252019-08-03 21:58:38 +02003152 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003153 len = get_name_len(arg, &alias, evaluate, TRUE);
3154 if (alias != NULL)
3155 name = alias;
3156
3157 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003158 {
3159 if (verbose)
3160 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003161 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003162 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003163 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003164 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003165 if (**arg != '(')
3166 {
3167 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003169 ret = FAIL;
3170 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003171 else if (VIM_ISWHITE((*arg)[-1]))
3172 {
3173 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003174 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003175 ret = FAIL;
3176 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003177 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02003178 ret = eval_func(arg, name, len, rettv,
3179 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003180 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003181
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003182 // Clear the funcref afterwards, so that deleting it while
3183 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003184 if (evaluate)
3185 clear_tv(&base);
3186
Bram Moolenaarac92e252019-08-03 21:58:38 +02003187 return ret;
3188}
3189
3190/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003191 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3192 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003193 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3194 */
3195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003196eval_index(
3197 char_u **arg,
3198 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003199 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003200 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003201{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003202 int evaluate = evalarg != NULL
3203 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003204 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003205 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003206 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003207 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003208 long len = -1;
3209 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003210 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003211 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003212
Bram Moolenaara03f2332016-02-06 18:09:59 +01003213 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003214 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003215 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003216 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003217 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003218 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003219 return FAIL;
3220 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003221#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003222 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003223 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003224 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003225#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003226 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003227 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003228 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003229 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003230 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003231 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003232 return FAIL;
3233 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003234 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003236 if (evaluate)
3237 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003238 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003239
3240 case VAR_STRING:
3241 case VAR_NUMBER:
3242 case VAR_LIST:
3243 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003244 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003245 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003246 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003247
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003248 init_tv(&var1);
3249 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003250 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003251 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003252 /*
3253 * dict.name
3254 */
3255 key = *arg + 1;
3256 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3257 ;
3258 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003259 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003260 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003261 }
3262 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003263 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003264 /*
3265 * something[idx]
3266 *
3267 * Get the (first) variable from inside the [].
3268 */
3269 *arg = skipwhite(*arg + 1);
3270 if (**arg == ':')
3271 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003272 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003273 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003274 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003275 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003276 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003277 clear_tv(&var1);
3278 return FAIL;
3279 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003280
3281 /*
3282 * Get the second variable from inside the [:].
3283 */
3284 if (**arg == ':')
3285 {
3286 range = TRUE;
3287 *arg = skipwhite(*arg + 1);
3288 if (**arg == ']')
3289 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003290 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003292 if (!empty1)
3293 clear_tv(&var1);
3294 return FAIL;
3295 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003296 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003297 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003298 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003299 if (!empty1)
3300 clear_tv(&var1);
3301 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003302 return FAIL;
3303 }
3304 }
3305
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003306 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003307 if (**arg != ']')
3308 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003309 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003310 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003311 clear_tv(&var1);
3312 if (range)
3313 clear_tv(&var2);
3314 return FAIL;
3315 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003316 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003317 }
3318
3319 if (evaluate)
3320 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003321 n1 = 0;
3322 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003323 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003324 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003325 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003326 }
3327 if (range)
3328 {
3329 if (empty2)
3330 n2 = -1;
3331 else
3332 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003333 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003334 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003335 }
3336 }
3337
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003338 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003339 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003340 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003341 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003343 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003344 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003345 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003346 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003347 case VAR_SPECIAL:
3348 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003349 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003350 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003351
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003352 case VAR_NUMBER:
3353 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003354 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003355 len = (long)STRLEN(s);
3356 if (range)
3357 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003358 // The resulting variable is a substring. If the indexes
3359 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003360 if (n1 < 0)
3361 {
3362 n1 = len + n1;
3363 if (n1 < 0)
3364 n1 = 0;
3365 }
3366 if (n2 < 0)
3367 n2 = len + n2;
3368 else if (n2 >= len)
3369 n2 = len;
3370 if (n1 >= len || n2 < 0 || n1 > n2)
3371 s = NULL;
3372 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003373 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003374 }
3375 else
3376 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003377 // The resulting variable is a string of a single
3378 // character. If the index is too big or negative the
3379 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003380 if (n1 >= len || n1 < 0)
3381 s = NULL;
3382 else
3383 s = vim_strnsave(s + n1, 1);
3384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003385 clear_tv(rettv);
3386 rettv->v_type = VAR_STRING;
3387 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003388 break;
3389
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003390 case VAR_BLOB:
3391 len = blob_len(rettv->vval.v_blob);
3392 if (range)
3393 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003394 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003395 // are out of range the result is empty.
3396 if (n1 < 0)
3397 {
3398 n1 = len + n1;
3399 if (n1 < 0)
3400 n1 = 0;
3401 }
3402 if (n2 < 0)
3403 n2 = len + n2;
3404 else if (n2 >= len)
3405 n2 = len - 1;
3406 if (n1 >= len || n2 < 0 || n1 > n2)
3407 {
3408 clear_tv(rettv);
3409 rettv->v_type = VAR_BLOB;
3410 rettv->vval.v_blob = NULL;
3411 }
3412 else
3413 {
3414 blob_T *blob = blob_alloc();
3415
3416 if (blob != NULL)
3417 {
3418 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3419 {
3420 blob_free(blob);
3421 return FAIL;
3422 }
3423 blob->bv_ga.ga_len = n2 - n1 + 1;
3424 for (i = n1; i <= n2; i++)
3425 blob_set(blob, i - n1,
3426 blob_get(rettv->vval.v_blob, i));
3427
3428 clear_tv(rettv);
3429 rettv_blob_set(rettv, blob);
3430 }
3431 }
3432 }
3433 else
3434 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003435 // The resulting variable is a byte value.
3436 // If the index is too big or negative that is an error.
3437 if (n1 < 0)
3438 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003439 if (n1 < len && n1 >= 0)
3440 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003441 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003442
3443 clear_tv(rettv);
3444 rettv->v_type = VAR_NUMBER;
3445 rettv->vval.v_number = v;
3446 }
3447 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003448 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003449 }
3450 break;
3451
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003452 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003453 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003454 if (n1 < 0)
3455 n1 = len + n1;
3456 if (!empty1 && (n1 < 0 || n1 >= len))
3457 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003458 // For a range we allow invalid values and return an empty
3459 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003460 if (!range)
3461 {
3462 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003463 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003464 return FAIL;
3465 }
3466 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003467 }
3468 if (range)
3469 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003470 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003471
3472 if (n2 < 0)
3473 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003474 else if (n2 >= len)
3475 n2 = len - 1;
3476 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003477 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003478 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003479 if (l == NULL)
3480 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003482 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003483 }
3484 else
3485 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003486 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003487 clear_tv(rettv);
3488 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003489 }
3490 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003491
3492 case VAR_DICT:
3493 if (range)
3494 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003495 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003496 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003497 if (len == -1)
3498 clear_tv(&var1);
3499 return FAIL;
3500 }
3501 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003502 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003503
3504 if (len == -1)
3505 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003506 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003507 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003508 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003509 clear_tv(&var1);
3510 return FAIL;
3511 }
3512 }
3513
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003514 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003515
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003516 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003517 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003518 if (len == -1)
3519 clear_tv(&var1);
3520 if (item == NULL)
3521 return FAIL;
3522
3523 copy_tv(&item->di_tv, &var1);
3524 clear_tv(rettv);
3525 *rettv = var1;
3526 }
3527 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003528 }
3529 }
3530
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003531 return OK;
3532}
3533
3534/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003535 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003536 */
3537 char_u *
3538partial_name(partial_T *pt)
3539{
3540 if (pt->pt_name != NULL)
3541 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003542 if (pt->pt_func != NULL)
3543 return pt->pt_func->uf_name;
3544 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003545}
3546
Bram Moolenaarddecc252016-04-06 22:59:37 +02003547 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003548partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003549{
3550 int i;
3551
3552 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003553 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003554 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003555 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003556 if (pt->pt_name != NULL)
3557 {
3558 func_unref(pt->pt_name);
3559 vim_free(pt->pt_name);
3560 }
3561 else
3562 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003563
3564 if (pt->pt_funcstack != NULL)
3565 {
3566 // Decrease the reference count for the context of a closure. If down
3567 // to zero free it and clear the variables on the stack.
3568 if (--pt->pt_funcstack->fs_refcount == 0)
3569 {
3570 garray_T *gap = &pt->pt_funcstack->fs_ga;
3571 typval_T *stack = gap->ga_data;
3572
3573 for (i = 0; i < gap->ga_len; ++i)
3574 clear_tv(stack + i);
3575 ga_clear(gap);
3576 vim_free(pt->pt_funcstack);
3577 }
3578 pt->pt_funcstack = NULL;
3579 }
3580
Bram Moolenaarddecc252016-04-06 22:59:37 +02003581 vim_free(pt);
3582}
3583
3584/*
3585 * Unreference a closure: decrement the reference count and free it when it
3586 * becomes zero.
3587 */
3588 void
3589partial_unref(partial_T *pt)
3590{
3591 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003592 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003593}
3594
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003595/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003596 * Return the next (unique) copy ID.
3597 * Used for serializing nested structures.
3598 */
3599 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003600get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003601{
3602 current_copyID += COPYID_INC;
3603 return current_copyID;
3604}
3605
3606/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003607 * Garbage collection for lists and dictionaries.
3608 *
3609 * We use reference counts to be able to free most items right away when they
3610 * are no longer used. But for composite items it's possible that it becomes
3611 * unused while the reference count is > 0: When there is a recursive
3612 * reference. Example:
3613 * :let l = [1, 2, 3]
3614 * :let d = {9: l}
3615 * :let l[1] = d
3616 *
3617 * Since this is quite unusual we handle this with garbage collection: every
3618 * once in a while find out which lists and dicts are not referenced from any
3619 * variable.
3620 *
3621 * Here is a good reference text about garbage collection (refers to Python
3622 * but it applies to all reference-counting mechanisms):
3623 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003624 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003625
3626/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003627 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003628 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003629 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003630 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003631 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003632garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003633{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003634 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003635 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003636 buf_T *buf;
3637 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003638 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003639 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003640
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003641 if (!testing)
3642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003643 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003644 want_garbage_collect = FALSE;
3645 may_garbage_collect = FALSE;
3646 garbage_collect_at_exit = FALSE;
3647 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003648
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003649 // The execution stack can grow big, limit the size.
3650 if (exestack.ga_maxlen - exestack.ga_len > 500)
3651 {
3652 size_t new_len;
3653 char_u *pp;
3654 int n;
3655
3656 // Keep 150% of the current size, with a minimum of the growth size.
3657 n = exestack.ga_len / 2;
3658 if (n < exestack.ga_growsize)
3659 n = exestack.ga_growsize;
3660
3661 // Don't make it bigger though.
3662 if (exestack.ga_len + n < exestack.ga_maxlen)
3663 {
3664 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3665 pp = vim_realloc(exestack.ga_data, new_len);
3666 if (pp == NULL)
3667 return FAIL;
3668 exestack.ga_maxlen = exestack.ga_len + n;
3669 exestack.ga_data = pp;
3670 }
3671 }
3672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003673 // We advance by two because we add one for items referenced through
3674 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003675 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003676
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003677 /*
3678 * 1. Go through all accessible variables and mark all lists and dicts
3679 * with copyID.
3680 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003681
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003682 // Don't free variables in the previous_funccal list unless they are only
3683 // referenced through previous_funccal. This must be first, because if
3684 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003685 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003686
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003687 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003688 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003689
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003690 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003691 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003692 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3693 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003694
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003695 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003696 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003697 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3698 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003699 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003700 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3701 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003702#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003703 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003704 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3705 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003706 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003707 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003708 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3709 NULL, NULL);
3710#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003712 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003713 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003714 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3715 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003716 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003717 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003718
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003719 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003720 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003721
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003722 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003723 abort = abort || set_ref_in_functions(copyID);
3724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003725 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003726 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003727
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003728 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003729 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003730
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003731 // callbacks in buffers
3732 abort = abort || set_ref_in_buffers(copyID);
3733
Bram Moolenaar1dced572012-04-05 16:54:08 +02003734#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003735 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003736#endif
3737
Bram Moolenaardb913952012-06-29 12:54:53 +02003738#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003739 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003740#endif
3741
3742#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003743 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003744#endif
3745
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003746#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003747 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003748 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003749#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003750#ifdef FEAT_NETBEANS_INTG
3751 abort = abort || set_ref_in_nb_channel(copyID);
3752#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003753
Bram Moolenaare3188e22016-05-31 21:13:04 +02003754#ifdef FEAT_TIMERS
3755 abort = abort || set_ref_in_timer(copyID);
3756#endif
3757
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003758#ifdef FEAT_QUICKFIX
3759 abort = abort || set_ref_in_quickfix(copyID);
3760#endif
3761
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003762#ifdef FEAT_TERMINAL
3763 abort = abort || set_ref_in_term(copyID);
3764#endif
3765
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003766#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003767 abort = abort || set_ref_in_popups(copyID);
3768#endif
3769
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003770 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003771 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003772 /*
3773 * 2. Free lists and dictionaries that are not referenced.
3774 */
3775 did_free = free_unref_items(copyID);
3776
3777 /*
3778 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003779 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003780 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003781 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003782 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003783 else if (p_verbose > 0)
3784 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003785 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003786 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003787
3788 return did_free;
3789}
3790
3791/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003792 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003793 */
3794 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003795free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003796{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003797 int did_free = FALSE;
3798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003799 // Let all "free" functions know that we are here. This means no
3800 // dictionaries, lists, channels or jobs are to be freed, because we will
3801 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003802 in_free_unref_items = TRUE;
3803
3804 /*
3805 * PASS 1: free the contents of the items. We don't free the items
3806 * themselves yet, so that it is possible to decrement refcount counters
3807 */
3808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003809 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003810 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003811
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003812 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003813 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003814
3815#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003816 // Go through the list of jobs and free items without the copyID. This
3817 // must happen before doing channels, because jobs refer to channels, but
3818 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003819 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003821 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003822 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3823#endif
3824
3825 /*
3826 * PASS 2: free the items themselves.
3827 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003828 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003829 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003830
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003831#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003832 // Go through the list of jobs and free items without the copyID. This
3833 // must happen before doing channels, because jobs refer to channels, but
3834 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003835 free_unused_jobs(copyID, COPYID_MASK);
3836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003837 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003838 free_unused_channels(copyID, COPYID_MASK);
3839#endif
3840
3841 in_free_unref_items = FALSE;
3842
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003843 return did_free;
3844}
3845
3846/*
3847 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003848 * "list_stack" is used to add lists to be marked. Can be NULL.
3849 *
3850 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003851 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003852 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003853set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003854{
3855 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003856 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003857 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003858 hashtab_T *cur_ht;
3859 ht_stack_T *ht_stack = NULL;
3860 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003861
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003862 cur_ht = ht;
3863 for (;;)
3864 {
3865 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003866 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003867 // Mark each item in the hashtab. If the item contains a hashtab
3868 // it is added to ht_stack, if it contains a list it is added to
3869 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003870 todo = (int)cur_ht->ht_used;
3871 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3872 if (!HASHITEM_EMPTY(hi))
3873 {
3874 --todo;
3875 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3876 &ht_stack, list_stack);
3877 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003878 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003879
3880 if (ht_stack == NULL)
3881 break;
3882
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003883 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003884 cur_ht = ht_stack->ht;
3885 tempitem = ht_stack;
3886 ht_stack = ht_stack->prev;
3887 free(tempitem);
3888 }
3889
3890 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003891}
3892
3893/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003894 * Mark a dict and its items with "copyID".
3895 * Returns TRUE if setting references failed somehow.
3896 */
3897 int
3898set_ref_in_dict(dict_T *d, int copyID)
3899{
3900 if (d != NULL && d->dv_copyID != copyID)
3901 {
3902 d->dv_copyID = copyID;
3903 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3904 }
3905 return FALSE;
3906}
3907
3908/*
3909 * Mark a list and its items with "copyID".
3910 * Returns TRUE if setting references failed somehow.
3911 */
3912 int
3913set_ref_in_list(list_T *ll, int copyID)
3914{
3915 if (ll != NULL && ll->lv_copyID != copyID)
3916 {
3917 ll->lv_copyID = copyID;
3918 return set_ref_in_list_items(ll, copyID, NULL);
3919 }
3920 return FALSE;
3921}
3922
3923/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003924 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003925 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3926 *
3927 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003928 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003929 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003930set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003931{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003932 listitem_T *li;
3933 int abort = FALSE;
3934 list_T *cur_l;
3935 list_stack_T *list_stack = NULL;
3936 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003937
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003938 cur_l = l;
3939 for (;;)
3940 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003941 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003942 // Mark each item in the list. If the item contains a hashtab
3943 // it is added to ht_stack, if it contains a list it is added to
3944 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003945 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
3946 abort = abort || set_ref_in_item(&li->li_tv, copyID,
3947 ht_stack, &list_stack);
3948 if (list_stack == NULL)
3949 break;
3950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003951 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003952 cur_l = list_stack->list;
3953 tempitem = list_stack;
3954 list_stack = list_stack->prev;
3955 free(tempitem);
3956 }
3957
3958 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003959}
3960
3961/*
3962 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003963 * "list_stack" is used to add lists to be marked. Can be NULL.
3964 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3965 *
3966 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003967 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003968 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003969set_ref_in_item(
3970 typval_T *tv,
3971 int copyID,
3972 ht_stack_T **ht_stack,
3973 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003974{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003975 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00003976
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003977 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003978 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003979 dict_T *dd = tv->vval.v_dict;
3980
Bram Moolenaara03f2332016-02-06 18:09:59 +01003981 if (dd != NULL && dd->dv_copyID != copyID)
3982 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003983 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003984 dd->dv_copyID = copyID;
3985 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003986 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003987 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
3988 }
3989 else
3990 {
3991 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
3992 if (newitem == NULL)
3993 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003994 else
3995 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003996 newitem->ht = &dd->dv_hashtab;
3997 newitem->prev = *ht_stack;
3998 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003999 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004000 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004001 }
4002 }
4003 else if (tv->v_type == VAR_LIST)
4004 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004005 list_T *ll = tv->vval.v_list;
4006
Bram Moolenaara03f2332016-02-06 18:09:59 +01004007 if (ll != NULL && ll->lv_copyID != copyID)
4008 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004009 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004010 ll->lv_copyID = copyID;
4011 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004012 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004013 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004014 }
4015 else
4016 {
4017 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004018 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004019 if (newitem == NULL)
4020 abort = TRUE;
4021 else
4022 {
4023 newitem->list = ll;
4024 newitem->prev = *list_stack;
4025 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004026 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004027 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004028 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004029 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004030 else if (tv->v_type == VAR_FUNC)
4031 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004032 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004033 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004034 else if (tv->v_type == VAR_PARTIAL)
4035 {
4036 partial_T *pt = tv->vval.v_partial;
4037 int i;
4038
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004039 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004040 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004041 // Didn't see this partial yet.
4042 pt->pt_copyID = copyID;
4043
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004044 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004045
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004046 if (pt->pt_dict != NULL)
4047 {
4048 typval_T dtv;
4049
4050 dtv.v_type = VAR_DICT;
4051 dtv.vval.v_dict = pt->pt_dict;
4052 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4053 }
4054
4055 for (i = 0; i < pt->pt_argc; ++i)
4056 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4057 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004058 if (pt->pt_funcstack != NULL)
4059 {
4060 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4061
4062 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4063 abort = abort || set_ref_in_item(stack + i, copyID,
4064 ht_stack, list_stack);
4065 }
4066
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004067 }
4068 }
4069#ifdef FEAT_JOB_CHANNEL
4070 else if (tv->v_type == VAR_JOB)
4071 {
4072 job_T *job = tv->vval.v_job;
4073 typval_T dtv;
4074
4075 if (job != NULL && job->jv_copyID != copyID)
4076 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004077 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004078 if (job->jv_channel != NULL)
4079 {
4080 dtv.v_type = VAR_CHANNEL;
4081 dtv.vval.v_channel = job->jv_channel;
4082 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4083 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004084 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004085 {
4086 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004087 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004088 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4089 }
4090 }
4091 }
4092 else if (tv->v_type == VAR_CHANNEL)
4093 {
4094 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004095 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004096 typval_T dtv;
4097 jsonq_T *jq;
4098 cbq_T *cq;
4099
4100 if (ch != NULL && ch->ch_copyID != copyID)
4101 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004102 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004103 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004104 {
4105 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4106 jq = jq->jq_next)
4107 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4108 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4109 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004110 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004111 {
4112 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004113 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004114 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4115 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004116 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004117 {
4118 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004119 dtv.vval.v_partial =
4120 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004121 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4122 }
4123 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004124 if (ch->ch_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 = ch->ch_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_close_cb.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 = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004134 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4135 }
4136 }
4137 }
4138#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004139 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004140}
4141
Bram Moolenaar8c711452005-01-14 21:53:12 +00004142/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004143 * Return a string with the string representation of a variable.
4144 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004145 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004146 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004147 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4148 * stings as "string()", otherwise does not put quotes around strings, as
4149 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004150 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4151 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004152 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004153 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004154 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004155echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004156 typval_T *tv,
4157 char_u **tofree,
4158 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004159 int copyID,
4160 int echo_style,
4161 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004162 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004163{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004164 static int recurse = 0;
4165 char_u *r = NULL;
4166
Bram Moolenaar33570922005-01-25 22:26:29 +00004167 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004168 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004169 if (!did_echo_string_emsg)
4170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004171 // Only give this message once for a recursive call to avoid
4172 // flooding the user with errors. And stop iterating over lists
4173 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004174 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004175 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004176 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004177 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004178 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004179 }
4180 ++recurse;
4181
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004182 switch (tv->v_type)
4183 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004184 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004185 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004186 {
4187 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004188 r = tv->vval.v_string;
4189 if (r == NULL)
4190 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004191 }
4192 else
4193 {
4194 *tofree = string_quote(tv->vval.v_string, FALSE);
4195 r = *tofree;
4196 }
4197 break;
4198
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004199 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004200 if (echo_style)
4201 {
4202 *tofree = NULL;
4203 r = tv->vval.v_string;
4204 }
4205 else
4206 {
4207 *tofree = string_quote(tv->vval.v_string, TRUE);
4208 r = *tofree;
4209 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004210 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004211
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004212 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004213 {
4214 partial_T *pt = tv->vval.v_partial;
4215 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004216 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004217 garray_T ga;
4218 int i;
4219 char_u *tf;
4220
4221 ga_init2(&ga, 1, 100);
4222 ga_concat(&ga, (char_u *)"function(");
4223 if (fname != NULL)
4224 {
4225 ga_concat(&ga, fname);
4226 vim_free(fname);
4227 }
4228 if (pt != NULL && pt->pt_argc > 0)
4229 {
4230 ga_concat(&ga, (char_u *)", [");
4231 for (i = 0; i < pt->pt_argc; ++i)
4232 {
4233 if (i > 0)
4234 ga_concat(&ga, (char_u *)", ");
4235 ga_concat(&ga,
4236 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4237 vim_free(tf);
4238 }
4239 ga_concat(&ga, (char_u *)"]");
4240 }
4241 if (pt != NULL && pt->pt_dict != NULL)
4242 {
4243 typval_T dtv;
4244
4245 ga_concat(&ga, (char_u *)", ");
4246 dtv.v_type = VAR_DICT;
4247 dtv.vval.v_dict = pt->pt_dict;
4248 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4249 vim_free(tf);
4250 }
4251 ga_concat(&ga, (char_u *)")");
4252
4253 *tofree = ga.ga_data;
4254 r = *tofree;
4255 break;
4256 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004257
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004258 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004259 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004260 break;
4261
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004262 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004263 if (tv->vval.v_list == NULL)
4264 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004265 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004266 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004267 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004268 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004269 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4270 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004271 {
4272 *tofree = NULL;
4273 r = (char_u *)"[...]";
4274 }
4275 else
4276 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004277 int old_copyID = tv->vval.v_list->lv_copyID;
4278
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004279 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004280 *tofree = list2string(tv, copyID, restore_copyID);
4281 if (restore_copyID)
4282 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004283 r = *tofree;
4284 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004285 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004286
Bram Moolenaar8c711452005-01-14 21:53:12 +00004287 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004288 if (tv->vval.v_dict == NULL)
4289 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004290 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004291 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004292 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004293 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004294 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4295 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004296 {
4297 *tofree = NULL;
4298 r = (char_u *)"{...}";
4299 }
4300 else
4301 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004302 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004303
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004304 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004305 *tofree = dict2string(tv, copyID, restore_copyID);
4306 if (restore_copyID)
4307 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004308 r = *tofree;
4309 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004310 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004311
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004313 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004314 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004316 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004317 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004318 break;
4319
Bram Moolenaar835dc632016-02-07 14:27:38 +01004320 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004321 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004322 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004323 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004324 if (composite_val)
4325 {
4326 *tofree = string_quote(r, FALSE);
4327 r = *tofree;
4328 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004329 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004330
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004331 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004332#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004333 *tofree = NULL;
4334 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4335 r = numbuf;
4336 break;
4337#endif
4338
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004339 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004340 case VAR_SPECIAL:
4341 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004342 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004343 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004344 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004345
Bram Moolenaar8502c702014-06-17 12:51:16 +02004346 if (--recurse == 0)
4347 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004348 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004349}
4350
4351/*
4352 * Return a string with the string representation of a variable.
4353 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4354 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004355 * Does not put quotes around strings, as ":echo" displays values.
4356 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4357 * May return NULL.
4358 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004359 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004360echo_string(
4361 typval_T *tv,
4362 char_u **tofree,
4363 char_u *numbuf,
4364 int copyID)
4365{
4366 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4367}
4368
4369/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004370 * Return string "str" in ' quotes, doubling ' characters.
4371 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004372 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004373 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004374 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004375string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004376{
Bram Moolenaar33570922005-01-25 22:26:29 +00004377 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004378 char_u *p, *r, *s;
4379
Bram Moolenaar33570922005-01-25 22:26:29 +00004380 len = (function ? 13 : 3);
4381 if (str != NULL)
4382 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004383 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004384 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004385 if (*p == '\'')
4386 ++len;
4387 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004388 s = r = alloc(len);
4389 if (r != NULL)
4390 {
4391 if (function)
4392 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004393 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004394 r += 10;
4395 }
4396 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004397 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004398 if (str != NULL)
4399 for (p = str; *p != NUL; )
4400 {
4401 if (*p == '\'')
4402 *r++ = '\'';
4403 MB_COPY_CHAR(p, r);
4404 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004405 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004406 if (function)
4407 *r++ = ')';
4408 *r++ = NUL;
4409 }
4410 return s;
4411}
4412
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004413#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004414/*
4415 * Convert the string "text" to a floating point number.
4416 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4417 * this always uses a decimal point.
4418 * Returns the length of the text that was consumed.
4419 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004420 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004421string2float(
4422 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004423 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004424{
4425 char *s = (char *)text;
4426 float_T f;
4427
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004428 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004429 if (STRNICMP(text, "inf", 3) == 0)
4430 {
4431 *value = INFINITY;
4432 return 3;
4433 }
4434 if (STRNICMP(text, "-inf", 3) == 0)
4435 {
4436 *value = -INFINITY;
4437 return 4;
4438 }
4439 if (STRNICMP(text, "nan", 3) == 0)
4440 {
4441 *value = NAN;
4442 return 3;
4443 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004444 f = strtod(s, &s);
4445 *value = f;
4446 return (int)((char_u *)s - text);
4447}
4448#endif
4449
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004452 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004454 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004455var2fpos(
4456 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004457 int dollar_lnum, // TRUE when $ is last line
4458 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004460 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004462 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004464 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004465 if (varp->v_type == VAR_LIST)
4466 {
4467 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004468 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004469 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004470 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004471
4472 l = varp->vval.v_list;
4473 if (l == NULL)
4474 return NULL;
4475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004476 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004477 pos.lnum = list_find_nr(l, 0L, &error);
4478 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004481 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004482 pos.col = list_find_nr(l, 1L, &error);
4483 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004484 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004485 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004487 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004488 li = list_find(l, 1L);
4489 if (li != NULL && li->li_tv.v_type == VAR_STRING
4490 && li->li_tv.vval.v_string != NULL
4491 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4492 pos.col = len + 1;
4493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004494 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004495 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004496 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004497 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004498
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004499 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004500 pos.coladd = list_find_nr(l, 2L, &error);
4501 if (error)
4502 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004503
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004504 return &pos;
4505 }
4506
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004507 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004508 if (name == NULL)
4509 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004510 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004512 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004513 {
4514 if (VIsual_active)
4515 return &VIsual;
4516 return &curwin->w_cursor;
4517 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004518 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004520 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4522 return NULL;
4523 return pp;
4524 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004525
Bram Moolenaara5525202006-03-02 22:52:09 +00004526 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004527
Bram Moolenaar477933c2007-07-17 14:32:23 +00004528 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004529 {
4530 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004531 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004532 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004533 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004534 // In silent Ex mode topline is zero, but that's not a valid line
4535 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004536 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004537 return &pos;
4538 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004539 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004540 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004541 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004542 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004543 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004544 return &pos;
4545 }
4546 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004547 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004549 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 {
4551 pos.lnum = curbuf->b_ml.ml_line_count;
4552 pos.col = 0;
4553 }
4554 else
4555 {
4556 pos.lnum = curwin->w_cursor.lnum;
4557 pos.col = (colnr_T)STRLEN(ml_get_curline());
4558 }
4559 return &pos;
4560 }
4561 return NULL;
4562}
4563
4564/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004565 * Convert list in "arg" into a position and optional file number.
4566 * When "fnump" is NULL there is no file number, only 3 items.
4567 * Note that the column is passed on as-is, the caller may want to decrement
4568 * it to use 1 for the first column.
4569 * Return FAIL when conversion is not possible, doesn't check the position for
4570 * validity.
4571 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004572 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004573list2fpos(
4574 typval_T *arg,
4575 pos_T *posp,
4576 int *fnump,
4577 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004578{
4579 list_T *l = arg->vval.v_list;
4580 long i = 0;
4581 long n;
4582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004583 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4584 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004585 if (arg->v_type != VAR_LIST
4586 || l == NULL
4587 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004588 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004589 return FAIL;
4590
4591 if (fnump != NULL)
4592 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004593 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004594 if (n < 0)
4595 return FAIL;
4596 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004597 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004598 *fnump = n;
4599 }
4600
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004601 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004602 if (n < 0)
4603 return FAIL;
4604 posp->lnum = n;
4605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004606 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004607 if (n < 0)
4608 return FAIL;
4609 posp->col = n;
4610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004611 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004612 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004613 posp->coladd = 0;
4614 else
4615 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004616
Bram Moolenaar493c1782014-05-28 14:34:46 +02004617 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004618 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004619
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004620 return OK;
4621}
4622
4623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 * Get the length of an environment variable name.
4625 * Advance "arg" to the first character after the name.
4626 * Return 0 for error.
4627 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004629get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 char_u *p;
4632 int len;
4633
4634 for (p = *arg; vim_isIDc(*p); ++p)
4635 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004636 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 return 0;
4638
4639 len = (int)(p - *arg);
4640 *arg = p;
4641 return len;
4642}
4643
4644/*
4645 * Get the length of the name of a function or internal variable.
4646 * "arg" is advanced to the first non-white character after the name.
4647 * Return 0 if something is wrong.
4648 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004649 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004650get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651{
4652 char_u *p;
4653 int len;
4654
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004655 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004657 {
4658 if (*p == ':')
4659 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004660 // "s:" is start of "s:var", but "n:" is not and can be used in
4661 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004662 len = (int)(p - *arg);
4663 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4664 || len > 1)
4665 break;
4666 }
4667 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004668 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 return 0;
4670
4671 len = (int)(p - *arg);
4672 *arg = skipwhite(p);
4673
4674 return len;
4675}
4676
4677/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004678 * Get the length of the name of a variable or function.
4679 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004681 * Return -1 if curly braces expansion failed.
4682 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 * If the name contains 'magic' {}'s, expand them and return the
4684 * expanded name in an allocated string via 'alias' - caller must free.
4685 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004686 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004687get_name_len(
4688 char_u **arg,
4689 char_u **alias,
4690 int evaluate,
4691 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
4693 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 char_u *p;
4695 char_u *expr_start;
4696 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004698 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
4700 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4701 && (*arg)[2] == (int)KE_SNR)
4702 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004703 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 *arg += 3;
4705 return get_id_len(arg) + 3;
4706 }
4707 len = eval_fname_script(*arg);
4708 if (len > 0)
4709 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004710 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 *arg += len;
4712 }
4713
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004715 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004717 p = find_name_end(*arg, &expr_start, &expr_end,
4718 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 if (expr_start != NULL)
4720 {
4721 char_u *temp_string;
4722
4723 if (!evaluate)
4724 {
4725 len += (int)(p - *arg);
4726 *arg = skipwhite(p);
4727 return len;
4728 }
4729
4730 /*
4731 * Include any <SID> etc in the expanded string:
4732 * Thus the -len here.
4733 */
4734 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4735 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004736 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 *alias = temp_string;
4738 *arg = skipwhite(p);
4739 return (int)STRLEN(temp_string);
4740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741
4742 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004743 // Only give an error when there is something, otherwise it will be
4744 // reported at a higher level.
4745 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004746 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
4748 return len;
4749}
4750
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004751/*
4752 * Find the end of a variable or function name, taking care of magic braces.
4753 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4754 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004755 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 * Return a pointer to just after the name. Equal to "arg" if there is no
4757 * valid name.
4758 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004759 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004760find_name_end(
4761 char_u *arg,
4762 char_u **expr_start,
4763 char_u **expr_end,
4764 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 int mb_nest = 0;
4767 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004769 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004770 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 *expr_start = NULL;
4775 *expr_end = NULL;
4776 }
4777
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004778 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004779 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4780 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004781 return arg;
4782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 for (p = arg; *p != NUL
4784 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004785 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004786 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004788 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004789 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004790 if (*p == '\'')
4791 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004792 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004793 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004794 ;
4795 if (*p == NUL)
4796 break;
4797 }
4798 else if (*p == '"')
4799 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004800 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004801 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004802 if (*p == '\\' && p[1] != NUL)
4803 ++p;
4804 if (*p == NUL)
4805 break;
4806 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004807 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4808 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004809 // "s:" is start of "s:var", but "n:" is not and can be used in
4810 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004811 len = (int)(p - arg);
4812 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004813 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004814 break;
4815 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004816
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004817 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819 if (*p == '[')
4820 ++br_nest;
4821 else if (*p == ']')
4822 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004824
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004825 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 if (*p == '{')
4828 {
4829 mb_nest++;
4830 if (expr_start != NULL && *expr_start == NULL)
4831 *expr_start = p;
4832 }
4833 else if (*p == '}')
4834 {
4835 mb_nest--;
4836 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4837 *expr_end = p;
4838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841
4842 return p;
4843}
4844
4845/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004846 * Expands out the 'magic' {}'s in a variable/function name.
4847 * Note that this can call itself recursively, to deal with
4848 * constructs like foo{bar}{baz}{bam}
4849 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4850 * "in_start" ^
4851 * "expr_start" ^
4852 * "expr_end" ^
4853 * "in_end" ^
4854 *
4855 * Returns a new allocated string, which the caller must free.
4856 * Returns NULL for failure.
4857 */
4858 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004859make_expanded_name(
4860 char_u *in_start,
4861 char_u *expr_start,
4862 char_u *expr_end,
4863 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004864{
4865 char_u c1;
4866 char_u *retval = NULL;
4867 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004868
4869 if (expr_end == NULL || in_end == NULL)
4870 return NULL;
4871 *expr_start = NUL;
4872 *expr_end = NUL;
4873 c1 = *in_end;
4874 *in_end = NUL;
4875
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004876 temp_result = eval_to_string(expr_start + 1, FALSE);
4877 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004878 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004879 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4880 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004881 if (retval != NULL)
4882 {
4883 STRCPY(retval, in_start);
4884 STRCAT(retval, temp_result);
4885 STRCAT(retval, expr_end + 1);
4886 }
4887 }
4888 vim_free(temp_result);
4889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004890 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004891 *expr_start = '{';
4892 *expr_end = '}';
4893
4894 if (retval != NULL)
4895 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004896 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004897 if (expr_start != NULL)
4898 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004899 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004900 temp_result = make_expanded_name(retval, expr_start,
4901 expr_end, temp_result);
4902 vim_free(retval);
4903 retval = temp_result;
4904 }
4905 }
4906
4907 return retval;
4908}
4909
4910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004912 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004914 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004915eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004917 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4918}
4919
4920/*
4921 * Return TRUE if character "c" can be used as the first character in a
4922 * variable or function name (excluding '{' and '}').
4923 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004925eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004926{
4927 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928}
4929
4930/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004931 * Handle:
4932 * - expr[expr], expr[expr:expr] subscript
4933 * - ".name" lookup
4934 * - function call with Funcref variable: func(expr)
4935 * - method call: var->method()
4936 *
4937 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004938 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004939 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004940handle_subscript(
4941 char_u **arg,
4942 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004943 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004944 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004945{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004946 int evaluate = evalarg != NULL
4947 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004948 int ret = OK;
4949 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004950
Bram Moolenaar61343f02019-07-20 21:11:13 +02004951 // "." is ".name" lookup when we found a dict or when evaluating and
4952 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004953 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02004954 && (((**arg == '['
4955 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02004956 || (!evaluate
4957 && (*arg)[1] != '.'
4958 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004959 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004960 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004961 && !VIM_ISWHITE(*(*arg - 1)))
4962 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004963 {
4964 if (**arg == '(')
4965 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004966 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01004967
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004968 // Stop the expression evaluation when immediately aborting on
4969 // error, or when an interrupt occurred or an exception was thrown
4970 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004971 if (aborting())
4972 {
4973 if (ret == OK)
4974 clear_tv(rettv);
4975 ret = FAIL;
4976 }
4977 dict_unref(selfdict);
4978 selfdict = NULL;
4979 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004980 else if (**arg == '-')
4981 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004982 if (ret == OK)
4983 {
4984 if ((*arg)[2] == '{')
4985 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004986 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004987 else
4988 // expr->name()
4989 ret = eval_method(arg, rettv, evaluate, verbose);
4990 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004991 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004992 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004993 {
4994 dict_unref(selfdict);
4995 if (rettv->v_type == VAR_DICT)
4996 {
4997 selfdict = rettv->vval.v_dict;
4998 if (selfdict != NULL)
4999 ++selfdict->dv_refcount;
5000 }
5001 else
5002 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005003 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005004 {
5005 clear_tv(rettv);
5006 ret = FAIL;
5007 }
5008 }
5009 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005010
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005011 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5012 // Don't do this when "Func" is already a partial that was bound
5013 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005014 if (selfdict != NULL
5015 && (rettv->v_type == VAR_FUNC
5016 || (rettv->v_type == VAR_PARTIAL
5017 && (rettv->vval.v_partial->pt_auto
5018 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005019 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005020
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005021 dict_unref(selfdict);
5022 return ret;
5023}
5024
5025/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005026 * Make a copy of an item.
5027 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005028 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5029 * reference to an already copied list/dict can be used.
5030 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005031 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005032 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005033item_copy(
5034 typval_T *from,
5035 typval_T *to,
5036 int deep,
5037 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005038{
5039 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005040 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005041
Bram Moolenaar33570922005-01-25 22:26:29 +00005042 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005043 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005044 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005045 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005046 }
5047 ++recurse;
5048
5049 switch (from->v_type)
5050 {
5051 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005052 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005053 case VAR_STRING:
5054 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005055 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005056 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005057 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005058 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005059 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005060 copy_tv(from, to);
5061 break;
5062 case VAR_LIST:
5063 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005064 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005065 if (from->vval.v_list == NULL)
5066 to->vval.v_list = NULL;
5067 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5068 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005069 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005070 to->vval.v_list = from->vval.v_list->lv_copylist;
5071 ++to->vval.v_list->lv_refcount;
5072 }
5073 else
5074 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5075 if (to->vval.v_list == NULL)
5076 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005077 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005078 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005080 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005081 case VAR_DICT:
5082 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005083 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005084 if (from->vval.v_dict == NULL)
5085 to->vval.v_dict = NULL;
5086 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5087 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005088 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005089 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5090 ++to->vval.v_dict->dv_refcount;
5091 }
5092 else
5093 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5094 if (to->vval.v_dict == NULL)
5095 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005096 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005097 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005098 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005100 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005101 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005102 }
5103 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005104 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005105}
5106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 void
5108echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5109{
5110 char_u *tofree;
5111 char_u numbuf[NUMBUFLEN];
5112 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5113
5114 if (*atstart)
5115 {
5116 *atstart = FALSE;
5117 // Call msg_start() after eval1(), evaluating the expression
5118 // may cause a message to appear.
5119 if (with_space)
5120 {
5121 // Mark the saved text as finishing the line, so that what
5122 // follows is displayed on a new line when scrolling back
5123 // at the more prompt.
5124 msg_sb_eol();
5125 msg_start();
5126 }
5127 }
5128 else if (with_space)
5129 msg_puts_attr(" ", echo_attr);
5130
5131 if (p != NULL)
5132 for ( ; *p != NUL && !got_int; ++p)
5133 {
5134 if (*p == '\n' || *p == '\r' || *p == TAB)
5135 {
5136 if (*p != TAB && *needclr)
5137 {
5138 // remove any text still there from the command
5139 msg_clr_eos();
5140 *needclr = FALSE;
5141 }
5142 msg_putchar_attr(*p, echo_attr);
5143 }
5144 else
5145 {
5146 if (has_mbyte)
5147 {
5148 int i = (*mb_ptr2len)(p);
5149
5150 (void)msg_outtrans_len_attr(p, i, echo_attr);
5151 p += i - 1;
5152 }
5153 else
5154 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5155 }
5156 }
5157 vim_free(tofree);
5158}
5159
Bram Moolenaare9a41262005-01-15 22:18:47 +00005160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 * ":echo expr1 ..." print each argument separated with a space, add a
5162 * newline at the end.
5163 * ":echon expr1 ..." print each argument plain.
5164 */
5165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005166ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
5168 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005169 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 char_u *p;
5171 int needclr = TRUE;
5172 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005173 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005174 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005175 evalarg_T evalarg;
5176
5177 CLEAR_FIELD(evalarg);
5178 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
5180 if (eap->skip)
5181 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005182 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005184 // If eval1() causes an error message the text from the command may
5185 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005186 need_clr_eos = needclr;
5187
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005189 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
5191 /*
5192 * Report the invalid expression unless the expression evaluation
5193 * has been cancelled due to an aborting error, an interrupt, or an
5194 * exception.
5195 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005196 if (!aborting() && did_emsg == did_emsg_before
5197 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005198 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005199 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 break;
5201 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005202 need_clr_eos = FALSE;
5203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005206
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 arg = skipwhite(arg);
5209 }
5210 eap->nextcmd = check_nextcmd(arg);
5211
5212 if (eap->skip)
5213 --emsg_skip;
5214 else
5215 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005216 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (needclr)
5218 msg_clr_eos();
5219 if (eap->cmdidx == CMD_echo)
5220 msg_end();
5221 }
5222}
5223
5224/*
5225 * ":echohl {name}".
5226 */
5227 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005228ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005230 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231}
5232
5233/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005234 * Returns the :echo attribute
5235 */
5236 int
5237get_echo_attr(void)
5238{
5239 return echo_attr;
5240}
5241
5242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 * ":execute expr1 ..." execute the result of an expression.
5244 * ":echomsg expr1 ..." Print a message
5245 * ":echoerr expr1 ..." Print an error
5246 * Each gets spaces around each argument and a newline at the end for
5247 * echo commands
5248 */
5249 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005250ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
5252 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005253 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 int ret = OK;
5255 char_u *p;
5256 garray_T ga;
5257 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
5259 ga_init2(&ga, 1, 80);
5260
5261 if (eap->skip)
5262 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005263 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005265 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5266 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268
5269 if (!eap->skip)
5270 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005271 char_u buf[NUMBUFLEN];
5272
5273 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005274 {
5275 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5276 {
5277 emsg(_(e_inval_string));
5278 p = NULL;
5279 }
5280 else
5281 p = tv_get_string_buf(&rettv, buf);
5282 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005283 else
5284 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005285 if (p == NULL)
5286 {
5287 clear_tv(&rettv);
5288 ret = FAIL;
5289 break;
5290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 len = (int)STRLEN(p);
5292 if (ga_grow(&ga, len + 2) == FAIL)
5293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005294 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 ret = FAIL;
5296 break;
5297 }
5298 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 ga.ga_len += len;
5302 }
5303
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005304 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 arg = skipwhite(arg);
5306 }
5307
5308 if (ret != FAIL && ga.ga_data != NULL)
5309 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005310 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5311 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005312 // Mark the already saved text as finishing the line, so that what
5313 // follows is displayed on a new line when scrolling back at the
5314 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005315 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005316 }
5317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005319 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005320 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005321 out_flush();
5322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 else if (eap->cmdidx == CMD_echoerr)
5324 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005325 int save_did_emsg = did_emsg;
5326
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005327 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005328 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 if (!force_abort)
5330 did_emsg = save_did_emsg;
5331 }
5332 else if (eap->cmdidx == CMD_execute)
5333 do_cmdline((char_u *)ga.ga_data,
5334 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5335 }
5336
5337 ga_clear(&ga);
5338
5339 if (eap->skip)
5340 --emsg_skip;
5341
5342 eap->nextcmd = check_nextcmd(arg);
5343}
5344
5345/*
5346 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5347 * "arg" points to the "&" or '+' when called, to "option" when returning.
5348 * Returns NULL when no option name found. Otherwise pointer to the char
5349 * after the option name.
5350 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005351 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005352find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 char_u *p = *arg;
5355
5356 ++p;
5357 if (*p == 'g' && p[1] == ':')
5358 {
5359 *opt_flags = OPT_GLOBAL;
5360 p += 2;
5361 }
5362 else if (*p == 'l' && p[1] == ':')
5363 {
5364 *opt_flags = OPT_LOCAL;
5365 p += 2;
5366 }
5367 else
5368 *opt_flags = 0;
5369
5370 if (!ASCII_ISALPHA(*p))
5371 return NULL;
5372 *arg = p;
5373
5374 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005375 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 else
5377 while (ASCII_ISALPHA(*p))
5378 ++p;
5379 return p;
5380}
5381
5382/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005383 * Display script name where an item was last set.
5384 * Should only be invoked when 'verbose' is non-zero.
5385 */
5386 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005387last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005388{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005389 char_u *p;
5390
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005391 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005392 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005393 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005394 if (p != NULL)
5395 {
5396 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005397 msg_puts(_("\n\tLast set from "));
5398 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005399 if (script_ctx.sc_lnum > 0)
5400 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005401 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005402 msg_outnum((long)script_ctx.sc_lnum);
5403 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005404 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005405 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005406 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005407 }
5408}
5409
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005410#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
5412/*
5413 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005414 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 * "flags" can be "g" to do a global substitute.
5416 * Returns an allocated string, NULL for error.
5417 */
5418 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005419do_string_sub(
5420 char_u *str,
5421 char_u *pat,
5422 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005423 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005424 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425{
5426 int sublen;
5427 regmatch_T regmatch;
5428 int i;
5429 int do_all;
5430 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005431 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 garray_T ga;
5433 char_u *ret;
5434 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005435 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005437 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005439 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
5441 ga_init2(&ga, 1, 200);
5442
5443 do_all = (flags[0] == 'g');
5444
5445 regmatch.rm_ic = p_ic;
5446 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5447 if (regmatch.regprog != NULL)
5448 {
5449 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005450 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5452 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005454 if (regmatch.startp[0] == regmatch.endp[0])
5455 {
5456 if (zero_width == regmatch.startp[0])
5457 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005458 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005459 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005460 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5461 (size_t)i);
5462 ga.ga_len += i;
5463 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005464 continue;
5465 }
5466 zero_width = regmatch.startp[0];
5467 }
5468
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 /*
5470 * Get some space for a temporary buffer to do the substitution
5471 * into. It will contain:
5472 * - The text up to where the match is.
5473 * - The substituted text.
5474 * - The text after the match.
5475 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005476 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005477 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5479 {
5480 ga_clear(&ga);
5481 break;
5482 }
5483
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005484 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 i = (int)(regmatch.startp[0] - tail);
5486 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005487 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005488 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 + ga.ga_len + i, TRUE, TRUE, FALSE);
5490 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005491 tail = regmatch.endp[0];
5492 if (*tail == NUL)
5493 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 if (!do_all)
5495 break;
5496 }
5497
5498 if (ga.ga_data != NULL)
5499 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5500
Bram Moolenaar473de612013-06-08 18:19:48 +02005501 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 }
5503
5504 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5505 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005506 if (p_cpo == empty_option)
5507 p_cpo = save_cpo;
5508 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005509 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005510 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511
5512 return ret;
5513}