blob: 119b7cc189ac4c4cb68d0782ab2c0365d558fdad [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010025#define NAMESPACE_CHAR (char_u *)"abglstvw"
26
Bram Moolenaar532c7802005-01-27 14:44:31 +000027/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000028 * When recursively copying lists and dicts we need to remember which ones we
29 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000030 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000031 */
32static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020033
Bram Moolenaar071d4272004-06-13 20:20:40 +000034/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000035 * Info used by a ":for" loop.
36 */
Bram Moolenaar33570922005-01-25 22:26:29 +000037typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000038{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010039 int fi_semicolon; // TRUE if ending in '; var]'
40 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020041 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010042 listwatch_T fi_lw; // keep an eye on the item used.
43 list_T *fi_list; // list being used
44 int fi_bi; // index of blob
45 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
54static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
62 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020063 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010064num_divide(varnumber_T n1, varnumber_T n2)
65{
66 varnumber_T result;
67
68 if (n2 == 0) // give an error message?
69 {
70 if (n1 == 0)
71 result = VARNUM_MIN; // similar to NaN
72 else if (n1 < 0)
73 result = -VARNUM_MAX;
74 else
75 result = VARNUM_MAX;
76 }
77 else
78 result = n1 / n2;
79
80 return result;
81}
82
83/*
84 * Return "n1" modulus "n2", taking care of dividing by zero.
85 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020086 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010087num_modulus(varnumber_T n1, varnumber_T n2)
88{
89 // Give an error when n2 is 0?
90 return (n2 == 0) ? 0 : (n1 % n2);
91}
92
Bram Moolenaara1fa8922017-01-12 20:06:33 +010093#if defined(EBCDIC) || defined(PROTO)
94/*
95 * Compare struct fst by function name.
96 */
97 static int
98compare_func_name(const void *s1, const void *s2)
99{
100 struct fst *p1 = (struct fst *)s1;
101 struct fst *p2 = (struct fst *)s2;
102
103 return STRCMP(p1->f_name, p2->f_name);
104}
105
106/*
107 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100108 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100109 * On machines using EBCDIC we have to sort it.
110 */
111 static void
112sortFunctions(void)
113{
114 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
115
116 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
117}
118#endif
119
Bram Moolenaar33570922005-01-25 22:26:29 +0000120/*
121 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000122 */
123 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100124eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000125{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200127 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000128
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200129#ifdef EBCDIC
130 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100131 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200132 */
133 sortFunctions();
134#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000135}
136
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000137#if defined(EXITFREE) || defined(PROTO)
138 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100139eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200141 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100142 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200143 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000144
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200145 // autoloaded script names
146 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000147
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200148 // unreferenced lists and dicts
149 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200150
151 // functions not garbage collected
152 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000153}
154#endif
155
Bram Moolenaare6b53242020-07-01 17:28:33 +0200156 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200157fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
158{
159 CLEAR_FIELD(*evalarg);
160 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
161 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
162 {
163 evalarg->eval_getline = eap->getline;
164 evalarg->eval_cookie = eap->cookie;
165 }
166}
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
169 * Top level evaluation function, returning a boolean.
170 * Sets "error" to TRUE if there was an error.
171 * Return TRUE or FALSE.
172 */
173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100174eval_to_bool(
175 char_u *arg,
176 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200177 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100178 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179{
Bram Moolenaar33570922005-01-25 22:26:29 +0000180 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200181 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200182 evalarg_T evalarg;
183
Bram Moolenaar37c83712020-06-30 21:18:36 +0200184 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186 if (skip)
187 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200188 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 else
191 {
192 *error = FALSE;
193 if (!skip)
194 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100195 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000196 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 }
198 }
199 if (skip)
200 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200201 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200203 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204}
205
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206/*
207 * Call eval1() and give an error message if not done at a lower level.
208 */
209 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200210eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100211{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100212 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100213 int ret;
214 int did_emsg_before = did_emsg;
215 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200216 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100217
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200218 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
219
220 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100221 if (ret == FAIL)
222 {
223 // Report the invalid expression unless the expression evaluation has
224 // been cancelled due to an aborting error, an interrupt, or an
225 // exception, or we already gave a more specific error.
226 // Also check called_emsg for when using assert_fails().
227 if (!aborting() && did_emsg == did_emsg_before
228 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100229 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100230 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200231 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100232 return ret;
233}
234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200236 * Return whether a typval is a valid expression to pass to eval_expr_typval()
237 * or eval_expr_to_bool(). An empty string returns FALSE;
238 */
239 int
240eval_expr_valid_arg(typval_T *tv)
241{
242 return tv->v_type != VAR_UNKNOWN
243 && (tv->v_type != VAR_STRING
244 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
245}
246
247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 * Evaluate an expression, which can be a function, partial or string.
249 * Pass arguments "argv[argc]".
250 * Return the result in "rettv" and OK or FAIL.
251 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200252 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100253eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
254{
255 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100256 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200257 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100258
259 if (expr->v_type == VAR_FUNC)
260 {
261 s = expr->vval.v_string;
262 if (s == NULL || *s == NUL)
263 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200264 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200265 funcexe.evaluate = TRUE;
266 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267 return FAIL;
268 }
269 else if (expr->v_type == VAR_PARTIAL)
270 {
271 partial_T *partial = expr->vval.v_partial;
272
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200273 if (partial == NULL)
274 return FAIL;
275
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200277 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200279 if (call_def_function(partial->pt_func, argc, argv,
280 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 return FAIL;
282 }
283 else
284 {
285 s = partial_name(partial);
286 if (s == NULL || *s == NUL)
287 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200288 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 funcexe.evaluate = TRUE;
290 funcexe.partial = partial;
291 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
292 return FAIL;
293 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 }
295 else
296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100297 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 if (s == NULL)
299 return FAIL;
300 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200301 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100303 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100304 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200305 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100306 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100307 return FAIL;
308 }
309 }
310 return OK;
311}
312
313/*
314 * Like eval_to_bool() but using a typval_T instead of a string.
315 * Works for string, funcref and partial.
316 */
317 int
318eval_expr_to_bool(typval_T *expr, int *error)
319{
320 typval_T rettv;
321 int res;
322
323 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
324 {
325 *error = TRUE;
326 return FALSE;
327 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100328 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100329 clear_tv(&rettv);
330 return res;
331}
332
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333/*
334 * Top level evaluation function, returning a string. If "skip" is TRUE,
335 * only parsing to "nextcmd" is done, without reporting errors. Return
336 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
337 */
338 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100339eval_to_string_skip(
340 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200341 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100342 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
Bram Moolenaar33570922005-01-25 22:26:29 +0000344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200346 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar37c83712020-06-30 21:18:36 +0200348 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 if (skip)
350 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200351 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 retval = NULL;
353 else
354 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100355 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000356 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 }
358 if (skip)
359 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200360 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
362 return retval;
363}
364
365/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366 * Skip over an expression at "*pp".
367 * Return FAIL for an error, OK otherwise.
368 */
369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100370skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000371{
Bram Moolenaar33570922005-01-25 22:26:29 +0000372 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000373
374 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200375 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000376}
377
378/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200379 * Skip over an expression at "*pp".
380 * If in Vim9 script and line breaks are encountered, the lines are
381 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200382 * "arg" is advanced to just after the expression.
383 * "start" is set to the start of the expression, "end" to just after the end.
384 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200385 * Return FAIL for an error, OK otherwise.
386 */
387 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200388skip_expr_concatenate(
389 char_u **arg,
390 char_u **start,
391 char_u **end,
392 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200393{
394 typval_T rettv;
395 int res;
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 Moolenaareb6880b2020-07-12 17:07:05 +0200823 if (in_vim9script() && *p == ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100825 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 char_u *tp = skipwhite(p + 1);
827
828 // parse the type after the name
829 lp->ll_type = parse_type(&tp, &si->sn_type_list);
830 lp->ll_name_end = tp;
831 }
832 }
833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100834 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
836 return p;
837
838 cc = *p;
839 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100840 // Only pass &ht when we would write to the variable, it prevents autoload
841 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100842 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
843 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100845 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000846 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000847 if (v == NULL)
848 return NULL;
849
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 /*
851 * Loop until no more [idx] or .key is following.
852 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000853 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100854 var1.v_type = VAR_UNKNOWN;
855 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000857 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
859 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100860 && lp->ll_tv->vval.v_dict != NULL)
861 && !(lp->ll_tv->v_type == VAR_BLOB
862 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000864 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100865 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000867 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100871 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000873 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000874
Bram Moolenaar8c711452005-01-14 21:53:12 +0000875 len = -1;
876 if (*p == '.')
877 {
878 key = p + 1;
879 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
880 ;
881 if (len == 0)
882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100884 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000885 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000886 }
887 p = key + len;
888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000889 else
890 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100891 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000892 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000893 if (*p == ':')
894 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000895 else
896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000897 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200898 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100900 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100902 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000903 clear_tv(&var1);
904 return NULL;
905 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000906 }
907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100908 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000909 if (*p == ':')
910 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100915 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000916 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000917 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100918 if (rettv != NULL
919 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100920 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100921 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100922 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000924 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100925 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100926 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000927 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000928 }
929 p = skipwhite(p + 1);
930 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000931 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000932 else
933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000934 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200935 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200936 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000937 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100938 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000939 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000940 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100941 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000942 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100943 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100944 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000945 clear_tv(&var2);
946 return NULL;
947 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000948 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000951 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000953
Bram Moolenaar8c711452005-01-14 21:53:12 +0000954 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000955 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100957 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100958 clear_tv(&var1);
959 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000960 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000961 }
962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100963 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000964 ++p;
965 }
966
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000968 {
969 if (len == -1)
970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100971 // "[key]": get key from "var1"
972 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200973 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000974 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000977 }
978 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000979 lp->ll_list = NULL;
980 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000981 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100983 // When assigning to a scope dictionary check that a function and
984 // variable name is valid (only variable name unless it is l: or
985 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200986 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200987 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200988 int prevval;
989 int wrong;
990
991 if (len != -1)
992 {
993 prevval = key[len];
994 key[len] = NUL;
995 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200996 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100997 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200998 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
999 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001000 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001001 || !valid_varname(key);
1002 if (len != -1)
1003 key[len] = prevval;
1004 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001005 {
1006 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001007 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001008 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001009 }
1010
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001012 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001013 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001014 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001015 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001016 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001017 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001018 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001019 return NULL;
1020 }
1021
Bram Moolenaar31b81602019-02-10 22:14:27 +01001022 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001026 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001027 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 }
1030 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001031 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001032 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001034 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001036 p = NULL;
1037 break;
1038 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001039 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001040 else if ((flags & GLV_READ_ONLY) == 0
1041 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001042 {
1043 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001044 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001045 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001046
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001047 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001050 else if (lp->ll_tv->v_type == VAR_BLOB)
1051 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001052 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1053
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001054 /*
1055 * Get the number and item for the only or first index of the List.
1056 */
1057 if (empty1)
1058 lp->ll_n1 = 0;
1059 else
1060 // is number or string
1061 lp->ll_n1 = (long)tv_get_number(&var1);
1062 clear_tv(&var1);
1063
1064 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001065 || lp->ll_n1 > bloblen
1066 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001067 {
1068 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001069 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001070 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001071 return NULL;
1072 }
1073 if (lp->ll_range && !lp->ll_empty2)
1074 {
1075 lp->ll_n2 = (long)tv_get_number(&var2);
1076 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001077 if (lp->ll_n2 < 0
1078 || lp->ll_n2 >= bloblen
1079 || lp->ll_n2 < lp->ll_n1)
1080 {
1081 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001082 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001083 return NULL;
1084 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001085 }
1086 lp->ll_blob = lp->ll_tv->vval.v_blob;
1087 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001088 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001089 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001090 else
1091 {
1092 /*
1093 * Get the number and item for the only or first index of the List.
1094 */
1095 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001096 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001098 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001099 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001100 clear_tv(&var1);
1101
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001102 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001103 lp->ll_list = lp->ll_tv->vval.v_list;
1104 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1105 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001106 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001107 if (lp->ll_n1 < 0)
1108 {
1109 lp->ll_n1 = 0;
1110 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1111 }
1112 }
1113 if (lp->ll_li == NULL)
1114 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001115 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001116 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001117 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001118 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001119 }
1120
1121 /*
1122 * May need to find the item or absolute index for the second
1123 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 * When no index given: "lp->ll_empty2" is TRUE.
1125 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001126 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001128 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001129 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001130 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001131 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001132 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001133 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001136 {
1137 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001138 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001140 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 }
1143
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001144 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 if (lp->ll_n1 < 0)
1146 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1147 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001148 {
1149 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001150 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001152 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001153 }
1154
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001155 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001157 }
1158
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001159 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 return p;
1162}
1163
1164/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001165 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001168clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001169{
1170 vim_free(lp->ll_exp_name);
1171 vim_free(lp->ll_newkey);
1172}
1173
1174/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001175 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001177 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1178 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001179 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001181set_var_lval(
1182 lval_T *lp,
1183 char_u *endp,
1184 typval_T *rettv,
1185 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001187 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001188{
1189 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001190 listitem_T *ri;
1191 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001192
1193 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001194 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001195 cc = *endp;
1196 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001197 if (lp->ll_blob != NULL)
1198 {
1199 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001200
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001201 if (op != NULL && *op != '=')
1202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001204 return;
1205 }
1206
1207 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1208 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001209 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001210
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001211 if (lp->ll_empty2)
1212 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1213
1214 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001217 return;
1218 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001219 if (lp->ll_empty2)
1220 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001221
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001222 ir = 0;
1223 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1224 blob_set(lp->ll_blob, il,
1225 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001226 }
1227 else
1228 {
1229 val = (int)tv_get_number_chk(rettv, &error);
1230 if (!error)
1231 {
1232 garray_T *gap = &lp->ll_blob->bv_ga;
1233
1234 // Allow for appending a byte. Setting a byte beyond
1235 // the end is an error otherwise.
1236 if (lp->ll_n1 < gap->ga_len
1237 || (lp->ll_n1 == gap->ga_len
1238 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1239 {
1240 blob_set(lp->ll_blob, lp->ll_n1, val);
1241 if (lp->ll_n1 == gap->ga_len)
1242 ++gap->ga_len;
1243 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001244 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001245 }
1246 }
1247 }
1248 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001249 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001250 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001253 {
1254 emsg(_(e_cannot_mod));
1255 *endp = cc;
1256 return;
1257 }
1258
Bram Moolenaarff697e62019-02-12 22:28:33 +01001259 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001260 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001261 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001262 &tv, &di, TRUE, FALSE) == OK)
1263 {
1264 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001265 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1266 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001267 && tv_op(&tv, rettv, op) == OK)
1268 set_var(lp->ll_name, &tv, FALSE);
1269 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001271 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001272 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001273 {
1274 if (lp->ll_type != NULL
1275 && check_typval_type(lp->ll_type, rettv) == FAIL)
1276 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001278 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001279 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001281 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001282 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001283 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001284 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001285 else if (lp->ll_range)
1286 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001287 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001288 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001291 {
1292 emsg(_("E996: Cannot lock a range"));
1293 return;
1294 }
1295
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001296 /*
1297 * Check whether any of the list items is locked
1298 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001299 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001300 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001301 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001302 return;
1303 ri = ri->li_next;
1304 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1305 break;
1306 ll_li = ll_li->li_next;
1307 ++ll_n1;
1308 }
1309
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001310 /*
1311 * Assign the List values to the list items.
1312 */
1313 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001314 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001315 if (op != NULL && *op != '=')
1316 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1317 else
1318 {
1319 clear_tv(&lp->ll_li->li_tv);
1320 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1321 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 ri = ri->li_next;
1323 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1324 break;
1325 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001326 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001327 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001328 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001329 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 ri = NULL;
1331 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001332 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001333 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 lp->ll_li = lp->ll_li->li_next;
1335 ++lp->ll_n1;
1336 }
1337 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001338 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001339 else if (lp->ll_empty2
1340 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001341 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001342 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001343 }
1344 else
1345 {
1346 /*
1347 * Assign to a List or Dictionary item.
1348 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001350 {
1351 emsg(_("E996: Cannot lock a list or dict"));
1352 return;
1353 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001354 if (lp->ll_newkey != NULL)
1355 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001356 if (op != NULL && *op != '=')
1357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001358 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359 return;
1360 }
1361
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001362 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001363 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001364 if (di == NULL)
1365 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001366 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1367 {
1368 vim_free(di);
1369 return;
1370 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001371 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001372 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001373 else if (op != NULL && *op != '=')
1374 {
1375 tv_op(lp->ll_tv, rettv, op);
1376 return;
1377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001378 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001379 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001380
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001381 /*
1382 * Assign the value to the variable or list item.
1383 */
1384 if (copy)
1385 copy_tv(rettv, lp->ll_tv);
1386 else
1387 {
1388 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001389 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001390 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 }
1392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393}
1394
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001395/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001396 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1397 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001398 * Returns OK or FAIL.
1399 */
1400 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001401tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001402{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001403 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001404 char_u numbuf[NUMBUFLEN];
1405 char_u *s;
1406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001407 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001408 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001409 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001410 {
1411 switch (tv1->v_type)
1412 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001413 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001414 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001416 case VAR_DICT:
1417 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001418 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001419 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001420 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001421 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001422 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001423 break;
1424
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001425 case VAR_BLOB:
1426 if (*op != '+' || tv2->v_type != VAR_BLOB)
1427 break;
1428 // BLOB += BLOB
1429 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1430 {
1431 blob_T *b1 = tv1->vval.v_blob;
1432 blob_T *b2 = tv2->vval.v_blob;
1433 int i, len = blob_len(b2);
1434 for (i = 0; i < len; i++)
1435 ga_append(&b1->bv_ga, blob_get(b2, i));
1436 }
1437 return OK;
1438
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001439 case VAR_LIST:
1440 if (*op != '+' || tv2->v_type != VAR_LIST)
1441 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001442 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001443 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1444 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1445 return OK;
1446
1447 case VAR_NUMBER:
1448 case VAR_STRING:
1449 if (tv2->v_type == VAR_LIST)
1450 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001451 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001452 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001453 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001454 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001455#ifdef FEAT_FLOAT
1456 if (tv2->v_type == VAR_FLOAT)
1457 {
1458 float_T f = n;
1459
Bram Moolenaarff697e62019-02-12 22:28:33 +01001460 if (*op == '%')
1461 break;
1462 switch (*op)
1463 {
1464 case '+': f += tv2->vval.v_float; break;
1465 case '-': f -= tv2->vval.v_float; break;
1466 case '*': f *= tv2->vval.v_float; break;
1467 case '/': f /= tv2->vval.v_float; break;
1468 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001469 clear_tv(tv1);
1470 tv1->v_type = VAR_FLOAT;
1471 tv1->vval.v_float = f;
1472 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001473 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001474#endif
1475 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001476 switch (*op)
1477 {
1478 case '+': n += tv_get_number(tv2); break;
1479 case '-': n -= tv_get_number(tv2); break;
1480 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001481 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1482 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001483 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001484 clear_tv(tv1);
1485 tv1->v_type = VAR_NUMBER;
1486 tv1->vval.v_number = n;
1487 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001488 }
1489 else
1490 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001491 if (tv2->v_type == VAR_FLOAT)
1492 break;
1493
Bram Moolenaarff697e62019-02-12 22:28:33 +01001494 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001495 s = tv_get_string(tv1);
1496 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001497 clear_tv(tv1);
1498 tv1->v_type = VAR_STRING;
1499 tv1->vval.v_string = s;
1500 }
1501 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001502
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001503 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001504#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001505 {
1506 float_T f;
1507
Bram Moolenaarff697e62019-02-12 22:28:33 +01001508 if (*op == '%' || *op == '.'
1509 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001510 && tv2->v_type != VAR_NUMBER
1511 && tv2->v_type != VAR_STRING))
1512 break;
1513 if (tv2->v_type == VAR_FLOAT)
1514 f = tv2->vval.v_float;
1515 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001516 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001517 switch (*op)
1518 {
1519 case '+': tv1->vval.v_float += f; break;
1520 case '-': tv1->vval.v_float -= f; break;
1521 case '*': tv1->vval.v_float *= f; break;
1522 case '/': tv1->vval.v_float /= f; break;
1523 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001524 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001525#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001526 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001527 }
1528 }
1529
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001530 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001531 return FAIL;
1532}
1533
1534/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001535 * Evaluate the expression used in a ":for var in expr" command.
1536 * "arg" points to "var".
1537 * Set "*errp" to TRUE for an error, FALSE otherwise;
1538 * Return a pointer that holds the info. Null when there is an error.
1539 */
1540 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001541eval_for_line(
1542 char_u *arg,
1543 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001544 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001545 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001546{
Bram Moolenaar33570922005-01-25 22:26:29 +00001547 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001548 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001549 typval_T tv;
1550 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001551 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001552
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001553 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001554
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001555 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001556 if (fi == NULL)
1557 return NULL;
1558
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001559 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001560 if (expr == NULL)
1561 return fi;
1562
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001563 expr = skipwhite_and_linebreak(expr, evalarg);
1564 if (expr[0] != 'i' || expr[1] != 'n'
1565 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001566 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001568 return fi;
1569 }
1570
1571 if (skip)
1572 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001573 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1574 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001575 {
1576 *errp = FALSE;
1577 if (!skip)
1578 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001579 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001580 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001581 l = tv.vval.v_list;
1582 if (l == NULL)
1583 {
1584 // a null list is like an empty list: do nothing
1585 clear_tv(&tv);
1586 }
1587 else
1588 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001590 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001592 // No need to increment the refcount, it's already set for
1593 // the list being used in "tv".
1594 fi->fi_list = l;
1595 list_add_watch(l, &fi->fi_lw);
1596 fi->fi_lw.lw_item = l->lv_first;
1597 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001598 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001599 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001600 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001601 fi->fi_bi = 0;
1602 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001603 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001604 typval_T btv;
1605
1606 // Make a copy, so that the iteration still works when the
1607 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001609 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001610 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001611 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001612 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613 else
1614 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001615 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001616 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001617 }
1618 }
1619 }
1620 if (skip)
1621 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001622 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623
1624 return fi;
1625}
1626
1627/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001628 * Used when looping over a :for line, skip the "in expr" part.
1629 */
1630 void
1631skip_for_lines(void *fi_void, evalarg_T *evalarg)
1632{
1633 forinfo_T *fi = (forinfo_T *)fi_void;
1634 int i;
1635
1636 for (i = 0; i < fi->fi_break_count; ++i)
1637 eval_next_line(evalarg);
1638}
1639
1640/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001641 * Use the first item in a ":for" list. Advance to the next.
1642 * Assign the values to the variable (list). "arg" points to the first one.
1643 * Return TRUE when a valid item was found, FALSE when at end of list or
1644 * something wrong.
1645 */
1646 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001647next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001649 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650 int result;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001651 int flag = in_vim9script() ? LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001652 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001654 if (fi->fi_blob != NULL)
1655 {
1656 typval_T tv;
1657
1658 if (fi->fi_bi >= blob_len(fi->fi_blob))
1659 return FALSE;
1660 tv.v_type = VAR_NUMBER;
1661 tv.v_lock = VAR_FIXED;
1662 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1663 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001664 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001665 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001666 }
1667
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 item = fi->fi_lw.lw_item;
1669 if (item == NULL)
1670 result = FALSE;
1671 else
1672 {
1673 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001674 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001675 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001676 }
1677 return result;
1678}
1679
1680/*
1681 * Free the structure used to store info used by ":for".
1682 */
1683 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001684free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685{
Bram Moolenaar33570922005-01-25 22:26:29 +00001686 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001688 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001689 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001691 list_unref(fi->fi_list);
1692 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001693 if (fi != NULL && fi->fi_blob != NULL)
1694 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 vim_free(fi);
1696}
1697
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001699set_context_for_expression(
1700 expand_T *xp,
1701 char_u *arg,
1702 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703{
1704 int got_eq = FALSE;
1705 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001708 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 {
1710 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001711 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001713 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001714 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 {
1716 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001717 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001718 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 break;
1720 }
1721 return;
1722 }
1723 }
1724 else
1725 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1726 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 while ((xp->xp_pattern = vim_strpbrk(arg,
1728 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1729 {
1730 c = *xp->xp_pattern;
1731 if (c == '&')
1732 {
1733 c = xp->xp_pattern[1];
1734 if (c == '&')
1735 {
1736 ++xp->xp_pattern;
1737 xp->xp_context = cmdidx != CMD_let || got_eq
1738 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1739 }
1740 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001741 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001743 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1744 xp->xp_pattern += 2;
1745
1746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 }
1748 else if (c == '$')
1749 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001750 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 xp->xp_context = EXPAND_ENV_VARS;
1752 }
1753 else if (c == '=')
1754 {
1755 got_eq = TRUE;
1756 xp->xp_context = EXPAND_EXPRESSION;
1757 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001758 else if (c == '#'
1759 && xp->xp_context == EXPAND_EXPRESSION)
1760 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001761 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001762 break;
1763 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001764 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 && xp->xp_context == EXPAND_FUNCTIONS
1766 && vim_strchr(xp->xp_pattern, '(') == NULL)
1767 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001768 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 break;
1770 }
1771 else if (cmdidx != CMD_let || got_eq)
1772 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001773 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 {
1775 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1776 if (c == '\\' && xp->xp_pattern[1] != NUL)
1777 ++xp->xp_pattern;
1778 xp->xp_context = EXPAND_NOTHING;
1779 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001780 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001782 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1784 /* skip */ ;
1785 xp->xp_context = EXPAND_NOTHING;
1786 }
1787 else if (c == '|')
1788 {
1789 if (xp->xp_pattern[1] == '|')
1790 {
1791 ++xp->xp_pattern;
1792 xp->xp_context = EXPAND_EXPRESSION;
1793 }
1794 else
1795 xp->xp_context = EXPAND_COMMANDS;
1796 }
1797 else
1798 xp->xp_context = EXPAND_EXPRESSION;
1799 }
1800 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001801 // Doesn't look like something valid, expand as an expression
1802 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 arg = xp->xp_pattern;
1805 if (*arg != NUL)
1806 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1807 /* skip */ ;
1808 }
1809 xp->xp_pattern = arg;
1810}
1811
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001813 * Return TRUE if "pat" matches "text".
1814 * Does not use 'cpo' and always uses 'magic'.
1815 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001816 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001817pattern_match(char_u *pat, char_u *text, int ic)
1818{
1819 int matches = FALSE;
1820 char_u *save_cpo;
1821 regmatch_T regmatch;
1822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001823 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001824 save_cpo = p_cpo;
1825 p_cpo = (char_u *)"";
1826 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1827 if (regmatch.regprog != NULL)
1828 {
1829 regmatch.rm_ic = ic;
1830 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1831 vim_regfree(regmatch.regprog);
1832 }
1833 p_cpo = save_cpo;
1834 return matches;
1835}
1836
1837/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001838 * Handle a name followed by "(". Both for just "name(arg)" and for
1839 * "expr->name(arg)".
1840 * Returns OK or FAIL.
1841 */
1842 static int
1843eval_func(
1844 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001845 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001846 char_u *name,
1847 int name_len,
1848 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001849 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001850 typval_T *basetv) // "expr" for "expr->name(arg)"
1851{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001852 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001853 char_u *s = name;
1854 int len = name_len;
1855 partial_T *partial;
1856 int ret = OK;
1857
1858 if (!evaluate)
1859 check_vars(s, len);
1860
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001861 // If "s" is the name of a variable of type VAR_FUNC
1862 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001863 s = deref_func_name(s, &len, &partial, !evaluate);
1864
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001865 // Need to make a copy, in case evaluating the arguments makes
1866 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001867 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001868 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001869 ret = FAIL;
1870 else
1871 {
1872 funcexe_T funcexe;
1873
1874 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001875 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001876 funcexe.firstline = curwin->w_cursor.lnum;
1877 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001878 funcexe.evaluate = evaluate;
1879 funcexe.partial = partial;
1880 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001881 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001882 }
1883 vim_free(s);
1884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001885 // If evaluate is FALSE rettv->v_type was not set in
1886 // get_func_tv, but it's needed in handle_subscript() to parse
1887 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001888 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1889 {
1890 rettv->vval.v_string = NULL;
1891 rettv->v_type = VAR_FUNC;
1892 }
1893
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001894 // Stop the expression evaluation when immediately
1895 // aborting on error, or when an interrupt occurred or
1896 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001897 if (evaluate && aborting())
1898 {
1899 if (ret == OK)
1900 clear_tv(rettv);
1901 ret = FAIL;
1902 }
1903 return ret;
1904}
1905
1906/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001907 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1908 * comment) and there is a next line, return the next line (skipping blanks)
1909 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001910 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1911 * "arg" must point somewhere inside a line, not at the start.
1912 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001913 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001914eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1915{
Bram Moolenaar9d489562020-07-30 20:08:50 +02001916 char_u *p = skipwhite(arg);
1917
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001918 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001919 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001920 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001921 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001922 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001923 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02001924 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001925
1926 if (evalarg->eval_cookie != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001927 next = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001928 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001929 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001930
Bram Moolenaar9d489562020-07-30 20:08:50 +02001931 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001932 {
1933 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001934 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001935 }
1936 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02001937 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001938}
1939
1940/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001941 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001942 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001943 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001944eval_next_line(evalarg_T *evalarg)
1945{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001946 garray_T *gap = &evalarg->eval_ga;
1947 char_u *line;
1948
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001949 if (evalarg->eval_cookie != NULL)
1950 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
1951 else
1952 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001953 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001954 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1955 {
1956 // Going to concatenate the lines after parsing.
1957 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1958 ++gap->ga_len;
1959 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001960 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001961 {
1962 vim_free(evalarg->eval_tofree);
1963 evalarg->eval_tofree = line;
1964 }
1965 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001966}
1967
Bram Moolenaare6b53242020-07-01 17:28:33 +02001968/*
1969 * Call eval_next_non_blank() and get the next line if needed.
1970 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001971 char_u *
1972skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1973{
1974 int getnext;
1975 char_u *p = skipwhite(arg);
1976
Bram Moolenaar962d7212020-07-04 14:15:00 +02001977 if (evalarg == NULL)
1978 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02001979 eval_next_non_blank(p, evalarg, &getnext);
1980 if (getnext)
1981 return eval_next_line(evalarg);
1982 return p;
1983}
1984
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001985/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001986 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001987 */
1988 void
1989clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1990{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001991 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001992 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001993 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001994 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001995 if (eap != NULL)
1996 {
1997 // We may need to keep the original command line, e.g. for
1998 // ":let" it has the variable names. But we may also need the
1999 // new one, "nextcmd" points into it. Keep both.
2000 vim_free(eap->cmdline_tofree);
2001 eap->cmdline_tofree = *eap->cmdlinep;
2002 *eap->cmdlinep = evalarg->eval_tofree;
2003 }
2004 else
2005 vim_free(evalarg->eval_tofree);
2006 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002007 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002008
2009 vim_free(evalarg->eval_tofree_lambda);
2010 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002011 }
2012}
2013
2014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2018 */
2019
2020/*
2021 * Handle zero level expression.
2022 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002023 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002024 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002025 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 * Return OK or FAIL.
2027 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002028 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002029eval0(
2030 char_u *arg,
2031 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002032 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002033 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034{
2035 int ret;
2036 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002037 int did_emsg_before = did_emsg;
2038 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002039 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040
2041 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002042 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002043 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002044
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002045 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
2047 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002048 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 /*
2050 * Report the invalid expression unless the expression evaluation has
2051 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002052 * exception, or we already gave a more specific error.
2053 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002055 if (!aborting()
2056 && did_emsg == did_emsg_before
2057 && called_emsg == called_emsg_before
2058 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002059 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 ret = FAIL;
2061 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002062
2063 if (eap != NULL)
2064 eap->nextcmd = check_nextcmd(p);
2065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 return ret;
2067}
2068
2069/*
2070 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002071 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 *
2073 * "arg" must point to the first non-white of the expression.
2074 * "arg" is advanced to the next non-white after the recognized expression.
2075 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002076 * Note: "rettv.v_lock" is not set.
2077 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 * Return OK or FAIL.
2079 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002080 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002081eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002083 char_u *p;
2084 int getnext;
2085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 /*
2087 * Get the first variable.
2088 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002089 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 return FAIL;
2091
Bram Moolenaar793648f2020-06-26 21:28:25 +02002092 p = eval_next_non_blank(*arg, evalarg, &getnext);
2093 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002095 int result;
2096 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002097 evalarg_T *evalarg_used = evalarg;
2098 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002099 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002100 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002101
2102 if (evalarg == NULL)
2103 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002104 CLEAR_FIELD(local_evalarg);
2105 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002106 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002107 orig_flags = evalarg_used->eval_flags;
2108 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002109
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002110 if (getnext)
2111 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002112 else
2113 *arg = p;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002118 int error = FALSE;
2119
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002120 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002122 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002123 if (error)
2124 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
2126
2127 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002128 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002130 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2131 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002132 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002133 if (eval1(arg, rettv, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 return FAIL;
2135
2136 /*
2137 * Check for the ":".
2138 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002139 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar793648f2020-06-26 21:28:25 +02002140 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002144 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 return FAIL;
2146 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002147 if (getnext)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002148 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002149 else
2150 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002153 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002155 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2156 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002157 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002158 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
2160 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 return FAIL;
2163 }
2164 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165 *rettv = var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002166
2167 if (evalarg == NULL)
2168 clear_evalarg(&local_evalarg, NULL);
2169 else
2170 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172
2173 return OK;
2174}
2175
2176/*
2177 * Handle first level expression:
2178 * expr2 || expr2 || expr2 logical OR
2179 *
2180 * "arg" must point to the first non-white of the expression.
2181 * "arg" is advanced to the next non-white after the recognized expression.
2182 *
2183 * Return OK or FAIL.
2184 */
2185 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002186eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002188 char_u *p;
2189 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
2191 /*
2192 * Get the first variable.
2193 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002194 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 return FAIL;
2196
2197 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002198 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002200 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002201 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002203 evalarg_T *evalarg_used = evalarg;
2204 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002205 int evaluate;
2206 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002207 long result = FALSE;
2208 typval_T var2;
2209 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002210 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002211
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002212 if (evalarg == NULL)
2213 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002214 CLEAR_FIELD(local_evalarg);
2215 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002216 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002217 orig_flags = evalarg_used->eval_flags;
2218 evaluate = orig_flags & EVAL_EVALUATE;
2219 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002220 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002221 if (vim9script)
2222 {
2223 result = tv2bool(rettv);
2224 }
2225 else
2226 {
2227 error = FALSE;
2228 if (tv_get_number_chk(rettv, &error) != 0)
2229 result = TRUE;
2230 clear_tv(rettv);
2231 if (error)
2232 return FAIL;
2233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 }
2235
2236 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002237 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002239 while (p[0] == '|' && p[1] == '|')
2240 {
2241 if (getnext)
2242 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002243 else
2244 *arg = p;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002245
2246 /*
2247 * Get the second variable.
2248 */
2249 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2250 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002251 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002252 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002253 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002254
2255 /*
2256 * Compute the result.
2257 */
2258 if (evaluate && !result)
2259 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002260 if (vim9script)
2261 {
2262 clear_tv(rettv);
2263 *rettv = var2;
2264 result = tv2bool(rettv);
2265 }
2266 else
2267 {
2268 if (tv_get_number_chk(&var2, &error) != 0)
2269 result = TRUE;
2270 clear_tv(&var2);
2271 if (error)
2272 return FAIL;
2273 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002274 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002275 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002276 {
2277 rettv->v_type = VAR_NUMBER;
2278 rettv->vval.v_number = result;
2279 }
2280
2281 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002283
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002284 if (evalarg == NULL)
2285 clear_evalarg(&local_evalarg, NULL);
2286 else
2287 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289
2290 return OK;
2291}
2292
2293/*
2294 * Handle second level expression:
2295 * expr3 && expr3 && expr3 logical AND
2296 *
2297 * "arg" must point to the first non-white of the expression.
2298 * "arg" is advanced to the next non-white after the recognized expression.
2299 *
2300 * Return OK or FAIL.
2301 */
2302 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002303eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002305 char_u *p;
2306 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
2308 /*
2309 * Get the first variable.
2310 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002311 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 return FAIL;
2313
2314 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002315 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002317 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002318 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002320 evalarg_T *evalarg_used = evalarg;
2321 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002322 int orig_flags;
2323 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002324 long result = TRUE;
2325 typval_T var2;
2326 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002327 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002328
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002329 if (evalarg == NULL)
2330 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002331 CLEAR_FIELD(local_evalarg);
2332 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002333 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002334 orig_flags = evalarg_used->eval_flags;
2335 evaluate = orig_flags & EVAL_EVALUATE;
2336 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002337 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002338 if (vim9script)
2339 {
2340 result = tv2bool(rettv);
2341 }
2342 else
2343 {
2344 error = FALSE;
2345 if (tv_get_number_chk(rettv, &error) == 0)
2346 result = FALSE;
2347 clear_tv(rettv);
2348 if (error)
2349 return FAIL;
2350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352
2353 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002354 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002356 while (p[0] == '&' && p[1] == '&')
2357 {
2358 if (getnext)
2359 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002360 else
2361 *arg = p;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002362
2363 /*
2364 * Get the second variable.
2365 */
2366 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2367 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002368 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002369 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002370 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002371
2372 /*
2373 * Compute the result.
2374 */
2375 if (evaluate && result)
2376 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002377 if (vim9script)
2378 {
2379 clear_tv(rettv);
2380 *rettv = var2;
2381 result = tv2bool(rettv);
2382 }
2383 else
2384 {
2385 if (tv_get_number_chk(&var2, &error) == 0)
2386 result = FALSE;
2387 clear_tv(&var2);
2388 if (error)
2389 return FAIL;
2390 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002391 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002392 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002393 {
2394 rettv->v_type = VAR_NUMBER;
2395 rettv->vval.v_number = result;
2396 }
2397
2398 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002400
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002401 if (evalarg == NULL)
2402 clear_evalarg(&local_evalarg, NULL);
2403 else
2404 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
2406
2407 return OK;
2408}
2409
2410/*
2411 * Handle third level expression:
2412 * var1 == var2
2413 * var1 =~ var2
2414 * var1 != var2
2415 * var1 !~ var2
2416 * var1 > var2
2417 * var1 >= var2
2418 * var1 < var2
2419 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002420 * var1 is var2
2421 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 *
2423 * "arg" must point to the first non-white of the expression.
2424 * "arg" is advanced to the next non-white after the recognized expression.
2425 *
2426 * Return OK or FAIL.
2427 */
2428 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002429eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002432 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002433 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002435 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436
2437 /*
2438 * Get the first variable.
2439 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002440 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 return FAIL;
2442
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002443 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002444 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
2446 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002447 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002449 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002451 typval_T var2;
2452 int ic;
2453 int vim9script = in_vim9script();
2454
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002455 if (getnext)
2456 *arg = eval_next_line(evalarg);
2457
Bram Moolenaar696ba232020-07-29 21:20:41 +02002458 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2459 {
2460 semsg(_(e_invexpr2), p);
2461 clear_tv(rettv);
2462 return FAIL;
2463 }
2464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002465 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (p[len] == '?')
2467 {
2468 ic = TRUE;
2469 ++len;
2470 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002471 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 else if (p[len] == '#')
2473 {
2474 ic = FALSE;
2475 ++len;
2476 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002477 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002479 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 /*
2482 * Get the second variable.
2483 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002484 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002485 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 return FAIL;
2489 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002490 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002491 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002492 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002493
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002494 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002495 {
2496 ret = FAIL;
2497 clear_tv(rettv);
2498 }
2499 else
2500 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002501 clear_tv(&var2);
2502 return ret;
2503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 }
2505
2506 return OK;
2507}
2508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 void
2510eval_addblob(typval_T *tv1, typval_T *tv2)
2511{
2512 blob_T *b1 = tv1->vval.v_blob;
2513 blob_T *b2 = tv2->vval.v_blob;
2514 blob_T *b = blob_alloc();
2515 int i;
2516
2517 if (b != NULL)
2518 {
2519 for (i = 0; i < blob_len(b1); i++)
2520 ga_append(&b->bv_ga, blob_get(b1, i));
2521 for (i = 0; i < blob_len(b2); i++)
2522 ga_append(&b->bv_ga, blob_get(b2, i));
2523
2524 clear_tv(tv1);
2525 rettv_blob_set(tv1, b);
2526 }
2527}
2528
2529 int
2530eval_addlist(typval_T *tv1, typval_T *tv2)
2531{
2532 typval_T var3;
2533
2534 // concatenate Lists
2535 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2536 {
2537 clear_tv(tv1);
2538 clear_tv(tv2);
2539 return FAIL;
2540 }
2541 clear_tv(tv1);
2542 *tv1 = var3;
2543 return OK;
2544}
2545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546/*
2547 * Handle fourth level expression:
2548 * + number addition
2549 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002550 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002551 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 *
2553 * "arg" must point to the first non-white of the expression.
2554 * "arg" is advanced to the next non-white after the recognized expression.
2555 *
2556 * Return OK or FAIL.
2557 */
2558 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002559eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 /*
2562 * Get the first variable.
2563 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002564 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 return FAIL;
2566
2567 /*
2568 * Repeat computing, until no '+', '-' or '.' is following.
2569 */
2570 for (;;)
2571 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002572 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002573 int getnext;
2574 char_u *p;
2575 int op;
2576 int concat;
2577 typval_T var2;
2578
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002579 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002580 p = eval_next_non_blank(*arg, evalarg, &getnext);
2581 op = *p;
2582 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002583 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002585
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002586 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002587 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002588 else
2589 *arg = p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002590 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002591 if ((op != '+' || (rettv->v_type != VAR_LIST
2592 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002593#ifdef FEAT_FLOAT
2594 && (op == '.' || rettv->v_type != VAR_FLOAT)
2595#endif
2596 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002597 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002598 // For "list + ...", an illegal use of the first operand as
2599 // a number cannot be determined before evaluating the 2nd
2600 // operand: if this is also a list, all is ok.
2601 // For "something . ...", "something - ..." or "non-list + ...",
2602 // we know that the first operand needs to be a string or number
2603 // without evaluating the 2nd operand. So check before to avoid
2604 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002605 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002606 {
2607 clear_tv(rettv);
2608 return FAIL;
2609 }
2610 }
2611
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 /*
2613 * Get the second variable.
2614 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002615 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2616 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002617 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002618 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 return FAIL;
2622 }
2623
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002624 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
2626 /*
2627 * Compute the result.
2628 */
2629 if (op == '.')
2630 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002631 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2632 char_u *s1 = tv_get_string_buf(rettv, buf1);
2633 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2634
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002635 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002636 {
2637 clear_tv(rettv);
2638 clear_tv(&var2);
2639 return FAIL;
2640 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002641 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 clear_tv(rettv);
2643 rettv->v_type = VAR_STRING;
2644 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002646 else if (op == '+' && rettv->v_type == VAR_BLOB
2647 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002649 else if (op == '+' && rettv->v_type == VAR_LIST
2650 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002651 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002653 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 else
2656 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002657 int error = FALSE;
2658 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002659#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002660 float_T f1 = 0, f2 = 0;
2661
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002662 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002663 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002664 f1 = rettv->vval.v_float;
2665 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002666 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002667 else
2668#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002669 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002670 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002671 if (error)
2672 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002673 // This can only happen for "list + non-list". For
2674 // "non-list + ..." or "something - ...", we returned
2675 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002676 clear_tv(rettv);
2677 return FAIL;
2678 }
2679#ifdef FEAT_FLOAT
2680 if (var2.v_type == VAR_FLOAT)
2681 f1 = n1;
2682#endif
2683 }
2684#ifdef FEAT_FLOAT
2685 if (var2.v_type == VAR_FLOAT)
2686 {
2687 f2 = var2.vval.v_float;
2688 n2 = 0;
2689 }
2690 else
2691#endif
2692 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002693 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002694 if (error)
2695 {
2696 clear_tv(rettv);
2697 clear_tv(&var2);
2698 return FAIL;
2699 }
2700#ifdef FEAT_FLOAT
2701 if (rettv->v_type == VAR_FLOAT)
2702 f2 = n2;
2703#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002704 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002705 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002706
2707#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002708 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002709 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2710 {
2711 if (op == '+')
2712 f1 = f1 + f2;
2713 else
2714 f1 = f1 - f2;
2715 rettv->v_type = VAR_FLOAT;
2716 rettv->vval.v_float = f1;
2717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002719#endif
2720 {
2721 if (op == '+')
2722 n1 = n1 + n2;
2723 else
2724 n1 = n1 - n2;
2725 rettv->v_type = VAR_NUMBER;
2726 rettv->vval.v_number = n1;
2727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002729 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731 }
2732 return OK;
2733}
2734
2735/*
2736 * Handle fifth level expression:
2737 * * number multiplication
2738 * / number division
2739 * % number modulo
2740 *
2741 * "arg" must point to the first non-white of the expression.
2742 * "arg" is advanced to the next non-white after the recognized expression.
2743 *
2744 * Return OK or FAIL.
2745 */
2746 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002747eval6(
2748 char_u **arg,
2749 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002750 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002751 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002753#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002754 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757 /*
2758 * Get the first variable.
2759 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002760 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 return FAIL;
2762
2763 /*
2764 * Repeat computing, until no '*', '/' or '%' is following.
2765 */
2766 for (;;)
2767 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002768 int evaluate;
2769 int getnext;
2770 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002771 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002772 int op;
2773 varnumber_T n1, n2;
2774#ifdef FEAT_FLOAT
2775 float_T f1, f2;
2776#endif
2777 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002778
Bram Moolenaar9d489562020-07-30 20:08:50 +02002779 p = eval_next_non_blank(*arg, evalarg, &getnext);
2780 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002781 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002783
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002784 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002785 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002786 else
2787 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002789#ifdef FEAT_FLOAT
2790 f1 = 0;
2791 f2 = 0;
2792#endif
2793 error = FALSE;
2794 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002795 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002797#ifdef FEAT_FLOAT
2798 if (rettv->v_type == VAR_FLOAT)
2799 {
2800 f1 = rettv->vval.v_float;
2801 use_float = TRUE;
2802 n1 = 0;
2803 }
2804 else
2805#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002806 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002807 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002808 if (error)
2809 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
2811 else
2812 n1 = 0;
2813
2814 /*
2815 * Get the second variable.
2816 */
2817 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002818 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 return FAIL;
2820
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002821 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002823#ifdef FEAT_FLOAT
2824 if (var2.v_type == VAR_FLOAT)
2825 {
2826 if (!use_float)
2827 {
2828 f1 = n1;
2829 use_float = TRUE;
2830 }
2831 f2 = var2.vval.v_float;
2832 n2 = 0;
2833 }
2834 else
2835#endif
2836 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002837 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002838 clear_tv(&var2);
2839 if (error)
2840 return FAIL;
2841#ifdef FEAT_FLOAT
2842 if (use_float)
2843 f2 = n2;
2844#endif
2845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846
2847 /*
2848 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002849 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002851#ifdef FEAT_FLOAT
2852 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002854 if (op == '*')
2855 f1 = f1 * f2;
2856 else if (op == '/')
2857 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002858# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002859 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002860 if (f2 == 0.0)
2861 {
2862 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002863 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002864 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002865 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002866 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002867 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002868 }
2869 else
2870 f1 = f1 / f2;
2871# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002872 // We rely on the floating point library to handle divide
2873 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002874 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002875# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002878 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002880 return FAIL;
2881 }
2882 rettv->v_type = VAR_FLOAT;
2883 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
2885 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002888 if (op == '*')
2889 n1 = n1 * n2;
2890 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002891 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002893 n1 = num_modulus(n1, n2);
2894
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002895 rettv->v_type = VAR_NUMBER;
2896 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
2899 }
2900
2901 return OK;
2902}
2903
2904/*
2905 * Handle sixth level expression:
2906 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002907 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002908 * "string" string constant
2909 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 * &option-name option value
2911 * @r register contents
2912 * identifier variable value
2913 * function() function call
2914 * $VAR environment variable
2915 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002916 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002918 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002919 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 *
2921 * Also handle:
2922 * ! in front logical NOT
2923 * - in front unary minus
2924 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002925 * trailing [] subscript in String or List
2926 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002927 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 *
2929 * "arg" must point to the first non-white of the expression.
2930 * "arg" is advanced to the next non-white after the recognized expression.
2931 *
2932 * Return OK or FAIL.
2933 */
2934 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002935eval7(
2936 char_u **arg,
2937 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002938 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002941 int evaluate = evalarg != NULL
2942 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 int len;
2944 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 char_u *start_leader, *end_leader;
2946 int ret = OK;
2947 char_u *alias;
2948
2949 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002950 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002951 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002953 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
2955 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002956 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 */
2958 start_leader = *arg;
2959 while (**arg == '!' || **arg == '-' || **arg == '+')
2960 *arg = skipwhite(*arg + 1);
2961 end_leader = *arg;
2962
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002963 if (**arg == '.' && (!isdigit(*(*arg + 1))
2964#ifdef FEAT_FLOAT
2965 || current_sctx.sc_version < 2
2966#endif
2967 ))
2968 {
2969 semsg(_(e_invexpr2), *arg);
2970 ++*arg;
2971 return FAIL;
2972 }
2973
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 switch (**arg)
2975 {
2976 /*
2977 * Number constant.
2978 */
2979 case '0':
2980 case '1':
2981 case '2':
2982 case '3':
2983 case '4':
2984 case '5':
2985 case '6':
2986 case '7':
2987 case '8':
2988 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002989 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002990
2991 // Apply prefixed "-" and "+" now. Matters especially when
2992 // "->" follows.
2993 if (ret == OK && evaluate && end_leader > start_leader)
2994 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 break;
2996
2997 /*
2998 * String constant: "string".
2999 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003000 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 break;
3002
3003 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003004 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003006 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003007 break;
3008
3009 /*
3010 * List: [expr, expr]
3011 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003012 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 break;
3014
3015 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003016 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003017 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003018 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003019 {
3020 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003021 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003022 }
3023 else
3024 ret = NOTDONE;
3025 break;
3026
3027 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003028 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003029 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003030 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003031 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003032 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003033 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003034 break;
3035
3036 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003037 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003039 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 break;
3041
3042 /*
3043 * Environment variable: $VAR.
3044 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003045 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 break;
3047
3048 /*
3049 * Register contents: @r.
3050 */
3051 case '@': ++*arg;
3052 if (evaluate)
3053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003055 rettv->vval.v_string = get_reg_contents(**arg,
3056 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 }
3058 if (**arg != NUL)
3059 ++*arg;
3060 break;
3061
3062 /*
3063 * nested expression: (expression).
3064 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003065 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003066 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003067 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003068
Bram Moolenaar9215f012020-06-27 21:18:00 +02003069 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003070 if (**arg == ')')
3071 ++*arg;
3072 else if (ret == OK)
3073 {
3074 emsg(_(e_missing_close));
3075 clear_tv(rettv);
3076 ret = FAIL;
3077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 }
3079 break;
3080
Bram Moolenaar8c711452005-01-14 21:53:12 +00003081 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 break;
3083 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003084
3085 if (ret == NOTDONE)
3086 {
3087 /*
3088 * Must be a variable or function name.
3089 * Can also be a curly-braces kind of name: {expr}.
3090 */
3091 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003092 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003093 if (alias != NULL)
3094 s = alias;
3095
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003096 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003097 ret = FAIL;
3098 else
3099 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003100 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3101
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003102 if (**arg == '(')
3103 // "name(..." recursive!
Bram Moolenaare6b53242020-07-01 17:28:33 +02003104 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003105 else if (flags & EVAL_CONSTANT)
3106 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003107 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003108 {
3109 // get the value of "true", "false" or a variable
3110 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3111 {
3112 rettv->v_type = VAR_BOOL;
3113 rettv->vval.v_number = VVAL_TRUE;
3114 }
3115 else if (len == 5 && in_vim9script()
3116 && STRNCMP(s, "false", 4) == 0)
3117 {
3118 rettv->v_type = VAR_BOOL;
3119 rettv->vval.v_number = VVAL_FALSE;
3120 }
3121 else
3122 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3123 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003124 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003125 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003126 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003127 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003128 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003129 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003130 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003131 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003132 }
3133
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003134 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3135 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003136 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003137 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
3139 /*
3140 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3141 */
3142 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003143 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003144 return ret;
3145}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003146
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003147/*
3148 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003149 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003150 * Adjusts "end_leaderp" until it is at "start_leader".
3151 */
3152 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003153eval7_leader(
3154 typval_T *rettv,
3155 int numeric_only,
3156 char_u *start_leader,
3157 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003158{
3159 char_u *end_leader = *end_leaderp;
3160 int ret = OK;
3161 int error = FALSE;
3162 varnumber_T val = 0;
3163#ifdef FEAT_FLOAT
3164 float_T f = 0.0;
3165
3166 if (rettv->v_type == VAR_FLOAT)
3167 f = rettv->vval.v_float;
3168 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003170 val = tv_get_number_chk(rettv, &error);
3171 if (error)
3172 {
3173 clear_tv(rettv);
3174 ret = FAIL;
3175 }
3176 else
3177 {
3178 while (end_leader > start_leader)
3179 {
3180 --end_leader;
3181 if (*end_leader == '!')
3182 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003183 if (numeric_only)
3184 {
3185 ++end_leader;
3186 break;
3187 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003188#ifdef FEAT_FLOAT
3189 if (rettv->v_type == VAR_FLOAT)
3190 f = !f;
3191 else
3192#endif
3193 val = !val;
3194 }
3195 else if (*end_leader == '-')
3196 {
3197#ifdef FEAT_FLOAT
3198 if (rettv->v_type == VAR_FLOAT)
3199 f = -f;
3200 else
3201#endif
3202 val = -val;
3203 }
3204 }
3205#ifdef FEAT_FLOAT
3206 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003208 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003209 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003211 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003212#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003213 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003214 clear_tv(rettv);
3215 rettv->v_type = VAR_NUMBER;
3216 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003219 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 return ret;
3221}
3222
3223/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003224 * Call the function referred to in "rettv".
3225 */
3226 static int
3227call_func_rettv(
3228 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003229 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003230 typval_T *rettv,
3231 int evaluate,
3232 dict_T *selfdict,
3233 typval_T *basetv)
3234{
3235 partial_T *pt = NULL;
3236 funcexe_T funcexe;
3237 typval_T functv;
3238 char_u *s;
3239 int ret;
3240
3241 // need to copy the funcref so that we can clear rettv
3242 if (evaluate)
3243 {
3244 functv = *rettv;
3245 rettv->v_type = VAR_UNKNOWN;
3246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003247 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003248 if (functv.v_type == VAR_PARTIAL)
3249 {
3250 pt = functv.vval.v_partial;
3251 s = partial_name(pt);
3252 }
3253 else
3254 s = functv.vval.v_string;
3255 }
3256 else
3257 s = (char_u *)"";
3258
Bram Moolenaara80faa82020-04-12 19:37:17 +02003259 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003260 funcexe.firstline = curwin->w_cursor.lnum;
3261 funcexe.lastline = curwin->w_cursor.lnum;
3262 funcexe.evaluate = evaluate;
3263 funcexe.partial = pt;
3264 funcexe.selfdict = selfdict;
3265 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003266 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003268 // Clear the funcref afterwards, so that deleting it while
3269 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003270 if (evaluate)
3271 clear_tv(&functv);
3272
3273 return ret;
3274}
3275
3276/*
3277 * Evaluate "->method()".
3278 * "*arg" points to the '-'.
3279 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3280 */
3281 static int
3282eval_lambda(
3283 char_u **arg,
3284 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003285 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003286 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003287{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003288 int evaluate = evalarg != NULL
3289 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003290 typval_T base = *rettv;
3291 int ret;
3292
3293 // Skip over the ->.
3294 *arg += 2;
3295 rettv->v_type = VAR_UNKNOWN;
3296
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003297 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003298 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003299 return FAIL;
3300 else if (**arg != '(')
3301 {
3302 if (verbose)
3303 {
3304 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003305 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003306 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003308 }
3309 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003310 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003311 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003312 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003313 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003314
3315 // Clear the funcref afterwards, so that deleting it while
3316 // evaluating the arguments is possible (see test55).
3317 if (evaluate)
3318 clear_tv(&base);
3319
3320 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003321}
3322
3323/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003324 * Evaluate "->method()".
3325 * "*arg" points to the '-'.
3326 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3327 */
3328 static int
3329eval_method(
3330 char_u **arg,
3331 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003332 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003333 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003334{
3335 char_u *name;
3336 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003337 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003338 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003339 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003340 int evaluate = evalarg != NULL
3341 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003342
3343 // Skip over the ->.
3344 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003345 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003346
Bram Moolenaarac92e252019-08-03 21:58:38 +02003347 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003348 len = get_name_len(arg, &alias, evaluate, TRUE);
3349 if (alias != NULL)
3350 name = alias;
3351
3352 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003353 {
3354 if (verbose)
3355 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003356 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003357 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003358 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003359 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003360 if (**arg != '(')
3361 {
3362 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003364 ret = FAIL;
3365 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003366 else if (VIM_ISWHITE((*arg)[-1]))
3367 {
3368 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003369 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003370 ret = FAIL;
3371 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003372 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003373 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003374 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003375 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003376
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003377 // Clear the funcref afterwards, so that deleting it while
3378 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003379 if (evaluate)
3380 clear_tv(&base);
3381
Bram Moolenaarac92e252019-08-03 21:58:38 +02003382 return ret;
3383}
3384
3385/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003386 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3387 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003388 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3389 */
3390 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003391eval_index(
3392 char_u **arg,
3393 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003394 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003395 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003396{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003397 int evaluate = evalarg != NULL
3398 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003399 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003400 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003401 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003402 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003403 long len = -1;
3404 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003405 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003406 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003407
Bram Moolenaara03f2332016-02-06 18:09:59 +01003408 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003409 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003410 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003411 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003412 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003413 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003414 return FAIL;
3415 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003416#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003417 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003418 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003419 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003420#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003421 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003422 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003423 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003424 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003425 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003426 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003427 return FAIL;
3428 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003429 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003431 if (evaluate)
3432 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003433 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003434
3435 case VAR_STRING:
3436 case VAR_NUMBER:
3437 case VAR_LIST:
3438 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003439 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003440 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003441 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003442
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003443 init_tv(&var1);
3444 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003445 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003446 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003447 /*
3448 * dict.name
3449 */
3450 key = *arg + 1;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003451 for (len = 0; eval_isdictc(key[len]); ++len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003452 ;
3453 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003454 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003455 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003456 }
3457 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003458 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003459 /*
3460 * something[idx]
3461 *
3462 * Get the (first) variable from inside the [].
3463 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003464 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003465 if (**arg == ':')
3466 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003467 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003468 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003469 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003470 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003471 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003472 clear_tv(&var1);
3473 return FAIL;
3474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003475
3476 /*
3477 * Get the second variable from inside the [:].
3478 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003479 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003480 if (**arg == ':')
3481 {
3482 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003483 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003484 if (**arg == ']')
3485 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003486 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003488 if (!empty1)
3489 clear_tv(&var1);
3490 return FAIL;
3491 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003492 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003493 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003494 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003495 if (!empty1)
3496 clear_tv(&var1);
3497 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003498 return FAIL;
3499 }
3500 }
3501
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003502 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003503 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003504 if (**arg != ']')
3505 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003506 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003507 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003508 clear_tv(&var1);
3509 if (range)
3510 clear_tv(&var2);
3511 return FAIL;
3512 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003513 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003514 }
3515
3516 if (evaluate)
3517 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003518 n1 = 0;
3519 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003521 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003522 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003523 }
3524 if (range)
3525 {
3526 if (empty2)
3527 n2 = -1;
3528 else
3529 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003530 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003531 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003532 }
3533 }
3534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003535 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003536 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003537 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003538 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003540 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003541 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003542 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003543 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003544 case VAR_SPECIAL:
3545 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003546 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003547 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003548
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003549 case VAR_NUMBER:
3550 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003551 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003552 len = (long)STRLEN(s);
3553 if (range)
3554 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003555 // The resulting variable is a substring. If the indexes
3556 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003557 if (n1 < 0)
3558 {
3559 n1 = len + n1;
3560 if (n1 < 0)
3561 n1 = 0;
3562 }
3563 if (n2 < 0)
3564 n2 = len + n2;
3565 else if (n2 >= len)
3566 n2 = len;
3567 if (n1 >= len || n2 < 0 || n1 > n2)
3568 s = NULL;
3569 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003570 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003571 }
3572 else
3573 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003574 // The resulting variable is a string of a single
3575 // character. If the index is too big or negative the
3576 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003577 if (n1 >= len || n1 < 0)
3578 s = NULL;
3579 else
3580 s = vim_strnsave(s + n1, 1);
3581 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003582 clear_tv(rettv);
3583 rettv->v_type = VAR_STRING;
3584 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003585 break;
3586
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003587 case VAR_BLOB:
3588 len = blob_len(rettv->vval.v_blob);
3589 if (range)
3590 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003591 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003592 // are out of range the result is empty.
3593 if (n1 < 0)
3594 {
3595 n1 = len + n1;
3596 if (n1 < 0)
3597 n1 = 0;
3598 }
3599 if (n2 < 0)
3600 n2 = len + n2;
3601 else if (n2 >= len)
3602 n2 = len - 1;
3603 if (n1 >= len || n2 < 0 || n1 > n2)
3604 {
3605 clear_tv(rettv);
3606 rettv->v_type = VAR_BLOB;
3607 rettv->vval.v_blob = NULL;
3608 }
3609 else
3610 {
3611 blob_T *blob = blob_alloc();
3612
3613 if (blob != NULL)
3614 {
3615 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3616 {
3617 blob_free(blob);
3618 return FAIL;
3619 }
3620 blob->bv_ga.ga_len = n2 - n1 + 1;
3621 for (i = n1; i <= n2; i++)
3622 blob_set(blob, i - n1,
3623 blob_get(rettv->vval.v_blob, i));
3624
3625 clear_tv(rettv);
3626 rettv_blob_set(rettv, blob);
3627 }
3628 }
3629 }
3630 else
3631 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003632 // The resulting variable is a byte value.
3633 // If the index is too big or negative that is an error.
3634 if (n1 < 0)
3635 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003636 if (n1 < len && n1 >= 0)
3637 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003638 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003639
3640 clear_tv(rettv);
3641 rettv->v_type = VAR_NUMBER;
3642 rettv->vval.v_number = v;
3643 }
3644 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003645 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003646 }
3647 break;
3648
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003649 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003650 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003651 if (n1 < 0)
3652 n1 = len + n1;
3653 if (!empty1 && (n1 < 0 || n1 >= len))
3654 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003655 // For a range we allow invalid values and return an empty
3656 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003657 if (!range)
3658 {
3659 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003660 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003661 return FAIL;
3662 }
3663 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003664 }
3665 if (range)
3666 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003667 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003668
3669 if (n2 < 0)
3670 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003671 else if (n2 >= len)
3672 n2 = len - 1;
3673 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003674 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003675 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003676 if (l == NULL)
3677 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003678 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003679 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003680 }
3681 else
3682 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003683 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003684 clear_tv(rettv);
3685 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003686 }
3687 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003688
3689 case VAR_DICT:
3690 if (range)
3691 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003692 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003693 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003694 if (len == -1)
3695 clear_tv(&var1);
3696 return FAIL;
3697 }
3698 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003699 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003700
3701 if (len == -1)
3702 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003703 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003704 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003706 clear_tv(&var1);
3707 return FAIL;
3708 }
3709 }
3710
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003711 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003712
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003713 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003714 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003715 if (len == -1)
3716 clear_tv(&var1);
3717 if (item == NULL)
3718 return FAIL;
3719
3720 copy_tv(&item->di_tv, &var1);
3721 clear_tv(rettv);
3722 *rettv = var1;
3723 }
3724 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003725 }
3726 }
3727
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003728 return OK;
3729}
3730
3731/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003732 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003733 */
3734 char_u *
3735partial_name(partial_T *pt)
3736{
3737 if (pt->pt_name != NULL)
3738 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003739 if (pt->pt_func != NULL)
3740 return pt->pt_func->uf_name;
3741 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003742}
3743
Bram Moolenaarddecc252016-04-06 22:59:37 +02003744 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003745partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003746{
3747 int i;
3748
3749 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003750 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003751 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003752 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003753 if (pt->pt_name != NULL)
3754 {
3755 func_unref(pt->pt_name);
3756 vim_free(pt->pt_name);
3757 }
3758 else
3759 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003760
3761 if (pt->pt_funcstack != NULL)
3762 {
3763 // Decrease the reference count for the context of a closure. If down
3764 // to zero free it and clear the variables on the stack.
3765 if (--pt->pt_funcstack->fs_refcount == 0)
3766 {
3767 garray_T *gap = &pt->pt_funcstack->fs_ga;
3768 typval_T *stack = gap->ga_data;
3769
3770 for (i = 0; i < gap->ga_len; ++i)
3771 clear_tv(stack + i);
3772 ga_clear(gap);
3773 vim_free(pt->pt_funcstack);
3774 }
3775 pt->pt_funcstack = NULL;
3776 }
3777
Bram Moolenaarddecc252016-04-06 22:59:37 +02003778 vim_free(pt);
3779}
3780
3781/*
3782 * Unreference a closure: decrement the reference count and free it when it
3783 * becomes zero.
3784 */
3785 void
3786partial_unref(partial_T *pt)
3787{
3788 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003789 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003790}
3791
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003792/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003793 * Return the next (unique) copy ID.
3794 * Used for serializing nested structures.
3795 */
3796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003797get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003798{
3799 current_copyID += COPYID_INC;
3800 return current_copyID;
3801}
3802
3803/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003804 * Garbage collection for lists and dictionaries.
3805 *
3806 * We use reference counts to be able to free most items right away when they
3807 * are no longer used. But for composite items it's possible that it becomes
3808 * unused while the reference count is > 0: When there is a recursive
3809 * reference. Example:
3810 * :let l = [1, 2, 3]
3811 * :let d = {9: l}
3812 * :let l[1] = d
3813 *
3814 * Since this is quite unusual we handle this with garbage collection: every
3815 * once in a while find out which lists and dicts are not referenced from any
3816 * variable.
3817 *
3818 * Here is a good reference text about garbage collection (refers to Python
3819 * but it applies to all reference-counting mechanisms):
3820 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003821 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003822
3823/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003824 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003825 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003826 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003827 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003828 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003829garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003830{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003831 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003832 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003833 buf_T *buf;
3834 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003835 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003836 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003837
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003838 if (!testing)
3839 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003840 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003841 want_garbage_collect = FALSE;
3842 may_garbage_collect = FALSE;
3843 garbage_collect_at_exit = FALSE;
3844 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003845
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003846 // The execution stack can grow big, limit the size.
3847 if (exestack.ga_maxlen - exestack.ga_len > 500)
3848 {
3849 size_t new_len;
3850 char_u *pp;
3851 int n;
3852
3853 // Keep 150% of the current size, with a minimum of the growth size.
3854 n = exestack.ga_len / 2;
3855 if (n < exestack.ga_growsize)
3856 n = exestack.ga_growsize;
3857
3858 // Don't make it bigger though.
3859 if (exestack.ga_len + n < exestack.ga_maxlen)
3860 {
3861 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3862 pp = vim_realloc(exestack.ga_data, new_len);
3863 if (pp == NULL)
3864 return FAIL;
3865 exestack.ga_maxlen = exestack.ga_len + n;
3866 exestack.ga_data = pp;
3867 }
3868 }
3869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003870 // We advance by two because we add one for items referenced through
3871 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003872 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003873
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003874 /*
3875 * 1. Go through all accessible variables and mark all lists and dicts
3876 * with copyID.
3877 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003878
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003879 // Don't free variables in the previous_funccal list unless they are only
3880 // referenced through previous_funccal. This must be first, because if
3881 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003882 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003884 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003885 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003887 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003888 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003889 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3890 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003891
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003892 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003893 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003894 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3895 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003896 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003897 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3898 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003899#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003900 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003901 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3902 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003903 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003904 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003905 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3906 NULL, NULL);
3907#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003909 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003910 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003911 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3912 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003913 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003914 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003916 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003917 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003918
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003919 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003920 abort = abort || set_ref_in_functions(copyID);
3921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003922 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003923 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003925 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003926 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003927
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003928 // callbacks in buffers
3929 abort = abort || set_ref_in_buffers(copyID);
3930
Bram Moolenaar1dced572012-04-05 16:54:08 +02003931#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003932 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003933#endif
3934
Bram Moolenaardb913952012-06-29 12:54:53 +02003935#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003936 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003937#endif
3938
3939#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003940 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003941#endif
3942
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003943#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003944 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003945 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003946#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003947#ifdef FEAT_NETBEANS_INTG
3948 abort = abort || set_ref_in_nb_channel(copyID);
3949#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003950
Bram Moolenaare3188e22016-05-31 21:13:04 +02003951#ifdef FEAT_TIMERS
3952 abort = abort || set_ref_in_timer(copyID);
3953#endif
3954
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003955#ifdef FEAT_QUICKFIX
3956 abort = abort || set_ref_in_quickfix(copyID);
3957#endif
3958
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003959#ifdef FEAT_TERMINAL
3960 abort = abort || set_ref_in_term(copyID);
3961#endif
3962
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003963#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003964 abort = abort || set_ref_in_popups(copyID);
3965#endif
3966
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003967 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003968 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003969 /*
3970 * 2. Free lists and dictionaries that are not referenced.
3971 */
3972 did_free = free_unref_items(copyID);
3973
3974 /*
3975 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003976 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003977 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003978 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003979 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003980 else if (p_verbose > 0)
3981 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003982 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003983 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003984
3985 return did_free;
3986}
3987
3988/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003989 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003990 */
3991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003992free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003993{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003994 int did_free = FALSE;
3995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003996 // Let all "free" functions know that we are here. This means no
3997 // dictionaries, lists, channels or jobs are to be freed, because we will
3998 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003999 in_free_unref_items = TRUE;
4000
4001 /*
4002 * PASS 1: free the contents of the items. We don't free the items
4003 * themselves yet, so that it is possible to decrement refcount counters
4004 */
4005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004006 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004007 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004008
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004009 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004010 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004011
4012#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004013 // Go through the list of jobs and free items without the copyID. This
4014 // must happen before doing channels, because jobs refer to channels, but
4015 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004016 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4017
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004018 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004019 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4020#endif
4021
4022 /*
4023 * PASS 2: free the items themselves.
4024 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004025 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004026 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004027
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004028#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004029 // Go through the list of jobs and free items without the copyID. This
4030 // must happen before doing channels, because jobs refer to channels, but
4031 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004032 free_unused_jobs(copyID, COPYID_MASK);
4033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004034 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004035 free_unused_channels(copyID, COPYID_MASK);
4036#endif
4037
4038 in_free_unref_items = FALSE;
4039
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004040 return did_free;
4041}
4042
4043/*
4044 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004045 * "list_stack" is used to add lists to be marked. Can be NULL.
4046 *
4047 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004048 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004049 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004050set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004051{
4052 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004053 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004054 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004055 hashtab_T *cur_ht;
4056 ht_stack_T *ht_stack = NULL;
4057 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004058
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004059 cur_ht = ht;
4060 for (;;)
4061 {
4062 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004064 // Mark each item in the hashtab. If the item contains a hashtab
4065 // it is added to ht_stack, if it contains a list it is added to
4066 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004067 todo = (int)cur_ht->ht_used;
4068 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4069 if (!HASHITEM_EMPTY(hi))
4070 {
4071 --todo;
4072 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4073 &ht_stack, list_stack);
4074 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004075 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004076
4077 if (ht_stack == NULL)
4078 break;
4079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004080 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004081 cur_ht = ht_stack->ht;
4082 tempitem = ht_stack;
4083 ht_stack = ht_stack->prev;
4084 free(tempitem);
4085 }
4086
4087 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004088}
4089
4090/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004091 * Mark a dict and its items with "copyID".
4092 * Returns TRUE if setting references failed somehow.
4093 */
4094 int
4095set_ref_in_dict(dict_T *d, int copyID)
4096{
4097 if (d != NULL && d->dv_copyID != copyID)
4098 {
4099 d->dv_copyID = copyID;
4100 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4101 }
4102 return FALSE;
4103}
4104
4105/*
4106 * Mark a list and its items with "copyID".
4107 * Returns TRUE if setting references failed somehow.
4108 */
4109 int
4110set_ref_in_list(list_T *ll, int copyID)
4111{
4112 if (ll != NULL && ll->lv_copyID != copyID)
4113 {
4114 ll->lv_copyID = copyID;
4115 return set_ref_in_list_items(ll, copyID, NULL);
4116 }
4117 return FALSE;
4118}
4119
4120/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004121 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004122 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4123 *
4124 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004125 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004126 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004127set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004128{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004129 listitem_T *li;
4130 int abort = FALSE;
4131 list_T *cur_l;
4132 list_stack_T *list_stack = NULL;
4133 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004134
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004135 cur_l = l;
4136 for (;;)
4137 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004138 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004139 // Mark each item in the list. If the item contains a hashtab
4140 // it is added to ht_stack, if it contains a list it is added to
4141 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004142 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4143 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4144 ht_stack, &list_stack);
4145 if (list_stack == NULL)
4146 break;
4147
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004148 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004149 cur_l = list_stack->list;
4150 tempitem = list_stack;
4151 list_stack = list_stack->prev;
4152 free(tempitem);
4153 }
4154
4155 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004156}
4157
4158/*
4159 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004160 * "list_stack" is used to add lists to be marked. Can be NULL.
4161 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4162 *
4163 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004164 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004165 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004166set_ref_in_item(
4167 typval_T *tv,
4168 int copyID,
4169 ht_stack_T **ht_stack,
4170 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004171{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004172 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004173
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004174 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004175 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004176 dict_T *dd = tv->vval.v_dict;
4177
Bram Moolenaara03f2332016-02-06 18:09:59 +01004178 if (dd != NULL && dd->dv_copyID != copyID)
4179 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004180 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004181 dd->dv_copyID = copyID;
4182 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004183 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004184 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4185 }
4186 else
4187 {
4188 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4189 if (newitem == NULL)
4190 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004191 else
4192 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004193 newitem->ht = &dd->dv_hashtab;
4194 newitem->prev = *ht_stack;
4195 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004196 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004197 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004198 }
4199 }
4200 else if (tv->v_type == VAR_LIST)
4201 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004202 list_T *ll = tv->vval.v_list;
4203
Bram Moolenaara03f2332016-02-06 18:09:59 +01004204 if (ll != NULL && ll->lv_copyID != copyID)
4205 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004206 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004207 ll->lv_copyID = copyID;
4208 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004209 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004210 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004211 }
4212 else
4213 {
4214 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004215 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004216 if (newitem == NULL)
4217 abort = TRUE;
4218 else
4219 {
4220 newitem->list = ll;
4221 newitem->prev = *list_stack;
4222 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004223 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004224 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004225 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004226 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004227 else if (tv->v_type == VAR_FUNC)
4228 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004229 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004230 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004231 else if (tv->v_type == VAR_PARTIAL)
4232 {
4233 partial_T *pt = tv->vval.v_partial;
4234 int i;
4235
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004236 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004237 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004238 // Didn't see this partial yet.
4239 pt->pt_copyID = copyID;
4240
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004241 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004242
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004243 if (pt->pt_dict != NULL)
4244 {
4245 typval_T dtv;
4246
4247 dtv.v_type = VAR_DICT;
4248 dtv.vval.v_dict = pt->pt_dict;
4249 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4250 }
4251
4252 for (i = 0; i < pt->pt_argc; ++i)
4253 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4254 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004255 if (pt->pt_funcstack != NULL)
4256 {
4257 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4258
4259 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4260 abort = abort || set_ref_in_item(stack + i, copyID,
4261 ht_stack, list_stack);
4262 }
4263
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004264 }
4265 }
4266#ifdef FEAT_JOB_CHANNEL
4267 else if (tv->v_type == VAR_JOB)
4268 {
4269 job_T *job = tv->vval.v_job;
4270 typval_T dtv;
4271
4272 if (job != NULL && job->jv_copyID != copyID)
4273 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004274 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004275 if (job->jv_channel != NULL)
4276 {
4277 dtv.v_type = VAR_CHANNEL;
4278 dtv.vval.v_channel = job->jv_channel;
4279 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4280 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004281 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004282 {
4283 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004284 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004285 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4286 }
4287 }
4288 }
4289 else if (tv->v_type == VAR_CHANNEL)
4290 {
4291 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004292 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004293 typval_T dtv;
4294 jsonq_T *jq;
4295 cbq_T *cq;
4296
4297 if (ch != NULL && ch->ch_copyID != copyID)
4298 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004299 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004300 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004301 {
4302 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4303 jq = jq->jq_next)
4304 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4305 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4306 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004307 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004308 {
4309 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004310 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004311 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4312 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004313 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004314 {
4315 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004316 dtv.vval.v_partial =
4317 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004318 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4319 }
4320 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004321 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004322 {
4323 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004324 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004325 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4326 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004327 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004328 {
4329 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004330 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004331 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4332 }
4333 }
4334 }
4335#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004336 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004337}
4338
Bram Moolenaar8c711452005-01-14 21:53:12 +00004339/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004340 * Return a string with the string representation of a variable.
4341 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004342 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004343 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004344 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4345 * stings as "string()", otherwise does not put quotes around strings, as
4346 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004347 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4348 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004349 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004350 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004351 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004352echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004353 typval_T *tv,
4354 char_u **tofree,
4355 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004356 int copyID,
4357 int echo_style,
4358 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004359 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004360{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004361 static int recurse = 0;
4362 char_u *r = NULL;
4363
Bram Moolenaar33570922005-01-25 22:26:29 +00004364 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004365 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004366 if (!did_echo_string_emsg)
4367 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 // Only give this message once for a recursive call to avoid
4369 // flooding the user with errors. And stop iterating over lists
4370 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004371 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004372 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004373 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004374 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004375 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004376 }
4377 ++recurse;
4378
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004379 switch (tv->v_type)
4380 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004381 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004382 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004383 {
4384 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004385 r = tv->vval.v_string;
4386 if (r == NULL)
4387 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004388 }
4389 else
4390 {
4391 *tofree = string_quote(tv->vval.v_string, FALSE);
4392 r = *tofree;
4393 }
4394 break;
4395
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004396 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004397 if (echo_style)
4398 {
4399 *tofree = NULL;
4400 r = tv->vval.v_string;
4401 }
4402 else
4403 {
4404 *tofree = string_quote(tv->vval.v_string, TRUE);
4405 r = *tofree;
4406 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004407 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004408
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004409 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004410 {
4411 partial_T *pt = tv->vval.v_partial;
4412 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004413 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004414 garray_T ga;
4415 int i;
4416 char_u *tf;
4417
4418 ga_init2(&ga, 1, 100);
4419 ga_concat(&ga, (char_u *)"function(");
4420 if (fname != NULL)
4421 {
4422 ga_concat(&ga, fname);
4423 vim_free(fname);
4424 }
4425 if (pt != NULL && pt->pt_argc > 0)
4426 {
4427 ga_concat(&ga, (char_u *)", [");
4428 for (i = 0; i < pt->pt_argc; ++i)
4429 {
4430 if (i > 0)
4431 ga_concat(&ga, (char_u *)", ");
4432 ga_concat(&ga,
4433 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4434 vim_free(tf);
4435 }
4436 ga_concat(&ga, (char_u *)"]");
4437 }
4438 if (pt != NULL && pt->pt_dict != NULL)
4439 {
4440 typval_T dtv;
4441
4442 ga_concat(&ga, (char_u *)", ");
4443 dtv.v_type = VAR_DICT;
4444 dtv.vval.v_dict = pt->pt_dict;
4445 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4446 vim_free(tf);
4447 }
4448 ga_concat(&ga, (char_u *)")");
4449
4450 *tofree = ga.ga_data;
4451 r = *tofree;
4452 break;
4453 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004454
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004455 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004456 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004457 break;
4458
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004459 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004460 if (tv->vval.v_list == NULL)
4461 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004462 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004463 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004464 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004465 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004466 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4467 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004468 {
4469 *tofree = NULL;
4470 r = (char_u *)"[...]";
4471 }
4472 else
4473 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004474 int old_copyID = tv->vval.v_list->lv_copyID;
4475
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004476 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004477 *tofree = list2string(tv, copyID, restore_copyID);
4478 if (restore_copyID)
4479 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004480 r = *tofree;
4481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004482 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004483
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004485 if (tv->vval.v_dict == NULL)
4486 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004487 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004488 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004489 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004490 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004491 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4492 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004493 {
4494 *tofree = NULL;
4495 r = (char_u *)"{...}";
4496 }
4497 else
4498 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004499 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004500
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004501 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004502 *tofree = dict2string(tv, copyID, restore_copyID);
4503 if (restore_copyID)
4504 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004505 r = *tofree;
4506 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004507 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004508
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004509 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004510 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004511 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004513 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004514 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004515 break;
4516
Bram Moolenaar835dc632016-02-07 14:27:38 +01004517 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004518 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004519 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004520 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004521 if (composite_val)
4522 {
4523 *tofree = string_quote(r, FALSE);
4524 r = *tofree;
4525 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004526 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004527
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004528 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004529#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004530 *tofree = NULL;
4531 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4532 r = numbuf;
4533 break;
4534#endif
4535
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004536 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004537 case VAR_SPECIAL:
4538 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004539 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004540 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004541 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004542
Bram Moolenaar8502c702014-06-17 12:51:16 +02004543 if (--recurse == 0)
4544 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004545 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004546}
4547
4548/*
4549 * Return a string with the string representation of a variable.
4550 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4551 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004552 * Does not put quotes around strings, as ":echo" displays values.
4553 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4554 * May return NULL.
4555 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004556 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004557echo_string(
4558 typval_T *tv,
4559 char_u **tofree,
4560 char_u *numbuf,
4561 int copyID)
4562{
4563 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4564}
4565
4566/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004567 * Return string "str" in ' quotes, doubling ' characters.
4568 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004569 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004570 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004571 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004572string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004573{
Bram Moolenaar33570922005-01-25 22:26:29 +00004574 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004575 char_u *p, *r, *s;
4576
Bram Moolenaar33570922005-01-25 22:26:29 +00004577 len = (function ? 13 : 3);
4578 if (str != NULL)
4579 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004580 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004581 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004582 if (*p == '\'')
4583 ++len;
4584 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004585 s = r = alloc(len);
4586 if (r != NULL)
4587 {
4588 if (function)
4589 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004590 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004591 r += 10;
4592 }
4593 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004594 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004595 if (str != NULL)
4596 for (p = str; *p != NUL; )
4597 {
4598 if (*p == '\'')
4599 *r++ = '\'';
4600 MB_COPY_CHAR(p, r);
4601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004602 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004603 if (function)
4604 *r++ = ')';
4605 *r++ = NUL;
4606 }
4607 return s;
4608}
4609
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004610#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004611/*
4612 * Convert the string "text" to a floating point number.
4613 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4614 * this always uses a decimal point.
4615 * Returns the length of the text that was consumed.
4616 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004618string2float(
4619 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004620 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004621{
4622 char *s = (char *)text;
4623 float_T f;
4624
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004625 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004626 if (STRNICMP(text, "inf", 3) == 0)
4627 {
4628 *value = INFINITY;
4629 return 3;
4630 }
4631 if (STRNICMP(text, "-inf", 3) == 0)
4632 {
4633 *value = -INFINITY;
4634 return 4;
4635 }
4636 if (STRNICMP(text, "nan", 3) == 0)
4637 {
4638 *value = NAN;
4639 return 3;
4640 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004641 f = strtod(s, &s);
4642 *value = f;
4643 return (int)((char_u *)s - text);
4644}
4645#endif
4646
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004649 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004651 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004652var2fpos(
4653 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004654 int dollar_lnum, // TRUE when $ is last line
4655 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004657 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004659 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004661 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004662 if (varp->v_type == VAR_LIST)
4663 {
4664 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004665 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004666 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004667 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004668
4669 l = varp->vval.v_list;
4670 if (l == NULL)
4671 return NULL;
4672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004673 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004674 pos.lnum = list_find_nr(l, 0L, &error);
4675 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004676 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004677
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004678 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004679 pos.col = list_find_nr(l, 1L, &error);
4680 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004681 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004682 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004683
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004684 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004685 li = list_find(l, 1L);
4686 if (li != NULL && li->li_tv.v_type == VAR_STRING
4687 && li->li_tv.vval.v_string != NULL
4688 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4689 pos.col = len + 1;
4690
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004691 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004692 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004693 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004694 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004695
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004696 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004697 pos.coladd = list_find_nr(l, 2L, &error);
4698 if (error)
4699 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004700
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004701 return &pos;
4702 }
4703
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004704 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004705 if (name == NULL)
4706 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004707 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004709 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004710 {
4711 if (VIsual_active)
4712 return &VIsual;
4713 return &curwin->w_cursor;
4714 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004715 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004717 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4719 return NULL;
4720 return pp;
4721 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004722
Bram Moolenaara5525202006-03-02 22:52:09 +00004723 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004724
Bram Moolenaar477933c2007-07-17 14:32:23 +00004725 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004726 {
4727 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004728 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004729 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004730 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004731 // In silent Ex mode topline is zero, but that's not a valid line
4732 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004733 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004734 return &pos;
4735 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004736 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004737 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004738 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004739 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004740 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004741 return &pos;
4742 }
4743 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004744 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004746 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 pos.lnum = curbuf->b_ml.ml_line_count;
4749 pos.col = 0;
4750 }
4751 else
4752 {
4753 pos.lnum = curwin->w_cursor.lnum;
4754 pos.col = (colnr_T)STRLEN(ml_get_curline());
4755 }
4756 return &pos;
4757 }
4758 return NULL;
4759}
4760
4761/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004762 * Convert list in "arg" into a position and optional file number.
4763 * When "fnump" is NULL there is no file number, only 3 items.
4764 * Note that the column is passed on as-is, the caller may want to decrement
4765 * it to use 1 for the first column.
4766 * Return FAIL when conversion is not possible, doesn't check the position for
4767 * validity.
4768 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004769 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004770list2fpos(
4771 typval_T *arg,
4772 pos_T *posp,
4773 int *fnump,
4774 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004775{
4776 list_T *l = arg->vval.v_list;
4777 long i = 0;
4778 long n;
4779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004780 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4781 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004782 if (arg->v_type != VAR_LIST
4783 || l == NULL
4784 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004785 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004786 return FAIL;
4787
4788 if (fnump != NULL)
4789 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004790 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004791 if (n < 0)
4792 return FAIL;
4793 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004794 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004795 *fnump = n;
4796 }
4797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004798 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004799 if (n < 0)
4800 return FAIL;
4801 posp->lnum = n;
4802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004803 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004804 if (n < 0)
4805 return FAIL;
4806 posp->col = n;
4807
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004808 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004809 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004810 posp->coladd = 0;
4811 else
4812 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004813
Bram Moolenaar493c1782014-05-28 14:34:46 +02004814 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004815 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004816
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004817 return OK;
4818}
4819
4820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 * Get the length of an environment variable name.
4822 * Advance "arg" to the first character after the name.
4823 * Return 0 for error.
4824 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004825 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004826get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827{
4828 char_u *p;
4829 int len;
4830
4831 for (p = *arg; vim_isIDc(*p); ++p)
4832 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004833 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 return 0;
4835
4836 len = (int)(p - *arg);
4837 *arg = p;
4838 return len;
4839}
4840
4841/*
4842 * Get the length of the name of a function or internal variable.
4843 * "arg" is advanced to the first non-white character after the name.
4844 * Return 0 if something is wrong.
4845 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004846 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004847get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
4849 char_u *p;
4850 int len;
4851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004852 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004854 {
4855 if (*p == ':')
4856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004857 // "s:" is start of "s:var", but "n:" is not and can be used in
4858 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004859 len = (int)(p - *arg);
4860 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4861 || len > 1)
4862 break;
4863 }
4864 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004865 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 return 0;
4867
4868 len = (int)(p - *arg);
4869 *arg = skipwhite(p);
4870
4871 return len;
4872}
4873
4874/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004875 * Get the length of the name of a variable or function.
4876 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004878 * Return -1 if curly braces expansion failed.
4879 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 * If the name contains 'magic' {}'s, expand them and return the
4881 * expanded name in an allocated string via 'alias' - caller must free.
4882 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004883 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004884get_name_len(
4885 char_u **arg,
4886 char_u **alias,
4887 int evaluate,
4888 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889{
4890 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 char_u *p;
4892 char_u *expr_start;
4893 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004895 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
4897 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4898 && (*arg)[2] == (int)KE_SNR)
4899 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004900 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 *arg += 3;
4902 return get_id_len(arg) + 3;
4903 }
4904 len = eval_fname_script(*arg);
4905 if (len > 0)
4906 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004907 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 *arg += len;
4909 }
4910
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004914 p = find_name_end(*arg, &expr_start, &expr_end,
4915 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (expr_start != NULL)
4917 {
4918 char_u *temp_string;
4919
4920 if (!evaluate)
4921 {
4922 len += (int)(p - *arg);
4923 *arg = skipwhite(p);
4924 return len;
4925 }
4926
4927 /*
4928 * Include any <SID> etc in the expanded string:
4929 * Thus the -len here.
4930 */
4931 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4932 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004933 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 *alias = temp_string;
4935 *arg = skipwhite(p);
4936 return (int)STRLEN(temp_string);
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
4939 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004940 // Only give an error when there is something, otherwise it will be
4941 // reported at a higher level.
4942 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004943 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945 return len;
4946}
4947
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004948/*
4949 * Find the end of a variable or function name, taking care of magic braces.
4950 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4951 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004952 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004953 * Return a pointer to just after the name. Equal to "arg" if there is no
4954 * valid name.
4955 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004956 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004957find_name_end(
4958 char_u *arg,
4959 char_u **expr_start,
4960 char_u **expr_end,
4961 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004963 int mb_nest = 0;
4964 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004966 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004967 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004969 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004971 *expr_start = NULL;
4972 *expr_end = NULL;
4973 }
4974
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004975 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004976 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4977 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004978 return arg;
4979
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004980 for (p = arg; *p != NUL
4981 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004982 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02004983 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02004984 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004985 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004986 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004987 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004988 if (*p == '\'')
4989 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004990 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004991 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004992 ;
4993 if (*p == NUL)
4994 break;
4995 }
4996 else if (*p == '"')
4997 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004998 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004999 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005000 if (*p == '\\' && p[1] != NUL)
5001 ++p;
5002 if (*p == NUL)
5003 break;
5004 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005005 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5006 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005007 // "s:" is start of "s:var", but "n:" is not and can be used in
5008 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005009 len = (int)(p - arg);
5010 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005011 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005012 break;
5013 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005014
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005015 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005017 if (*p == '[')
5018 ++br_nest;
5019 else if (*p == ']')
5020 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005022
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005023 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005025 if (*p == '{')
5026 {
5027 mb_nest++;
5028 if (expr_start != NULL && *expr_start == NULL)
5029 *expr_start = p;
5030 }
5031 else if (*p == '}')
5032 {
5033 mb_nest--;
5034 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5035 *expr_end = p;
5036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039
5040 return p;
5041}
5042
5043/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005044 * Expands out the 'magic' {}'s in a variable/function name.
5045 * Note that this can call itself recursively, to deal with
5046 * constructs like foo{bar}{baz}{bam}
5047 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5048 * "in_start" ^
5049 * "expr_start" ^
5050 * "expr_end" ^
5051 * "in_end" ^
5052 *
5053 * Returns a new allocated string, which the caller must free.
5054 * Returns NULL for failure.
5055 */
5056 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005057make_expanded_name(
5058 char_u *in_start,
5059 char_u *expr_start,
5060 char_u *expr_end,
5061 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005062{
5063 char_u c1;
5064 char_u *retval = NULL;
5065 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005066
5067 if (expr_end == NULL || in_end == NULL)
5068 return NULL;
5069 *expr_start = NUL;
5070 *expr_end = NUL;
5071 c1 = *in_end;
5072 *in_end = NUL;
5073
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005074 temp_result = eval_to_string(expr_start + 1, FALSE);
5075 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005076 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005077 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5078 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005079 if (retval != NULL)
5080 {
5081 STRCPY(retval, in_start);
5082 STRCAT(retval, temp_result);
5083 STRCAT(retval, expr_end + 1);
5084 }
5085 }
5086 vim_free(temp_result);
5087
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005088 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005089 *expr_start = '{';
5090 *expr_end = '}';
5091
5092 if (retval != NULL)
5093 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005094 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005095 if (expr_start != NULL)
5096 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005097 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005098 temp_result = make_expanded_name(retval, expr_start,
5099 expr_end, temp_result);
5100 vim_free(retval);
5101 retval = temp_result;
5102 }
5103 }
5104
5105 return retval;
5106}
5107
5108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005110 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005112 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005113eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005115 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005116}
5117
5118/*
5119 * Return TRUE if character "c" can be used as the first character in a
5120 * variable or function name (excluding '{' and '}').
5121 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005122 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005123eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005124{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005125 return ASCII_ISALPHA(c) || c == '_';
5126}
5127
5128/*
5129 * Return TRUE if character "c" can be used as the first character of a
5130 * dictionary key.
5131 */
5132 int
5133eval_isdictc(int c)
5134{
5135 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136}
5137
5138/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005139 * Handle:
5140 * - expr[expr], expr[expr:expr] subscript
5141 * - ".name" lookup
5142 * - function call with Funcref variable: func(expr)
5143 * - method call: var->method()
5144 *
5145 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005146 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005147 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005148handle_subscript(
5149 char_u **arg,
5150 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005151 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005152 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005153{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005154 int evaluate = evalarg != NULL
5155 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005156 int ret = OK;
5157 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005158 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005159 int getnext;
5160 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005161
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005162 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005163 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005164 // When at the end of the line and ".name" or "->{" or "->X" follows in
5165 // the next line then consume the line break.
5166 p = eval_next_non_blank(*arg, evalarg, &getnext);
5167 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005168 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005169 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005170 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005171 {
5172 *arg = eval_next_line(evalarg);
5173 check_white = FALSE;
5174 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005175
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005176 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5177 || rettv->v_type == VAR_PARTIAL))
5178 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005179 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005180 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5181 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005182
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005183 // Stop the expression evaluation when immediately aborting on
5184 // error, or when an interrupt occurred or an exception was thrown
5185 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005186 if (aborting())
5187 {
5188 if (ret == OK)
5189 clear_tv(rettv);
5190 ret = FAIL;
5191 }
5192 dict_unref(selfdict);
5193 selfdict = NULL;
5194 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005195 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005196 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005197 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005198 if (ret == OK)
5199 {
5200 if ((*arg)[2] == '{')
5201 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005202 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005203 else
5204 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005205 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005206 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005207 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005208 // "." is ".name" lookup when we found a dict or when evaluating and
5209 // scriptversion is at least 2, where string concatenation is "..".
5210 else if (**arg == '['
5211 || (**arg == '.' && (rettv->v_type == VAR_DICT
5212 || (!evaluate
5213 && (*arg)[1] != '.'
5214 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005215 {
5216 dict_unref(selfdict);
5217 if (rettv->v_type == VAR_DICT)
5218 {
5219 selfdict = rettv->vval.v_dict;
5220 if (selfdict != NULL)
5221 ++selfdict->dv_refcount;
5222 }
5223 else
5224 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005225 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005226 {
5227 clear_tv(rettv);
5228 ret = FAIL;
5229 }
5230 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005231 else
5232 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005233 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005234
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005235 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5236 // Don't do this when "Func" is already a partial that was bound
5237 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005238 if (selfdict != NULL
5239 && (rettv->v_type == VAR_FUNC
5240 || (rettv->v_type == VAR_PARTIAL
5241 && (rettv->vval.v_partial->pt_auto
5242 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005243 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005244
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005245 dict_unref(selfdict);
5246 return ret;
5247}
5248
5249/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005250 * Make a copy of an item.
5251 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005252 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5253 * reference to an already copied list/dict can be used.
5254 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005255 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005256 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005257item_copy(
5258 typval_T *from,
5259 typval_T *to,
5260 int deep,
5261 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005262{
5263 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005264 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005265
Bram Moolenaar33570922005-01-25 22:26:29 +00005266 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005268 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005269 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005270 }
5271 ++recurse;
5272
5273 switch (from->v_type)
5274 {
5275 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005276 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005277 case VAR_STRING:
5278 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005279 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005280 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005281 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005282 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005283 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005284 copy_tv(from, to);
5285 break;
5286 case VAR_LIST:
5287 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005288 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005289 if (from->vval.v_list == NULL)
5290 to->vval.v_list = NULL;
5291 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5292 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005293 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005294 to->vval.v_list = from->vval.v_list->lv_copylist;
5295 ++to->vval.v_list->lv_refcount;
5296 }
5297 else
5298 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5299 if (to->vval.v_list == NULL)
5300 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005301 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005302 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005303 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005304 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005305 case VAR_DICT:
5306 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005307 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005308 if (from->vval.v_dict == NULL)
5309 to->vval.v_dict = NULL;
5310 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5311 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005312 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005313 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5314 ++to->vval.v_dict->dv_refcount;
5315 }
5316 else
5317 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5318 if (to->vval.v_dict == NULL)
5319 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005320 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005321 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005322 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005323 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005324 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005325 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005326 }
5327 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005328 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005329}
5330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 void
5332echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5333{
5334 char_u *tofree;
5335 char_u numbuf[NUMBUFLEN];
5336 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5337
5338 if (*atstart)
5339 {
5340 *atstart = FALSE;
5341 // Call msg_start() after eval1(), evaluating the expression
5342 // may cause a message to appear.
5343 if (with_space)
5344 {
5345 // Mark the saved text as finishing the line, so that what
5346 // follows is displayed on a new line when scrolling back
5347 // at the more prompt.
5348 msg_sb_eol();
5349 msg_start();
5350 }
5351 }
5352 else if (with_space)
5353 msg_puts_attr(" ", echo_attr);
5354
5355 if (p != NULL)
5356 for ( ; *p != NUL && !got_int; ++p)
5357 {
5358 if (*p == '\n' || *p == '\r' || *p == TAB)
5359 {
5360 if (*p != TAB && *needclr)
5361 {
5362 // remove any text still there from the command
5363 msg_clr_eos();
5364 *needclr = FALSE;
5365 }
5366 msg_putchar_attr(*p, echo_attr);
5367 }
5368 else
5369 {
5370 if (has_mbyte)
5371 {
5372 int i = (*mb_ptr2len)(p);
5373
5374 (void)msg_outtrans_len_attr(p, i, echo_attr);
5375 p += i - 1;
5376 }
5377 else
5378 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5379 }
5380 }
5381 vim_free(tofree);
5382}
5383
Bram Moolenaare9a41262005-01-15 22:18:47 +00005384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 * ":echo expr1 ..." print each argument separated with a space, add a
5386 * newline at the end.
5387 * ":echon expr1 ..." print each argument plain.
5388 */
5389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005390ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005393 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 char_u *p;
5395 int needclr = TRUE;
5396 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005397 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005398 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005399 evalarg_T evalarg;
5400
Bram Moolenaare707c882020-07-01 13:07:37 +02005401 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
5403 if (eap->skip)
5404 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005405 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005407 // If eval1() causes an error message the text from the command may
5408 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005409 need_clr_eos = needclr;
5410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005412 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {
5414 /*
5415 * Report the invalid expression unless the expression evaluation
5416 * has been cancelled due to an aborting error, an interrupt, or an
5417 * exception.
5418 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005419 if (!aborting() && did_emsg == did_emsg_before
5420 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005421 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005422 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 break;
5424 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005425 need_clr_eos = FALSE;
5426
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005429
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 arg = skipwhite(arg);
5432 }
5433 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005434 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
5436 if (eap->skip)
5437 --emsg_skip;
5438 else
5439 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005440 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 if (needclr)
5442 msg_clr_eos();
5443 if (eap->cmdidx == CMD_echo)
5444 msg_end();
5445 }
5446}
5447
5448/*
5449 * ":echohl {name}".
5450 */
5451 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005452ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005454 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455}
5456
5457/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005458 * Returns the :echo attribute
5459 */
5460 int
5461get_echo_attr(void)
5462{
5463 return echo_attr;
5464}
5465
5466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 * ":execute expr1 ..." execute the result of an expression.
5468 * ":echomsg expr1 ..." Print a message
5469 * ":echoerr expr1 ..." Print an error
5470 * Each gets spaces around each argument and a newline at the end for
5471 * echo commands
5472 */
5473 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005474ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 int ret = OK;
5479 char_u *p;
5480 garray_T ga;
5481 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483 ga_init2(&ga, 1, 80);
5484
5485 if (eap->skip)
5486 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005487 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005489 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005490 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
5493 if (!eap->skip)
5494 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005495 char_u buf[NUMBUFLEN];
5496
5497 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005498 {
5499 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5500 {
5501 emsg(_(e_inval_string));
5502 p = NULL;
5503 }
5504 else
5505 p = tv_get_string_buf(&rettv, buf);
5506 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005507 else
5508 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005509 if (p == NULL)
5510 {
5511 clear_tv(&rettv);
5512 ret = FAIL;
5513 break;
5514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 len = (int)STRLEN(p);
5516 if (ga_grow(&ga, len + 2) == FAIL)
5517 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 ret = FAIL;
5520 break;
5521 }
5522 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 ga.ga_len += len;
5526 }
5527
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 arg = skipwhite(arg);
5530 }
5531
5532 if (ret != FAIL && ga.ga_data != NULL)
5533 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005534 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5535 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005536 // Mark the already saved text as finishing the line, so that what
5537 // follows is displayed on a new line when scrolling back at the
5538 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005539 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005540 }
5541
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005543 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005544 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005545 out_flush();
5546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 else if (eap->cmdidx == CMD_echoerr)
5548 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005549 int save_did_emsg = did_emsg;
5550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005551 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005552 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 if (!force_abort)
5554 did_emsg = save_did_emsg;
5555 }
5556 else if (eap->cmdidx == CMD_execute)
5557 do_cmdline((char_u *)ga.ga_data,
5558 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5559 }
5560
5561 ga_clear(&ga);
5562
5563 if (eap->skip)
5564 --emsg_skip;
5565
5566 eap->nextcmd = check_nextcmd(arg);
5567}
5568
5569/*
5570 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5571 * "arg" points to the "&" or '+' when called, to "option" when returning.
5572 * Returns NULL when no option name found. Otherwise pointer to the char
5573 * after the option name.
5574 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005575 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005576find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 char_u *p = *arg;
5579
5580 ++p;
5581 if (*p == 'g' && p[1] == ':')
5582 {
5583 *opt_flags = OPT_GLOBAL;
5584 p += 2;
5585 }
5586 else if (*p == 'l' && p[1] == ':')
5587 {
5588 *opt_flags = OPT_LOCAL;
5589 p += 2;
5590 }
5591 else
5592 *opt_flags = 0;
5593
5594 if (!ASCII_ISALPHA(*p))
5595 return NULL;
5596 *arg = p;
5597
5598 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005599 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 else
5601 while (ASCII_ISALPHA(*p))
5602 ++p;
5603 return p;
5604}
5605
5606/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005607 * Display script name where an item was last set.
5608 * Should only be invoked when 'verbose' is non-zero.
5609 */
5610 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005611last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005612{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005613 char_u *p;
5614
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005615 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005616 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005617 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005618 if (p != NULL)
5619 {
5620 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005621 msg_puts(_("\n\tLast set from "));
5622 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005623 if (script_ctx.sc_lnum > 0)
5624 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005625 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005626 msg_outnum((long)script_ctx.sc_lnum);
5627 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005628 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005629 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005630 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005631 }
5632}
5633
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005634#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
5636/*
5637 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005638 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 * "flags" can be "g" to do a global substitute.
5640 * Returns an allocated string, NULL for error.
5641 */
5642 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005643do_string_sub(
5644 char_u *str,
5645 char_u *pat,
5646 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005647 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005648 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
5650 int sublen;
5651 regmatch_T regmatch;
5652 int i;
5653 int do_all;
5654 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005655 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 garray_T ga;
5657 char_u *ret;
5658 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005659 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005661 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005663 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
5665 ga_init2(&ga, 1, 200);
5666
5667 do_all = (flags[0] == 'g');
5668
5669 regmatch.rm_ic = p_ic;
5670 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5671 if (regmatch.regprog != NULL)
5672 {
5673 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005674 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5676 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005677 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005678 if (regmatch.startp[0] == regmatch.endp[0])
5679 {
5680 if (zero_width == regmatch.startp[0])
5681 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005682 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005683 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005684 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5685 (size_t)i);
5686 ga.ga_len += i;
5687 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005688 continue;
5689 }
5690 zero_width = regmatch.startp[0];
5691 }
5692
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 /*
5694 * Get some space for a temporary buffer to do the substitution
5695 * into. It will contain:
5696 * - The text up to where the match is.
5697 * - The substituted text.
5698 * - The text after the match.
5699 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005700 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005701 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5703 {
5704 ga_clear(&ga);
5705 break;
5706 }
5707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005708 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 i = (int)(regmatch.startp[0] - tail);
5710 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005711 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005712 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 + ga.ga_len + i, TRUE, TRUE, FALSE);
5714 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005715 tail = regmatch.endp[0];
5716 if (*tail == NUL)
5717 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 if (!do_all)
5719 break;
5720 }
5721
5722 if (ga.ga_data != NULL)
5723 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5724
Bram Moolenaar473de612013-06-08 18:19:48 +02005725 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727
5728 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5729 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005730 if (p_cpo == empty_option)
5731 p_cpo = save_cpo;
5732 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005733 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005734 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
5736 return ret;
5737}