blob: 595cd06663946b9f7e535642cee903ace69cf811 [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
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020041 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010042 listwatch_T fi_lw; // keep an eye on the item used.
43 list_T *fi_list; // list being used
44 int fi_bi; // index of blob
45 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
54static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static 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 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
62 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020063 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010064num_divide(varnumber_T n1, varnumber_T n2)
65{
66 varnumber_T result;
67
68 if (n2 == 0) // give an error message?
69 {
70 if (n1 == 0)
71 result = VARNUM_MIN; // similar to NaN
72 else if (n1 < 0)
73 result = -VARNUM_MAX;
74 else
75 result = VARNUM_MAX;
76 }
77 else
78 result = n1 / n2;
79
80 return result;
81}
82
83/*
84 * Return "n1" modulus "n2", taking care of dividing by zero.
85 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020086 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010087num_modulus(varnumber_T n1, varnumber_T n2)
88{
89 // Give an error when n2 is 0?
90 return (n2 == 0) ? 0 : (n1 % n2);
91}
92
Bram Moolenaara1fa8922017-01-12 20:06:33 +010093#if defined(EBCDIC) || defined(PROTO)
94/*
95 * Compare struct fst by function name.
96 */
97 static int
98compare_func_name(const void *s1, const void *s2)
99{
100 struct fst *p1 = (struct fst *)s1;
101 struct fst *p2 = (struct fst *)s2;
102
103 return STRCMP(p1->f_name, p2->f_name);
104}
105
106/*
107 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100108 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100109 * On machines using EBCDIC we have to sort it.
110 */
111 static void
112sortFunctions(void)
113{
114 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
115
116 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
117}
118#endif
119
Bram Moolenaar33570922005-01-25 22:26:29 +0000120/*
121 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000122 */
123 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100124eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000125{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200127 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000128
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200129#ifdef EBCDIC
130 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100131 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200132 */
133 sortFunctions();
134#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000135}
136
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000137#if defined(EXITFREE) || defined(PROTO)
138 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100139eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200141 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100142 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200143 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000144
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200145 // autoloaded script names
146 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000147
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200148 // unreferenced lists and dicts
149 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200150
151 // functions not garbage collected
152 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000153}
154#endif
155
Bram Moolenaare6b53242020-07-01 17:28:33 +0200156 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200157fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
158{
159 CLEAR_FIELD(*evalarg);
160 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
161 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
162 {
163 evalarg->eval_getline = eap->getline;
164 evalarg->eval_cookie = eap->cookie;
165 }
166}
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
169 * Top level evaluation function, returning a boolean.
170 * Sets "error" to TRUE if there was an error.
171 * Return TRUE or FALSE.
172 */
173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100174eval_to_bool(
175 char_u *arg,
176 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200177 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100178 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179{
Bram Moolenaar33570922005-01-25 22:26:29 +0000180 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200181 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200182 evalarg_T evalarg;
183
Bram Moolenaar37c83712020-06-30 21:18:36 +0200184 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186 if (skip)
187 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200188 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 else
191 {
192 *error = FALSE;
193 if (!skip)
194 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100195 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000196 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 }
198 }
199 if (skip)
200 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200201 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200203 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204}
205
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206/*
207 * Call eval1() and give an error message if not done at a lower level.
208 */
209 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200210eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100211{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100212 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100213 int ret;
214 int did_emsg_before = did_emsg;
215 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200216 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100217
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200218 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
219
220 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100221 if (ret == FAIL)
222 {
223 // Report the invalid expression unless the expression evaluation has
224 // been cancelled due to an aborting error, an interrupt, or an
225 // exception, or we already gave a more specific error.
226 // Also check called_emsg for when using assert_fails().
227 if (!aborting() && did_emsg == did_emsg_before
228 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100229 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100230 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200231 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100232 return ret;
233}
234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200236 * Return whether a typval is a valid expression to pass to eval_expr_typval()
237 * or eval_expr_to_bool(). An empty string returns FALSE;
238 */
239 int
240eval_expr_valid_arg(typval_T *tv)
241{
242 return tv->v_type != VAR_UNKNOWN
243 && (tv->v_type != VAR_STRING
244 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
245}
246
247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 * Evaluate an expression, which can be a function, partial or string.
249 * Pass arguments "argv[argc]".
250 * Return the result in "rettv" and OK or FAIL.
251 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200252 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100253eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
254{
255 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100256 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200257 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100258
259 if (expr->v_type == VAR_FUNC)
260 {
261 s = expr->vval.v_string;
262 if (s == NULL || *s == NUL)
263 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200264 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200265 funcexe.evaluate = TRUE;
266 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267 return FAIL;
268 }
269 else if (expr->v_type == VAR_PARTIAL)
270 {
271 partial_T *partial = expr->vval.v_partial;
272
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200273 if (partial == NULL)
274 return FAIL;
275
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200277 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200279 if (call_def_function(partial->pt_func, argc, argv,
280 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 return FAIL;
282 }
283 else
284 {
285 s = partial_name(partial);
286 if (s == NULL || *s == NUL)
287 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200288 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 funcexe.evaluate = TRUE;
290 funcexe.partial = partial;
291 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
292 return FAIL;
293 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 }
295 else
296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100297 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 if (s == NULL)
299 return FAIL;
300 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200301 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100303 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100304 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200305 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100306 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100307 return FAIL;
308 }
309 }
310 return OK;
311}
312
313/*
314 * Like eval_to_bool() but using a typval_T instead of a string.
315 * Works for string, funcref and partial.
316 */
317 int
318eval_expr_to_bool(typval_T *expr, int *error)
319{
320 typval_T rettv;
321 int res;
322
323 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
324 {
325 *error = TRUE;
326 return FALSE;
327 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100328 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100329 clear_tv(&rettv);
330 return res;
331}
332
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333/*
334 * Top level evaluation function, returning a string. If "skip" is TRUE,
335 * only parsing to "nextcmd" is done, without reporting errors. Return
336 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
337 */
338 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100339eval_to_string_skip(
340 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200341 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100342 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
Bram Moolenaar33570922005-01-25 22:26:29 +0000344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200346 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar37c83712020-06-30 21:18:36 +0200348 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 if (skip)
350 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200351 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 retval = NULL;
353 else
354 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100355 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000356 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 }
358 if (skip)
359 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200360 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
362 return retval;
363}
364
365/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366 * Skip over an expression at "*pp".
367 * Return FAIL for an error, OK otherwise.
368 */
369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100370skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000371{
Bram Moolenaar33570922005-01-25 22:26:29 +0000372 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000373
374 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200375 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000376}
377
378/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200379 * Skip over an expression at "*pp".
380 * If in Vim9 script and line breaks are encountered, the lines are
381 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200382 * "arg" is advanced to just after the expression.
383 * "start" is set to the start of the expression, "end" to just after the end.
384 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200385 * Return FAIL for an error, OK otherwise.
386 */
387 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200388skip_expr_concatenate(
389 char_u **arg,
390 char_u **start,
391 char_u **end,
392 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200393{
394 typval_T rettv;
395 int res;
396 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
397 garray_T *gap = &evalarg->eval_ga;
398 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
399
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200400 if (vim9script
401 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 {
403 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 ++gap->ga_len;
407 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200408 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409
410 // Don't evaluate the expression.
411 if (evalarg != NULL)
412 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200413 *arg = skipwhite(*arg);
414 res = eval1(arg, &rettv, evalarg);
415 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200416 if (evalarg != NULL)
417 evalarg->eval_flags = save_flags;
418
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 if (vim9script
420 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200421 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200422 if (evalarg->eval_ga.ga_len == 1)
423 {
424 // just one line, no need to concatenate
425 ga_clear(gap);
426 gap->ga_itemsize = 0;
427 }
428 else
429 {
430 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200431 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200432
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200433 // Line breaks encountered, concatenate all the lines.
434 *((char_u **)gap->ga_data) = *start;
435 p = ga_concat_strings(gap, "");
436
437 // free the lines only when using getsourceline()
438 if (evalarg->eval_cookie != NULL)
439 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200440 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200441 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200442 // Do not free the last line, "arg" points into it, free it
443 // later.
444 vim_free(evalarg->eval_tofree);
445 evalarg->eval_tofree =
446 ((char_u **)gap->ga_data)[gap->ga_len - 1];
447 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200448 ga_clear_strings(gap);
449 }
450 else
451 ga_clear(gap);
452 gap->ga_itemsize = 0;
453 if (p == NULL)
454 return FAIL;
455 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200456 vim_free(evalarg->eval_tofree_lambda);
457 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200458 // Compute "end" relative to the end.
459 *end = *start + STRLEN(*start) - endoff;
460 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461 }
462
463 return res;
464}
465
466/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200467 * Top level evaluation function, returning a string. Does not handle line
468 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000469 * When "convert" is TRUE convert a List into a sequence of lines and convert
470 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 * Return pointer to allocated memory, or NULL for failure.
472 */
473 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100474eval_to_string(
475 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100476 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477{
Bram Moolenaar33570922005-01-25 22:26:29 +0000478 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000480 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000481#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000482 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200485 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 retval = NULL;
487 else
488 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000489 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000490 {
491 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000492 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200493 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200494 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200495 if (tv.vval.v_list->lv_len > 0)
496 ga_append(&ga, NL);
497 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000498 ga_append(&ga, NUL);
499 retval = (char_u *)ga.ga_data;
500 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000501#ifdef FEAT_FLOAT
502 else if (convert && tv.v_type == VAR_FLOAT)
503 {
504 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
505 retval = vim_strsave(numbuf);
506 }
507#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000508 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100509 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000510 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200512 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
514 return retval;
515}
516
517/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000518 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200519 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 */
521 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100522eval_to_string_safe(
523 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100524 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
526 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200527 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200529 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000530 if (use_sandbox)
531 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200532 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200533 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000534 if (use_sandbox)
535 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200536 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200537 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 return retval;
539}
540
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541/*
542 * Top level evaluation function, returning a number.
543 * Evaluates "expr" silently.
544 * Returns -1 for an error.
545 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200546 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100547eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
Bram Moolenaar33570922005-01-25 22:26:29 +0000549 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200550 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000551 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
553 ++emsg_off;
554
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200555 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 retval = -1;
557 else
558 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100559 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000560 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 }
562 --emsg_off;
563
564 return retval;
565}
566
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000567/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000568 * Top level evaluation function.
569 * Returns an allocated typval_T with the result.
570 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000571 */
572 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200573eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000574{
575 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200576 evalarg_T evalarg;
577
578 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000579
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200580 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200581 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100582 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000583
Bram Moolenaar37c83712020-06-30 21:18:36 +0200584 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000585 return tv;
586}
587
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100589 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200590 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
591 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000592 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200594 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100595call_vim_function(
596 char_u *func,
597 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200598 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200599 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000601 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200602 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100604 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200605 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200606 funcexe.firstline = curwin->w_cursor.lnum;
607 funcexe.lastline = curwin->w_cursor.lnum;
608 funcexe.evaluate = TRUE;
609 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000610 if (ret == FAIL)
611 clear_tv(rettv);
612
613 return ret;
614}
615
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100616/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100617 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100618 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200619 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
620 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100621 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200622 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100623call_func_retnr(
624 char_u *func,
625 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200626 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100627{
628 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200629 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100630
Bram Moolenaarded27a12018-08-01 19:06:03 +0200631 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100632 return -1;
633
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100634 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100635 clear_tv(&rettv);
636 return retval;
637}
638
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000639/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100640 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000641 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200642 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
643 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000644 */
645 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100646call_func_retstr(
647 char_u *func,
648 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200649 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000650{
651 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000652 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000653
Bram Moolenaarded27a12018-08-01 19:06:03 +0200654 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000655 return NULL;
656
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100657 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000658 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 return retval;
660}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000661
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000662/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100663 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200664 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
665 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000666 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000667 */
668 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100669call_func_retlist(
670 char_u *func,
671 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200672 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000673{
674 typval_T rettv;
675
Bram Moolenaarded27a12018-08-01 19:06:03 +0200676 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000677 return NULL;
678
679 if (rettv.v_type != VAR_LIST)
680 {
681 clear_tv(&rettv);
682 return NULL;
683 }
684
685 return rettv.vval.v_list;
686}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#ifdef FEAT_FOLDING
689/*
690 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
691 * it in "*cp". Doesn't give error messages.
692 */
693 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100694eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695{
Bram Moolenaar33570922005-01-25 22:26:29 +0000696 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200697 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000699 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
700 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
702 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000703 if (use_sandbox)
704 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200705 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200707 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 retval = 0;
709 else
710 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100711 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000712 if (tv.v_type == VAR_NUMBER)
713 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000714 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 retval = 0;
716 else
717 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100718 // If the result is a string, check if there is a non-digit before
719 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000720 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 if (!VIM_ISDIGIT(*s) && *s != '-')
722 *cp = *s++;
723 retval = atol((char *)s);
724 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000725 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 }
727 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000728 if (use_sandbox)
729 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200730 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200731 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200733 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734}
735#endif
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000738 * Get an lval: variable, Dict item or List item that can be assigned a value
739 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
740 * "name.key", "name.key[expr]" etc.
741 * Indexing only works if "name" is an existing List or Dictionary.
742 * "name" points to the start of the name.
743 * If "rettv" is not NULL it points to the value to be assigned.
744 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
745 * wrong; must end in space or cmd separator.
746 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100747 * flags:
748 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100749 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100750 * GLV_NO_AUTOLOAD: do not use script autoloading
751 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000752 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000753 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000754 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000755 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200756 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100757get_lval(
758 char_u *name,
759 typval_T *rettv,
760 lval_T *lp,
761 int unlet,
762 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100763 int flags, // GLV_ values
764 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000765{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000766 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000767 char_u *expr_start, *expr_end;
768 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000769 dictitem_T *v;
770 typval_T var1;
771 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000773 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000774 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000775 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000776 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100777 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000778
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100779 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200780 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000781
782 if (skip)
783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100784 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000785 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200786 lp->ll_name_end = find_name_end(name, NULL, NULL,
787 FNE_INCL_BR | fne_flags);
788 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000789 }
790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100791 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000792 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000794 if (expr_start != NULL)
795 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100796 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100797 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000798 && *p != '[' && *p != '.')
799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100800 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 return NULL;
802 }
803
804 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
805 if (lp->ll_exp_name == NULL)
806 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100807 // Report an invalid expression in braces, unless the
808 // expression evaluation has been cancelled due to an
809 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000810 if (!aborting() && !quiet)
811 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000812 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100813 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000814 return NULL;
815 }
816 }
817 lp->ll_name = lp->ll_exp_name;
818 }
819 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 lp->ll_name = name;
822
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
824 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100825 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 char_u *tp = skipwhite(p + 1);
827
828 // parse the type after the name
829 lp->ll_type = parse_type(&tp, &si->sn_type_list);
830 lp->ll_name_end = tp;
831 }
832 }
833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100834 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
836 return p;
837
838 cc = *p;
839 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100840 // Only pass &ht when we would write to the variable, it prevents autoload
841 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100842 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
843 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100845 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000846 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000847 if (v == NULL)
848 return NULL;
849
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 /*
851 * Loop until no more [idx] or .key is following.
852 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000853 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100854 var1.v_type = VAR_UNKNOWN;
855 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000857 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
859 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100860 && lp->ll_tv->vval.v_dict != NULL)
861 && !(lp->ll_tv->v_type == VAR_BLOB
862 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000864 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100865 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000867 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100871 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000873 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000874
Bram Moolenaar8c711452005-01-14 21:53:12 +0000875 len = -1;
876 if (*p == '.')
877 {
878 key = p + 1;
879 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
880 ;
881 if (len == 0)
882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100884 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000885 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000886 }
887 p = key + len;
888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000889 else
890 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100891 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000892 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000893 if (*p == ':')
894 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000895 else
896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000897 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200898 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100900 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100902 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000903 clear_tv(&var1);
904 return NULL;
905 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000906 }
907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100908 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000909 if (*p == ':')
910 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100915 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000916 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000917 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100918 if (rettv != NULL
919 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100920 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100921 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100922 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000924 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100925 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100926 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000927 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000928 }
929 p = skipwhite(p + 1);
930 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000931 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000932 else
933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000934 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200935 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200936 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000937 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100938 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000939 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000940 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100941 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000942 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100943 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100944 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000945 clear_tv(&var2);
946 return NULL;
947 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000948 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000951 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000953
Bram Moolenaar8c711452005-01-14 21:53:12 +0000954 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000955 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100957 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100958 clear_tv(&var1);
959 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000960 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000961 }
962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100963 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000964 ++p;
965 }
966
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000968 {
969 if (len == -1)
970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100971 // "[key]": get key from "var1"
972 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200973 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000974 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000977 }
978 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000979 lp->ll_list = NULL;
980 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000981 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100983 // When assigning to a scope dictionary check that a function and
984 // variable name is valid (only variable name unless it is l: or
985 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200986 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200987 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200988 int prevval;
989 int wrong;
990
991 if (len != -1)
992 {
993 prevval = key[len];
994 key[len] = NUL;
995 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200996 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100997 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200998 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
999 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001000 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001001 || !valid_varname(key);
1002 if (len != -1)
1003 key[len] = prevval;
1004 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001005 {
1006 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001007 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001008 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001009 }
1010
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001012 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001013 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001014 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001015 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001016 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001017 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001018 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001019 return NULL;
1020 }
1021
Bram Moolenaar31b81602019-02-10 22:14:27 +01001022 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001026 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001027 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 }
1030 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001031 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001032 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001034 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001036 p = NULL;
1037 break;
1038 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001039 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001040 else if ((flags & GLV_READ_ONLY) == 0
1041 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001042 {
1043 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001044 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001045 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001046
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001047 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001050 else if (lp->ll_tv->v_type == VAR_BLOB)
1051 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001052 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1053
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001054 /*
1055 * Get the number and item for the only or first index of the List.
1056 */
1057 if (empty1)
1058 lp->ll_n1 = 0;
1059 else
1060 // is number or string
1061 lp->ll_n1 = (long)tv_get_number(&var1);
1062 clear_tv(&var1);
1063
1064 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001065 || lp->ll_n1 > bloblen
1066 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001067 {
1068 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001069 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001070 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001071 return NULL;
1072 }
1073 if (lp->ll_range && !lp->ll_empty2)
1074 {
1075 lp->ll_n2 = (long)tv_get_number(&var2);
1076 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001077 if (lp->ll_n2 < 0
1078 || lp->ll_n2 >= bloblen
1079 || lp->ll_n2 < lp->ll_n1)
1080 {
1081 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001082 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001083 return NULL;
1084 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001085 }
1086 lp->ll_blob = lp->ll_tv->vval.v_blob;
1087 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001088 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001089 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001090 else
1091 {
1092 /*
1093 * Get the number and item for the only or first index of the List.
1094 */
1095 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001096 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001098 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001099 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001100 clear_tv(&var1);
1101
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001102 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001103 lp->ll_list = lp->ll_tv->vval.v_list;
1104 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1105 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001106 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001107 if (lp->ll_n1 < 0)
1108 {
1109 lp->ll_n1 = 0;
1110 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1111 }
1112 }
1113 if (lp->ll_li == NULL)
1114 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001115 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001116 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001117 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001118 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001119 }
1120
1121 /*
1122 * May need to find the item or absolute index for the second
1123 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 * When no index given: "lp->ll_empty2" is TRUE.
1125 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001126 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001128 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001129 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001130 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001131 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001132 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001133 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001136 {
1137 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001138 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001140 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 }
1143
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001144 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 if (lp->ll_n1 < 0)
1146 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1147 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001148 {
1149 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001150 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001152 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001153 }
1154
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001155 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001157 }
1158
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001159 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 return p;
1162}
1163
1164/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001165 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001168clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001169{
1170 vim_free(lp->ll_exp_name);
1171 vim_free(lp->ll_newkey);
1172}
1173
1174/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001175 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001177 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1178 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001179 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001181set_var_lval(
1182 lval_T *lp,
1183 char_u *endp,
1184 typval_T *rettv,
1185 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001187 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001188{
1189 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001190 listitem_T *ri;
1191 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001192
1193 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001194 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001195 cc = *endp;
1196 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001197 if (lp->ll_blob != NULL)
1198 {
1199 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001200
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001201 if (op != NULL && *op != '=')
1202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001204 return;
1205 }
1206
1207 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1208 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001209 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001210
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001211 if (lp->ll_empty2)
1212 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1213
1214 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001217 return;
1218 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001219 if (lp->ll_empty2)
1220 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001221
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001222 ir = 0;
1223 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1224 blob_set(lp->ll_blob, il,
1225 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001226 }
1227 else
1228 {
1229 val = (int)tv_get_number_chk(rettv, &error);
1230 if (!error)
1231 {
1232 garray_T *gap = &lp->ll_blob->bv_ga;
1233
1234 // Allow for appending a byte. Setting a byte beyond
1235 // the end is an error otherwise.
1236 if (lp->ll_n1 < gap->ga_len
1237 || (lp->ll_n1 == gap->ga_len
1238 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1239 {
1240 blob_set(lp->ll_blob, lp->ll_n1, val);
1241 if (lp->ll_n1 == gap->ga_len)
1242 ++gap->ga_len;
1243 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001244 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001245 }
1246 }
1247 }
1248 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001249 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001250 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001253 {
1254 emsg(_(e_cannot_mod));
1255 *endp = cc;
1256 return;
1257 }
1258
Bram Moolenaarff697e62019-02-12 22:28:33 +01001259 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001260 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001261 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001262 &tv, &di, TRUE, FALSE) == OK)
1263 {
1264 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001265 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1266 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001267 && tv_op(&tv, rettv, op) == OK)
1268 set_var(lp->ll_name, &tv, FALSE);
1269 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001271 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001272 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001274 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001275 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001276 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001277 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001278 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001279 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280 else if (lp->ll_range)
1281 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001282 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001283 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001286 {
1287 emsg(_("E996: Cannot lock a range"));
1288 return;
1289 }
1290
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001291 /*
1292 * Check whether any of the list items is locked
1293 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001294 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001295 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001296 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001297 return;
1298 ri = ri->li_next;
1299 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1300 break;
1301 ll_li = ll_li->li_next;
1302 ++ll_n1;
1303 }
1304
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305 /*
1306 * Assign the List values to the list items.
1307 */
1308 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001309 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001310 if (op != NULL && *op != '=')
1311 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1312 else
1313 {
1314 clear_tv(&lp->ll_li->li_tv);
1315 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1316 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001317 ri = ri->li_next;
1318 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1319 break;
1320 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001321 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001322 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001323 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001324 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 ri = NULL;
1326 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001327 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001328 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001329 lp->ll_li = lp->ll_li->li_next;
1330 ++lp->ll_n1;
1331 }
1332 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001333 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334 else if (lp->ll_empty2
1335 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001336 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001337 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 }
1339 else
1340 {
1341 /*
1342 * Assign to a List or Dictionary item.
1343 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001345 {
1346 emsg(_("E996: Cannot lock a list or dict"));
1347 return;
1348 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 if (lp->ll_newkey != NULL)
1350 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001351 if (op != NULL && *op != '=')
1352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001353 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001354 return;
1355 }
1356
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001357 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001358 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001359 if (di == NULL)
1360 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001361 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1362 {
1363 vim_free(di);
1364 return;
1365 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001367 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001368 else if (op != NULL && *op != '=')
1369 {
1370 tv_op(lp->ll_tv, rettv, op);
1371 return;
1372 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001374 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001375
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001376 /*
1377 * Assign the value to the variable or list item.
1378 */
1379 if (copy)
1380 copy_tv(rettv, lp->ll_tv);
1381 else
1382 {
1383 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001384 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001385 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 }
1387 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001388}
1389
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001390/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001391 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1392 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001393 * Returns OK or FAIL.
1394 */
1395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001396tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001397{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001398 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001399 char_u numbuf[NUMBUFLEN];
1400 char_u *s;
1401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001402 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001403 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001404 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001405 {
1406 switch (tv1->v_type)
1407 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001408 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001409 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001411 case VAR_DICT:
1412 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001413 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001414 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001415 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001416 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001417 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001418 break;
1419
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001420 case VAR_BLOB:
1421 if (*op != '+' || tv2->v_type != VAR_BLOB)
1422 break;
1423 // BLOB += BLOB
1424 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1425 {
1426 blob_T *b1 = tv1->vval.v_blob;
1427 blob_T *b2 = tv2->vval.v_blob;
1428 int i, len = blob_len(b2);
1429 for (i = 0; i < len; i++)
1430 ga_append(&b1->bv_ga, blob_get(b2, i));
1431 }
1432 return OK;
1433
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001434 case VAR_LIST:
1435 if (*op != '+' || tv2->v_type != VAR_LIST)
1436 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001437 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001438 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1439 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1440 return OK;
1441
1442 case VAR_NUMBER:
1443 case VAR_STRING:
1444 if (tv2->v_type == VAR_LIST)
1445 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001446 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001447 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001448 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001449 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001450#ifdef FEAT_FLOAT
1451 if (tv2->v_type == VAR_FLOAT)
1452 {
1453 float_T f = n;
1454
Bram Moolenaarff697e62019-02-12 22:28:33 +01001455 if (*op == '%')
1456 break;
1457 switch (*op)
1458 {
1459 case '+': f += tv2->vval.v_float; break;
1460 case '-': f -= tv2->vval.v_float; break;
1461 case '*': f *= tv2->vval.v_float; break;
1462 case '/': f /= tv2->vval.v_float; break;
1463 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001464 clear_tv(tv1);
1465 tv1->v_type = VAR_FLOAT;
1466 tv1->vval.v_float = f;
1467 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001468 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001469#endif
1470 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001471 switch (*op)
1472 {
1473 case '+': n += tv_get_number(tv2); break;
1474 case '-': n -= tv_get_number(tv2); break;
1475 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001476 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1477 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001478 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001479 clear_tv(tv1);
1480 tv1->v_type = VAR_NUMBER;
1481 tv1->vval.v_number = n;
1482 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001483 }
1484 else
1485 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001486 if (tv2->v_type == VAR_FLOAT)
1487 break;
1488
Bram Moolenaarff697e62019-02-12 22:28:33 +01001489 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001490 s = tv_get_string(tv1);
1491 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492 clear_tv(tv1);
1493 tv1->v_type = VAR_STRING;
1494 tv1->vval.v_string = s;
1495 }
1496 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001497
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001498 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001499#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001500 {
1501 float_T f;
1502
Bram Moolenaarff697e62019-02-12 22:28:33 +01001503 if (*op == '%' || *op == '.'
1504 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001505 && tv2->v_type != VAR_NUMBER
1506 && tv2->v_type != VAR_STRING))
1507 break;
1508 if (tv2->v_type == VAR_FLOAT)
1509 f = tv2->vval.v_float;
1510 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001511 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001512 switch (*op)
1513 {
1514 case '+': tv1->vval.v_float += f; break;
1515 case '-': tv1->vval.v_float -= f; break;
1516 case '*': tv1->vval.v_float *= f; break;
1517 case '/': tv1->vval.v_float /= f; break;
1518 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001519 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001520#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001521 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001522 }
1523 }
1524
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001525 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001526 return FAIL;
1527}
1528
1529/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001530 * Evaluate the expression used in a ":for var in expr" command.
1531 * "arg" points to "var".
1532 * Set "*errp" to TRUE for an error, FALSE otherwise;
1533 * Return a pointer that holds the info. Null when there is an error.
1534 */
1535 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001536eval_for_line(
1537 char_u *arg,
1538 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001539 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001540 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001541{
Bram Moolenaar33570922005-01-25 22:26:29 +00001542 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001543 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001544 typval_T tv;
1545 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001546 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001548 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001549
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001550 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001551 if (fi == NULL)
1552 return NULL;
1553
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001554 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001555 if (expr == NULL)
1556 return fi;
1557
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001558 expr = skipwhite_and_linebreak(expr, evalarg);
1559 if (expr[0] != 'i' || expr[1] != 'n'
1560 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563 return fi;
1564 }
1565
1566 if (skip)
1567 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001568 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1569 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001570 {
1571 *errp = FALSE;
1572 if (!skip)
1573 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001574 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001575 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001576 l = tv.vval.v_list;
1577 if (l == NULL)
1578 {
1579 // a null list is like an empty list: do nothing
1580 clear_tv(&tv);
1581 }
1582 else
1583 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001585 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001587 // No need to increment the refcount, it's already set for
1588 // the list being used in "tv".
1589 fi->fi_list = l;
1590 list_add_watch(l, &fi->fi_lw);
1591 fi->fi_lw.lw_item = l->lv_first;
1592 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001593 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001594 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001595 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001596 fi->fi_bi = 0;
1597 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001598 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001599 typval_T btv;
1600
1601 // Make a copy, so that the iteration still works when the
1602 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001604 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001605 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001606 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001607 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001608 else
1609 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001610 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001611 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001612 }
1613 }
1614 }
1615 if (skip)
1616 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001617 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001618
1619 return fi;
1620}
1621
1622/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001623 * Used when looping over a :for line, skip the "in expr" part.
1624 */
1625 void
1626skip_for_lines(void *fi_void, evalarg_T *evalarg)
1627{
1628 forinfo_T *fi = (forinfo_T *)fi_void;
1629 int i;
1630
1631 for (i = 0; i < fi->fi_break_count; ++i)
1632 eval_next_line(evalarg);
1633}
1634
1635/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001636 * Use the first item in a ":for" list. Advance to the next.
1637 * Assign the values to the variable (list). "arg" points to the first one.
1638 * Return TRUE when a valid item was found, FALSE when at end of list or
1639 * something wrong.
1640 */
1641 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001642next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001644 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001645 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001646 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1647 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001648 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001649
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001650 if (fi->fi_blob != NULL)
1651 {
1652 typval_T tv;
1653
1654 if (fi->fi_bi >= blob_len(fi->fi_blob))
1655 return FALSE;
1656 tv.v_type = VAR_NUMBER;
1657 tv.v_lock = VAR_FIXED;
1658 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1659 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001660 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001661 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001662 }
1663
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 item = fi->fi_lw.lw_item;
1665 if (item == NULL)
1666 result = FALSE;
1667 else
1668 {
1669 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001670 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001671 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672 }
1673 return result;
1674}
1675
1676/*
1677 * Free the structure used to store info used by ":for".
1678 */
1679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001680free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681{
Bram Moolenaar33570922005-01-25 22:26:29 +00001682 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001684 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001685 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001687 list_unref(fi->fi_list);
1688 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001689 if (fi != NULL && fi->fi_blob != NULL)
1690 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 vim_free(fi);
1692}
1693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001695set_context_for_expression(
1696 expand_T *xp,
1697 char_u *arg,
1698 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 int got_eq = FALSE;
1701 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001704 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 {
1706 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001707 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001708 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001709 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001710 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 {
1712 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001713 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001714 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 break;
1716 }
1717 return;
1718 }
1719 }
1720 else
1721 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1722 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 while ((xp->xp_pattern = vim_strpbrk(arg,
1724 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1725 {
1726 c = *xp->xp_pattern;
1727 if (c == '&')
1728 {
1729 c = xp->xp_pattern[1];
1730 if (c == '&')
1731 {
1732 ++xp->xp_pattern;
1733 xp->xp_context = cmdidx != CMD_let || got_eq
1734 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1735 }
1736 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001737 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001739 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1740 xp->xp_pattern += 2;
1741
1742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
1744 else if (c == '$')
1745 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001746 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 xp->xp_context = EXPAND_ENV_VARS;
1748 }
1749 else if (c == '=')
1750 {
1751 got_eq = TRUE;
1752 xp->xp_context = EXPAND_EXPRESSION;
1753 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001754 else if (c == '#'
1755 && xp->xp_context == EXPAND_EXPRESSION)
1756 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001757 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001758 break;
1759 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001760 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 && xp->xp_context == EXPAND_FUNCTIONS
1762 && vim_strchr(xp->xp_pattern, '(') == NULL)
1763 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001764 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 break;
1766 }
1767 else if (cmdidx != CMD_let || got_eq)
1768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001769 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
1771 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1772 if (c == '\\' && xp->xp_pattern[1] != NUL)
1773 ++xp->xp_pattern;
1774 xp->xp_context = EXPAND_NOTHING;
1775 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001776 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001778 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1780 /* skip */ ;
1781 xp->xp_context = EXPAND_NOTHING;
1782 }
1783 else if (c == '|')
1784 {
1785 if (xp->xp_pattern[1] == '|')
1786 {
1787 ++xp->xp_pattern;
1788 xp->xp_context = EXPAND_EXPRESSION;
1789 }
1790 else
1791 xp->xp_context = EXPAND_COMMANDS;
1792 }
1793 else
1794 xp->xp_context = EXPAND_EXPRESSION;
1795 }
1796 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001797 // Doesn't look like something valid, expand as an expression
1798 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001799 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 arg = xp->xp_pattern;
1801 if (*arg != NUL)
1802 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1803 /* skip */ ;
1804 }
1805 xp->xp_pattern = arg;
1806}
1807
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001809 * Return TRUE if "pat" matches "text".
1810 * Does not use 'cpo' and always uses 'magic'.
1811 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001812 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001813pattern_match(char_u *pat, char_u *text, int ic)
1814{
1815 int matches = FALSE;
1816 char_u *save_cpo;
1817 regmatch_T regmatch;
1818
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001819 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001820 save_cpo = p_cpo;
1821 p_cpo = (char_u *)"";
1822 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1823 if (regmatch.regprog != NULL)
1824 {
1825 regmatch.rm_ic = ic;
1826 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1827 vim_regfree(regmatch.regprog);
1828 }
1829 p_cpo = save_cpo;
1830 return matches;
1831}
1832
1833/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001834 * Handle a name followed by "(". Both for just "name(arg)" and for
1835 * "expr->name(arg)".
1836 * Returns OK or FAIL.
1837 */
1838 static int
1839eval_func(
1840 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001841 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001842 char_u *name,
1843 int name_len,
1844 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001845 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001846 typval_T *basetv) // "expr" for "expr->name(arg)"
1847{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001848 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001849 char_u *s = name;
1850 int len = name_len;
1851 partial_T *partial;
1852 int ret = OK;
1853
1854 if (!evaluate)
1855 check_vars(s, len);
1856
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001857 // If "s" is the name of a variable of type VAR_FUNC
1858 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001859 s = deref_func_name(s, &len, &partial, !evaluate);
1860
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001861 // Need to make a copy, in case evaluating the arguments makes
1862 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001863 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001864 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001865 ret = FAIL;
1866 else
1867 {
1868 funcexe_T funcexe;
1869
1870 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001871 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001872 funcexe.firstline = curwin->w_cursor.lnum;
1873 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001874 funcexe.evaluate = evaluate;
1875 funcexe.partial = partial;
1876 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001877 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001878 }
1879 vim_free(s);
1880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001881 // If evaluate is FALSE rettv->v_type was not set in
1882 // get_func_tv, but it's needed in handle_subscript() to parse
1883 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001884 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1885 {
1886 rettv->vval.v_string = NULL;
1887 rettv->v_type = VAR_FUNC;
1888 }
1889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001890 // Stop the expression evaluation when immediately
1891 // aborting on error, or when an interrupt occurred or
1892 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001893 if (evaluate && aborting())
1894 {
1895 if (ret == OK)
1896 clear_tv(rettv);
1897 ret = FAIL;
1898 }
1899 return ret;
1900}
1901
1902/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001903 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1904 * comment) and there is a next line, return the next line (skipping blanks)
1905 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001906 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1907 * "arg" must point somewhere inside a line, not at the start.
1908 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001909 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001910eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1911{
1912 *getnext = FALSE;
1913 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1914 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001915 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001916 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
Bram Moolenaar962d7212020-07-04 14:15:00 +02001917 && *arg == '#' && arg[1] != '{')))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001918 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001919 char_u *p;
1920
1921 if (evalarg->eval_cookie != NULL)
1922 p = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
1923 else
1924 p = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001925
1926 if (p != NULL)
1927 {
1928 *getnext = TRUE;
1929 return skipwhite(p);
1930 }
1931 }
1932 return arg;
1933}
1934
1935/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001936 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001937 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001938 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001939eval_next_line(evalarg_T *evalarg)
1940{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001941 garray_T *gap = &evalarg->eval_ga;
1942 char_u *line;
1943
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001944 if (evalarg->eval_cookie != NULL)
1945 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
1946 else
1947 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001948 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001949 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1950 {
1951 // Going to concatenate the lines after parsing.
1952 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1953 ++gap->ga_len;
1954 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001955 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001956 {
1957 vim_free(evalarg->eval_tofree);
1958 evalarg->eval_tofree = line;
1959 }
1960 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001961}
1962
Bram Moolenaare6b53242020-07-01 17:28:33 +02001963/*
1964 * Call eval_next_non_blank() and get the next line if needed.
1965 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001966 char_u *
1967skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1968{
1969 int getnext;
1970 char_u *p = skipwhite(arg);
1971
Bram Moolenaar962d7212020-07-04 14:15:00 +02001972 if (evalarg == NULL)
1973 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02001974 eval_next_non_blank(p, evalarg, &getnext);
1975 if (getnext)
1976 return eval_next_line(evalarg);
1977 return p;
1978}
1979
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001980/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001981 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001982 */
1983 void
1984clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1985{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001986 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001987 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001988 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001989 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001990 if (eap != NULL)
1991 {
1992 // We may need to keep the original command line, e.g. for
1993 // ":let" it has the variable names. But we may also need the
1994 // new one, "nextcmd" points into it. Keep both.
1995 vim_free(eap->cmdline_tofree);
1996 eap->cmdline_tofree = *eap->cmdlinep;
1997 *eap->cmdlinep = evalarg->eval_tofree;
1998 }
1999 else
2000 vim_free(evalarg->eval_tofree);
2001 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002002 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002003
2004 vim_free(evalarg->eval_tofree_lambda);
2005 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002006 }
2007}
2008
2009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002011 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2013 */
2014
2015/*
2016 * Handle zero level expression.
2017 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002019 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002020 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 * Return OK or FAIL.
2022 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002024eval0(
2025 char_u *arg,
2026 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002027 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002028 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029{
2030 int ret;
2031 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002032 int did_emsg_before = did_emsg;
2033 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002034 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002037 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002038
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002039 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
2041 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 /*
2044 * Report the invalid expression unless the expression evaluation has
2045 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002046 * exception, or we already gave a more specific error.
2047 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002049 if (!aborting()
2050 && did_emsg == did_emsg_before
2051 && called_emsg == called_emsg_before
2052 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002053 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 ret = FAIL;
2055 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002056
2057 if (eap != NULL)
2058 eap->nextcmd = check_nextcmd(p);
2059
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 return ret;
2061}
2062
2063/*
2064 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002065 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 *
2067 * "arg" must point to the first non-white of the expression.
2068 * "arg" is advanced to the next non-white after the recognized expression.
2069 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002070 * Note: "rettv.v_lock" is not set.
2071 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 * Return OK or FAIL.
2073 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002074 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002075eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002077 char_u *p;
2078 int getnext;
2079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 /*
2081 * Get the first variable.
2082 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002083 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 return FAIL;
2085
Bram Moolenaar793648f2020-06-26 21:28:25 +02002086 p = eval_next_non_blank(*arg, evalarg, &getnext);
2087 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002089 int result;
2090 typval_T var2;
2091 evalarg_T nested_evalarg;
2092 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002093 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002094
Bram Moolenaar793648f2020-06-26 21:28:25 +02002095 if (getnext)
2096 *arg = eval_next_line(evalarg);
2097
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002098 if (evalarg == NULL)
2099 {
2100 CLEAR_FIELD(nested_evalarg);
2101 orig_flags = 0;
2102 }
2103 else
2104 {
2105 nested_evalarg = *evalarg;
2106 orig_flags = evalarg->eval_flags;
2107 }
2108
Bram Moolenaar7acde512020-06-24 23:02:40 +02002109 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002111 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002113 int error = FALSE;
2114
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002115 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002118 if (error)
2119 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
2121
2122 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002123 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002125 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002126 nested_evalarg.eval_flags = result ? orig_flags
2127 : orig_flags & ~EVAL_EVALUATE;
2128 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 return FAIL;
2130
2131 /*
2132 * Check for the ":".
2133 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002134 p = eval_next_non_blank(*arg, evalarg, &getnext);
2135 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 return FAIL;
2141 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002142 if (getnext)
2143 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
2145 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002146 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002148 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002149 nested_evalarg.eval_flags = !result ? orig_flags
2150 : orig_flags & ~EVAL_EVALUATE;
2151 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
2153 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 return FAIL;
2156 }
2157 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 }
2160
2161 return OK;
2162}
2163
2164/*
2165 * Handle first level expression:
2166 * expr2 || expr2 || expr2 logical OR
2167 *
2168 * "arg" must point to the first non-white of the expression.
2169 * "arg" is advanced to the next non-white after the recognized expression.
2170 *
2171 * Return OK or FAIL.
2172 */
2173 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002174eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002176 char_u *p;
2177 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002178 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 long result;
2180 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002181 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
2183 /*
2184 * Get the first variable.
2185 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002186 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 return FAIL;
2188
2189 /*
2190 * Repeat until there is no following "||".
2191 */
2192 first = TRUE;
2193 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002194 p = eval_next_non_blank(*arg, evalarg, &getnext);
2195 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002197 evalarg_T nested_evalarg;
2198 int evaluate;
2199 int orig_flags;
2200
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002201 if (getnext)
2202 *arg = eval_next_line(evalarg);
2203
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002204 if (evalarg == NULL)
2205 {
2206 CLEAR_FIELD(nested_evalarg);
2207 orig_flags = 0;
2208 evaluate = FALSE;
2209 }
2210 else
2211 {
2212 nested_evalarg = *evalarg;
2213 orig_flags = evalarg->eval_flags;
2214 evaluate = orig_flags & EVAL_EVALUATE;
2215 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (evaluate && first)
2218 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002219 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002222 if (error)
2223 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 first = FALSE;
2225 }
2226
2227 /*
2228 * Get the second variable.
2229 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002230 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002231 nested_evalarg.eval_flags = !result ? orig_flags
2232 : orig_flags & ~EVAL_EVALUATE;
2233 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 return FAIL;
2235
2236 /*
2237 * Compute the result.
2238 */
2239 if (evaluate && !result)
2240 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002241 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002244 if (error)
2245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247 if (evaluate)
2248 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 rettv->v_type = VAR_NUMBER;
2250 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002252
2253 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
2255
2256 return OK;
2257}
2258
2259/*
2260 * Handle second level expression:
2261 * expr3 && expr3 && expr3 logical AND
2262 *
2263 * "arg" must point to the first non-white of the expression.
2264 * "arg" is advanced to the next non-white after the recognized expression.
2265 *
2266 * Return OK or FAIL.
2267 */
2268 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002269eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002271 char_u *p;
2272 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002273 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 long result;
2275 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002276 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278 /*
2279 * Get the first variable.
2280 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002281 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 return FAIL;
2283
2284 /*
2285 * Repeat until there is no following "&&".
2286 */
2287 first = TRUE;
2288 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002289 p = eval_next_non_blank(*arg, evalarg, &getnext);
2290 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002292 evalarg_T nested_evalarg;
2293 int orig_flags;
2294 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002295
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002296 if (getnext)
2297 *arg = eval_next_line(evalarg);
2298
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002299 if (evalarg == NULL)
2300 {
2301 CLEAR_FIELD(nested_evalarg);
2302 orig_flags = 0;
2303 evaluate = FALSE;
2304 }
2305 else
2306 {
2307 nested_evalarg = *evalarg;
2308 orig_flags = evalarg->eval_flags;
2309 evaluate = orig_flags & EVAL_EVALUATE;
2310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (evaluate && first)
2312 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002313 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002316 if (error)
2317 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 first = FALSE;
2319 }
2320
2321 /*
2322 * Get the second variable.
2323 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002324 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002325 nested_evalarg.eval_flags = result ? orig_flags
2326 : orig_flags & ~EVAL_EVALUATE;
2327 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 return FAIL;
2329
2330 /*
2331 * Compute the result.
2332 */
2333 if (evaluate && result)
2334 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002335 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 if (error)
2339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341 if (evaluate)
2342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 rettv->v_type = VAR_NUMBER;
2344 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002346
2347 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
2349
2350 return OK;
2351}
2352
2353/*
2354 * Handle third level expression:
2355 * var1 == var2
2356 * var1 =~ var2
2357 * var1 != var2
2358 * var1 !~ var2
2359 * var1 > var2
2360 * var1 >= var2
2361 * var1 < var2
2362 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002363 * var1 is var2
2364 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 *
2366 * "arg" must point to the first non-white of the expression.
2367 * "arg" is advanced to the next non-white after the recognized expression.
2368 *
2369 * Return OK or FAIL.
2370 */
2371 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002372eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
Bram Moolenaar33570922005-01-25 22:26:29 +00002374 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002376 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002378 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
2382 /*
2383 * Get the first variable.
2384 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002385 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 return FAIL;
2387
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002388 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 switch (p[0])
2390 {
2391 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002392 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002394 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 break;
2396 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002397 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002399 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 break;
2401 case '>': if (p[1] != '=')
2402 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002403 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 len = 1;
2405 }
2406 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002407 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 break;
2409 case '<': if (p[1] != '=')
2410 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002411 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 len = 1;
2413 }
2414 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002415 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002417 case 'i': if (p[1] == 's')
2418 {
2419 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2420 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002421 i = p[len];
2422 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002423 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002424 }
2425 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 }
2427
2428 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002429 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002431 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002433 if (getnext)
2434 *arg = eval_next_line(evalarg);
2435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002436 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 if (p[len] == '?')
2438 {
2439 ic = TRUE;
2440 ++len;
2441 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002442 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 else if (p[len] == '#')
2444 {
2445 ic = FALSE;
2446 ++len;
2447 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002448 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 else
2450 ic = p_ic;
2451
2452 /*
2453 * Get the second variable.
2454 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002455 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002456 if (eval5(arg, &var2, evalarg) == 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 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002461 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002462 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002463 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002464
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002465 if (in_vim9script() && check_compare_types(
2466 type, rettv, &var2) == FAIL)
2467 {
2468 ret = FAIL;
2469 clear_tv(rettv);
2470 }
2471 else
2472 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002473 clear_tv(&var2);
2474 return ret;
2475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477
2478 return OK;
2479}
2480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 void
2482eval_addblob(typval_T *tv1, typval_T *tv2)
2483{
2484 blob_T *b1 = tv1->vval.v_blob;
2485 blob_T *b2 = tv2->vval.v_blob;
2486 blob_T *b = blob_alloc();
2487 int i;
2488
2489 if (b != NULL)
2490 {
2491 for (i = 0; i < blob_len(b1); i++)
2492 ga_append(&b->bv_ga, blob_get(b1, i));
2493 for (i = 0; i < blob_len(b2); i++)
2494 ga_append(&b->bv_ga, blob_get(b2, i));
2495
2496 clear_tv(tv1);
2497 rettv_blob_set(tv1, b);
2498 }
2499}
2500
2501 int
2502eval_addlist(typval_T *tv1, typval_T *tv2)
2503{
2504 typval_T var3;
2505
2506 // concatenate Lists
2507 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2508 {
2509 clear_tv(tv1);
2510 clear_tv(tv2);
2511 return FAIL;
2512 }
2513 clear_tv(tv1);
2514 *tv1 = var3;
2515 return OK;
2516}
2517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518/*
2519 * Handle fourth level expression:
2520 * + number addition
2521 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002522 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002523 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 *
2525 * "arg" must point to the first non-white of the expression.
2526 * "arg" is advanced to the next non-white after the recognized expression.
2527 *
2528 * Return OK or FAIL.
2529 */
2530 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002531eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002533 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535 /*
2536 * Get the first variable.
2537 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002538 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 return FAIL;
2540
2541 /*
2542 * Repeat computing, until no '+', '-' or '.' is following.
2543 */
2544 for (;;)
2545 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002546 int getnext;
2547 char_u *p;
2548 int op;
2549 int concat;
2550 typval_T var2;
2551
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002552 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002553 p = eval_next_non_blank(*arg, evalarg, &getnext);
2554 op = *p;
2555 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002556 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002558 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002559 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002561 if ((op != '+' || (rettv->v_type != VAR_LIST
2562 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002563#ifdef FEAT_FLOAT
2564 && (op == '.' || rettv->v_type != VAR_FLOAT)
2565#endif
2566 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002567 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002568 // For "list + ...", an illegal use of the first operand as
2569 // a number cannot be determined before evaluating the 2nd
2570 // operand: if this is also a list, all is ok.
2571 // For "something . ...", "something - ..." or "non-list + ...",
2572 // we know that the first operand needs to be a string or number
2573 // without evaluating the 2nd operand. So check before to avoid
2574 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002575 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002576 {
2577 clear_tv(rettv);
2578 return FAIL;
2579 }
2580 }
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 /*
2583 * Get the second variable.
2584 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002585 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2586 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002587 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002588 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 return FAIL;
2592 }
2593
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002594 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
2596 /*
2597 * Compute the result.
2598 */
2599 if (op == '.')
2600 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002601 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2602 char_u *s1 = tv_get_string_buf(rettv, buf1);
2603 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002605 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002606 {
2607 clear_tv(rettv);
2608 clear_tv(&var2);
2609 return FAIL;
2610 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002611 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 clear_tv(rettv);
2613 rettv->v_type = VAR_STRING;
2614 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002616 else if (op == '+' && rettv->v_type == VAR_BLOB
2617 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002619 else if (op == '+' && rettv->v_type == VAR_LIST
2620 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002621 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002623 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 else
2626 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002627 int error = FALSE;
2628 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002629#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002630 float_T f1 = 0, f2 = 0;
2631
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002632 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002633 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002634 f1 = rettv->vval.v_float;
2635 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002636 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002637 else
2638#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002639 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002640 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002641 if (error)
2642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002643 // This can only happen for "list + non-list". For
2644 // "non-list + ..." or "something - ...", we returned
2645 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002646 clear_tv(rettv);
2647 return FAIL;
2648 }
2649#ifdef FEAT_FLOAT
2650 if (var2.v_type == VAR_FLOAT)
2651 f1 = n1;
2652#endif
2653 }
2654#ifdef FEAT_FLOAT
2655 if (var2.v_type == VAR_FLOAT)
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 if (error)
2665 {
2666 clear_tv(rettv);
2667 clear_tv(&var2);
2668 return FAIL;
2669 }
2670#ifdef FEAT_FLOAT
2671 if (rettv->v_type == VAR_FLOAT)
2672 f2 = n2;
2673#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002675 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002676
2677#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002678 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002679 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2680 {
2681 if (op == '+')
2682 f1 = f1 + f2;
2683 else
2684 f1 = f1 - f2;
2685 rettv->v_type = VAR_FLOAT;
2686 rettv->vval.v_float = f1;
2687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002689#endif
2690 {
2691 if (op == '+')
2692 n1 = n1 + n2;
2693 else
2694 n1 = n1 - n2;
2695 rettv->v_type = VAR_NUMBER;
2696 rettv->vval.v_number = n1;
2697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701 }
2702 return OK;
2703}
2704
2705/*
2706 * Handle fifth level expression:
2707 * * number multiplication
2708 * / number division
2709 * % number modulo
2710 *
2711 * "arg" must point to the first non-white of the expression.
2712 * "arg" is advanced to the next non-white after the recognized expression.
2713 *
2714 * Return OK or FAIL.
2715 */
2716 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002717eval6(
2718 char_u **arg,
2719 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002720 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002721 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
Bram Moolenaar33570922005-01-25 22:26:29 +00002723 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002725 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002726#ifdef FEAT_FLOAT
2727 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002728 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002729#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002730 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 /*
2733 * Get the first variable.
2734 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002735 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 return FAIL;
2737
2738 /*
2739 * Repeat computing, until no '*', '/' or '%' is following.
2740 */
2741 for (;;)
2742 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002743 int evaluate = evalarg == NULL ? 0
2744 : (evalarg->eval_flags & EVAL_EVALUATE);
2745 int getnext;
2746
2747 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002748 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002750 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002751 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002753 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002755#ifdef FEAT_FLOAT
2756 if (rettv->v_type == VAR_FLOAT)
2757 {
2758 f1 = rettv->vval.v_float;
2759 use_float = TRUE;
2760 n1 = 0;
2761 }
2762 else
2763#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002764 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002765 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002766 if (error)
2767 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 else
2770 n1 = 0;
2771
2772 /*
2773 * Get the second variable.
2774 */
2775 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002776 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 return FAIL;
2778
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002779 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002781#ifdef FEAT_FLOAT
2782 if (var2.v_type == VAR_FLOAT)
2783 {
2784 if (!use_float)
2785 {
2786 f1 = n1;
2787 use_float = TRUE;
2788 }
2789 f2 = var2.vval.v_float;
2790 n2 = 0;
2791 }
2792 else
2793#endif
2794 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002795 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002796 clear_tv(&var2);
2797 if (error)
2798 return FAIL;
2799#ifdef FEAT_FLOAT
2800 if (use_float)
2801 f2 = n2;
2802#endif
2803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
2805 /*
2806 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002807 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002809#ifdef FEAT_FLOAT
2810 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002812 if (op == '*')
2813 f1 = f1 * f2;
2814 else if (op == '/')
2815 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002816# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002817 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002818 if (f2 == 0.0)
2819 {
2820 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002821 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002822 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002823 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002824 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002825 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002826 }
2827 else
2828 f1 = f1 / f2;
2829# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002830 // We rely on the floating point library to handle divide
2831 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002832 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002833# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002836 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002838 return FAIL;
2839 }
2840 rettv->v_type = VAR_FLOAT;
2841 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 }
2843 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002846 if (op == '*')
2847 n1 = n1 * n2;
2848 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002849 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002851 n1 = num_modulus(n1, n2);
2852
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002853 rettv->v_type = VAR_NUMBER;
2854 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857 }
2858
2859 return OK;
2860}
2861
2862/*
2863 * Handle sixth level expression:
2864 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002865 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002866 * "string" string constant
2867 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 * &option-name option value
2869 * @r register contents
2870 * identifier variable value
2871 * function() function call
2872 * $VAR environment variable
2873 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002874 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002876 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002877 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 *
2879 * Also handle:
2880 * ! in front logical NOT
2881 * - in front unary minus
2882 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 * trailing [] subscript in String or List
2884 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002885 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 *
2887 * "arg" must point to the first non-white of the expression.
2888 * "arg" is advanced to the next non-white after the recognized expression.
2889 *
2890 * Return OK or FAIL.
2891 */
2892 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002893eval7(
2894 char_u **arg,
2895 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002896 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002899 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2900 int evaluate = evalarg != NULL
2901 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 int len;
2903 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 char_u *start_leader, *end_leader;
2905 int ret = OK;
2906 char_u *alias;
2907
2908 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002910 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
2914 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002915 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 */
2917 start_leader = *arg;
2918 while (**arg == '!' || **arg == '-' || **arg == '+')
2919 *arg = skipwhite(*arg + 1);
2920 end_leader = *arg;
2921
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002922 if (**arg == '.' && (!isdigit(*(*arg + 1))
2923#ifdef FEAT_FLOAT
2924 || current_sctx.sc_version < 2
2925#endif
2926 ))
2927 {
2928 semsg(_(e_invexpr2), *arg);
2929 ++*arg;
2930 return FAIL;
2931 }
2932
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 switch (**arg)
2934 {
2935 /*
2936 * Number constant.
2937 */
2938 case '0':
2939 case '1':
2940 case '2':
2941 case '3':
2942 case '4':
2943 case '5':
2944 case '6':
2945 case '7':
2946 case '8':
2947 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002948 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002949
2950 // Apply prefixed "-" and "+" now. Matters especially when
2951 // "->" follows.
2952 if (ret == OK && evaluate && end_leader > start_leader)
2953 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 break;
2955
2956 /*
2957 * String constant: "string".
2958 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002959 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 break;
2961
2962 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002963 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002965 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002966 break;
2967
2968 /*
2969 * List: [expr, expr]
2970 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002971 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 break;
2973
2974 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002975 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002976 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002977 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002978 {
2979 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002980 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002981 }
2982 else
2983 ret = NOTDONE;
2984 break;
2985
2986 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002987 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002988 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002989 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002990 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002991 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002992 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002993 break;
2994
2995 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002996 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002998 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 break;
3000
3001 /*
3002 * Environment variable: $VAR.
3003 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003004 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 break;
3006
3007 /*
3008 * Register contents: @r.
3009 */
3010 case '@': ++*arg;
3011 if (evaluate)
3012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003013 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003014 rettv->vval.v_string = get_reg_contents(**arg,
3015 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
3017 if (**arg != NUL)
3018 ++*arg;
3019 break;
3020
3021 /*
3022 * nested expression: (expression).
3023 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003024 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003025 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003026 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003027
Bram Moolenaar9215f012020-06-27 21:18:00 +02003028 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003029 if (**arg == ')')
3030 ++*arg;
3031 else if (ret == OK)
3032 {
3033 emsg(_(e_missing_close));
3034 clear_tv(rettv);
3035 ret = FAIL;
3036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 }
3038 break;
3039
Bram Moolenaar8c711452005-01-14 21:53:12 +00003040 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 break;
3042 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003043
3044 if (ret == NOTDONE)
3045 {
3046 /*
3047 * Must be a variable or function name.
3048 * Can also be a curly-braces kind of name: {expr}.
3049 */
3050 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003051 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003052 if (alias != NULL)
3053 s = alias;
3054
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003055 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003056 ret = FAIL;
3057 else
3058 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003059 if (**arg == '(')
3060 // "name(..." recursive!
Bram Moolenaare6b53242020-07-01 17:28:33 +02003061 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003062 else if (flags & EVAL_CONSTANT)
3063 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003064 else if (evaluate)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003065 // get value of variable
3066 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003067 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003068 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003069 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003070 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003072 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003073 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003074 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003075 }
3076
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 *arg = skipwhite(*arg);
3078
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003079 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3080 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003081 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003082 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
3084 /*
3085 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3086 */
3087 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003088 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003089 return ret;
3090}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003091
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003092/*
3093 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003094 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003095 * Adjusts "end_leaderp" until it is at "start_leader".
3096 */
3097 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003098eval7_leader(
3099 typval_T *rettv,
3100 int numeric_only,
3101 char_u *start_leader,
3102 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003103{
3104 char_u *end_leader = *end_leaderp;
3105 int ret = OK;
3106 int error = FALSE;
3107 varnumber_T val = 0;
3108#ifdef FEAT_FLOAT
3109 float_T f = 0.0;
3110
3111 if (rettv->v_type == VAR_FLOAT)
3112 f = rettv->vval.v_float;
3113 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003114#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003115 val = tv_get_number_chk(rettv, &error);
3116 if (error)
3117 {
3118 clear_tv(rettv);
3119 ret = FAIL;
3120 }
3121 else
3122 {
3123 while (end_leader > start_leader)
3124 {
3125 --end_leader;
3126 if (*end_leader == '!')
3127 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003128 if (numeric_only)
3129 {
3130 ++end_leader;
3131 break;
3132 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003133#ifdef FEAT_FLOAT
3134 if (rettv->v_type == VAR_FLOAT)
3135 f = !f;
3136 else
3137#endif
3138 val = !val;
3139 }
3140 else if (*end_leader == '-')
3141 {
3142#ifdef FEAT_FLOAT
3143 if (rettv->v_type == VAR_FLOAT)
3144 f = -f;
3145 else
3146#endif
3147 val = -val;
3148 }
3149 }
3150#ifdef FEAT_FLOAT
3151 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003153 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003154 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003156 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003157#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003158 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003159 clear_tv(rettv);
3160 rettv->v_type = VAR_NUMBER;
3161 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003164 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 return ret;
3166}
3167
3168/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003169 * Call the function referred to in "rettv".
3170 */
3171 static int
3172call_func_rettv(
3173 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003174 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003175 typval_T *rettv,
3176 int evaluate,
3177 dict_T *selfdict,
3178 typval_T *basetv)
3179{
3180 partial_T *pt = NULL;
3181 funcexe_T funcexe;
3182 typval_T functv;
3183 char_u *s;
3184 int ret;
3185
3186 // need to copy the funcref so that we can clear rettv
3187 if (evaluate)
3188 {
3189 functv = *rettv;
3190 rettv->v_type = VAR_UNKNOWN;
3191
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003192 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003193 if (functv.v_type == VAR_PARTIAL)
3194 {
3195 pt = functv.vval.v_partial;
3196 s = partial_name(pt);
3197 }
3198 else
3199 s = functv.vval.v_string;
3200 }
3201 else
3202 s = (char_u *)"";
3203
Bram Moolenaara80faa82020-04-12 19:37:17 +02003204 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003205 funcexe.firstline = curwin->w_cursor.lnum;
3206 funcexe.lastline = curwin->w_cursor.lnum;
3207 funcexe.evaluate = evaluate;
3208 funcexe.partial = pt;
3209 funcexe.selfdict = selfdict;
3210 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003211 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003212
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003213 // Clear the funcref afterwards, so that deleting it while
3214 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003215 if (evaluate)
3216 clear_tv(&functv);
3217
3218 return ret;
3219}
3220
3221/*
3222 * Evaluate "->method()".
3223 * "*arg" points to the '-'.
3224 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3225 */
3226 static int
3227eval_lambda(
3228 char_u **arg,
3229 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003230 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003231 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003232{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003233 int evaluate = evalarg != NULL
3234 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003235 typval_T base = *rettv;
3236 int ret;
3237
3238 // Skip over the ->.
3239 *arg += 2;
3240 rettv->v_type = VAR_UNKNOWN;
3241
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003242 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003243 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003244 return FAIL;
3245 else if (**arg != '(')
3246 {
3247 if (verbose)
3248 {
3249 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003250 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003251 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003253 }
3254 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003255 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003256 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003257 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003258 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003259
3260 // Clear the funcref afterwards, so that deleting it while
3261 // evaluating the arguments is possible (see test55).
3262 if (evaluate)
3263 clear_tv(&base);
3264
3265 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003266}
3267
3268/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003269 * Evaluate "->method()".
3270 * "*arg" points to the '-'.
3271 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3272 */
3273 static int
3274eval_method(
3275 char_u **arg,
3276 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003277 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003278 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003279{
3280 char_u *name;
3281 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003282 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003283 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003284 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003285 int evaluate = evalarg != NULL
3286 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003287
3288 // Skip over the ->.
3289 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003290 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003291
Bram Moolenaarac92e252019-08-03 21:58:38 +02003292 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003293 len = get_name_len(arg, &alias, evaluate, TRUE);
3294 if (alias != NULL)
3295 name = alias;
3296
3297 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003298 {
3299 if (verbose)
3300 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003301 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003302 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003303 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003304 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003305 if (**arg != '(')
3306 {
3307 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003309 ret = FAIL;
3310 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003311 else if (VIM_ISWHITE((*arg)[-1]))
3312 {
3313 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003314 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003315 ret = FAIL;
3316 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003317 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003318 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003319 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003320 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003321
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003322 // Clear the funcref afterwards, so that deleting it while
3323 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003324 if (evaluate)
3325 clear_tv(&base);
3326
Bram Moolenaarac92e252019-08-03 21:58:38 +02003327 return ret;
3328}
3329
3330/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003331 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3332 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003333 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3334 */
3335 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336eval_index(
3337 char_u **arg,
3338 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003339 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003340 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003341{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003342 int evaluate = evalarg != NULL
3343 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003344 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003345 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003346 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003347 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003348 long len = -1;
3349 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003350 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003351 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003352
Bram Moolenaara03f2332016-02-06 18:09:59 +01003353 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003354 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003355 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003356 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003357 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003358 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003359 return FAIL;
3360 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003361#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003362 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003363 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003364 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003365#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003366 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003367 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003368 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003369 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003370 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003371 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003372 return FAIL;
3373 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003374 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003376 if (evaluate)
3377 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003378 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003379
3380 case VAR_STRING:
3381 case VAR_NUMBER:
3382 case VAR_LIST:
3383 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003384 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003385 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003386 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003387
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003388 init_tv(&var1);
3389 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003390 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003391 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003392 /*
3393 * dict.name
3394 */
3395 key = *arg + 1;
3396 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3397 ;
3398 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003399 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003400 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003401 }
3402 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003403 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003404 /*
3405 * something[idx]
3406 *
3407 * Get the (first) variable from inside the [].
3408 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003409 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003410 if (**arg == ':')
3411 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003412 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003413 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003414 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003415 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003416 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003417 clear_tv(&var1);
3418 return FAIL;
3419 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003420
3421 /*
3422 * Get the second variable from inside the [:].
3423 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003424 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003425 if (**arg == ':')
3426 {
3427 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003428 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003429 if (**arg == ']')
3430 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003431 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003433 if (!empty1)
3434 clear_tv(&var1);
3435 return FAIL;
3436 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003437 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003438 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003439 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003440 if (!empty1)
3441 clear_tv(&var1);
3442 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003443 return FAIL;
3444 }
3445 }
3446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003447 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003448 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003449 if (**arg != ']')
3450 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003451 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003452 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003453 clear_tv(&var1);
3454 if (range)
3455 clear_tv(&var2);
3456 return FAIL;
3457 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003458 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003459 }
3460
3461 if (evaluate)
3462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003463 n1 = 0;
3464 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003465 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003466 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003468 }
3469 if (range)
3470 {
3471 if (empty2)
3472 n2 = -1;
3473 else
3474 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003475 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003476 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003477 }
3478 }
3479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003480 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003481 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003482 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003483 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003485 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003486 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003487 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003488 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003489 case VAR_SPECIAL:
3490 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003491 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003492 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003493
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003494 case VAR_NUMBER:
3495 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003496 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003497 len = (long)STRLEN(s);
3498 if (range)
3499 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003500 // The resulting variable is a substring. If the indexes
3501 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003502 if (n1 < 0)
3503 {
3504 n1 = len + n1;
3505 if (n1 < 0)
3506 n1 = 0;
3507 }
3508 if (n2 < 0)
3509 n2 = len + n2;
3510 else if (n2 >= len)
3511 n2 = len;
3512 if (n1 >= len || n2 < 0 || n1 > n2)
3513 s = NULL;
3514 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003515 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003516 }
3517 else
3518 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003519 // The resulting variable is a string of a single
3520 // character. If the index is too big or negative the
3521 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003522 if (n1 >= len || n1 < 0)
3523 s = NULL;
3524 else
3525 s = vim_strnsave(s + n1, 1);
3526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003527 clear_tv(rettv);
3528 rettv->v_type = VAR_STRING;
3529 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003530 break;
3531
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003532 case VAR_BLOB:
3533 len = blob_len(rettv->vval.v_blob);
3534 if (range)
3535 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003536 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003537 // are out of range the result is empty.
3538 if (n1 < 0)
3539 {
3540 n1 = len + n1;
3541 if (n1 < 0)
3542 n1 = 0;
3543 }
3544 if (n2 < 0)
3545 n2 = len + n2;
3546 else if (n2 >= len)
3547 n2 = len - 1;
3548 if (n1 >= len || n2 < 0 || n1 > n2)
3549 {
3550 clear_tv(rettv);
3551 rettv->v_type = VAR_BLOB;
3552 rettv->vval.v_blob = NULL;
3553 }
3554 else
3555 {
3556 blob_T *blob = blob_alloc();
3557
3558 if (blob != NULL)
3559 {
3560 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3561 {
3562 blob_free(blob);
3563 return FAIL;
3564 }
3565 blob->bv_ga.ga_len = n2 - n1 + 1;
3566 for (i = n1; i <= n2; i++)
3567 blob_set(blob, i - n1,
3568 blob_get(rettv->vval.v_blob, i));
3569
3570 clear_tv(rettv);
3571 rettv_blob_set(rettv, blob);
3572 }
3573 }
3574 }
3575 else
3576 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003577 // The resulting variable is a byte value.
3578 // If the index is too big or negative that is an error.
3579 if (n1 < 0)
3580 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003581 if (n1 < len && n1 >= 0)
3582 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003583 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003584
3585 clear_tv(rettv);
3586 rettv->v_type = VAR_NUMBER;
3587 rettv->vval.v_number = v;
3588 }
3589 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003590 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003591 }
3592 break;
3593
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003594 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003595 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003596 if (n1 < 0)
3597 n1 = len + n1;
3598 if (!empty1 && (n1 < 0 || n1 >= len))
3599 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003600 // For a range we allow invalid values and return an empty
3601 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003602 if (!range)
3603 {
3604 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003605 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003606 return FAIL;
3607 }
3608 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003609 }
3610 if (range)
3611 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003612 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003613
3614 if (n2 < 0)
3615 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003616 else if (n2 >= len)
3617 n2 = len - 1;
3618 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003619 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003620 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003621 if (l == NULL)
3622 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003623 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003624 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003625 }
3626 else
3627 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003628 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003629 clear_tv(rettv);
3630 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003631 }
3632 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633
3634 case VAR_DICT:
3635 if (range)
3636 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003637 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003638 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003639 if (len == -1)
3640 clear_tv(&var1);
3641 return FAIL;
3642 }
3643 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003644 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003645
3646 if (len == -1)
3647 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003648 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003649 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003651 clear_tv(&var1);
3652 return FAIL;
3653 }
3654 }
3655
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003656 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003658 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003659 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 if (len == -1)
3661 clear_tv(&var1);
3662 if (item == NULL)
3663 return FAIL;
3664
3665 copy_tv(&item->di_tv, &var1);
3666 clear_tv(rettv);
3667 *rettv = var1;
3668 }
3669 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003670 }
3671 }
3672
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003673 return OK;
3674}
3675
3676/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003677 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003678 */
3679 char_u *
3680partial_name(partial_T *pt)
3681{
3682 if (pt->pt_name != NULL)
3683 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003684 if (pt->pt_func != NULL)
3685 return pt->pt_func->uf_name;
3686 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003687}
3688
Bram Moolenaarddecc252016-04-06 22:59:37 +02003689 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003690partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003691{
3692 int i;
3693
3694 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003695 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003696 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003697 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003698 if (pt->pt_name != NULL)
3699 {
3700 func_unref(pt->pt_name);
3701 vim_free(pt->pt_name);
3702 }
3703 else
3704 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003705
3706 if (pt->pt_funcstack != NULL)
3707 {
3708 // Decrease the reference count for the context of a closure. If down
3709 // to zero free it and clear the variables on the stack.
3710 if (--pt->pt_funcstack->fs_refcount == 0)
3711 {
3712 garray_T *gap = &pt->pt_funcstack->fs_ga;
3713 typval_T *stack = gap->ga_data;
3714
3715 for (i = 0; i < gap->ga_len; ++i)
3716 clear_tv(stack + i);
3717 ga_clear(gap);
3718 vim_free(pt->pt_funcstack);
3719 }
3720 pt->pt_funcstack = NULL;
3721 }
3722
Bram Moolenaarddecc252016-04-06 22:59:37 +02003723 vim_free(pt);
3724}
3725
3726/*
3727 * Unreference a closure: decrement the reference count and free it when it
3728 * becomes zero.
3729 */
3730 void
3731partial_unref(partial_T *pt)
3732{
3733 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003734 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003735}
3736
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003737/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003738 * Return the next (unique) copy ID.
3739 * Used for serializing nested structures.
3740 */
3741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003742get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003743{
3744 current_copyID += COPYID_INC;
3745 return current_copyID;
3746}
3747
3748/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003749 * Garbage collection for lists and dictionaries.
3750 *
3751 * We use reference counts to be able to free most items right away when they
3752 * are no longer used. But for composite items it's possible that it becomes
3753 * unused while the reference count is > 0: When there is a recursive
3754 * reference. Example:
3755 * :let l = [1, 2, 3]
3756 * :let d = {9: l}
3757 * :let l[1] = d
3758 *
3759 * Since this is quite unusual we handle this with garbage collection: every
3760 * once in a while find out which lists and dicts are not referenced from any
3761 * variable.
3762 *
3763 * Here is a good reference text about garbage collection (refers to Python
3764 * but it applies to all reference-counting mechanisms):
3765 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003766 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003767
3768/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003769 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003770 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003771 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003772 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003773 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003774garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003775{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003776 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003777 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003778 buf_T *buf;
3779 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003780 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003781 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003782
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003783 if (!testing)
3784 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003785 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003786 want_garbage_collect = FALSE;
3787 may_garbage_collect = FALSE;
3788 garbage_collect_at_exit = FALSE;
3789 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003790
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003791 // The execution stack can grow big, limit the size.
3792 if (exestack.ga_maxlen - exestack.ga_len > 500)
3793 {
3794 size_t new_len;
3795 char_u *pp;
3796 int n;
3797
3798 // Keep 150% of the current size, with a minimum of the growth size.
3799 n = exestack.ga_len / 2;
3800 if (n < exestack.ga_growsize)
3801 n = exestack.ga_growsize;
3802
3803 // Don't make it bigger though.
3804 if (exestack.ga_len + n < exestack.ga_maxlen)
3805 {
3806 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3807 pp = vim_realloc(exestack.ga_data, new_len);
3808 if (pp == NULL)
3809 return FAIL;
3810 exestack.ga_maxlen = exestack.ga_len + n;
3811 exestack.ga_data = pp;
3812 }
3813 }
3814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003815 // We advance by two because we add one for items referenced through
3816 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003817 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003818
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003819 /*
3820 * 1. Go through all accessible variables and mark all lists and dicts
3821 * with copyID.
3822 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003823
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003824 // Don't free variables in the previous_funccal list unless they are only
3825 // referenced through previous_funccal. This must be first, because if
3826 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003827 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003829 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003830 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003832 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003833 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003834 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3835 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003837 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003838 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003839 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3840 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003841 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003842 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3843 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003844#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003845 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003846 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3847 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003848 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003849 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003850 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3851 NULL, NULL);
3852#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003853
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003854 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003855 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003856 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3857 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003858 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003859 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003860
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003861 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003862 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003864 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003865 abort = abort || set_ref_in_functions(copyID);
3866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003867 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003868 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003870 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003871 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003872
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003873 // callbacks in buffers
3874 abort = abort || set_ref_in_buffers(copyID);
3875
Bram Moolenaar1dced572012-04-05 16:54:08 +02003876#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003877 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003878#endif
3879
Bram Moolenaardb913952012-06-29 12:54:53 +02003880#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003881 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003882#endif
3883
3884#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003885 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003886#endif
3887
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003888#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003889 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003890 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003891#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003892#ifdef FEAT_NETBEANS_INTG
3893 abort = abort || set_ref_in_nb_channel(copyID);
3894#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003895
Bram Moolenaare3188e22016-05-31 21:13:04 +02003896#ifdef FEAT_TIMERS
3897 abort = abort || set_ref_in_timer(copyID);
3898#endif
3899
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003900#ifdef FEAT_QUICKFIX
3901 abort = abort || set_ref_in_quickfix(copyID);
3902#endif
3903
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003904#ifdef FEAT_TERMINAL
3905 abort = abort || set_ref_in_term(copyID);
3906#endif
3907
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003908#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003909 abort = abort || set_ref_in_popups(copyID);
3910#endif
3911
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003912 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003913 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003914 /*
3915 * 2. Free lists and dictionaries that are not referenced.
3916 */
3917 did_free = free_unref_items(copyID);
3918
3919 /*
3920 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003921 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003922 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003923 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003924 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003925 else if (p_verbose > 0)
3926 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003927 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003928 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003929
3930 return did_free;
3931}
3932
3933/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003934 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003935 */
3936 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003937free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003938{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003939 int did_free = FALSE;
3940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003941 // Let all "free" functions know that we are here. This means no
3942 // dictionaries, lists, channels or jobs are to be freed, because we will
3943 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003944 in_free_unref_items = TRUE;
3945
3946 /*
3947 * PASS 1: free the contents of the items. We don't free the items
3948 * themselves yet, so that it is possible to decrement refcount counters
3949 */
3950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003951 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003952 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003954 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003955 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003956
3957#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003958 // Go through the list of jobs and free items without the copyID. This
3959 // must happen before doing channels, because jobs refer to channels, but
3960 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003961 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003963 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003964 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3965#endif
3966
3967 /*
3968 * PASS 2: free the items themselves.
3969 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003970 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003971 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003972
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003973#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003974 // Go through the list of jobs and free items without the copyID. This
3975 // must happen before doing channels, because jobs refer to channels, but
3976 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003977 free_unused_jobs(copyID, COPYID_MASK);
3978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003979 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003980 free_unused_channels(copyID, COPYID_MASK);
3981#endif
3982
3983 in_free_unref_items = FALSE;
3984
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003985 return did_free;
3986}
3987
3988/*
3989 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003990 * "list_stack" is used to add lists to be marked. Can be NULL.
3991 *
3992 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003993 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003995set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003996{
3997 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003998 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003999 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004000 hashtab_T *cur_ht;
4001 ht_stack_T *ht_stack = NULL;
4002 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004003
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004004 cur_ht = ht;
4005 for (;;)
4006 {
4007 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004008 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004009 // Mark each item in the hashtab. If the item contains a hashtab
4010 // it is added to ht_stack, if it contains a list it is added to
4011 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004012 todo = (int)cur_ht->ht_used;
4013 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4014 if (!HASHITEM_EMPTY(hi))
4015 {
4016 --todo;
4017 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4018 &ht_stack, list_stack);
4019 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004020 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004021
4022 if (ht_stack == NULL)
4023 break;
4024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004025 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004026 cur_ht = ht_stack->ht;
4027 tempitem = ht_stack;
4028 ht_stack = ht_stack->prev;
4029 free(tempitem);
4030 }
4031
4032 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004033}
4034
4035/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004036 * Mark a dict and its items with "copyID".
4037 * Returns TRUE if setting references failed somehow.
4038 */
4039 int
4040set_ref_in_dict(dict_T *d, int copyID)
4041{
4042 if (d != NULL && d->dv_copyID != copyID)
4043 {
4044 d->dv_copyID = copyID;
4045 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4046 }
4047 return FALSE;
4048}
4049
4050/*
4051 * Mark a list and its items with "copyID".
4052 * Returns TRUE if setting references failed somehow.
4053 */
4054 int
4055set_ref_in_list(list_T *ll, int copyID)
4056{
4057 if (ll != NULL && ll->lv_copyID != copyID)
4058 {
4059 ll->lv_copyID = copyID;
4060 return set_ref_in_list_items(ll, copyID, NULL);
4061 }
4062 return FALSE;
4063}
4064
4065/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004066 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004067 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4068 *
4069 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004070 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004071 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004072set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004073{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004074 listitem_T *li;
4075 int abort = FALSE;
4076 list_T *cur_l;
4077 list_stack_T *list_stack = NULL;
4078 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004079
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004080 cur_l = l;
4081 for (;;)
4082 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004083 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004084 // Mark each item in the list. If the item contains a hashtab
4085 // it is added to ht_stack, if it contains a list it is added to
4086 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004087 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4088 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4089 ht_stack, &list_stack);
4090 if (list_stack == NULL)
4091 break;
4092
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004093 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004094 cur_l = list_stack->list;
4095 tempitem = list_stack;
4096 list_stack = list_stack->prev;
4097 free(tempitem);
4098 }
4099
4100 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004101}
4102
4103/*
4104 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004105 * "list_stack" is used to add lists to be marked. Can be NULL.
4106 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4107 *
4108 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004109 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004111set_ref_in_item(
4112 typval_T *tv,
4113 int copyID,
4114 ht_stack_T **ht_stack,
4115 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004116{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004117 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004118
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004119 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004120 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004121 dict_T *dd = tv->vval.v_dict;
4122
Bram Moolenaara03f2332016-02-06 18:09:59 +01004123 if (dd != NULL && dd->dv_copyID != copyID)
4124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004125 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004126 dd->dv_copyID = copyID;
4127 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004128 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004129 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4130 }
4131 else
4132 {
4133 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4134 if (newitem == NULL)
4135 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004136 else
4137 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004138 newitem->ht = &dd->dv_hashtab;
4139 newitem->prev = *ht_stack;
4140 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004141 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004142 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004143 }
4144 }
4145 else if (tv->v_type == VAR_LIST)
4146 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004147 list_T *ll = tv->vval.v_list;
4148
Bram Moolenaara03f2332016-02-06 18:09:59 +01004149 if (ll != NULL && ll->lv_copyID != copyID)
4150 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004151 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004152 ll->lv_copyID = copyID;
4153 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004154 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004155 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004156 }
4157 else
4158 {
4159 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004160 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004161 if (newitem == NULL)
4162 abort = TRUE;
4163 else
4164 {
4165 newitem->list = ll;
4166 newitem->prev = *list_stack;
4167 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004168 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004169 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004170 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004171 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004172 else if (tv->v_type == VAR_FUNC)
4173 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004174 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004175 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004176 else if (tv->v_type == VAR_PARTIAL)
4177 {
4178 partial_T *pt = tv->vval.v_partial;
4179 int i;
4180
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004181 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004182 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004183 // Didn't see this partial yet.
4184 pt->pt_copyID = copyID;
4185
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004186 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004187
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004188 if (pt->pt_dict != NULL)
4189 {
4190 typval_T dtv;
4191
4192 dtv.v_type = VAR_DICT;
4193 dtv.vval.v_dict = pt->pt_dict;
4194 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4195 }
4196
4197 for (i = 0; i < pt->pt_argc; ++i)
4198 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4199 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004200 if (pt->pt_funcstack != NULL)
4201 {
4202 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4203
4204 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4205 abort = abort || set_ref_in_item(stack + i, copyID,
4206 ht_stack, list_stack);
4207 }
4208
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004209 }
4210 }
4211#ifdef FEAT_JOB_CHANNEL
4212 else if (tv->v_type == VAR_JOB)
4213 {
4214 job_T *job = tv->vval.v_job;
4215 typval_T dtv;
4216
4217 if (job != NULL && job->jv_copyID != copyID)
4218 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004219 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004220 if (job->jv_channel != NULL)
4221 {
4222 dtv.v_type = VAR_CHANNEL;
4223 dtv.vval.v_channel = job->jv_channel;
4224 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4225 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004226 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004227 {
4228 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004229 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004230 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4231 }
4232 }
4233 }
4234 else if (tv->v_type == VAR_CHANNEL)
4235 {
4236 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004237 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004238 typval_T dtv;
4239 jsonq_T *jq;
4240 cbq_T *cq;
4241
4242 if (ch != NULL && ch->ch_copyID != copyID)
4243 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004244 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004245 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004246 {
4247 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4248 jq = jq->jq_next)
4249 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4250 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4251 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004252 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004253 {
4254 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004255 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004256 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4257 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004258 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004259 {
4260 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004261 dtv.vval.v_partial =
4262 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004263 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4264 }
4265 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004266 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004267 {
4268 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004269 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004270 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4271 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004272 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004273 {
4274 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004275 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004276 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4277 }
4278 }
4279 }
4280#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004281 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004282}
4283
Bram Moolenaar8c711452005-01-14 21:53:12 +00004284/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004285 * Return a string with the string representation of a variable.
4286 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004287 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004288 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004289 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4290 * stings as "string()", otherwise does not put quotes around strings, as
4291 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004292 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4293 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004294 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004295 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004296 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004297echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004298 typval_T *tv,
4299 char_u **tofree,
4300 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004301 int copyID,
4302 int echo_style,
4303 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004304 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004305{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004306 static int recurse = 0;
4307 char_u *r = NULL;
4308
Bram Moolenaar33570922005-01-25 22:26:29 +00004309 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004310 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004311 if (!did_echo_string_emsg)
4312 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004313 // Only give this message once for a recursive call to avoid
4314 // flooding the user with errors. And stop iterating over lists
4315 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004316 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004317 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004318 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004319 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004320 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004321 }
4322 ++recurse;
4323
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004324 switch (tv->v_type)
4325 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004326 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004327 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004328 {
4329 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004330 r = tv->vval.v_string;
4331 if (r == NULL)
4332 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004333 }
4334 else
4335 {
4336 *tofree = string_quote(tv->vval.v_string, FALSE);
4337 r = *tofree;
4338 }
4339 break;
4340
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004341 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004342 if (echo_style)
4343 {
4344 *tofree = NULL;
4345 r = tv->vval.v_string;
4346 }
4347 else
4348 {
4349 *tofree = string_quote(tv->vval.v_string, TRUE);
4350 r = *tofree;
4351 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004352 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004353
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004354 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004355 {
4356 partial_T *pt = tv->vval.v_partial;
4357 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004358 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004359 garray_T ga;
4360 int i;
4361 char_u *tf;
4362
4363 ga_init2(&ga, 1, 100);
4364 ga_concat(&ga, (char_u *)"function(");
4365 if (fname != NULL)
4366 {
4367 ga_concat(&ga, fname);
4368 vim_free(fname);
4369 }
4370 if (pt != NULL && pt->pt_argc > 0)
4371 {
4372 ga_concat(&ga, (char_u *)", [");
4373 for (i = 0; i < pt->pt_argc; ++i)
4374 {
4375 if (i > 0)
4376 ga_concat(&ga, (char_u *)", ");
4377 ga_concat(&ga,
4378 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4379 vim_free(tf);
4380 }
4381 ga_concat(&ga, (char_u *)"]");
4382 }
4383 if (pt != NULL && pt->pt_dict != NULL)
4384 {
4385 typval_T dtv;
4386
4387 ga_concat(&ga, (char_u *)", ");
4388 dtv.v_type = VAR_DICT;
4389 dtv.vval.v_dict = pt->pt_dict;
4390 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4391 vim_free(tf);
4392 }
4393 ga_concat(&ga, (char_u *)")");
4394
4395 *tofree = ga.ga_data;
4396 r = *tofree;
4397 break;
4398 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004399
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004400 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004401 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004402 break;
4403
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004404 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004405 if (tv->vval.v_list == NULL)
4406 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004407 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004408 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004409 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004410 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004411 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4412 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004413 {
4414 *tofree = NULL;
4415 r = (char_u *)"[...]";
4416 }
4417 else
4418 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004419 int old_copyID = tv->vval.v_list->lv_copyID;
4420
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004421 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004422 *tofree = list2string(tv, copyID, restore_copyID);
4423 if (restore_copyID)
4424 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004425 r = *tofree;
4426 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004427 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004428
Bram Moolenaar8c711452005-01-14 21:53:12 +00004429 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004430 if (tv->vval.v_dict == NULL)
4431 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004432 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004433 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004434 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004435 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004436 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4437 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004438 {
4439 *tofree = NULL;
4440 r = (char_u *)"{...}";
4441 }
4442 else
4443 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004444 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004445
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004446 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004447 *tofree = dict2string(tv, copyID, restore_copyID);
4448 if (restore_copyID)
4449 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004450 r = *tofree;
4451 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004452 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004453
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004454 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004455 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004456 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004458 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004459 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004460 break;
4461
Bram Moolenaar835dc632016-02-07 14:27:38 +01004462 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004463 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004464 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004465 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004466 if (composite_val)
4467 {
4468 *tofree = string_quote(r, FALSE);
4469 r = *tofree;
4470 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004471 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004472
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004473 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004474#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004475 *tofree = NULL;
4476 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4477 r = numbuf;
4478 break;
4479#endif
4480
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004481 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004482 case VAR_SPECIAL:
4483 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004484 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004485 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004486 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004487
Bram Moolenaar8502c702014-06-17 12:51:16 +02004488 if (--recurse == 0)
4489 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004490 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004491}
4492
4493/*
4494 * Return a string with the string representation of a variable.
4495 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4496 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004497 * Does not put quotes around strings, as ":echo" displays values.
4498 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4499 * May return NULL.
4500 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004501 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004502echo_string(
4503 typval_T *tv,
4504 char_u **tofree,
4505 char_u *numbuf,
4506 int copyID)
4507{
4508 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4509}
4510
4511/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004512 * Return string "str" in ' quotes, doubling ' characters.
4513 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004514 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004515 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004516 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004517string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004518{
Bram Moolenaar33570922005-01-25 22:26:29 +00004519 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004520 char_u *p, *r, *s;
4521
Bram Moolenaar33570922005-01-25 22:26:29 +00004522 len = (function ? 13 : 3);
4523 if (str != NULL)
4524 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004525 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004526 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004527 if (*p == '\'')
4528 ++len;
4529 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004530 s = r = alloc(len);
4531 if (r != NULL)
4532 {
4533 if (function)
4534 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004535 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004536 r += 10;
4537 }
4538 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004539 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004540 if (str != NULL)
4541 for (p = str; *p != NUL; )
4542 {
4543 if (*p == '\'')
4544 *r++ = '\'';
4545 MB_COPY_CHAR(p, r);
4546 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004548 if (function)
4549 *r++ = ')';
4550 *r++ = NUL;
4551 }
4552 return s;
4553}
4554
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004555#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004556/*
4557 * Convert the string "text" to a floating point number.
4558 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4559 * this always uses a decimal point.
4560 * Returns the length of the text that was consumed.
4561 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004563string2float(
4564 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004565 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004566{
4567 char *s = (char *)text;
4568 float_T f;
4569
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004570 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004571 if (STRNICMP(text, "inf", 3) == 0)
4572 {
4573 *value = INFINITY;
4574 return 3;
4575 }
4576 if (STRNICMP(text, "-inf", 3) == 0)
4577 {
4578 *value = -INFINITY;
4579 return 4;
4580 }
4581 if (STRNICMP(text, "nan", 3) == 0)
4582 {
4583 *value = NAN;
4584 return 3;
4585 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004586 f = strtod(s, &s);
4587 *value = f;
4588 return (int)((char_u *)s - text);
4589}
4590#endif
4591
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004594 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004596 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004597var2fpos(
4598 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004599 int dollar_lnum, // TRUE when $ is last line
4600 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004602 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004604 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004606 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004607 if (varp->v_type == VAR_LIST)
4608 {
4609 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004610 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004611 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004612 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004613
4614 l = varp->vval.v_list;
4615 if (l == NULL)
4616 return NULL;
4617
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004618 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004619 pos.lnum = list_find_nr(l, 0L, &error);
4620 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004621 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004623 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004624 pos.col = list_find_nr(l, 1L, &error);
4625 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004626 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004627 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004628
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004629 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004630 li = list_find(l, 1L);
4631 if (li != NULL && li->li_tv.v_type == VAR_STRING
4632 && li->li_tv.vval.v_string != NULL
4633 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4634 pos.col = len + 1;
4635
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004636 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004637 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004638 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004639 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004640
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004641 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004642 pos.coladd = list_find_nr(l, 2L, &error);
4643 if (error)
4644 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004645
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004646 return &pos;
4647 }
4648
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004649 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004650 if (name == NULL)
4651 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004652 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004654 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004655 {
4656 if (VIsual_active)
4657 return &VIsual;
4658 return &curwin->w_cursor;
4659 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004660 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004662 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4664 return NULL;
4665 return pp;
4666 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004667
Bram Moolenaara5525202006-03-02 22:52:09 +00004668 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004669
Bram Moolenaar477933c2007-07-17 14:32:23 +00004670 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004671 {
4672 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004673 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004674 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004675 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004676 // In silent Ex mode topline is zero, but that's not a valid line
4677 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004678 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004679 return &pos;
4680 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004681 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004682 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004683 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004684 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004685 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004686 return &pos;
4687 }
4688 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004689 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004691 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
4693 pos.lnum = curbuf->b_ml.ml_line_count;
4694 pos.col = 0;
4695 }
4696 else
4697 {
4698 pos.lnum = curwin->w_cursor.lnum;
4699 pos.col = (colnr_T)STRLEN(ml_get_curline());
4700 }
4701 return &pos;
4702 }
4703 return NULL;
4704}
4705
4706/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004707 * Convert list in "arg" into a position and optional file number.
4708 * When "fnump" is NULL there is no file number, only 3 items.
4709 * Note that the column is passed on as-is, the caller may want to decrement
4710 * it to use 1 for the first column.
4711 * Return FAIL when conversion is not possible, doesn't check the position for
4712 * validity.
4713 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004714 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004715list2fpos(
4716 typval_T *arg,
4717 pos_T *posp,
4718 int *fnump,
4719 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004720{
4721 list_T *l = arg->vval.v_list;
4722 long i = 0;
4723 long n;
4724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004725 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4726 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004727 if (arg->v_type != VAR_LIST
4728 || l == NULL
4729 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004730 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004731 return FAIL;
4732
4733 if (fnump != NULL)
4734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004735 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004736 if (n < 0)
4737 return FAIL;
4738 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004739 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004740 *fnump = n;
4741 }
4742
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004743 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004744 if (n < 0)
4745 return FAIL;
4746 posp->lnum = n;
4747
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004748 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004749 if (n < 0)
4750 return FAIL;
4751 posp->col = n;
4752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004753 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004754 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004755 posp->coladd = 0;
4756 else
4757 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004758
Bram Moolenaar493c1782014-05-28 14:34:46 +02004759 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004760 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004761
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004762 return OK;
4763}
4764
4765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 * Get the length of an environment variable name.
4767 * Advance "arg" to the first character after the name.
4768 * Return 0 for error.
4769 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004771get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772{
4773 char_u *p;
4774 int len;
4775
4776 for (p = *arg; vim_isIDc(*p); ++p)
4777 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004778 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 return 0;
4780
4781 len = (int)(p - *arg);
4782 *arg = p;
4783 return len;
4784}
4785
4786/*
4787 * Get the length of the name of a function or internal variable.
4788 * "arg" is advanced to the first non-white character after the name.
4789 * Return 0 if something is wrong.
4790 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004791 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004792get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
4794 char_u *p;
4795 int len;
4796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004797 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004799 {
4800 if (*p == ':')
4801 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004802 // "s:" is start of "s:var", but "n:" is not and can be used in
4803 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004804 len = (int)(p - *arg);
4805 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4806 || len > 1)
4807 break;
4808 }
4809 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004810 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 return 0;
4812
4813 len = (int)(p - *arg);
4814 *arg = skipwhite(p);
4815
4816 return len;
4817}
4818
4819/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004820 * Get the length of the name of a variable or function.
4821 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004823 * Return -1 if curly braces expansion failed.
4824 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 * If the name contains 'magic' {}'s, expand them and return the
4826 * expanded name in an allocated string via 'alias' - caller must free.
4827 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004829get_name_len(
4830 char_u **arg,
4831 char_u **alias,
4832 int evaluate,
4833 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
4835 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 char_u *p;
4837 char_u *expr_start;
4838 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004840 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841
4842 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4843 && (*arg)[2] == (int)KE_SNR)
4844 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004845 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 *arg += 3;
4847 return get_id_len(arg) + 3;
4848 }
4849 len = eval_fname_script(*arg);
4850 if (len > 0)
4851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004852 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 *arg += len;
4854 }
4855
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004859 p = find_name_end(*arg, &expr_start, &expr_end,
4860 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (expr_start != NULL)
4862 {
4863 char_u *temp_string;
4864
4865 if (!evaluate)
4866 {
4867 len += (int)(p - *arg);
4868 *arg = skipwhite(p);
4869 return len;
4870 }
4871
4872 /*
4873 * Include any <SID> etc in the expanded string:
4874 * Thus the -len here.
4875 */
4876 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4877 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004878 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 *alias = temp_string;
4880 *arg = skipwhite(p);
4881 return (int)STRLEN(temp_string);
4882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004885 // Only give an error when there is something, otherwise it will be
4886 // reported at a higher level.
4887 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004888 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 return len;
4891}
4892
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004893/*
4894 * Find the end of a variable or function name, taking care of magic braces.
4895 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4896 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004897 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 * Return a pointer to just after the name. Equal to "arg" if there is no
4899 * valid name.
4900 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004901 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004902find_name_end(
4903 char_u *arg,
4904 char_u **expr_start,
4905 char_u **expr_end,
4906 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004908 int mb_nest = 0;
4909 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004911 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004912 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004916 *expr_start = NULL;
4917 *expr_end = NULL;
4918 }
4919
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004920 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004921 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4922 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004923 return arg;
4924
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004925 for (p = arg; *p != NUL
4926 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004927 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004928 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004930 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004931 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004932 if (*p == '\'')
4933 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004934 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004935 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004936 ;
4937 if (*p == NUL)
4938 break;
4939 }
4940 else if (*p == '"')
4941 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004942 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004943 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004944 if (*p == '\\' && p[1] != NUL)
4945 ++p;
4946 if (*p == NUL)
4947 break;
4948 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004949 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4950 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004951 // "s:" is start of "s:var", but "n:" is not and can be used in
4952 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004953 len = (int)(p - arg);
4954 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004955 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004956 break;
4957 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004958
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004959 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004961 if (*p == '[')
4962 ++br_nest;
4963 else if (*p == ']')
4964 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004966
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004967 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004969 if (*p == '{')
4970 {
4971 mb_nest++;
4972 if (expr_start != NULL && *expr_start == NULL)
4973 *expr_start = p;
4974 }
4975 else if (*p == '}')
4976 {
4977 mb_nest--;
4978 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4979 *expr_end = p;
4980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
4983
4984 return p;
4985}
4986
4987/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004988 * Expands out the 'magic' {}'s in a variable/function name.
4989 * Note that this can call itself recursively, to deal with
4990 * constructs like foo{bar}{baz}{bam}
4991 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4992 * "in_start" ^
4993 * "expr_start" ^
4994 * "expr_end" ^
4995 * "in_end" ^
4996 *
4997 * Returns a new allocated string, which the caller must free.
4998 * Returns NULL for failure.
4999 */
5000 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005001make_expanded_name(
5002 char_u *in_start,
5003 char_u *expr_start,
5004 char_u *expr_end,
5005 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005006{
5007 char_u c1;
5008 char_u *retval = NULL;
5009 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005010
5011 if (expr_end == NULL || in_end == NULL)
5012 return NULL;
5013 *expr_start = NUL;
5014 *expr_end = NUL;
5015 c1 = *in_end;
5016 *in_end = NUL;
5017
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005018 temp_result = eval_to_string(expr_start + 1, FALSE);
5019 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005020 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005021 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5022 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005023 if (retval != NULL)
5024 {
5025 STRCPY(retval, in_start);
5026 STRCAT(retval, temp_result);
5027 STRCAT(retval, expr_end + 1);
5028 }
5029 }
5030 vim_free(temp_result);
5031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005032 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005033 *expr_start = '{';
5034 *expr_end = '}';
5035
5036 if (retval != NULL)
5037 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005038 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005039 if (expr_start != NULL)
5040 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005041 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005042 temp_result = make_expanded_name(retval, expr_start,
5043 expr_end, temp_result);
5044 vim_free(retval);
5045 retval = temp_result;
5046 }
5047 }
5048
5049 return retval;
5050}
5051
5052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005054 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005056 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005057eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005059 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5060}
5061
5062/*
5063 * Return TRUE if character "c" can be used as the first character in a
5064 * variable or function name (excluding '{' and '}').
5065 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005067eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005068{
5069 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070}
5071
5072/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005073 * Handle:
5074 * - expr[expr], expr[expr:expr] subscript
5075 * - ".name" lookup
5076 * - function call with Funcref variable: func(expr)
5077 * - method call: var->method()
5078 *
5079 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005080 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005082handle_subscript(
5083 char_u **arg,
5084 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005085 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005086 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005087{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005088 int evaluate = evalarg != NULL
5089 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005090 int ret = OK;
5091 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005092 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005093 int getnext;
5094 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005095
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005096 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005097 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005098 // When at the end of the line and ".name" or "->{" or "->X" follows in
5099 // the next line then consume the line break.
5100 p = eval_next_non_blank(*arg, evalarg, &getnext);
5101 if (getnext
5102 && ((rettv->v_type == VAR_DICT && *p == '.'
5103 && ASCII_ISALPHA(p[1]))
5104 || (*p == '-' && p[1] == '>'
5105 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005106 {
5107 *arg = eval_next_line(evalarg);
5108 check_white = FALSE;
5109 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005110
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005111 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5112 || rettv->v_type == VAR_PARTIAL))
5113 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005114 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005115 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5116 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005117
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005118 // Stop the expression evaluation when immediately aborting on
5119 // error, or when an interrupt occurred or an exception was thrown
5120 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005121 if (aborting())
5122 {
5123 if (ret == OK)
5124 clear_tv(rettv);
5125 ret = FAIL;
5126 }
5127 dict_unref(selfdict);
5128 selfdict = NULL;
5129 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005130 else if (**arg == '-' && (*arg)[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005131 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005132 if (ret == OK)
5133 {
5134 if ((*arg)[2] == '{')
5135 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005136 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005137 else
5138 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005139 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005140 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005141 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005142 // "." is ".name" lookup when we found a dict or when evaluating and
5143 // scriptversion is at least 2, where string concatenation is "..".
5144 else if (**arg == '['
5145 || (**arg == '.' && (rettv->v_type == VAR_DICT
5146 || (!evaluate
5147 && (*arg)[1] != '.'
5148 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005149 {
5150 dict_unref(selfdict);
5151 if (rettv->v_type == VAR_DICT)
5152 {
5153 selfdict = rettv->vval.v_dict;
5154 if (selfdict != NULL)
5155 ++selfdict->dv_refcount;
5156 }
5157 else
5158 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005159 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005160 {
5161 clear_tv(rettv);
5162 ret = FAIL;
5163 }
5164 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005165 else
5166 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005167 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005168
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005169 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5170 // Don't do this when "Func" is already a partial that was bound
5171 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005172 if (selfdict != NULL
5173 && (rettv->v_type == VAR_FUNC
5174 || (rettv->v_type == VAR_PARTIAL
5175 && (rettv->vval.v_partial->pt_auto
5176 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005177 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005178
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005179 dict_unref(selfdict);
5180 return ret;
5181}
5182
5183/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005184 * Make a copy of an item.
5185 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005186 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5187 * reference to an already copied list/dict can be used.
5188 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005189 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005190 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005191item_copy(
5192 typval_T *from,
5193 typval_T *to,
5194 int deep,
5195 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005196{
5197 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005198 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005199
Bram Moolenaar33570922005-01-25 22:26:29 +00005200 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005202 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005203 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005204 }
5205 ++recurse;
5206
5207 switch (from->v_type)
5208 {
5209 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005210 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005211 case VAR_STRING:
5212 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005213 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005214 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005215 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005216 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005217 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005218 copy_tv(from, to);
5219 break;
5220 case VAR_LIST:
5221 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005222 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005223 if (from->vval.v_list == NULL)
5224 to->vval.v_list = NULL;
5225 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5226 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005227 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005228 to->vval.v_list = from->vval.v_list->lv_copylist;
5229 ++to->vval.v_list->lv_refcount;
5230 }
5231 else
5232 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5233 if (to->vval.v_list == NULL)
5234 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005235 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005236 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005238 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005239 case VAR_DICT:
5240 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005241 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005242 if (from->vval.v_dict == NULL)
5243 to->vval.v_dict = NULL;
5244 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5245 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005246 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005247 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5248 ++to->vval.v_dict->dv_refcount;
5249 }
5250 else
5251 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5252 if (to->vval.v_dict == NULL)
5253 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005254 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005255 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005256 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005257 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005258 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005259 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005260 }
5261 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005262 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005263}
5264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265 void
5266echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5267{
5268 char_u *tofree;
5269 char_u numbuf[NUMBUFLEN];
5270 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5271
5272 if (*atstart)
5273 {
5274 *atstart = FALSE;
5275 // Call msg_start() after eval1(), evaluating the expression
5276 // may cause a message to appear.
5277 if (with_space)
5278 {
5279 // Mark the saved text as finishing the line, so that what
5280 // follows is displayed on a new line when scrolling back
5281 // at the more prompt.
5282 msg_sb_eol();
5283 msg_start();
5284 }
5285 }
5286 else if (with_space)
5287 msg_puts_attr(" ", echo_attr);
5288
5289 if (p != NULL)
5290 for ( ; *p != NUL && !got_int; ++p)
5291 {
5292 if (*p == '\n' || *p == '\r' || *p == TAB)
5293 {
5294 if (*p != TAB && *needclr)
5295 {
5296 // remove any text still there from the command
5297 msg_clr_eos();
5298 *needclr = FALSE;
5299 }
5300 msg_putchar_attr(*p, echo_attr);
5301 }
5302 else
5303 {
5304 if (has_mbyte)
5305 {
5306 int i = (*mb_ptr2len)(p);
5307
5308 (void)msg_outtrans_len_attr(p, i, echo_attr);
5309 p += i - 1;
5310 }
5311 else
5312 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5313 }
5314 }
5315 vim_free(tofree);
5316}
5317
Bram Moolenaare9a41262005-01-15 22:18:47 +00005318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 * ":echo expr1 ..." print each argument separated with a space, add a
5320 * newline at the end.
5321 * ":echon expr1 ..." print each argument plain.
5322 */
5323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005324ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
5326 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005327 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 char_u *p;
5329 int needclr = TRUE;
5330 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005331 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005332 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005333 evalarg_T evalarg;
5334
Bram Moolenaare707c882020-07-01 13:07:37 +02005335 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337 if (eap->skip)
5338 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005339 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005341 // If eval1() causes an error message the text from the command may
5342 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005343 need_clr_eos = needclr;
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005346 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
5348 /*
5349 * Report the invalid expression unless the expression evaluation
5350 * has been cancelled due to an aborting error, an interrupt, or an
5351 * exception.
5352 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005353 if (!aborting() && did_emsg == did_emsg_before
5354 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005355 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005356 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 break;
5358 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005359 need_clr_eos = FALSE;
5360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005362 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005363
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005364 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 arg = skipwhite(arg);
5366 }
5367 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005368 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370 if (eap->skip)
5371 --emsg_skip;
5372 else
5373 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005374 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 if (needclr)
5376 msg_clr_eos();
5377 if (eap->cmdidx == CMD_echo)
5378 msg_end();
5379 }
5380}
5381
5382/*
5383 * ":echohl {name}".
5384 */
5385 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005386ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005388 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389}
5390
5391/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005392 * Returns the :echo attribute
5393 */
5394 int
5395get_echo_attr(void)
5396{
5397 return echo_attr;
5398}
5399
5400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 * ":execute expr1 ..." execute the result of an expression.
5402 * ":echomsg expr1 ..." Print a message
5403 * ":echoerr expr1 ..." Print an error
5404 * Each gets spaces around each argument and a newline at the end for
5405 * echo commands
5406 */
5407 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005408ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
5410 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005411 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 int ret = OK;
5413 char_u *p;
5414 garray_T ga;
5415 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
5417 ga_init2(&ga, 1, 80);
5418
5419 if (eap->skip)
5420 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005421 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005423 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005424 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
5427 if (!eap->skip)
5428 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005429 char_u buf[NUMBUFLEN];
5430
5431 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005432 {
5433 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5434 {
5435 emsg(_(e_inval_string));
5436 p = NULL;
5437 }
5438 else
5439 p = tv_get_string_buf(&rettv, buf);
5440 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005441 else
5442 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005443 if (p == NULL)
5444 {
5445 clear_tv(&rettv);
5446 ret = FAIL;
5447 break;
5448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 len = (int)STRLEN(p);
5450 if (ga_grow(&ga, len + 2) == FAIL)
5451 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005452 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 ret = FAIL;
5454 break;
5455 }
5456 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 ga.ga_len += len;
5460 }
5461
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005462 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 arg = skipwhite(arg);
5464 }
5465
5466 if (ret != FAIL && ga.ga_data != NULL)
5467 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005468 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005470 // Mark the already saved text as finishing the line, so that what
5471 // follows is displayed on a new line when scrolling back at the
5472 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005473 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005474 }
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005477 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005478 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005479 out_flush();
5480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 else if (eap->cmdidx == CMD_echoerr)
5482 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005483 int save_did_emsg = did_emsg;
5484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005485 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005486 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 if (!force_abort)
5488 did_emsg = save_did_emsg;
5489 }
5490 else if (eap->cmdidx == CMD_execute)
5491 do_cmdline((char_u *)ga.ga_data,
5492 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5493 }
5494
5495 ga_clear(&ga);
5496
5497 if (eap->skip)
5498 --emsg_skip;
5499
5500 eap->nextcmd = check_nextcmd(arg);
5501}
5502
5503/*
5504 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5505 * "arg" points to the "&" or '+' when called, to "option" when returning.
5506 * Returns NULL when no option name found. Otherwise pointer to the char
5507 * after the option name.
5508 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005509 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005510find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511{
5512 char_u *p = *arg;
5513
5514 ++p;
5515 if (*p == 'g' && p[1] == ':')
5516 {
5517 *opt_flags = OPT_GLOBAL;
5518 p += 2;
5519 }
5520 else if (*p == 'l' && p[1] == ':')
5521 {
5522 *opt_flags = OPT_LOCAL;
5523 p += 2;
5524 }
5525 else
5526 *opt_flags = 0;
5527
5528 if (!ASCII_ISALPHA(*p))
5529 return NULL;
5530 *arg = p;
5531
5532 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005533 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 else
5535 while (ASCII_ISALPHA(*p))
5536 ++p;
5537 return p;
5538}
5539
5540/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005541 * Display script name where an item was last set.
5542 * Should only be invoked when 'verbose' is non-zero.
5543 */
5544 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005545last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005546{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005547 char_u *p;
5548
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005549 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005550 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005551 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005552 if (p != NULL)
5553 {
5554 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005555 msg_puts(_("\n\tLast set from "));
5556 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005557 if (script_ctx.sc_lnum > 0)
5558 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005559 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005560 msg_outnum((long)script_ctx.sc_lnum);
5561 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005562 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005563 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005564 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005565 }
5566}
5567
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005568#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
5570/*
5571 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005572 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 * "flags" can be "g" to do a global substitute.
5574 * Returns an allocated string, NULL for error.
5575 */
5576 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005577do_string_sub(
5578 char_u *str,
5579 char_u *pat,
5580 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005581 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005582 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 int sublen;
5585 regmatch_T regmatch;
5586 int i;
5587 int do_all;
5588 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005589 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 garray_T ga;
5591 char_u *ret;
5592 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005593 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005595 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005597 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
5599 ga_init2(&ga, 1, 200);
5600
5601 do_all = (flags[0] == 'g');
5602
5603 regmatch.rm_ic = p_ic;
5604 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5605 if (regmatch.regprog != NULL)
5606 {
5607 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005608 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5610 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005611 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005612 if (regmatch.startp[0] == regmatch.endp[0])
5613 {
5614 if (zero_width == regmatch.startp[0])
5615 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005616 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005617 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005618 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5619 (size_t)i);
5620 ga.ga_len += i;
5621 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005622 continue;
5623 }
5624 zero_width = regmatch.startp[0];
5625 }
5626
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 /*
5628 * Get some space for a temporary buffer to do the substitution
5629 * into. It will contain:
5630 * - The text up to where the match is.
5631 * - The substituted text.
5632 * - The text after the match.
5633 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005634 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005635 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5637 {
5638 ga_clear(&ga);
5639 break;
5640 }
5641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005642 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 i = (int)(regmatch.startp[0] - tail);
5644 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005645 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005646 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 + ga.ga_len + i, TRUE, TRUE, FALSE);
5648 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005649 tail = regmatch.endp[0];
5650 if (*tail == NUL)
5651 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 if (!do_all)
5653 break;
5654 }
5655
5656 if (ga.ga_data != NULL)
5657 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5658
Bram Moolenaar473de612013-06-08 18:19:48 +02005659 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
5661
5662 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5663 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005664 if (p_cpo == empty_option)
5665 p_cpo = save_cpo;
5666 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005667 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005668 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669
5670 return ret;
5671}