blob: c695468cdfc77fbd88a4c0276f2d393d3d64a407 [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 Moolenaarf96e9de2020-08-03 22:39:28 +0200303 if (*skipwhite(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;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200396 int vim9script = in_vim9script();
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200397 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 Moolenaar2d06bfd2020-07-23 17:16:18 +0200800 semsg(_(e_trailing_arg), p);
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 Moolenaar95dd9f22020-08-07 19:28:08 +0200823 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200825 // "a: type" is declaring variable "a" with a type, not "a:".
826 if (p == name + 2 && p[-1] == ':')
827 {
828 --p;
829 lp->ll_name_end = p;
830 }
831 if (*p == ':')
832 {
833 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
834 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200836 // parse the type after the name
837 lp->ll_type = parse_type(&tp, &si->sn_type_list);
838 lp->ll_name_end = tp;
839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 }
841 }
842
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100843 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
845 return p;
846
847 cc = *p;
848 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100849 // Only pass &ht when we would write to the variable, it prevents autoload
850 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100851 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
852 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000853 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100854 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000855 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000856 if (v == NULL)
857 return NULL;
858
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 /*
860 * Loop until no more [idx] or .key is following.
861 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100863 var1.v_type = VAR_UNKNOWN;
864 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000866 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000867 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
868 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100869 && lp->ll_tv->vval.v_dict != NULL)
870 && !(lp->ll_tv->v_type == VAR_BLOB
871 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100874 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000876 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100880 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000882 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000883
Bram Moolenaar8c711452005-01-14 21:53:12 +0000884 len = -1;
885 if (*p == '.')
886 {
887 key = p + 1;
888 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
889 ;
890 if (len == 0)
891 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000892 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100893 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000895 }
896 p = key + len;
897 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000898 else
899 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100900 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000901 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 if (*p == ':')
903 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000904 else
905 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000906 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200907 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100909 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000910 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100911 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000912 clear_tv(&var1);
913 return NULL;
914 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200915 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000916 }
917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100918 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000919 if (*p == ':')
920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000921 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000923 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100924 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100925 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000926 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000927 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100928 if (rettv != NULL
929 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100930 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100931 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100932 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000934 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100935 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100936 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000937 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000938 }
939 p = skipwhite(p + 1);
940 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000942 else
943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000944 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200945 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200946 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000947 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100948 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000950 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100951 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000952 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100953 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100954 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000955 clear_tv(&var2);
956 return NULL;
957 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000960 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000961 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000963
Bram Moolenaar8c711452005-01-14 21:53:12 +0000964 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000965 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100967 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100968 clear_tv(&var1);
969 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000970 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000971 }
972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100973 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000974 ++p;
975 }
976
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 {
979 if (len == -1)
980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100981 // "[key]": get key from "var1"
982 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200983 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000984 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000985 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000987 }
988 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000989 lp->ll_list = NULL;
990 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000991 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100993 // When assigning to a scope dictionary check that a function and
994 // variable name is valid (only variable name unless it is l: or
995 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200996 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200997 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200998 int prevval;
999 int wrong;
1000
1001 if (len != -1)
1002 {
1003 prevval = key[len];
1004 key[len] = NUL;
1005 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001006 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001007 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001008 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1009 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001010 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001011 || !valid_varname(key);
1012 if (len != -1)
1013 key[len] = prevval;
1014 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001015 {
1016 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001017 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001018 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001019 }
1020
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001022 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001023 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001024 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001025 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001027 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001028 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001029 return NULL;
1030 }
1031
Bram Moolenaar31b81602019-02-10 22:14:27 +01001032 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001036 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001037 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001039 }
1040 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001042 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001043 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001044 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001046 p = NULL;
1047 break;
1048 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001049 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001050 else if ((flags & GLV_READ_ONLY) == 0
1051 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001052 {
1053 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001054 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001055 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001056
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001057 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001059 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001060 else if (lp->ll_tv->v_type == VAR_BLOB)
1061 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001062 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1063
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001064 /*
1065 * Get the number and item for the only or first index of the List.
1066 */
1067 if (empty1)
1068 lp->ll_n1 = 0;
1069 else
1070 // is number or string
1071 lp->ll_n1 = (long)tv_get_number(&var1);
1072 clear_tv(&var1);
1073
1074 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001075 || lp->ll_n1 > bloblen
1076 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001077 {
1078 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001079 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001080 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001081 return NULL;
1082 }
1083 if (lp->ll_range && !lp->ll_empty2)
1084 {
1085 lp->ll_n2 = (long)tv_get_number(&var2);
1086 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001087 if (lp->ll_n2 < 0
1088 || lp->ll_n2 >= bloblen
1089 || lp->ll_n2 < lp->ll_n1)
1090 {
1091 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001092 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001093 return NULL;
1094 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001095 }
1096 lp->ll_blob = lp->ll_tv->vval.v_blob;
1097 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001098 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001099 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001100 else
1101 {
1102 /*
1103 * Get the number and item for the only or first index of the List.
1104 */
1105 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001106 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001107 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001108 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001109 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001110 clear_tv(&var1);
1111
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001112 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 lp->ll_list = lp->ll_tv->vval.v_list;
1114 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1115 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001116 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001117 if (lp->ll_n1 < 0)
1118 {
1119 lp->ll_n1 = 0;
1120 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1121 }
1122 }
1123 if (lp->ll_li == NULL)
1124 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001125 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001126 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001127 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001128 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001129 }
1130
1131 /*
1132 * May need to find the item or absolute index for the second
1133 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 * When no index given: "lp->ll_empty2" is TRUE.
1135 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001136 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001138 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001139 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001140 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001145 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001146 {
1147 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001148 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001150 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001152 }
1153
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001154 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001155 if (lp->ll_n1 < 0)
1156 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1157 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001158 {
1159 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001160 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001162 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001163 }
1164
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001165 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001166 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001167 }
1168
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001169 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001171 return p;
1172}
1173
1174/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001175 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001178clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001179{
1180 vim_free(lp->ll_exp_name);
1181 vim_free(lp->ll_newkey);
1182}
1183
1184/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001185 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001186 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001187 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1188 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001189 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001191set_var_lval(
1192 lval_T *lp,
1193 char_u *endp,
1194 typval_T *rettv,
1195 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001197 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198{
1199 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001200 listitem_T *ri;
1201 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202
1203 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001204 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001205 cc = *endp;
1206 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001207 if (lp->ll_blob != NULL)
1208 {
1209 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001210
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001211 if (op != NULL && *op != '=')
1212 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001213 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001214 return;
1215 }
1216
1217 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1218 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001219 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001220
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001221 if (lp->ll_empty2)
1222 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1223
1224 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001227 return;
1228 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001229 if (lp->ll_empty2)
1230 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001231
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001232 ir = 0;
1233 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1234 blob_set(lp->ll_blob, il,
1235 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001236 }
1237 else
1238 {
1239 val = (int)tv_get_number_chk(rettv, &error);
1240 if (!error)
1241 {
1242 garray_T *gap = &lp->ll_blob->bv_ga;
1243
1244 // Allow for appending a byte. Setting a byte beyond
1245 // the end is an error otherwise.
1246 if (lp->ll_n1 < gap->ga_len
1247 || (lp->ll_n1 == gap->ga_len
1248 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1249 {
1250 blob_set(lp->ll_blob, lp->ll_n1, val);
1251 if (lp->ll_n1 == gap->ga_len)
1252 ++gap->ga_len;
1253 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001254 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001255 }
1256 }
1257 }
1258 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001259 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001260 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001263 {
1264 emsg(_(e_cannot_mod));
1265 *endp = cc;
1266 return;
1267 }
1268
Bram Moolenaarff697e62019-02-12 22:28:33 +01001269 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001270 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001271 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001272 &tv, &di, TRUE, FALSE) == OK)
1273 {
1274 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001275 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1276 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001277 && tv_op(&tv, rettv, op) == OK)
1278 set_var(lp->ll_name, &tv, FALSE);
1279 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001281 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001282 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001283 {
1284 if (lp->ll_type != NULL
1285 && check_typval_type(lp->ll_type, rettv) == FAIL)
1286 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001288 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001289 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001291 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001292 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001293 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001294 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 else if (lp->ll_range)
1296 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001297 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001298 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001301 {
1302 emsg(_("E996: Cannot lock a range"));
1303 return;
1304 }
1305
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001306 /*
1307 * Check whether any of the list items is locked
1308 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001309 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001310 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001311 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001312 return;
1313 ri = ri->li_next;
1314 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1315 break;
1316 ll_li = ll_li->li_next;
1317 ++ll_n1;
1318 }
1319
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001320 /*
1321 * Assign the List values to the list items.
1322 */
1323 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001325 if (op != NULL && *op != '=')
1326 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1327 else
1328 {
1329 clear_tv(&lp->ll_li->li_tv);
1330 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1331 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 ri = ri->li_next;
1333 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1334 break;
1335 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001336 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001337 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001338 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001339 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 ri = NULL;
1341 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001342 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001343 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001344 lp->ll_li = lp->ll_li->li_next;
1345 ++lp->ll_n1;
1346 }
1347 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001348 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001349 else if (lp->ll_empty2
1350 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001351 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001352 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001353 }
1354 else
1355 {
1356 /*
1357 * Assign to a List or Dictionary item.
1358 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001360 {
1361 emsg(_("E996: Cannot lock a list or dict"));
1362 return;
1363 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001364 if (lp->ll_newkey != NULL)
1365 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001366 if (op != NULL && *op != '=')
1367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001368 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001369 return;
1370 }
1371
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001372 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001373 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001374 if (di == NULL)
1375 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001376 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1377 {
1378 vim_free(di);
1379 return;
1380 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001381 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001382 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001383 else if (op != NULL && *op != '=')
1384 {
1385 tv_op(lp->ll_tv, rettv, op);
1386 return;
1387 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001388 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001389 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001390
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001391 /*
1392 * Assign the value to the variable or list item.
1393 */
1394 if (copy)
1395 copy_tv(rettv, lp->ll_tv);
1396 else
1397 {
1398 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001399 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001400 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 }
1402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001403}
1404
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001405/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001406 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1407 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001408 * Returns OK or FAIL.
1409 */
1410 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001411tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001412{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001413 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001414 char_u numbuf[NUMBUFLEN];
1415 char_u *s;
1416
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001417 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001418 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001419 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001420 {
1421 switch (tv1->v_type)
1422 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001423 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001424 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001426 case VAR_DICT:
1427 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001428 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001429 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001430 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001431 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001432 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 break;
1434
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001435 case VAR_BLOB:
1436 if (*op != '+' || tv2->v_type != VAR_BLOB)
1437 break;
1438 // BLOB += BLOB
1439 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1440 {
1441 blob_T *b1 = tv1->vval.v_blob;
1442 blob_T *b2 = tv2->vval.v_blob;
1443 int i, len = blob_len(b2);
1444 for (i = 0; i < len; i++)
1445 ga_append(&b1->bv_ga, blob_get(b2, i));
1446 }
1447 return OK;
1448
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 case VAR_LIST:
1450 if (*op != '+' || tv2->v_type != VAR_LIST)
1451 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001452 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1454 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1455 return OK;
1456
1457 case VAR_NUMBER:
1458 case VAR_STRING:
1459 if (tv2->v_type == VAR_LIST)
1460 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001461 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001462 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001463 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001464 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001465#ifdef FEAT_FLOAT
1466 if (tv2->v_type == VAR_FLOAT)
1467 {
1468 float_T f = n;
1469
Bram Moolenaarff697e62019-02-12 22:28:33 +01001470 if (*op == '%')
1471 break;
1472 switch (*op)
1473 {
1474 case '+': f += tv2->vval.v_float; break;
1475 case '-': f -= tv2->vval.v_float; break;
1476 case '*': f *= tv2->vval.v_float; break;
1477 case '/': f /= tv2->vval.v_float; break;
1478 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001479 clear_tv(tv1);
1480 tv1->v_type = VAR_FLOAT;
1481 tv1->vval.v_float = f;
1482 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001483 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001484#endif
1485 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001486 switch (*op)
1487 {
1488 case '+': n += tv_get_number(tv2); break;
1489 case '-': n -= tv_get_number(tv2); break;
1490 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001491 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1492 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001493 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001494 clear_tv(tv1);
1495 tv1->v_type = VAR_NUMBER;
1496 tv1->vval.v_number = n;
1497 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001498 }
1499 else
1500 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001501 if (tv2->v_type == VAR_FLOAT)
1502 break;
1503
Bram Moolenaarff697e62019-02-12 22:28:33 +01001504 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001505 s = tv_get_string(tv1);
1506 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001507 clear_tv(tv1);
1508 tv1->v_type = VAR_STRING;
1509 tv1->vval.v_string = s;
1510 }
1511 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001512
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001513 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001514#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001515 {
1516 float_T f;
1517
Bram Moolenaarff697e62019-02-12 22:28:33 +01001518 if (*op == '%' || *op == '.'
1519 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001520 && tv2->v_type != VAR_NUMBER
1521 && tv2->v_type != VAR_STRING))
1522 break;
1523 if (tv2->v_type == VAR_FLOAT)
1524 f = tv2->vval.v_float;
1525 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001526 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001527 switch (*op)
1528 {
1529 case '+': tv1->vval.v_float += f; break;
1530 case '-': tv1->vval.v_float -= f; break;
1531 case '*': tv1->vval.v_float *= f; break;
1532 case '/': tv1->vval.v_float /= f; break;
1533 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001534 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001535#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001536 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001537 }
1538 }
1539
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001540 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541 return FAIL;
1542}
1543
1544/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001545 * Evaluate the expression used in a ":for var in expr" command.
1546 * "arg" points to "var".
1547 * Set "*errp" to TRUE for an error, FALSE otherwise;
1548 * Return a pointer that holds the info. Null when there is an error.
1549 */
1550 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001551eval_for_line(
1552 char_u *arg,
1553 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001554 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001555 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001556{
Bram Moolenaar33570922005-01-25 22:26:29 +00001557 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001558 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001559 typval_T tv;
1560 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001561 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001563 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001564
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001565 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001566 if (fi == NULL)
1567 return NULL;
1568
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001569 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001570 if (expr == NULL)
1571 return fi;
1572
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001573 expr = skipwhite_and_linebreak(expr, evalarg);
1574 if (expr[0] != 'i' || expr[1] != 'n'
1575 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001576 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001578 return fi;
1579 }
1580
1581 if (skip)
1582 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001583 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1584 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001585 {
1586 *errp = FALSE;
1587 if (!skip)
1588 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001589 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001590 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001591 l = tv.vval.v_list;
1592 if (l == NULL)
1593 {
1594 // a null list is like an empty list: do nothing
1595 clear_tv(&tv);
1596 }
1597 else
1598 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001600 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001602 // No need to increment the refcount, it's already set for
1603 // the list being used in "tv".
1604 fi->fi_list = l;
1605 list_add_watch(l, &fi->fi_lw);
1606 fi->fi_lw.lw_item = l->lv_first;
1607 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001608 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001609 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001610 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001611 fi->fi_bi = 0;
1612 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001613 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001614 typval_T btv;
1615
1616 // Make a copy, so that the iteration still works when the
1617 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001619 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001620 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001621 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001622 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623 else
1624 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001625 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001626 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001627 }
1628 }
1629 }
1630 if (skip)
1631 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001632 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001633
1634 return fi;
1635}
1636
1637/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001638 * Used when looping over a :for line, skip the "in expr" part.
1639 */
1640 void
1641skip_for_lines(void *fi_void, evalarg_T *evalarg)
1642{
1643 forinfo_T *fi = (forinfo_T *)fi_void;
1644 int i;
1645
1646 for (i = 0; i < fi->fi_break_count; ++i)
1647 eval_next_line(evalarg);
1648}
1649
1650/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 * Use the first item in a ":for" list. Advance to the next.
1652 * Assign the values to the variable (list). "arg" points to the first one.
1653 * Return TRUE when a valid item was found, FALSE when at end of list or
1654 * something wrong.
1655 */
1656 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001657next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001659 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 int result;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001661 int flag = in_vim9script() ? LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001662 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001664 if (fi->fi_blob != NULL)
1665 {
1666 typval_T tv;
1667
1668 if (fi->fi_bi >= blob_len(fi->fi_blob))
1669 return FALSE;
1670 tv.v_type = VAR_NUMBER;
1671 tv.v_lock = VAR_FIXED;
1672 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1673 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001674 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001675 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001676 }
1677
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 item = fi->fi_lw.lw_item;
1679 if (item == NULL)
1680 result = FALSE;
1681 else
1682 {
1683 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001684 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001685 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 }
1687 return result;
1688}
1689
1690/*
1691 * Free the structure used to store info used by ":for".
1692 */
1693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001694free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695{
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001698 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001699 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001701 list_unref(fi->fi_list);
1702 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001703 if (fi != NULL && fi->fi_blob != NULL)
1704 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 vim_free(fi);
1706}
1707
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001709set_context_for_expression(
1710 expand_T *xp,
1711 char_u *arg,
1712 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 int got_eq = FALSE;
1715 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001718 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 {
1720 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001721 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001723 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001724 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 {
1726 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001727 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001728 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 break;
1730 }
1731 return;
1732 }
1733 }
1734 else
1735 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1736 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 while ((xp->xp_pattern = vim_strpbrk(arg,
1738 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1739 {
1740 c = *xp->xp_pattern;
1741 if (c == '&')
1742 {
1743 c = xp->xp_pattern[1];
1744 if (c == '&')
1745 {
1746 ++xp->xp_pattern;
1747 xp->xp_context = cmdidx != CMD_let || got_eq
1748 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1749 }
1750 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001753 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1754 xp->xp_pattern += 2;
1755
1756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
1758 else if (c == '$')
1759 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001760 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 xp->xp_context = EXPAND_ENV_VARS;
1762 }
1763 else if (c == '=')
1764 {
1765 got_eq = TRUE;
1766 xp->xp_context = EXPAND_EXPRESSION;
1767 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001768 else if (c == '#'
1769 && xp->xp_context == EXPAND_EXPRESSION)
1770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001771 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001772 break;
1773 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001774 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 && xp->xp_context == EXPAND_FUNCTIONS
1776 && vim_strchr(xp->xp_pattern, '(') == NULL)
1777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001778 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 break;
1780 }
1781 else if (cmdidx != CMD_let || got_eq)
1782 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001783 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 {
1785 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1786 if (c == '\\' && xp->xp_pattern[1] != NUL)
1787 ++xp->xp_pattern;
1788 xp->xp_context = EXPAND_NOTHING;
1789 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001790 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001792 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1794 /* skip */ ;
1795 xp->xp_context = EXPAND_NOTHING;
1796 }
1797 else if (c == '|')
1798 {
1799 if (xp->xp_pattern[1] == '|')
1800 {
1801 ++xp->xp_pattern;
1802 xp->xp_context = EXPAND_EXPRESSION;
1803 }
1804 else
1805 xp->xp_context = EXPAND_COMMANDS;
1806 }
1807 else
1808 xp->xp_context = EXPAND_EXPRESSION;
1809 }
1810 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001811 // Doesn't look like something valid, expand as an expression
1812 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 arg = xp->xp_pattern;
1815 if (*arg != NUL)
1816 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1817 /* skip */ ;
1818 }
1819 xp->xp_pattern = arg;
1820}
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001823 * Return TRUE if "pat" matches "text".
1824 * Does not use 'cpo' and always uses 'magic'.
1825 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001826 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001827pattern_match(char_u *pat, char_u *text, int ic)
1828{
1829 int matches = FALSE;
1830 char_u *save_cpo;
1831 regmatch_T regmatch;
1832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001833 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001834 save_cpo = p_cpo;
1835 p_cpo = (char_u *)"";
1836 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1837 if (regmatch.regprog != NULL)
1838 {
1839 regmatch.rm_ic = ic;
1840 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1841 vim_regfree(regmatch.regprog);
1842 }
1843 p_cpo = save_cpo;
1844 return matches;
1845}
1846
1847/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001848 * Handle a name followed by "(". Both for just "name(arg)" and for
1849 * "expr->name(arg)".
1850 * Returns OK or FAIL.
1851 */
1852 static int
1853eval_func(
1854 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001855 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001856 char_u *name,
1857 int name_len,
1858 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001859 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001860 typval_T *basetv) // "expr" for "expr->name(arg)"
1861{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001862 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001863 char_u *s = name;
1864 int len = name_len;
1865 partial_T *partial;
1866 int ret = OK;
1867
1868 if (!evaluate)
1869 check_vars(s, len);
1870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001871 // If "s" is the name of a variable of type VAR_FUNC
1872 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001873 s = deref_func_name(s, &len, &partial, !evaluate);
1874
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001875 // Need to make a copy, in case evaluating the arguments makes
1876 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001877 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001878 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001879 ret = FAIL;
1880 else
1881 {
1882 funcexe_T funcexe;
1883
1884 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001885 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001886 funcexe.firstline = curwin->w_cursor.lnum;
1887 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001888 funcexe.evaluate = evaluate;
1889 funcexe.partial = partial;
1890 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001891 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001892 }
1893 vim_free(s);
1894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001895 // If evaluate is FALSE rettv->v_type was not set in
1896 // get_func_tv, but it's needed in handle_subscript() to parse
1897 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001898 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1899 {
1900 rettv->vval.v_string = NULL;
1901 rettv->v_type = VAR_FUNC;
1902 }
1903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001904 // Stop the expression evaluation when immediately
1905 // aborting on error, or when an interrupt occurred or
1906 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001907 if (evaluate && aborting())
1908 {
1909 if (ret == OK)
1910 clear_tv(rettv);
1911 ret = FAIL;
1912 }
1913 return ret;
1914}
1915
1916/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001917 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1918 * comment) and there is a next line, return the next line (skipping blanks)
1919 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001920 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1921 * "arg" must point somewhere inside a line, not at the start.
1922 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001923 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001924eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1925{
Bram Moolenaar9d489562020-07-30 20:08:50 +02001926 char_u *p = skipwhite(arg);
1927
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001928 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001929 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001930 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001931 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001932 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001933 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02001934 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001935
1936 if (evalarg->eval_cookie != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001937 next = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001938 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001939 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001940
Bram Moolenaar9d489562020-07-30 20:08:50 +02001941 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001942 {
1943 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001944 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001945 }
1946 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02001947 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001948}
1949
1950/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001951 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001952 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001953 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001954eval_next_line(evalarg_T *evalarg)
1955{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001956 garray_T *gap = &evalarg->eval_ga;
1957 char_u *line;
1958
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001959 if (evalarg->eval_cookie != NULL)
1960 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
1961 else
1962 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001963 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001964 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1965 {
1966 // Going to concatenate the lines after parsing.
1967 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1968 ++gap->ga_len;
1969 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001970 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001971 {
1972 vim_free(evalarg->eval_tofree);
1973 evalarg->eval_tofree = line;
1974 }
1975 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001976}
1977
Bram Moolenaare6b53242020-07-01 17:28:33 +02001978/*
1979 * Call eval_next_non_blank() and get the next line if needed.
1980 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001981 char_u *
1982skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1983{
1984 int getnext;
1985 char_u *p = skipwhite(arg);
1986
Bram Moolenaar962d7212020-07-04 14:15:00 +02001987 if (evalarg == NULL)
1988 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02001989 eval_next_non_blank(p, evalarg, &getnext);
1990 if (getnext)
1991 return eval_next_line(evalarg);
1992 return p;
1993}
1994
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001995/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001996 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001997 */
1998 void
1999clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2000{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002001 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002002 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002003 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002004 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002005 if (eap != NULL)
2006 {
2007 // We may need to keep the original command line, e.g. for
2008 // ":let" it has the variable names. But we may also need the
2009 // new one, "nextcmd" points into it. Keep both.
2010 vim_free(eap->cmdline_tofree);
2011 eap->cmdline_tofree = *eap->cmdlinep;
2012 *eap->cmdlinep = evalarg->eval_tofree;
2013 }
2014 else
2015 vim_free(evalarg->eval_tofree);
2016 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002017 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002018
2019 vim_free(evalarg->eval_tofree_lambda);
2020 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002021 }
2022}
2023
2024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2028 */
2029
2030/*
2031 * Handle zero level expression.
2032 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002034 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002035 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 * Return OK or FAIL.
2037 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002038 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002039eval0(
2040 char_u *arg,
2041 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002042 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002043 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
2045 int ret;
2046 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002047 int did_emsg_before = did_emsg;
2048 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002049 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002052 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002053 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002054
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002055 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
2057 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002058 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 /*
2060 * Report the invalid expression unless the expression evaluation has
2061 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002062 * exception, or we already gave a more specific error.
2063 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002065 if (!aborting()
2066 && did_emsg == did_emsg_before
2067 && called_emsg == called_emsg_before
2068 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002069 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 ret = FAIL;
2071 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002072
2073 if (eap != NULL)
2074 eap->nextcmd = check_nextcmd(p);
2075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 return ret;
2077}
2078
2079/*
2080 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002081 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 *
2083 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002084 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002086 * Note: "rettv.v_lock" is not set.
2087 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 * Return OK or FAIL.
2089 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002090 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002091eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002093 char_u *p;
2094 int getnext;
2095
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 /*
2097 * Get the first variable.
2098 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002099 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 return FAIL;
2101
Bram Moolenaar793648f2020-06-26 21:28:25 +02002102 p = eval_next_non_blank(*arg, evalarg, &getnext);
2103 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002105 int result;
2106 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002107 evalarg_T *evalarg_used = evalarg;
2108 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002109 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002110 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002111
2112 if (evalarg == NULL)
2113 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002114 CLEAR_FIELD(local_evalarg);
2115 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002117 orig_flags = evalarg_used->eval_flags;
2118 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002119
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002120 if (getnext)
2121 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002122 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002123 {
2124 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2125 {
2126 error_white_both(p, 1);
2127 clear_tv(rettv);
2128 return FAIL;
2129 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002130 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002131 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002132
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002134 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002136 int error = FALSE;
2137
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002138 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002141 if (error)
2142 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
2144
2145 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002146 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002148 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2149 {
2150 error_white_both(p, 1);
2151 clear_tv(rettv);
2152 return FAIL;
2153 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002154 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2155 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002156 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002157 if (eval1(arg, rettv, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 return FAIL;
2159
2160 /*
2161 * Check for the ":".
2162 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002163 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar793648f2020-06-26 21:28:25 +02002164 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 return FAIL;
2170 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002171 if (getnext)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002172 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002173 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002174 {
2175 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2176 {
2177 error_white_both(p, 1);
2178 clear_tv(rettv);
2179 return FAIL;
2180 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002181 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
2184 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002185 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002187 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2188 {
2189 error_white_both(p, 1);
2190 clear_tv(rettv);
2191 return FAIL;
2192 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002193 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2194 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002195 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002196 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
2198 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 return FAIL;
2201 }
2202 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 *rettv = var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002204
2205 if (evalarg == NULL)
2206 clear_evalarg(&local_evalarg, NULL);
2207 else
2208 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
2210
2211 return OK;
2212}
2213
2214/*
2215 * Handle first level expression:
2216 * expr2 || expr2 || expr2 logical OR
2217 *
2218 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002219 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 *
2221 * Return OK or FAIL.
2222 */
2223 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002224eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002226 char_u *p;
2227 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
2229 /*
2230 * Get the first variable.
2231 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002232 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 return FAIL;
2234
2235 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002236 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002238 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002239 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002241 evalarg_T *evalarg_used = evalarg;
2242 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002243 int evaluate;
2244 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002245 long result = FALSE;
2246 typval_T var2;
2247 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002248 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002249
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002250 if (evalarg == NULL)
2251 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002252 CLEAR_FIELD(local_evalarg);
2253 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002254 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002255 orig_flags = evalarg_used->eval_flags;
2256 evaluate = orig_flags & EVAL_EVALUATE;
2257 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002258 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002259 if (vim9script)
2260 {
2261 result = tv2bool(rettv);
2262 }
2263 else
2264 {
2265 error = FALSE;
2266 if (tv_get_number_chk(rettv, &error) != 0)
2267 result = TRUE;
2268 clear_tv(rettv);
2269 if (error)
2270 return FAIL;
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
2273
2274 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002275 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002277 while (p[0] == '|' && p[1] == '|')
2278 {
2279 if (getnext)
2280 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002281 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002282 {
2283 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2284 {
2285 error_white_both(p, 2);
2286 clear_tv(rettv);
2287 return FAIL;
2288 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002289 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002290 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002291
2292 /*
2293 * Get the second variable.
2294 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002295 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2296 {
2297 error_white_both(p, 2);
2298 clear_tv(rettv);
2299 return FAIL;
2300 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002301 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2302 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002303 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002304 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002305 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002306
2307 /*
2308 * Compute the result.
2309 */
2310 if (evaluate && !result)
2311 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002312 if (vim9script)
2313 {
2314 clear_tv(rettv);
2315 *rettv = var2;
2316 result = tv2bool(rettv);
2317 }
2318 else
2319 {
2320 if (tv_get_number_chk(&var2, &error) != 0)
2321 result = TRUE;
2322 clear_tv(&var2);
2323 if (error)
2324 return FAIL;
2325 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002326 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002327 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002328 {
2329 rettv->v_type = VAR_NUMBER;
2330 rettv->vval.v_number = result;
2331 }
2332
2333 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002335
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002336 if (evalarg == NULL)
2337 clear_evalarg(&local_evalarg, NULL);
2338 else
2339 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341
2342 return OK;
2343}
2344
2345/*
2346 * Handle second level expression:
2347 * expr3 && expr3 && expr3 logical AND
2348 *
2349 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002350 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 *
2352 * Return OK or FAIL.
2353 */
2354 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002355eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002357 char_u *p;
2358 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360 /*
2361 * Get the first variable.
2362 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002363 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 return FAIL;
2365
2366 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002367 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002369 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002370 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002372 evalarg_T *evalarg_used = evalarg;
2373 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002374 int orig_flags;
2375 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002376 long result = TRUE;
2377 typval_T var2;
2378 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002379 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002380
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002381 if (evalarg == NULL)
2382 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002383 CLEAR_FIELD(local_evalarg);
2384 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002385 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002386 orig_flags = evalarg_used->eval_flags;
2387 evaluate = orig_flags & EVAL_EVALUATE;
2388 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002389 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002390 if (vim9script)
2391 {
2392 result = tv2bool(rettv);
2393 }
2394 else
2395 {
2396 error = FALSE;
2397 if (tv_get_number_chk(rettv, &error) == 0)
2398 result = FALSE;
2399 clear_tv(rettv);
2400 if (error)
2401 return FAIL;
2402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404
2405 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002406 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002408 while (p[0] == '&' && p[1] == '&')
2409 {
2410 if (getnext)
2411 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002412 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002413 {
2414 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2415 {
2416 error_white_both(p, 2);
2417 clear_tv(rettv);
2418 return FAIL;
2419 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002420 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002421 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002422
2423 /*
2424 * Get the second variable.
2425 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002426 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2427 {
2428 error_white_both(p, 2);
2429 clear_tv(rettv);
2430 return FAIL;
2431 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002432 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2433 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002434 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002435 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002436 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002437
2438 /*
2439 * Compute the result.
2440 */
2441 if (evaluate && result)
2442 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002443 if (vim9script)
2444 {
2445 clear_tv(rettv);
2446 *rettv = var2;
2447 result = tv2bool(rettv);
2448 }
2449 else
2450 {
2451 if (tv_get_number_chk(&var2, &error) == 0)
2452 result = FALSE;
2453 clear_tv(&var2);
2454 if (error)
2455 return FAIL;
2456 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002457 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002458 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002459 {
2460 rettv->v_type = VAR_NUMBER;
2461 rettv->vval.v_number = result;
2462 }
2463
2464 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002466
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002467 if (evalarg == NULL)
2468 clear_evalarg(&local_evalarg, NULL);
2469 else
2470 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472
2473 return OK;
2474}
2475
2476/*
2477 * Handle third level expression:
2478 * var1 == var2
2479 * var1 =~ var2
2480 * var1 != var2
2481 * var1 !~ var2
2482 * var1 > var2
2483 * var1 >= var2
2484 * var1 < var2
2485 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002486 * var1 is var2
2487 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 *
2489 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002490 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 *
2492 * Return OK or FAIL.
2493 */
2494 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002495eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002498 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002499 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002501 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 /*
2504 * Get the first variable.
2505 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002506 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 return FAIL;
2508
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002509 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002510 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002513 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002515 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002517 typval_T var2;
2518 int ic;
2519 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002520 int evaluate = evalarg == NULL
2521 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002522
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002523 if (getnext)
2524 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002525 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2526 {
2527 error_white_both(p, len);
2528 clear_tv(rettv);
2529 return FAIL;
2530 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002531
Bram Moolenaar696ba232020-07-29 21:20:41 +02002532 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2533 {
2534 semsg(_(e_invexpr2), p);
2535 clear_tv(rettv);
2536 return FAIL;
2537 }
2538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002539 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (p[len] == '?')
2541 {
2542 ic = TRUE;
2543 ++len;
2544 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002545 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 else if (p[len] == '#')
2547 {
2548 ic = FALSE;
2549 ++len;
2550 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002551 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002553 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
2555 /*
2556 * Get the second variable.
2557 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002558 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2559 {
2560 error_white_both(p, 1);
2561 clear_tv(rettv);
2562 return FAIL;
2563 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002564 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002565 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 return FAIL;
2569 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002570 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002571 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002572 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002573
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002574 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002575 {
2576 ret = FAIL;
2577 clear_tv(rettv);
2578 }
2579 else
2580 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002581 clear_tv(&var2);
2582 return ret;
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585
2586 return OK;
2587}
2588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 void
2590eval_addblob(typval_T *tv1, typval_T *tv2)
2591{
2592 blob_T *b1 = tv1->vval.v_blob;
2593 blob_T *b2 = tv2->vval.v_blob;
2594 blob_T *b = blob_alloc();
2595 int i;
2596
2597 if (b != NULL)
2598 {
2599 for (i = 0; i < blob_len(b1); i++)
2600 ga_append(&b->bv_ga, blob_get(b1, i));
2601 for (i = 0; i < blob_len(b2); i++)
2602 ga_append(&b->bv_ga, blob_get(b2, i));
2603
2604 clear_tv(tv1);
2605 rettv_blob_set(tv1, b);
2606 }
2607}
2608
2609 int
2610eval_addlist(typval_T *tv1, typval_T *tv2)
2611{
2612 typval_T var3;
2613
2614 // concatenate Lists
2615 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2616 {
2617 clear_tv(tv1);
2618 clear_tv(tv2);
2619 return FAIL;
2620 }
2621 clear_tv(tv1);
2622 *tv1 = var3;
2623 return OK;
2624}
2625
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626/*
2627 * Handle fourth level expression:
2628 * + number addition
2629 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002630 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002631 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 *
2633 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002634 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 *
2636 * Return OK or FAIL.
2637 */
2638 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002639eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 /*
2642 * Get the first variable.
2643 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 return FAIL;
2646
2647 /*
2648 * Repeat computing, until no '+', '-' or '.' is following.
2649 */
2650 for (;;)
2651 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002652 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002653 int getnext;
2654 char_u *p;
2655 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002656 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002657 int concat;
2658 typval_T var2;
2659
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002660 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002661 p = eval_next_non_blank(*arg, evalarg, &getnext);
2662 op = *p;
2663 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002664 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002666
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002667 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002668 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002669 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002670 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002671 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002672 {
2673 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2674 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002675 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002676 clear_tv(rettv);
2677 return FAIL;
2678 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002679 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002680 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002681 if ((op != '+' || (rettv->v_type != VAR_LIST
2682 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002683#ifdef FEAT_FLOAT
2684 && (op == '.' || rettv->v_type != VAR_FLOAT)
2685#endif
2686 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002687 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002688 // For "list + ...", an illegal use of the first operand as
2689 // a number cannot be determined before evaluating the 2nd
2690 // operand: if this is also a list, all is ok.
2691 // For "something . ...", "something - ..." or "non-list + ...",
2692 // we know that the first operand needs to be a string or number
2693 // without evaluating the 2nd operand. So check before to avoid
2694 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002695 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002696 {
2697 clear_tv(rettv);
2698 return FAIL;
2699 }
2700 }
2701
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 /*
2703 * Get the second variable.
2704 */
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002705 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[oplen]))
2706 {
2707 error_white_both(p, oplen);
2708 clear_tv(rettv);
2709 return FAIL;
2710 }
2711 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002712 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002714 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 return FAIL;
2716 }
2717
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002718 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
2720 /*
2721 * Compute the result.
2722 */
2723 if (op == '.')
2724 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002725 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2726 char_u *s1 = tv_get_string_buf(rettv, buf1);
2727 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002729 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002730 {
2731 clear_tv(rettv);
2732 clear_tv(&var2);
2733 return FAIL;
2734 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002736 clear_tv(rettv);
2737 rettv->v_type = VAR_STRING;
2738 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002740 else if (op == '+' && rettv->v_type == VAR_BLOB
2741 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002743 else if (op == '+' && rettv->v_type == VAR_LIST
2744 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002745 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002747 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 else
2750 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002751 int error = FALSE;
2752 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002753#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002754 float_T f1 = 0, f2 = 0;
2755
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002756 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002757 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002758 f1 = rettv->vval.v_float;
2759 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002761 else
2762#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002763 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002764 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002765 if (error)
2766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002767 // This can only happen for "list + non-list". For
2768 // "non-list + ..." or "something - ...", we returned
2769 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002770 clear_tv(rettv);
2771 return FAIL;
2772 }
2773#ifdef FEAT_FLOAT
2774 if (var2.v_type == VAR_FLOAT)
2775 f1 = n1;
2776#endif
2777 }
2778#ifdef FEAT_FLOAT
2779 if (var2.v_type == VAR_FLOAT)
2780 {
2781 f2 = var2.vval.v_float;
2782 n2 = 0;
2783 }
2784 else
2785#endif
2786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002787 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002788 if (error)
2789 {
2790 clear_tv(rettv);
2791 clear_tv(&var2);
2792 return FAIL;
2793 }
2794#ifdef FEAT_FLOAT
2795 if (rettv->v_type == VAR_FLOAT)
2796 f2 = n2;
2797#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002799 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002800
2801#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002802 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002803 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2804 {
2805 if (op == '+')
2806 f1 = f1 + f2;
2807 else
2808 f1 = f1 - f2;
2809 rettv->v_type = VAR_FLOAT;
2810 rettv->vval.v_float = f1;
2811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002813#endif
2814 {
2815 if (op == '+')
2816 n1 = n1 + n2;
2817 else
2818 n1 = n1 - n2;
2819 rettv->v_type = VAR_NUMBER;
2820 rettv->vval.v_number = n1;
2821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002823 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825 }
2826 return OK;
2827}
2828
2829/*
2830 * Handle fifth level expression:
2831 * * number multiplication
2832 * / number division
2833 * % number modulo
2834 *
2835 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002836 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 *
2838 * Return OK or FAIL.
2839 */
2840 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002841eval6(
2842 char_u **arg,
2843 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002844 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002845 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002847#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002848 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
2851 /*
2852 * Get the first variable.
2853 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002854 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 return FAIL;
2856
2857 /*
2858 * Repeat computing, until no '*', '/' or '%' is following.
2859 */
2860 for (;;)
2861 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002862 int evaluate;
2863 int getnext;
2864 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002865 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002866 int op;
2867 varnumber_T n1, n2;
2868#ifdef FEAT_FLOAT
2869 float_T f1, f2;
2870#endif
2871 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002872
Bram Moolenaar9d489562020-07-30 20:08:50 +02002873 p = eval_next_non_blank(*arg, evalarg, &getnext);
2874 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002875 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002877
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002878 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002879 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002880 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002881 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002882 {
2883 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2884 {
2885 error_white_both(p, 1);
2886 clear_tv(rettv);
2887 return FAIL;
2888 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002889 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002892#ifdef FEAT_FLOAT
2893 f1 = 0;
2894 f2 = 0;
2895#endif
2896 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002897 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002899#ifdef FEAT_FLOAT
2900 if (rettv->v_type == VAR_FLOAT)
2901 {
2902 f1 = rettv->vval.v_float;
2903 use_float = TRUE;
2904 n1 = 0;
2905 }
2906 else
2907#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002908 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002910 if (error)
2911 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
2913 else
2914 n1 = 0;
2915
2916 /*
2917 * Get the second variable.
2918 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002919 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2920 {
2921 error_white_both(p, 1);
2922 clear_tv(rettv);
2923 return FAIL;
2924 }
2925 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002926 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 return FAIL;
2928
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002929 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002931#ifdef FEAT_FLOAT
2932 if (var2.v_type == VAR_FLOAT)
2933 {
2934 if (!use_float)
2935 {
2936 f1 = n1;
2937 use_float = TRUE;
2938 }
2939 f2 = var2.vval.v_float;
2940 n2 = 0;
2941 }
2942 else
2943#endif
2944 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002945 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002946 clear_tv(&var2);
2947 if (error)
2948 return FAIL;
2949#ifdef FEAT_FLOAT
2950 if (use_float)
2951 f2 = n2;
2952#endif
2953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
2955 /*
2956 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002957 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002959#ifdef FEAT_FLOAT
2960 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002962 if (op == '*')
2963 f1 = f1 * f2;
2964 else if (op == '/')
2965 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002966# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002967 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002968 if (f2 == 0.0)
2969 {
2970 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002971 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002972 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002973 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002974 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002975 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002976 }
2977 else
2978 f1 = f1 / f2;
2979# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002980 // We rely on the floating point library to handle divide
2981 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002982 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002983# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002986 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002988 return FAIL;
2989 }
2990 rettv->v_type = VAR_FLOAT;
2991 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
2993 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002996 if (op == '*')
2997 n1 = n1 * n2;
2998 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002999 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003001 n1 = num_modulus(n1, n2);
3002
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003003 rettv->v_type = VAR_NUMBER;
3004 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
3007 }
3008
3009 return OK;
3010}
3011
3012/*
3013 * Handle sixth level expression:
3014 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003015 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003016 * "string" string constant
3017 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 * &option-name option value
3019 * @r register contents
3020 * identifier variable value
3021 * function() function call
3022 * $VAR environment variable
3023 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003024 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003026 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003027 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 *
3029 * Also handle:
3030 * ! in front logical NOT
3031 * - in front unary minus
3032 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003033 * trailing [] subscript in String or List
3034 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003035 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 *
3037 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003038 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 *
3040 * Return OK or FAIL.
3041 */
3042 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003043eval7(
3044 char_u **arg,
3045 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003046 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003049 int evaluate = evalarg != NULL
3050 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 int len;
3052 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 char_u *start_leader, *end_leader;
3054 int ret = OK;
3055 char_u *alias;
3056
3057 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003058 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003059 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003064 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 */
3066 start_leader = *arg;
3067 while (**arg == '!' || **arg == '-' || **arg == '+')
3068 *arg = skipwhite(*arg + 1);
3069 end_leader = *arg;
3070
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003071 if (**arg == '.' && (!isdigit(*(*arg + 1))
3072#ifdef FEAT_FLOAT
3073 || current_sctx.sc_version < 2
3074#endif
3075 ))
3076 {
3077 semsg(_(e_invexpr2), *arg);
3078 ++*arg;
3079 return FAIL;
3080 }
3081
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 switch (**arg)
3083 {
3084 /*
3085 * Number constant.
3086 */
3087 case '0':
3088 case '1':
3089 case '2':
3090 case '3':
3091 case '4':
3092 case '5':
3093 case '6':
3094 case '7':
3095 case '8':
3096 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003097 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003098
3099 // Apply prefixed "-" and "+" now. Matters especially when
3100 // "->" follows.
3101 if (ret == OK && evaluate && end_leader > start_leader)
3102 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 break;
3104
3105 /*
3106 * String constant: "string".
3107 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003108 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 break;
3110
3111 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003112 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003114 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003115 break;
3116
3117 /*
3118 * List: [expr, expr]
3119 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003120 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 break;
3122
3123 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003124 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003125 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003126 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003127 {
3128 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003129 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003130 }
3131 else
3132 ret = NOTDONE;
3133 break;
3134
3135 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003136 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003137 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003138 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003139 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003140 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003141 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003142 break;
3143
3144 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003145 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003147 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 break;
3149
3150 /*
3151 * Environment variable: $VAR.
3152 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003153 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 break;
3155
3156 /*
3157 * Register contents: @r.
3158 */
3159 case '@': ++*arg;
3160 if (evaluate)
3161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003162 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003163 rettv->vval.v_string = get_reg_contents(**arg,
3164 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 }
3166 if (**arg != NUL)
3167 ++*arg;
3168 break;
3169
3170 /*
3171 * nested expression: (expression).
3172 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003173 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003174 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003175 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003176
Bram Moolenaar9215f012020-06-27 21:18:00 +02003177 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003178 if (**arg == ')')
3179 ++*arg;
3180 else if (ret == OK)
3181 {
3182 emsg(_(e_missing_close));
3183 clear_tv(rettv);
3184 ret = FAIL;
3185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187 break;
3188
Bram Moolenaar8c711452005-01-14 21:53:12 +00003189 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 break;
3191 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003192
3193 if (ret == NOTDONE)
3194 {
3195 /*
3196 * Must be a variable or function name.
3197 * Can also be a curly-braces kind of name: {expr}.
3198 */
3199 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003200 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003201 if (alias != NULL)
3202 s = alias;
3203
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003204 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003205 ret = FAIL;
3206 else
3207 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003208 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3209
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003210 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3211 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003212 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003213 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003214 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003215 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003216 else if (flags & EVAL_CONSTANT)
3217 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003218 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003219 {
3220 // get the value of "true", "false" or a variable
3221 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3222 {
3223 rettv->v_type = VAR_BOOL;
3224 rettv->vval.v_number = VVAL_TRUE;
3225 }
3226 else if (len == 5 && in_vim9script()
3227 && STRNCMP(s, "false", 4) == 0)
3228 {
3229 rettv->v_type = VAR_BOOL;
3230 rettv->vval.v_number = VVAL_FALSE;
3231 }
3232 else
3233 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3234 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003235 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003236 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003237 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003238 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003239 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003240 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003241 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003242 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003243 }
3244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003245 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3246 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003247 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003248 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
3250 /*
3251 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3252 */
3253 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003254 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003255 return ret;
3256}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003257
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003258/*
3259 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003260 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003261 * Adjusts "end_leaderp" until it is at "start_leader".
3262 */
3263 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003264eval7_leader(
3265 typval_T *rettv,
3266 int numeric_only,
3267 char_u *start_leader,
3268 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003269{
3270 char_u *end_leader = *end_leaderp;
3271 int ret = OK;
3272 int error = FALSE;
3273 varnumber_T val = 0;
3274#ifdef FEAT_FLOAT
3275 float_T f = 0.0;
3276
3277 if (rettv->v_type == VAR_FLOAT)
3278 f = rettv->vval.v_float;
3279 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003280#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003281 val = tv_get_number_chk(rettv, &error);
3282 if (error)
3283 {
3284 clear_tv(rettv);
3285 ret = FAIL;
3286 }
3287 else
3288 {
3289 while (end_leader > start_leader)
3290 {
3291 --end_leader;
3292 if (*end_leader == '!')
3293 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003294 if (numeric_only)
3295 {
3296 ++end_leader;
3297 break;
3298 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003299#ifdef FEAT_FLOAT
3300 if (rettv->v_type == VAR_FLOAT)
3301 f = !f;
3302 else
3303#endif
3304 val = !val;
3305 }
3306 else if (*end_leader == '-')
3307 {
3308#ifdef FEAT_FLOAT
3309 if (rettv->v_type == VAR_FLOAT)
3310 f = -f;
3311 else
3312#endif
3313 val = -val;
3314 }
3315 }
3316#ifdef FEAT_FLOAT
3317 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003319 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003320 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003322 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003323#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003324 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003325 clear_tv(rettv);
3326 rettv->v_type = VAR_NUMBER;
3327 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003330 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 return ret;
3332}
3333
3334/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003335 * Call the function referred to in "rettv".
3336 */
3337 static int
3338call_func_rettv(
3339 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003340 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003341 typval_T *rettv,
3342 int evaluate,
3343 dict_T *selfdict,
3344 typval_T *basetv)
3345{
3346 partial_T *pt = NULL;
3347 funcexe_T funcexe;
3348 typval_T functv;
3349 char_u *s;
3350 int ret;
3351
3352 // need to copy the funcref so that we can clear rettv
3353 if (evaluate)
3354 {
3355 functv = *rettv;
3356 rettv->v_type = VAR_UNKNOWN;
3357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003358 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003359 if (functv.v_type == VAR_PARTIAL)
3360 {
3361 pt = functv.vval.v_partial;
3362 s = partial_name(pt);
3363 }
3364 else
3365 s = functv.vval.v_string;
3366 }
3367 else
3368 s = (char_u *)"";
3369
Bram Moolenaara80faa82020-04-12 19:37:17 +02003370 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003371 funcexe.firstline = curwin->w_cursor.lnum;
3372 funcexe.lastline = curwin->w_cursor.lnum;
3373 funcexe.evaluate = evaluate;
3374 funcexe.partial = pt;
3375 funcexe.selfdict = selfdict;
3376 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003377 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003378
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003379 // Clear the funcref afterwards, so that deleting it while
3380 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003381 if (evaluate)
3382 clear_tv(&functv);
3383
3384 return ret;
3385}
3386
3387/*
3388 * Evaluate "->method()".
3389 * "*arg" points to the '-'.
3390 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3391 */
3392 static int
3393eval_lambda(
3394 char_u **arg,
3395 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003396 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003397 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003398{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003399 int evaluate = evalarg != NULL
3400 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003401 typval_T base = *rettv;
3402 int ret;
3403
3404 // Skip over the ->.
3405 *arg += 2;
3406 rettv->v_type = VAR_UNKNOWN;
3407
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003408 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003409 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003410 return FAIL;
3411 else if (**arg != '(')
3412 {
3413 if (verbose)
3414 {
3415 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003416 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003417 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003419 }
3420 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003421 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003422 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003423 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003424 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003425
3426 // Clear the funcref afterwards, so that deleting it while
3427 // evaluating the arguments is possible (see test55).
3428 if (evaluate)
3429 clear_tv(&base);
3430
3431 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003432}
3433
3434/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003435 * Evaluate "->method()".
3436 * "*arg" points to the '-'.
3437 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3438 */
3439 static int
3440eval_method(
3441 char_u **arg,
3442 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003443 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003444 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003445{
3446 char_u *name;
3447 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003448 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003449 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003450 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003451 int evaluate = evalarg != NULL
3452 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003453
3454 // Skip over the ->.
3455 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003456 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003457
Bram Moolenaarac92e252019-08-03 21:58:38 +02003458 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003459 len = get_name_len(arg, &alias, evaluate, TRUE);
3460 if (alias != NULL)
3461 name = alias;
3462
3463 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003464 {
3465 if (verbose)
3466 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003467 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003468 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003469 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003470 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003471 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003472 if (**arg != '(')
3473 {
3474 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003476 ret = FAIL;
3477 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003478 else if (VIM_ISWHITE((*arg)[-1]))
3479 {
3480 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003481 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003482 ret = FAIL;
3483 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003484 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003485 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003486 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003487 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003488
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003489 // Clear the funcref afterwards, so that deleting it while
3490 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003491 if (evaluate)
3492 clear_tv(&base);
3493
Bram Moolenaarac92e252019-08-03 21:58:38 +02003494 return ret;
3495}
3496
3497/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003498 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3499 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003500 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3501 */
3502 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003503eval_index(
3504 char_u **arg,
3505 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003506 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003507 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003508{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003509 int evaluate = evalarg != NULL
3510 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003511 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003512 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003513 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003514 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003515 long len = -1;
3516 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003517 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003518 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003519
Bram Moolenaara03f2332016-02-06 18:09:59 +01003520 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003521 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003522 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003523 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003524 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003525 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003526 return FAIL;
3527 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003528#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003529 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003530 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003531 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003532#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003533 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003534 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003535 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003536 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003537 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003538 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003539 return FAIL;
3540 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003541 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003543 if (evaluate)
3544 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003545 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003546
3547 case VAR_STRING:
3548 case VAR_NUMBER:
3549 case VAR_LIST:
3550 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003551 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003552 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003553 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003554
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003555 init_tv(&var1);
3556 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003557 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003558 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003559 /*
3560 * dict.name
3561 */
3562 key = *arg + 1;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003563 for (len = 0; eval_isdictc(key[len]); ++len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003564 ;
3565 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003566 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003567 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003568 }
3569 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003570 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003571 /*
3572 * something[idx]
3573 *
3574 * Get the (first) variable from inside the [].
3575 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003576 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003577 if (**arg == ':')
3578 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003579 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003581 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003582 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003583 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003584 clear_tv(&var1);
3585 return FAIL;
3586 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003587
3588 /*
3589 * Get the second variable from inside the [:].
3590 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003591 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003592 if (**arg == ':')
3593 {
3594 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003595 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 if (**arg == ']')
3597 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003598 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003600 if (!empty1)
3601 clear_tv(&var1);
3602 return FAIL;
3603 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003604 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003605 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003606 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003607 if (!empty1)
3608 clear_tv(&var1);
3609 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 return FAIL;
3611 }
3612 }
3613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003614 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003615 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 if (**arg != ']')
3617 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003618 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003619 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 clear_tv(&var1);
3621 if (range)
3622 clear_tv(&var2);
3623 return FAIL;
3624 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003625 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003626 }
3627
3628 if (evaluate)
3629 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003630 n1 = 0;
3631 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003632 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003633 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003634 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003635 }
3636 if (range)
3637 {
3638 if (empty2)
3639 n2 = -1;
3640 else
3641 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003642 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003643 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003644 }
3645 }
3646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003647 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003648 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003649 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003650 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003652 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003653 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003654 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003655 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003656 case VAR_SPECIAL:
3657 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003658 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003659 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003660
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003661 case VAR_NUMBER:
3662 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003663 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003664 len = (long)STRLEN(s);
3665 if (range)
3666 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003667 // The resulting variable is a substring. If the indexes
3668 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003669 if (n1 < 0)
3670 {
3671 n1 = len + n1;
3672 if (n1 < 0)
3673 n1 = 0;
3674 }
3675 if (n2 < 0)
3676 n2 = len + n2;
3677 else if (n2 >= len)
3678 n2 = len;
3679 if (n1 >= len || n2 < 0 || n1 > n2)
3680 s = NULL;
3681 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003682 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003683 }
3684 else
3685 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003686 // The resulting variable is a string of a single
3687 // character. If the index is too big or negative the
3688 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003689 if (n1 >= len || n1 < 0)
3690 s = NULL;
3691 else
3692 s = vim_strnsave(s + n1, 1);
3693 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003694 clear_tv(rettv);
3695 rettv->v_type = VAR_STRING;
3696 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003697 break;
3698
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003699 case VAR_BLOB:
3700 len = blob_len(rettv->vval.v_blob);
3701 if (range)
3702 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003703 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003704 // are out of range the result is empty.
3705 if (n1 < 0)
3706 {
3707 n1 = len + n1;
3708 if (n1 < 0)
3709 n1 = 0;
3710 }
3711 if (n2 < 0)
3712 n2 = len + n2;
3713 else if (n2 >= len)
3714 n2 = len - 1;
3715 if (n1 >= len || n2 < 0 || n1 > n2)
3716 {
3717 clear_tv(rettv);
3718 rettv->v_type = VAR_BLOB;
3719 rettv->vval.v_blob = NULL;
3720 }
3721 else
3722 {
3723 blob_T *blob = blob_alloc();
3724
3725 if (blob != NULL)
3726 {
3727 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3728 {
3729 blob_free(blob);
3730 return FAIL;
3731 }
3732 blob->bv_ga.ga_len = n2 - n1 + 1;
3733 for (i = n1; i <= n2; i++)
3734 blob_set(blob, i - n1,
3735 blob_get(rettv->vval.v_blob, i));
3736
3737 clear_tv(rettv);
3738 rettv_blob_set(rettv, blob);
3739 }
3740 }
3741 }
3742 else
3743 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003744 // The resulting variable is a byte value.
3745 // If the index is too big or negative that is an error.
3746 if (n1 < 0)
3747 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003748 if (n1 < len && n1 >= 0)
3749 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003750 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003751
3752 clear_tv(rettv);
3753 rettv->v_type = VAR_NUMBER;
3754 rettv->vval.v_number = v;
3755 }
3756 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003757 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003758 }
3759 break;
3760
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003761 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003762 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003763 if (n1 < 0)
3764 n1 = len + n1;
3765 if (!empty1 && (n1 < 0 || n1 >= len))
3766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003767 // For a range we allow invalid values and return an empty
3768 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003769 if (!range)
3770 {
3771 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003772 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003773 return FAIL;
3774 }
3775 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003776 }
3777 if (range)
3778 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003780
3781 if (n2 < 0)
3782 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003783 else if (n2 >= len)
3784 n2 = len - 1;
3785 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003786 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003787 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003788 if (l == NULL)
3789 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003790 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003791 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003792 }
3793 else
3794 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003795 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 clear_tv(rettv);
3797 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003798 }
3799 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003800
3801 case VAR_DICT:
3802 if (range)
3803 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003804 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003805 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003806 if (len == -1)
3807 clear_tv(&var1);
3808 return FAIL;
3809 }
3810 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003811 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003812
3813 if (len == -1)
3814 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003815 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003816 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003817 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003818 clear_tv(&var1);
3819 return FAIL;
3820 }
3821 }
3822
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003823 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003824
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003825 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003826 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003827 if (len == -1)
3828 clear_tv(&var1);
3829 if (item == NULL)
3830 return FAIL;
3831
3832 copy_tv(&item->di_tv, &var1);
3833 clear_tv(rettv);
3834 *rettv = var1;
3835 }
3836 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003837 }
3838 }
3839
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003840 return OK;
3841}
3842
3843/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003844 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003845 */
3846 char_u *
3847partial_name(partial_T *pt)
3848{
3849 if (pt->pt_name != NULL)
3850 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003851 if (pt->pt_func != NULL)
3852 return pt->pt_func->uf_name;
3853 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003854}
3855
Bram Moolenaarddecc252016-04-06 22:59:37 +02003856 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003857partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003858{
3859 int i;
3860
3861 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003862 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003863 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003864 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003865 if (pt->pt_name != NULL)
3866 {
3867 func_unref(pt->pt_name);
3868 vim_free(pt->pt_name);
3869 }
3870 else
3871 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003872
3873 if (pt->pt_funcstack != NULL)
3874 {
3875 // Decrease the reference count for the context of a closure. If down
3876 // to zero free it and clear the variables on the stack.
3877 if (--pt->pt_funcstack->fs_refcount == 0)
3878 {
3879 garray_T *gap = &pt->pt_funcstack->fs_ga;
3880 typval_T *stack = gap->ga_data;
3881
3882 for (i = 0; i < gap->ga_len; ++i)
3883 clear_tv(stack + i);
3884 ga_clear(gap);
3885 vim_free(pt->pt_funcstack);
3886 }
3887 pt->pt_funcstack = NULL;
3888 }
3889
Bram Moolenaarddecc252016-04-06 22:59:37 +02003890 vim_free(pt);
3891}
3892
3893/*
3894 * Unreference a closure: decrement the reference count and free it when it
3895 * becomes zero.
3896 */
3897 void
3898partial_unref(partial_T *pt)
3899{
3900 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003901 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003902}
3903
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003904/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003905 * Return the next (unique) copy ID.
3906 * Used for serializing nested structures.
3907 */
3908 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003909get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003910{
3911 current_copyID += COPYID_INC;
3912 return current_copyID;
3913}
3914
3915/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003916 * Garbage collection for lists and dictionaries.
3917 *
3918 * We use reference counts to be able to free most items right away when they
3919 * are no longer used. But for composite items it's possible that it becomes
3920 * unused while the reference count is > 0: When there is a recursive
3921 * reference. Example:
3922 * :let l = [1, 2, 3]
3923 * :let d = {9: l}
3924 * :let l[1] = d
3925 *
3926 * Since this is quite unusual we handle this with garbage collection: every
3927 * once in a while find out which lists and dicts are not referenced from any
3928 * variable.
3929 *
3930 * Here is a good reference text about garbage collection (refers to Python
3931 * but it applies to all reference-counting mechanisms):
3932 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003933 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003934
3935/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003936 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003937 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003938 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003939 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003940 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003941garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003942{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003943 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003944 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003945 buf_T *buf;
3946 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003947 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003948 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003949
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003950 if (!testing)
3951 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003952 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003953 want_garbage_collect = FALSE;
3954 may_garbage_collect = FALSE;
3955 garbage_collect_at_exit = FALSE;
3956 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003957
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003958 // The execution stack can grow big, limit the size.
3959 if (exestack.ga_maxlen - exestack.ga_len > 500)
3960 {
3961 size_t new_len;
3962 char_u *pp;
3963 int n;
3964
3965 // Keep 150% of the current size, with a minimum of the growth size.
3966 n = exestack.ga_len / 2;
3967 if (n < exestack.ga_growsize)
3968 n = exestack.ga_growsize;
3969
3970 // Don't make it bigger though.
3971 if (exestack.ga_len + n < exestack.ga_maxlen)
3972 {
3973 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3974 pp = vim_realloc(exestack.ga_data, new_len);
3975 if (pp == NULL)
3976 return FAIL;
3977 exestack.ga_maxlen = exestack.ga_len + n;
3978 exestack.ga_data = pp;
3979 }
3980 }
3981
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003982 // We advance by two because we add one for items referenced through
3983 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003984 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003985
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003986 /*
3987 * 1. Go through all accessible variables and mark all lists and dicts
3988 * with copyID.
3989 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003991 // Don't free variables in the previous_funccal list unless they are only
3992 // referenced through previous_funccal. This must be first, because if
3993 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003994 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003996 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003997 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003999 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004000 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004001 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4002 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004004 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004005 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004006 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4007 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004008 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004009 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4010 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004011#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004012 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004013 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4014 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004015 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004016 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004017 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4018 NULL, NULL);
4019#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004020
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004021 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004022 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004023 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4024 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004025 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004026 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004028 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004029 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004030
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004031 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004032 abort = abort || set_ref_in_functions(copyID);
4033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004034 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004035 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004037 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004038 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004039
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004040 // callbacks in buffers
4041 abort = abort || set_ref_in_buffers(copyID);
4042
Bram Moolenaar1dced572012-04-05 16:54:08 +02004043#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004044 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004045#endif
4046
Bram Moolenaardb913952012-06-29 12:54:53 +02004047#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004048 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004049#endif
4050
4051#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004052 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004053#endif
4054
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004055#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004056 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004057 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004058#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004059#ifdef FEAT_NETBEANS_INTG
4060 abort = abort || set_ref_in_nb_channel(copyID);
4061#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004062
Bram Moolenaare3188e22016-05-31 21:13:04 +02004063#ifdef FEAT_TIMERS
4064 abort = abort || set_ref_in_timer(copyID);
4065#endif
4066
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004067#ifdef FEAT_QUICKFIX
4068 abort = abort || set_ref_in_quickfix(copyID);
4069#endif
4070
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004071#ifdef FEAT_TERMINAL
4072 abort = abort || set_ref_in_term(copyID);
4073#endif
4074
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004075#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004076 abort = abort || set_ref_in_popups(copyID);
4077#endif
4078
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004079 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004080 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004081 /*
4082 * 2. Free lists and dictionaries that are not referenced.
4083 */
4084 did_free = free_unref_items(copyID);
4085
4086 /*
4087 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004088 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004089 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004090 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004091 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004092 else if (p_verbose > 0)
4093 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004094 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004095 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004096
4097 return did_free;
4098}
4099
4100/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004101 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004102 */
4103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004104free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004105{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004106 int did_free = FALSE;
4107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004108 // Let all "free" functions know that we are here. This means no
4109 // dictionaries, lists, channels or jobs are to be freed, because we will
4110 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004111 in_free_unref_items = TRUE;
4112
4113 /*
4114 * PASS 1: free the contents of the items. We don't free the items
4115 * themselves yet, so that it is possible to decrement refcount counters
4116 */
4117
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004118 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004119 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004120
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004121 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004122 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004123
4124#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004125 // Go through the list of jobs and free items without the copyID. This
4126 // must happen before doing channels, because jobs refer to channels, but
4127 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004128 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4129
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004130 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004131 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4132#endif
4133
4134 /*
4135 * PASS 2: free the items themselves.
4136 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004137 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004138 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004139
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004141 // Go through the list of jobs and free items without the copyID. This
4142 // must happen before doing channels, because jobs refer to channels, but
4143 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004144 free_unused_jobs(copyID, COPYID_MASK);
4145
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004146 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004147 free_unused_channels(copyID, COPYID_MASK);
4148#endif
4149
4150 in_free_unref_items = FALSE;
4151
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004152 return did_free;
4153}
4154
4155/*
4156 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004157 * "list_stack" is used to add lists to be marked. Can be NULL.
4158 *
4159 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004160 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004162set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004163{
4164 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004165 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004166 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004167 hashtab_T *cur_ht;
4168 ht_stack_T *ht_stack = NULL;
4169 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004170
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004171 cur_ht = ht;
4172 for (;;)
4173 {
4174 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004175 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004176 // Mark each item in the hashtab. If the item contains a hashtab
4177 // it is added to ht_stack, if it contains a list it is added to
4178 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004179 todo = (int)cur_ht->ht_used;
4180 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4181 if (!HASHITEM_EMPTY(hi))
4182 {
4183 --todo;
4184 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4185 &ht_stack, list_stack);
4186 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004187 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004188
4189 if (ht_stack == NULL)
4190 break;
4191
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004192 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004193 cur_ht = ht_stack->ht;
4194 tempitem = ht_stack;
4195 ht_stack = ht_stack->prev;
4196 free(tempitem);
4197 }
4198
4199 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004200}
4201
4202/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004203 * Mark a dict and its items with "copyID".
4204 * Returns TRUE if setting references failed somehow.
4205 */
4206 int
4207set_ref_in_dict(dict_T *d, int copyID)
4208{
4209 if (d != NULL && d->dv_copyID != copyID)
4210 {
4211 d->dv_copyID = copyID;
4212 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4213 }
4214 return FALSE;
4215}
4216
4217/*
4218 * Mark a list and its items with "copyID".
4219 * Returns TRUE if setting references failed somehow.
4220 */
4221 int
4222set_ref_in_list(list_T *ll, int copyID)
4223{
4224 if (ll != NULL && ll->lv_copyID != copyID)
4225 {
4226 ll->lv_copyID = copyID;
4227 return set_ref_in_list_items(ll, copyID, NULL);
4228 }
4229 return FALSE;
4230}
4231
4232/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004233 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004234 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4235 *
4236 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004237 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004238 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004239set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004240{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004241 listitem_T *li;
4242 int abort = FALSE;
4243 list_T *cur_l;
4244 list_stack_T *list_stack = NULL;
4245 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004246
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004247 cur_l = l;
4248 for (;;)
4249 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004250 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004251 // Mark each item in the list. If the item contains a hashtab
4252 // it is added to ht_stack, if it contains a list it is added to
4253 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004254 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4255 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4256 ht_stack, &list_stack);
4257 if (list_stack == NULL)
4258 break;
4259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004260 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004261 cur_l = list_stack->list;
4262 tempitem = list_stack;
4263 list_stack = list_stack->prev;
4264 free(tempitem);
4265 }
4266
4267 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004268}
4269
4270/*
4271 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004272 * "list_stack" is used to add lists to be marked. Can be NULL.
4273 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4274 *
4275 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004276 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004278set_ref_in_item(
4279 typval_T *tv,
4280 int copyID,
4281 ht_stack_T **ht_stack,
4282 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004283{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004284 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004285
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004286 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004287 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004288 dict_T *dd = tv->vval.v_dict;
4289
Bram Moolenaara03f2332016-02-06 18:09:59 +01004290 if (dd != NULL && dd->dv_copyID != copyID)
4291 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004292 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004293 dd->dv_copyID = copyID;
4294 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004295 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004296 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4297 }
4298 else
4299 {
4300 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4301 if (newitem == NULL)
4302 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004303 else
4304 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004305 newitem->ht = &dd->dv_hashtab;
4306 newitem->prev = *ht_stack;
4307 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004308 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004309 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004310 }
4311 }
4312 else if (tv->v_type == VAR_LIST)
4313 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004314 list_T *ll = tv->vval.v_list;
4315
Bram Moolenaara03f2332016-02-06 18:09:59 +01004316 if (ll != NULL && ll->lv_copyID != copyID)
4317 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004318 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004319 ll->lv_copyID = copyID;
4320 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004321 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004322 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004323 }
4324 else
4325 {
4326 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004327 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004328 if (newitem == NULL)
4329 abort = TRUE;
4330 else
4331 {
4332 newitem->list = ll;
4333 newitem->prev = *list_stack;
4334 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004335 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004336 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004337 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004338 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004339 else if (tv->v_type == VAR_FUNC)
4340 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004341 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004342 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004343 else if (tv->v_type == VAR_PARTIAL)
4344 {
4345 partial_T *pt = tv->vval.v_partial;
4346 int i;
4347
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004348 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004349 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004350 // Didn't see this partial yet.
4351 pt->pt_copyID = copyID;
4352
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004353 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004354
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004355 if (pt->pt_dict != NULL)
4356 {
4357 typval_T dtv;
4358
4359 dtv.v_type = VAR_DICT;
4360 dtv.vval.v_dict = pt->pt_dict;
4361 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4362 }
4363
4364 for (i = 0; i < pt->pt_argc; ++i)
4365 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4366 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004367 if (pt->pt_funcstack != NULL)
4368 {
4369 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4370
4371 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4372 abort = abort || set_ref_in_item(stack + i, copyID,
4373 ht_stack, list_stack);
4374 }
4375
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004376 }
4377 }
4378#ifdef FEAT_JOB_CHANNEL
4379 else if (tv->v_type == VAR_JOB)
4380 {
4381 job_T *job = tv->vval.v_job;
4382 typval_T dtv;
4383
4384 if (job != NULL && job->jv_copyID != copyID)
4385 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004386 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004387 if (job->jv_channel != NULL)
4388 {
4389 dtv.v_type = VAR_CHANNEL;
4390 dtv.vval.v_channel = job->jv_channel;
4391 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4392 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004393 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004394 {
4395 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004396 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004397 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4398 }
4399 }
4400 }
4401 else if (tv->v_type == VAR_CHANNEL)
4402 {
4403 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004404 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004405 typval_T dtv;
4406 jsonq_T *jq;
4407 cbq_T *cq;
4408
4409 if (ch != NULL && ch->ch_copyID != copyID)
4410 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004411 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004412 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004413 {
4414 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4415 jq = jq->jq_next)
4416 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4417 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4418 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004419 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004420 {
4421 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004422 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004423 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4424 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004425 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004426 {
4427 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004428 dtv.vval.v_partial =
4429 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004430 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4431 }
4432 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004433 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004434 {
4435 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004436 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004437 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4438 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004439 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004440 {
4441 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004442 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004443 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4444 }
4445 }
4446 }
4447#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004448 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004449}
4450
Bram Moolenaar8c711452005-01-14 21:53:12 +00004451/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452 * Return a string with the string representation of a variable.
4453 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004454 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004455 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004456 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4457 * stings as "string()", otherwise does not put quotes around strings, as
4458 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004459 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4460 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004461 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004462 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004463 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004464echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004465 typval_T *tv,
4466 char_u **tofree,
4467 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004468 int copyID,
4469 int echo_style,
4470 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004471 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004472{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004473 static int recurse = 0;
4474 char_u *r = NULL;
4475
Bram Moolenaar33570922005-01-25 22:26:29 +00004476 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004477 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004478 if (!did_echo_string_emsg)
4479 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004480 // Only give this message once for a recursive call to avoid
4481 // flooding the user with errors. And stop iterating over lists
4482 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004483 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004484 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004485 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004486 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004487 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004488 }
4489 ++recurse;
4490
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004491 switch (tv->v_type)
4492 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004493 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004494 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004495 {
4496 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004497 r = tv->vval.v_string;
4498 if (r == NULL)
4499 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004500 }
4501 else
4502 {
4503 *tofree = string_quote(tv->vval.v_string, FALSE);
4504 r = *tofree;
4505 }
4506 break;
4507
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004508 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004509 if (echo_style)
4510 {
4511 *tofree = NULL;
4512 r = tv->vval.v_string;
4513 }
4514 else
4515 {
4516 *tofree = string_quote(tv->vval.v_string, TRUE);
4517 r = *tofree;
4518 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004519 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004520
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004521 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004522 {
4523 partial_T *pt = tv->vval.v_partial;
4524 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004525 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004526 garray_T ga;
4527 int i;
4528 char_u *tf;
4529
4530 ga_init2(&ga, 1, 100);
4531 ga_concat(&ga, (char_u *)"function(");
4532 if (fname != NULL)
4533 {
4534 ga_concat(&ga, fname);
4535 vim_free(fname);
4536 }
4537 if (pt != NULL && pt->pt_argc > 0)
4538 {
4539 ga_concat(&ga, (char_u *)", [");
4540 for (i = 0; i < pt->pt_argc; ++i)
4541 {
4542 if (i > 0)
4543 ga_concat(&ga, (char_u *)", ");
4544 ga_concat(&ga,
4545 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4546 vim_free(tf);
4547 }
4548 ga_concat(&ga, (char_u *)"]");
4549 }
4550 if (pt != NULL && pt->pt_dict != NULL)
4551 {
4552 typval_T dtv;
4553
4554 ga_concat(&ga, (char_u *)", ");
4555 dtv.v_type = VAR_DICT;
4556 dtv.vval.v_dict = pt->pt_dict;
4557 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4558 vim_free(tf);
4559 }
4560 ga_concat(&ga, (char_u *)")");
4561
4562 *tofree = ga.ga_data;
4563 r = *tofree;
4564 break;
4565 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004566
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004567 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004568 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004569 break;
4570
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004571 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004572 if (tv->vval.v_list == NULL)
4573 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004574 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004575 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004576 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004577 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004578 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4579 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004580 {
4581 *tofree = NULL;
4582 r = (char_u *)"[...]";
4583 }
4584 else
4585 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004586 int old_copyID = tv->vval.v_list->lv_copyID;
4587
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004588 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004589 *tofree = list2string(tv, copyID, restore_copyID);
4590 if (restore_copyID)
4591 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004592 r = *tofree;
4593 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004594 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004595
Bram Moolenaar8c711452005-01-14 21:53:12 +00004596 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004597 if (tv->vval.v_dict == NULL)
4598 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004599 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004600 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004601 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004602 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004603 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4604 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004605 {
4606 *tofree = NULL;
4607 r = (char_u *)"{...}";
4608 }
4609 else
4610 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004611 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004612
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004613 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004614 *tofree = dict2string(tv, copyID, restore_copyID);
4615 if (restore_copyID)
4616 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004617 r = *tofree;
4618 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004619 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004620
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004621 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004622 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004623 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004624 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004625 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004626 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004627 break;
4628
Bram Moolenaar835dc632016-02-07 14:27:38 +01004629 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004630 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004631 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004632 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004633 if (composite_val)
4634 {
4635 *tofree = string_quote(r, FALSE);
4636 r = *tofree;
4637 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004639
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004640 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004641#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004642 *tofree = NULL;
4643 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4644 r = numbuf;
4645 break;
4646#endif
4647
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004648 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004649 case VAR_SPECIAL:
4650 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004651 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004652 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004653 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004654
Bram Moolenaar8502c702014-06-17 12:51:16 +02004655 if (--recurse == 0)
4656 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004657 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004658}
4659
4660/*
4661 * Return a string with the string representation of a variable.
4662 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4663 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004664 * Does not put quotes around strings, as ":echo" displays values.
4665 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4666 * May return NULL.
4667 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004668 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004669echo_string(
4670 typval_T *tv,
4671 char_u **tofree,
4672 char_u *numbuf,
4673 int copyID)
4674{
4675 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4676}
4677
4678/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004679 * Return string "str" in ' quotes, doubling ' characters.
4680 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004682 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004683 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004684string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004685{
Bram Moolenaar33570922005-01-25 22:26:29 +00004686 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004687 char_u *p, *r, *s;
4688
Bram Moolenaar33570922005-01-25 22:26:29 +00004689 len = (function ? 13 : 3);
4690 if (str != NULL)
4691 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004692 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004693 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004694 if (*p == '\'')
4695 ++len;
4696 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004697 s = r = alloc(len);
4698 if (r != NULL)
4699 {
4700 if (function)
4701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004702 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004703 r += 10;
4704 }
4705 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004706 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004707 if (str != NULL)
4708 for (p = str; *p != NUL; )
4709 {
4710 if (*p == '\'')
4711 *r++ = '\'';
4712 MB_COPY_CHAR(p, r);
4713 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004714 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004715 if (function)
4716 *r++ = ')';
4717 *r++ = NUL;
4718 }
4719 return s;
4720}
4721
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004722#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004723/*
4724 * Convert the string "text" to a floating point number.
4725 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4726 * this always uses a decimal point.
4727 * Returns the length of the text that was consumed.
4728 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004729 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004730string2float(
4731 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004732 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004733{
4734 char *s = (char *)text;
4735 float_T f;
4736
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004737 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004738 if (STRNICMP(text, "inf", 3) == 0)
4739 {
4740 *value = INFINITY;
4741 return 3;
4742 }
4743 if (STRNICMP(text, "-inf", 3) == 0)
4744 {
4745 *value = -INFINITY;
4746 return 4;
4747 }
4748 if (STRNICMP(text, "nan", 3) == 0)
4749 {
4750 *value = NAN;
4751 return 3;
4752 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004753 f = strtod(s, &s);
4754 *value = f;
4755 return (int)((char_u *)s - text);
4756}
4757#endif
4758
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004761 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004763 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004764var2fpos(
4765 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004766 int dollar_lnum, // TRUE when $ is last line
4767 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004769 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004771 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004773 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004774 if (varp->v_type == VAR_LIST)
4775 {
4776 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004777 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004778 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004779 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004780
4781 l = varp->vval.v_list;
4782 if (l == NULL)
4783 return NULL;
4784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004785 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004786 pos.lnum = list_find_nr(l, 0L, &error);
4787 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004788 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004790 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004791 pos.col = list_find_nr(l, 1L, &error);
4792 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004793 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004794 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004796 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004797 li = list_find(l, 1L);
4798 if (li != NULL && li->li_tv.v_type == VAR_STRING
4799 && li->li_tv.vval.v_string != NULL
4800 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4801 pos.col = len + 1;
4802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004803 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004804 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004805 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004806 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004807
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004808 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004809 pos.coladd = list_find_nr(l, 2L, &error);
4810 if (error)
4811 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004812
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004813 return &pos;
4814 }
4815
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004816 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004817 if (name == NULL)
4818 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004819 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004821 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004822 {
4823 if (VIsual_active)
4824 return &VIsual;
4825 return &curwin->w_cursor;
4826 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004827 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004829 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4831 return NULL;
4832 return pp;
4833 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004834
Bram Moolenaara5525202006-03-02 22:52:09 +00004835 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004836
Bram Moolenaar477933c2007-07-17 14:32:23 +00004837 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004838 {
4839 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004840 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004841 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004842 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004843 // In silent Ex mode topline is zero, but that's not a valid line
4844 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004845 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004846 return &pos;
4847 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004848 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004849 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004850 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004851 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004852 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004853 return &pos;
4854 }
4855 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004856 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004858 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
4860 pos.lnum = curbuf->b_ml.ml_line_count;
4861 pos.col = 0;
4862 }
4863 else
4864 {
4865 pos.lnum = curwin->w_cursor.lnum;
4866 pos.col = (colnr_T)STRLEN(ml_get_curline());
4867 }
4868 return &pos;
4869 }
4870 return NULL;
4871}
4872
4873/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004874 * Convert list in "arg" into a position and optional file number.
4875 * When "fnump" is NULL there is no file number, only 3 items.
4876 * Note that the column is passed on as-is, the caller may want to decrement
4877 * it to use 1 for the first column.
4878 * Return FAIL when conversion is not possible, doesn't check the position for
4879 * validity.
4880 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004881 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004882list2fpos(
4883 typval_T *arg,
4884 pos_T *posp,
4885 int *fnump,
4886 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004887{
4888 list_T *l = arg->vval.v_list;
4889 long i = 0;
4890 long n;
4891
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004892 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4893 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004894 if (arg->v_type != VAR_LIST
4895 || l == NULL
4896 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004897 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004898 return FAIL;
4899
4900 if (fnump != NULL)
4901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004902 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004903 if (n < 0)
4904 return FAIL;
4905 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004906 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004907 *fnump = n;
4908 }
4909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004910 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004911 if (n < 0)
4912 return FAIL;
4913 posp->lnum = n;
4914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004915 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004916 if (n < 0)
4917 return FAIL;
4918 posp->col = n;
4919
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004920 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004921 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004922 posp->coladd = 0;
4923 else
4924 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004925
Bram Moolenaar493c1782014-05-28 14:34:46 +02004926 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004927 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004928
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004929 return OK;
4930}
4931
4932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 * Get the length of an environment variable name.
4934 * Advance "arg" to the first character after the name.
4935 * Return 0 for error.
4936 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004937 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004938get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939{
4940 char_u *p;
4941 int len;
4942
4943 for (p = *arg; vim_isIDc(*p); ++p)
4944 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004945 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 return 0;
4947
4948 len = (int)(p - *arg);
4949 *arg = p;
4950 return len;
4951}
4952
4953/*
4954 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004955 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 * Return 0 if something is wrong.
4957 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004959get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960{
4961 char_u *p;
4962 int len;
4963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004964 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004966 {
4967 if (*p == ':')
4968 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004969 // "s:" is start of "s:var", but "n:" is not and can be used in
4970 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004971 len = (int)(p - *arg);
4972 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4973 || len > 1)
4974 break;
4975 }
4976 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004977 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 return 0;
4979
4980 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004981 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
4983 return len;
4984}
4985
4986/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004987 * Get the length of the name of a variable or function.
4988 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004990 * Return -1 if curly braces expansion failed.
4991 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 * If the name contains 'magic' {}'s, expand them and return the
4993 * expanded name in an allocated string via 'alias' - caller must free.
4994 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004995 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004996get_name_len(
4997 char_u **arg,
4998 char_u **alias,
4999 int evaluate,
5000 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
5002 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 char_u *p;
5004 char_u *expr_start;
5005 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005007 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
5009 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5010 && (*arg)[2] == (int)KE_SNR)
5011 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005012 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 *arg += 3;
5014 return get_id_len(arg) + 3;
5015 }
5016 len = eval_fname_script(*arg);
5017 if (len > 0)
5018 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005019 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 *arg += len;
5021 }
5022
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005024 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005026 p = find_name_end(*arg, &expr_start, &expr_end,
5027 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 if (expr_start != NULL)
5029 {
5030 char_u *temp_string;
5031
5032 if (!evaluate)
5033 {
5034 len += (int)(p - *arg);
5035 *arg = skipwhite(p);
5036 return len;
5037 }
5038
5039 /*
5040 * Include any <SID> etc in the expanded string:
5041 * Thus the -len here.
5042 */
5043 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5044 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005045 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 *alias = temp_string;
5047 *arg = skipwhite(p);
5048 return (int)STRLEN(temp_string);
5049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050
5051 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005052 // Only give an error when there is something, otherwise it will be
5053 // reported at a higher level.
5054 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005055 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056
5057 return len;
5058}
5059
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005060/*
5061 * Find the end of a variable or function name, taking care of magic braces.
5062 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5063 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005064 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 * Return a pointer to just after the name. Equal to "arg" if there is no
5066 * valid name.
5067 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005068 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005069find_name_end(
5070 char_u *arg,
5071 char_u **expr_start,
5072 char_u **expr_end,
5073 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005075 int mb_nest = 0;
5076 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005078 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005079 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005081 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 *expr_start = NULL;
5084 *expr_end = NULL;
5085 }
5086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005087 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005088 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5089 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005090 return arg;
5091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005092 for (p = arg; *p != NUL
5093 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005094 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005095 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005096 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005098 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005100 if (*p == '\'')
5101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005102 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005103 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005104 ;
5105 if (*p == NUL)
5106 break;
5107 }
5108 else if (*p == '"')
5109 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005110 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005111 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005112 if (*p == '\\' && p[1] != NUL)
5113 ++p;
5114 if (*p == NUL)
5115 break;
5116 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005117 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5118 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005119 // "s:" is start of "s:var", but "n:" is not and can be used in
5120 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005121 len = (int)(p - arg);
5122 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005123 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005124 break;
5125 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005126
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005129 if (*p == '[')
5130 ++br_nest;
5131 else if (*p == ']')
5132 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005134
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005135 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005137 if (*p == '{')
5138 {
5139 mb_nest++;
5140 if (expr_start != NULL && *expr_start == NULL)
5141 *expr_start = p;
5142 }
5143 else if (*p == '}')
5144 {
5145 mb_nest--;
5146 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5147 *expr_end = p;
5148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151
5152 return p;
5153}
5154
5155/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005156 * Expands out the 'magic' {}'s in a variable/function name.
5157 * Note that this can call itself recursively, to deal with
5158 * constructs like foo{bar}{baz}{bam}
5159 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5160 * "in_start" ^
5161 * "expr_start" ^
5162 * "expr_end" ^
5163 * "in_end" ^
5164 *
5165 * Returns a new allocated string, which the caller must free.
5166 * Returns NULL for failure.
5167 */
5168 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005169make_expanded_name(
5170 char_u *in_start,
5171 char_u *expr_start,
5172 char_u *expr_end,
5173 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005174{
5175 char_u c1;
5176 char_u *retval = NULL;
5177 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005178
5179 if (expr_end == NULL || in_end == NULL)
5180 return NULL;
5181 *expr_start = NUL;
5182 *expr_end = NUL;
5183 c1 = *in_end;
5184 *in_end = NUL;
5185
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005186 temp_result = eval_to_string(expr_start + 1, FALSE);
5187 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005188 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005189 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5190 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005191 if (retval != NULL)
5192 {
5193 STRCPY(retval, in_start);
5194 STRCAT(retval, temp_result);
5195 STRCAT(retval, expr_end + 1);
5196 }
5197 }
5198 vim_free(temp_result);
5199
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005200 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005201 *expr_start = '{';
5202 *expr_end = '}';
5203
5204 if (retval != NULL)
5205 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005206 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005207 if (expr_start != NULL)
5208 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005209 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005210 temp_result = make_expanded_name(retval, expr_start,
5211 expr_end, temp_result);
5212 vim_free(retval);
5213 retval = temp_result;
5214 }
5215 }
5216
5217 return retval;
5218}
5219
5220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005222 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005224 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005225eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005227 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005228}
5229
5230/*
5231 * Return TRUE if character "c" can be used as the first character in a
5232 * variable or function name (excluding '{' and '}').
5233 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005234 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005235eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005236{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005237 return ASCII_ISALPHA(c) || c == '_';
5238}
5239
5240/*
5241 * Return TRUE if character "c" can be used as the first character of a
5242 * dictionary key.
5243 */
5244 int
5245eval_isdictc(int c)
5246{
5247 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248}
5249
5250/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005251 * Handle:
5252 * - expr[expr], expr[expr:expr] subscript
5253 * - ".name" lookup
5254 * - function call with Funcref variable: func(expr)
5255 * - method call: var->method()
5256 *
5257 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005258 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005259 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005260handle_subscript(
5261 char_u **arg,
5262 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005263 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005264 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005266 int evaluate = evalarg != NULL
5267 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005268 int ret = OK;
5269 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005270 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005271 int getnext;
5272 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005273
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005274 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005275 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005276 // When at the end of the line and ".name" or "->{" or "->X" follows in
5277 // the next line then consume the line break.
5278 p = eval_next_non_blank(*arg, evalarg, &getnext);
5279 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005280 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005281 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005282 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005283 {
5284 *arg = eval_next_line(evalarg);
5285 check_white = FALSE;
5286 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005287
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005288 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5289 || rettv->v_type == VAR_PARTIAL))
5290 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005291 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005292 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5293 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005294
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005295 // Stop the expression evaluation when immediately aborting on
5296 // error, or when an interrupt occurred or an exception was thrown
5297 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005298 if (aborting())
5299 {
5300 if (ret == OK)
5301 clear_tv(rettv);
5302 ret = FAIL;
5303 }
5304 dict_unref(selfdict);
5305 selfdict = NULL;
5306 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005307 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005308 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005309 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005310 if (ret == OK)
5311 {
5312 if ((*arg)[2] == '{')
5313 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005314 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005315 else
5316 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005317 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005318 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005319 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005320 // "." is ".name" lookup when we found a dict or when evaluating and
5321 // scriptversion is at least 2, where string concatenation is "..".
5322 else if (**arg == '['
5323 || (**arg == '.' && (rettv->v_type == VAR_DICT
5324 || (!evaluate
5325 && (*arg)[1] != '.'
5326 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005327 {
5328 dict_unref(selfdict);
5329 if (rettv->v_type == VAR_DICT)
5330 {
5331 selfdict = rettv->vval.v_dict;
5332 if (selfdict != NULL)
5333 ++selfdict->dv_refcount;
5334 }
5335 else
5336 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005337 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005338 {
5339 clear_tv(rettv);
5340 ret = FAIL;
5341 }
5342 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005343 else
5344 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005345 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005346
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005347 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5348 // Don't do this when "Func" is already a partial that was bound
5349 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005350 if (selfdict != NULL
5351 && (rettv->v_type == VAR_FUNC
5352 || (rettv->v_type == VAR_PARTIAL
5353 && (rettv->vval.v_partial->pt_auto
5354 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005355 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005356
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005357 dict_unref(selfdict);
5358 return ret;
5359}
5360
5361/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005362 * Make a copy of an item.
5363 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005364 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5365 * reference to an already copied list/dict can be used.
5366 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005367 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005369item_copy(
5370 typval_T *from,
5371 typval_T *to,
5372 int deep,
5373 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005374{
5375 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005376 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005377
Bram Moolenaar33570922005-01-25 22:26:29 +00005378 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005380 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005381 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005382 }
5383 ++recurse;
5384
5385 switch (from->v_type)
5386 {
5387 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005388 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005389 case VAR_STRING:
5390 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005391 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005392 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005393 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005394 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005395 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005396 copy_tv(from, to);
5397 break;
5398 case VAR_LIST:
5399 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005400 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005401 if (from->vval.v_list == NULL)
5402 to->vval.v_list = NULL;
5403 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5404 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005405 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005406 to->vval.v_list = from->vval.v_list->lv_copylist;
5407 ++to->vval.v_list->lv_refcount;
5408 }
5409 else
5410 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5411 if (to->vval.v_list == NULL)
5412 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005413 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005414 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005415 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005416 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005417 case VAR_DICT:
5418 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005419 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005420 if (from->vval.v_dict == NULL)
5421 to->vval.v_dict = NULL;
5422 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5423 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005424 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005425 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5426 ++to->vval.v_dict->dv_refcount;
5427 }
5428 else
5429 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5430 if (to->vval.v_dict == NULL)
5431 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005432 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005433 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005434 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005435 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005436 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005437 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005438 }
5439 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005440 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005441}
5442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005443 void
5444echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5445{
5446 char_u *tofree;
5447 char_u numbuf[NUMBUFLEN];
5448 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5449
5450 if (*atstart)
5451 {
5452 *atstart = FALSE;
5453 // Call msg_start() after eval1(), evaluating the expression
5454 // may cause a message to appear.
5455 if (with_space)
5456 {
5457 // Mark the saved text as finishing the line, so that what
5458 // follows is displayed on a new line when scrolling back
5459 // at the more prompt.
5460 msg_sb_eol();
5461 msg_start();
5462 }
5463 }
5464 else if (with_space)
5465 msg_puts_attr(" ", echo_attr);
5466
5467 if (p != NULL)
5468 for ( ; *p != NUL && !got_int; ++p)
5469 {
5470 if (*p == '\n' || *p == '\r' || *p == TAB)
5471 {
5472 if (*p != TAB && *needclr)
5473 {
5474 // remove any text still there from the command
5475 msg_clr_eos();
5476 *needclr = FALSE;
5477 }
5478 msg_putchar_attr(*p, echo_attr);
5479 }
5480 else
5481 {
5482 if (has_mbyte)
5483 {
5484 int i = (*mb_ptr2len)(p);
5485
5486 (void)msg_outtrans_len_attr(p, i, echo_attr);
5487 p += i - 1;
5488 }
5489 else
5490 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5491 }
5492 }
5493 vim_free(tofree);
5494}
5495
Bram Moolenaare9a41262005-01-15 22:18:47 +00005496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 * ":echo expr1 ..." print each argument separated with a space, add a
5498 * newline at the end.
5499 * ":echon expr1 ..." print each argument plain.
5500 */
5501 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005502ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503{
5504 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005505 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 char_u *p;
5507 int needclr = TRUE;
5508 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005509 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005510 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005511 evalarg_T evalarg;
5512
Bram Moolenaare707c882020-07-01 13:07:37 +02005513 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514
5515 if (eap->skip)
5516 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005517 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005519 // If eval1() causes an error message the text from the command may
5520 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005521 need_clr_eos = needclr;
5522
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005524 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 {
5526 /*
5527 * Report the invalid expression unless the expression evaluation
5528 * has been cancelled due to an aborting error, an interrupt, or an
5529 * exception.
5530 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005531 if (!aborting() && did_emsg == did_emsg_before
5532 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005533 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005534 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 break;
5536 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005537 need_clr_eos = FALSE;
5538
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005540 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 arg = skipwhite(arg);
5544 }
5545 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005546 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548 if (eap->skip)
5549 --emsg_skip;
5550 else
5551 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005552 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 if (needclr)
5554 msg_clr_eos();
5555 if (eap->cmdidx == CMD_echo)
5556 msg_end();
5557 }
5558}
5559
5560/*
5561 * ":echohl {name}".
5562 */
5563 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005564ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005566 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567}
5568
5569/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005570 * Returns the :echo attribute
5571 */
5572 int
5573get_echo_attr(void)
5574{
5575 return echo_attr;
5576}
5577
5578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 * ":execute expr1 ..." execute the result of an expression.
5580 * ":echomsg expr1 ..." Print a message
5581 * ":echoerr expr1 ..." Print an error
5582 * Each gets spaces around each argument and a newline at the end for
5583 * echo commands
5584 */
5585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005586ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
5588 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005589 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 int ret = OK;
5591 char_u *p;
5592 garray_T ga;
5593 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
5595 ga_init2(&ga, 1, 80);
5596
5597 if (eap->skip)
5598 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005599 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005601 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005602 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
5605 if (!eap->skip)
5606 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005607 char_u buf[NUMBUFLEN];
5608
5609 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005610 {
5611 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5612 {
5613 emsg(_(e_inval_string));
5614 p = NULL;
5615 }
5616 else
5617 p = tv_get_string_buf(&rettv, buf);
5618 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005619 else
5620 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005621 if (p == NULL)
5622 {
5623 clear_tv(&rettv);
5624 ret = FAIL;
5625 break;
5626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 len = (int)STRLEN(p);
5628 if (ga_grow(&ga, len + 2) == FAIL)
5629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 ret = FAIL;
5632 break;
5633 }
5634 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 ga.ga_len += len;
5638 }
5639
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005640 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 arg = skipwhite(arg);
5642 }
5643
5644 if (ret != FAIL && ga.ga_data != NULL)
5645 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005646 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005648 // Mark the already saved text as finishing the line, so that what
5649 // follows is displayed on a new line when scrolling back at the
5650 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005651 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005652 }
5653
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005655 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005656 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005657 out_flush();
5658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 else if (eap->cmdidx == CMD_echoerr)
5660 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005661 int save_did_emsg = did_emsg;
5662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005663 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005664 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 if (!force_abort)
5666 did_emsg = save_did_emsg;
5667 }
5668 else if (eap->cmdidx == CMD_execute)
5669 do_cmdline((char_u *)ga.ga_data,
5670 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5671 }
5672
5673 ga_clear(&ga);
5674
5675 if (eap->skip)
5676 --emsg_skip;
5677
5678 eap->nextcmd = check_nextcmd(arg);
5679}
5680
5681/*
5682 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5683 * "arg" points to the "&" or '+' when called, to "option" when returning.
5684 * Returns NULL when no option name found. Otherwise pointer to the char
5685 * after the option name.
5686 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005687 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005688find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 char_u *p = *arg;
5691
5692 ++p;
5693 if (*p == 'g' && p[1] == ':')
5694 {
5695 *opt_flags = OPT_GLOBAL;
5696 p += 2;
5697 }
5698 else if (*p == 'l' && p[1] == ':')
5699 {
5700 *opt_flags = OPT_LOCAL;
5701 p += 2;
5702 }
5703 else
5704 *opt_flags = 0;
5705
5706 if (!ASCII_ISALPHA(*p))
5707 return NULL;
5708 *arg = p;
5709
5710 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005711 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 else
5713 while (ASCII_ISALPHA(*p))
5714 ++p;
5715 return p;
5716}
5717
5718/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005719 * Display script name where an item was last set.
5720 * Should only be invoked when 'verbose' is non-zero.
5721 */
5722 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005723last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005724{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005725 char_u *p;
5726
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005727 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005728 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005729 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005730 if (p != NULL)
5731 {
5732 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005733 msg_puts(_("\n\tLast set from "));
5734 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005735 if (script_ctx.sc_lnum > 0)
5736 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005737 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005738 msg_outnum((long)script_ctx.sc_lnum);
5739 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005740 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005741 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005742 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005743 }
5744}
5745
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005746#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
5748/*
5749 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005750 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 * "flags" can be "g" to do a global substitute.
5752 * Returns an allocated string, NULL for error.
5753 */
5754 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005755do_string_sub(
5756 char_u *str,
5757 char_u *pat,
5758 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005759 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005760 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761{
5762 int sublen;
5763 regmatch_T regmatch;
5764 int i;
5765 int do_all;
5766 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005767 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 garray_T ga;
5769 char_u *ret;
5770 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005771 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005773 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005775 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776
5777 ga_init2(&ga, 1, 200);
5778
5779 do_all = (flags[0] == 'g');
5780
5781 regmatch.rm_ic = p_ic;
5782 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5783 if (regmatch.regprog != NULL)
5784 {
5785 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005786 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5788 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005789 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005790 if (regmatch.startp[0] == regmatch.endp[0])
5791 {
5792 if (zero_width == regmatch.startp[0])
5793 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005794 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005795 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005796 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5797 (size_t)i);
5798 ga.ga_len += i;
5799 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005800 continue;
5801 }
5802 zero_width = regmatch.startp[0];
5803 }
5804
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 /*
5806 * Get some space for a temporary buffer to do the substitution
5807 * into. It will contain:
5808 * - The text up to where the match is.
5809 * - The substituted text.
5810 * - The text after the match.
5811 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005812 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005813 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5815 {
5816 ga_clear(&ga);
5817 break;
5818 }
5819
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005820 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 i = (int)(regmatch.startp[0] - tail);
5822 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005823 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005824 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 + ga.ga_len + i, TRUE, TRUE, FALSE);
5826 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005827 tail = regmatch.endp[0];
5828 if (*tail == NUL)
5829 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 if (!do_all)
5831 break;
5832 }
5833
5834 if (ga.ga_data != NULL)
5835 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5836
Bram Moolenaar473de612013-06-08 18:19:48 +02005837 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 }
5839
5840 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5841 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005842 if (p_cpo == empty_option)
5843 p_cpo = save_cpo;
5844 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005845 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005846 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
5848 return ret;
5849}