blob: c3f0ee5c9f88af3eeccc2b823692a2a8c9d5f107 [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 Moolenaarf9e3e092019-01-13 23:38:42 +0100800 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 return NULL;
802 }
803
804 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
805 if (lp->ll_exp_name == NULL)
806 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100807 // Report an invalid expression in braces, unless the
808 // expression evaluation has been cancelled due to an
809 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000810 if (!aborting() && !quiet)
811 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000812 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100813 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000814 return NULL;
815 }
816 }
817 lp->ll_name = lp->ll_exp_name;
818 }
819 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 lp->ll_name = name;
822
Bram 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 Moolenaar8a7d6542020-01-26 15:56:19 +01001273 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001274 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001275 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001276 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001277 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001278 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001279 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280 else if (lp->ll_range)
1281 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001282 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001283 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001284
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001286 {
1287 emsg(_("E996: Cannot lock a range"));
1288 return;
1289 }
1290
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001291 /*
1292 * Check whether any of the list items is locked
1293 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001294 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001295 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001296 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001297 return;
1298 ri = ri->li_next;
1299 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1300 break;
1301 ll_li = ll_li->li_next;
1302 ++ll_n1;
1303 }
1304
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305 /*
1306 * Assign the List values to the list items.
1307 */
1308 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001309 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001310 if (op != NULL && *op != '=')
1311 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1312 else
1313 {
1314 clear_tv(&lp->ll_li->li_tv);
1315 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1316 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001317 ri = ri->li_next;
1318 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1319 break;
1320 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001321 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001322 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001323 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001324 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 ri = NULL;
1326 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001327 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001328 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001329 lp->ll_li = lp->ll_li->li_next;
1330 ++lp->ll_n1;
1331 }
1332 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001333 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334 else if (lp->ll_empty2
1335 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001336 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001337 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 }
1339 else
1340 {
1341 /*
1342 * Assign to a List or Dictionary item.
1343 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001345 {
1346 emsg(_("E996: Cannot lock a list or dict"));
1347 return;
1348 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 if (lp->ll_newkey != NULL)
1350 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001351 if (op != NULL && *op != '=')
1352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001353 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001354 return;
1355 }
1356
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001357 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001358 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001359 if (di == NULL)
1360 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001361 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1362 {
1363 vim_free(di);
1364 return;
1365 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001367 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001368 else if (op != NULL && *op != '=')
1369 {
1370 tv_op(lp->ll_tv, rettv, op);
1371 return;
1372 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001374 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001375
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001376 /*
1377 * Assign the value to the variable or list item.
1378 */
1379 if (copy)
1380 copy_tv(rettv, lp->ll_tv);
1381 else
1382 {
1383 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001384 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001385 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 }
1387 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001388}
1389
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001390/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001391 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1392 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001393 * Returns OK or FAIL.
1394 */
1395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001396tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001397{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001398 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001399 char_u numbuf[NUMBUFLEN];
1400 char_u *s;
1401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001402 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001403 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001404 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001405 {
1406 switch (tv1->v_type)
1407 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001408 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001409 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001411 case VAR_DICT:
1412 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001413 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001414 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001415 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001416 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001417 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001418 break;
1419
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001420 case VAR_BLOB:
1421 if (*op != '+' || tv2->v_type != VAR_BLOB)
1422 break;
1423 // BLOB += BLOB
1424 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1425 {
1426 blob_T *b1 = tv1->vval.v_blob;
1427 blob_T *b2 = tv2->vval.v_blob;
1428 int i, len = blob_len(b2);
1429 for (i = 0; i < len; i++)
1430 ga_append(&b1->bv_ga, blob_get(b2, i));
1431 }
1432 return OK;
1433
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001434 case VAR_LIST:
1435 if (*op != '+' || tv2->v_type != VAR_LIST)
1436 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001437 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001438 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1439 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1440 return OK;
1441
1442 case VAR_NUMBER:
1443 case VAR_STRING:
1444 if (tv2->v_type == VAR_LIST)
1445 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001446 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001447 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001448 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001449 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001450#ifdef FEAT_FLOAT
1451 if (tv2->v_type == VAR_FLOAT)
1452 {
1453 float_T f = n;
1454
Bram Moolenaarff697e62019-02-12 22:28:33 +01001455 if (*op == '%')
1456 break;
1457 switch (*op)
1458 {
1459 case '+': f += tv2->vval.v_float; break;
1460 case '-': f -= tv2->vval.v_float; break;
1461 case '*': f *= tv2->vval.v_float; break;
1462 case '/': f /= tv2->vval.v_float; break;
1463 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001464 clear_tv(tv1);
1465 tv1->v_type = VAR_FLOAT;
1466 tv1->vval.v_float = f;
1467 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001468 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001469#endif
1470 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001471 switch (*op)
1472 {
1473 case '+': n += tv_get_number(tv2); break;
1474 case '-': n -= tv_get_number(tv2); break;
1475 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001476 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1477 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001478 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001479 clear_tv(tv1);
1480 tv1->v_type = VAR_NUMBER;
1481 tv1->vval.v_number = n;
1482 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001483 }
1484 else
1485 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001486 if (tv2->v_type == VAR_FLOAT)
1487 break;
1488
Bram Moolenaarff697e62019-02-12 22:28:33 +01001489 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001490 s = tv_get_string(tv1);
1491 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492 clear_tv(tv1);
1493 tv1->v_type = VAR_STRING;
1494 tv1->vval.v_string = s;
1495 }
1496 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001497
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001498 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001499#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001500 {
1501 float_T f;
1502
Bram Moolenaarff697e62019-02-12 22:28:33 +01001503 if (*op == '%' || *op == '.'
1504 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001505 && tv2->v_type != VAR_NUMBER
1506 && tv2->v_type != VAR_STRING))
1507 break;
1508 if (tv2->v_type == VAR_FLOAT)
1509 f = tv2->vval.v_float;
1510 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001511 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001512 switch (*op)
1513 {
1514 case '+': tv1->vval.v_float += f; break;
1515 case '-': tv1->vval.v_float -= f; break;
1516 case '*': tv1->vval.v_float *= f; break;
1517 case '/': tv1->vval.v_float /= f; break;
1518 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001519 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001520#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001521 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001522 }
1523 }
1524
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001525 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001526 return FAIL;
1527}
1528
1529/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001530 * Evaluate the expression used in a ":for var in expr" command.
1531 * "arg" points to "var".
1532 * Set "*errp" to TRUE for an error, FALSE otherwise;
1533 * Return a pointer that holds the info. Null when there is an error.
1534 */
1535 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001536eval_for_line(
1537 char_u *arg,
1538 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001539 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001540 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001541{
Bram Moolenaar33570922005-01-25 22:26:29 +00001542 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001543 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001544 typval_T tv;
1545 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001546 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001548 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001549
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001550 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001551 if (fi == NULL)
1552 return NULL;
1553
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001554 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001555 if (expr == NULL)
1556 return fi;
1557
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001558 expr = skipwhite_and_linebreak(expr, evalarg);
1559 if (expr[0] != 'i' || expr[1] != 'n'
1560 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563 return fi;
1564 }
1565
1566 if (skip)
1567 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001568 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1569 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001570 {
1571 *errp = FALSE;
1572 if (!skip)
1573 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001574 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001575 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001576 l = tv.vval.v_list;
1577 if (l == NULL)
1578 {
1579 // a null list is like an empty list: do nothing
1580 clear_tv(&tv);
1581 }
1582 else
1583 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001585 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001587 // No need to increment the refcount, it's already set for
1588 // the list being used in "tv".
1589 fi->fi_list = l;
1590 list_add_watch(l, &fi->fi_lw);
1591 fi->fi_lw.lw_item = l->lv_first;
1592 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001593 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001594 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001595 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001596 fi->fi_bi = 0;
1597 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001598 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001599 typval_T btv;
1600
1601 // Make a copy, so that the iteration still works when the
1602 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001604 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001605 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001606 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001607 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001608 else
1609 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001610 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001611 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001612 }
1613 }
1614 }
1615 if (skip)
1616 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001617 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001618
1619 return fi;
1620}
1621
1622/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001623 * Used when looping over a :for line, skip the "in expr" part.
1624 */
1625 void
1626skip_for_lines(void *fi_void, evalarg_T *evalarg)
1627{
1628 forinfo_T *fi = (forinfo_T *)fi_void;
1629 int i;
1630
1631 for (i = 0; i < fi->fi_break_count; ++i)
1632 eval_next_line(evalarg);
1633}
1634
1635/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001636 * Use the first item in a ":for" list. Advance to the next.
1637 * Assign the values to the variable (list). "arg" points to the first one.
1638 * Return TRUE when a valid item was found, FALSE when at end of list or
1639 * something wrong.
1640 */
1641 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001642next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001644 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001645 int result;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001646 int flag = in_vim9script() ? LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001647 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001649 if (fi->fi_blob != NULL)
1650 {
1651 typval_T tv;
1652
1653 if (fi->fi_bi >= blob_len(fi->fi_blob))
1654 return FALSE;
1655 tv.v_type = VAR_NUMBER;
1656 tv.v_lock = VAR_FIXED;
1657 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1658 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001659 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001660 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001661 }
1662
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663 item = fi->fi_lw.lw_item;
1664 if (item == NULL)
1665 result = FALSE;
1666 else
1667 {
1668 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001669 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001670 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 }
1672 return result;
1673}
1674
1675/*
1676 * Free the structure used to store info used by ":for".
1677 */
1678 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001679free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680{
Bram Moolenaar33570922005-01-25 22:26:29 +00001681 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001683 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001684 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001686 list_unref(fi->fi_list);
1687 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001688 if (fi != NULL && fi->fi_blob != NULL)
1689 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 vim_free(fi);
1691}
1692
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001694set_context_for_expression(
1695 expand_T *xp,
1696 char_u *arg,
1697 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
1699 int got_eq = FALSE;
1700 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001701 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001703 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 {
1705 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001706 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001708 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001709 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 {
1711 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001712 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001713 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 break;
1715 }
1716 return;
1717 }
1718 }
1719 else
1720 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1721 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 while ((xp->xp_pattern = vim_strpbrk(arg,
1723 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1724 {
1725 c = *xp->xp_pattern;
1726 if (c == '&')
1727 {
1728 c = xp->xp_pattern[1];
1729 if (c == '&')
1730 {
1731 ++xp->xp_pattern;
1732 xp->xp_context = cmdidx != CMD_let || got_eq
1733 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1734 }
1735 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001736 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001738 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1739 xp->xp_pattern += 2;
1740
1741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 }
1743 else if (c == '$')
1744 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001745 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 xp->xp_context = EXPAND_ENV_VARS;
1747 }
1748 else if (c == '=')
1749 {
1750 got_eq = TRUE;
1751 xp->xp_context = EXPAND_EXPRESSION;
1752 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001753 else if (c == '#'
1754 && xp->xp_context == EXPAND_EXPRESSION)
1755 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001756 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001757 break;
1758 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001759 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 && xp->xp_context == EXPAND_FUNCTIONS
1761 && vim_strchr(xp->xp_pattern, '(') == NULL)
1762 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001763 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 break;
1765 }
1766 else if (cmdidx != CMD_let || got_eq)
1767 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001768 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {
1770 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1771 if (c == '\\' && xp->xp_pattern[1] != NUL)
1772 ++xp->xp_pattern;
1773 xp->xp_context = EXPAND_NOTHING;
1774 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001775 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001777 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1779 /* skip */ ;
1780 xp->xp_context = EXPAND_NOTHING;
1781 }
1782 else if (c == '|')
1783 {
1784 if (xp->xp_pattern[1] == '|')
1785 {
1786 ++xp->xp_pattern;
1787 xp->xp_context = EXPAND_EXPRESSION;
1788 }
1789 else
1790 xp->xp_context = EXPAND_COMMANDS;
1791 }
1792 else
1793 xp->xp_context = EXPAND_EXPRESSION;
1794 }
1795 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001796 // Doesn't look like something valid, expand as an expression
1797 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001798 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 arg = xp->xp_pattern;
1800 if (*arg != NUL)
1801 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1802 /* skip */ ;
1803 }
1804 xp->xp_pattern = arg;
1805}
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001808 * Return TRUE if "pat" matches "text".
1809 * Does not use 'cpo' and always uses 'magic'.
1810 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001811 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001812pattern_match(char_u *pat, char_u *text, int ic)
1813{
1814 int matches = FALSE;
1815 char_u *save_cpo;
1816 regmatch_T regmatch;
1817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001818 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001819 save_cpo = p_cpo;
1820 p_cpo = (char_u *)"";
1821 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1822 if (regmatch.regprog != NULL)
1823 {
1824 regmatch.rm_ic = ic;
1825 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1826 vim_regfree(regmatch.regprog);
1827 }
1828 p_cpo = save_cpo;
1829 return matches;
1830}
1831
1832/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001833 * Handle a name followed by "(". Both for just "name(arg)" and for
1834 * "expr->name(arg)".
1835 * Returns OK or FAIL.
1836 */
1837 static int
1838eval_func(
1839 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001840 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001841 char_u *name,
1842 int name_len,
1843 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001844 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001845 typval_T *basetv) // "expr" for "expr->name(arg)"
1846{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001847 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001848 char_u *s = name;
1849 int len = name_len;
1850 partial_T *partial;
1851 int ret = OK;
1852
1853 if (!evaluate)
1854 check_vars(s, len);
1855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001856 // If "s" is the name of a variable of type VAR_FUNC
1857 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001858 s = deref_func_name(s, &len, &partial, !evaluate);
1859
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001860 // Need to make a copy, in case evaluating the arguments makes
1861 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001862 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001863 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001864 ret = FAIL;
1865 else
1866 {
1867 funcexe_T funcexe;
1868
1869 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001870 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001871 funcexe.firstline = curwin->w_cursor.lnum;
1872 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001873 funcexe.evaluate = evaluate;
1874 funcexe.partial = partial;
1875 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001876 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001877 }
1878 vim_free(s);
1879
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001880 // If evaluate is FALSE rettv->v_type was not set in
1881 // get_func_tv, but it's needed in handle_subscript() to parse
1882 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001883 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1884 {
1885 rettv->vval.v_string = NULL;
1886 rettv->v_type = VAR_FUNC;
1887 }
1888
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001889 // Stop the expression evaluation when immediately
1890 // aborting on error, or when an interrupt occurred or
1891 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001892 if (evaluate && aborting())
1893 {
1894 if (ret == OK)
1895 clear_tv(rettv);
1896 ret = FAIL;
1897 }
1898 return ret;
1899}
1900
1901/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001902 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1903 * comment) and there is a next line, return the next line (skipping blanks)
1904 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001905 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1906 * "arg" must point somewhere inside a line, not at the start.
1907 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001908 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001909eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1910{
1911 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001912 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001913 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001914 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001915 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
Bram Moolenaar75783bd2020-07-19 14:41:58 +02001916 && vim9_comment_start(arg))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001917 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001918 char_u *p;
1919
1920 if (evalarg->eval_cookie != NULL)
1921 p = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
1922 else
1923 p = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001924
1925 if (p != NULL)
1926 {
1927 *getnext = TRUE;
1928 return skipwhite(p);
1929 }
1930 }
1931 return arg;
1932}
1933
1934/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001935 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001936 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001937 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001938eval_next_line(evalarg_T *evalarg)
1939{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001940 garray_T *gap = &evalarg->eval_ga;
1941 char_u *line;
1942
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001943 if (evalarg->eval_cookie != NULL)
1944 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
1945 else
1946 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001947 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001948 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1949 {
1950 // Going to concatenate the lines after parsing.
1951 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1952 ++gap->ga_len;
1953 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001954 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001955 {
1956 vim_free(evalarg->eval_tofree);
1957 evalarg->eval_tofree = line;
1958 }
1959 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001960}
1961
Bram Moolenaare6b53242020-07-01 17:28:33 +02001962/*
1963 * Call eval_next_non_blank() and get the next line if needed.
1964 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001965 char_u *
1966skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1967{
1968 int getnext;
1969 char_u *p = skipwhite(arg);
1970
Bram Moolenaar962d7212020-07-04 14:15:00 +02001971 if (evalarg == NULL)
1972 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02001973 eval_next_non_blank(p, evalarg, &getnext);
1974 if (getnext)
1975 return eval_next_line(evalarg);
1976 return p;
1977}
1978
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001979/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001980 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001981 */
1982 void
1983clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1984{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001985 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001986 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001987 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001988 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001989 if (eap != NULL)
1990 {
1991 // We may need to keep the original command line, e.g. for
1992 // ":let" it has the variable names. But we may also need the
1993 // new one, "nextcmd" points into it. Keep both.
1994 vim_free(eap->cmdline_tofree);
1995 eap->cmdline_tofree = *eap->cmdlinep;
1996 *eap->cmdlinep = evalarg->eval_tofree;
1997 }
1998 else
1999 vim_free(evalarg->eval_tofree);
2000 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002001 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002002
2003 vim_free(evalarg->eval_tofree_lambda);
2004 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002005 }
2006}
2007
2008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2012 */
2013
2014/*
2015 * Handle zero level expression.
2016 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002018 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002019 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 * Return OK or FAIL.
2021 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002023eval0(
2024 char_u *arg,
2025 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002026 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002027 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
2029 int ret;
2030 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002031 int did_emsg_before = did_emsg;
2032 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002033 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
2035 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002036 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002037
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002038 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
2040 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 /*
2043 * Report the invalid expression unless the expression evaluation has
2044 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002045 * exception, or we already gave a more specific error.
2046 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002048 if (!aborting()
2049 && did_emsg == did_emsg_before
2050 && called_emsg == called_emsg_before
2051 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002052 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 ret = FAIL;
2054 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002055
2056 if (eap != NULL)
2057 eap->nextcmd = check_nextcmd(p);
2058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 return ret;
2060}
2061
2062/*
2063 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002064 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 *
2066 * "arg" must point to the first non-white of the expression.
2067 * "arg" is advanced to the next non-white after the recognized expression.
2068 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002069 * Note: "rettv.v_lock" is not set.
2070 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 * Return OK or FAIL.
2072 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002073 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002074eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002076 char_u *p;
2077 int getnext;
2078
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 /*
2080 * Get the first variable.
2081 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002082 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 return FAIL;
2084
Bram Moolenaar793648f2020-06-26 21:28:25 +02002085 p = eval_next_non_blank(*arg, evalarg, &getnext);
2086 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002088 int result;
2089 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002090 evalarg_T *evalarg_used = evalarg;
2091 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002092 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002093 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002094
2095 if (evalarg == NULL)
2096 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002097 CLEAR_FIELD(local_evalarg);
2098 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002099 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002100 orig_flags = evalarg_used->eval_flags;
2101 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002102
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002103 if (getnext)
2104 *arg = eval_next_line(evalarg_used);
2105
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002107 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002109 int error = FALSE;
2110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002111 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002113 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002114 if (error)
2115 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117
2118 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002119 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002121 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2122 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002123 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002124 if (eval1(arg, rettv, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 return FAIL;
2126
2127 /*
2128 * Check for the ":".
2129 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002130 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar793648f2020-06-26 21:28:25 +02002131 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002135 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 return FAIL;
2137 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002138 if (getnext)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002139 *arg = eval_next_line(evalarg_used);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002142 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002144 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2145 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002146 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002147 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
2149 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 return FAIL;
2152 }
2153 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154 *rettv = var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002155
2156 if (evalarg == NULL)
2157 clear_evalarg(&local_evalarg, NULL);
2158 else
2159 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
2161
2162 return OK;
2163}
2164
2165/*
2166 * Handle first level expression:
2167 * expr2 || expr2 || expr2 logical OR
2168 *
2169 * "arg" must point to the first non-white of the expression.
2170 * "arg" is advanced to the next non-white after the recognized expression.
2171 *
2172 * Return OK or FAIL.
2173 */
2174 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002177 char_u *p;
2178 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
2180 /*
2181 * Get the first variable.
2182 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002183 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 return FAIL;
2185
2186 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002187 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002189 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002190 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002192 evalarg_T *evalarg_used = evalarg;
2193 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002194 int evaluate;
2195 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002196 long result = FALSE;
2197 typval_T var2;
2198 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002199 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002200
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002201 if (evalarg == NULL)
2202 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002203 CLEAR_FIELD(local_evalarg);
2204 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002205 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002206 orig_flags = evalarg_used->eval_flags;
2207 evaluate = orig_flags & EVAL_EVALUATE;
2208 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002209 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002210 if (vim9script)
2211 {
2212 result = tv2bool(rettv);
2213 }
2214 else
2215 {
2216 error = FALSE;
2217 if (tv_get_number_chk(rettv, &error) != 0)
2218 result = TRUE;
2219 clear_tv(rettv);
2220 if (error)
2221 return FAIL;
2222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
2224
2225 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002226 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002228 while (p[0] == '|' && p[1] == '|')
2229 {
2230 if (getnext)
2231 *arg = eval_next_line(evalarg_used);
2232
2233 /*
2234 * Get the second variable.
2235 */
2236 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2237 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002238 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002239 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002240 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002241
2242 /*
2243 * Compute the result.
2244 */
2245 if (evaluate && !result)
2246 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002247 if (vim9script)
2248 {
2249 clear_tv(rettv);
2250 *rettv = var2;
2251 result = tv2bool(rettv);
2252 }
2253 else
2254 {
2255 if (tv_get_number_chk(&var2, &error) != 0)
2256 result = TRUE;
2257 clear_tv(&var2);
2258 if (error)
2259 return FAIL;
2260 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002261 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002262 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002263 {
2264 rettv->v_type = VAR_NUMBER;
2265 rettv->vval.v_number = result;
2266 }
2267
2268 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002270
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002271 if (evalarg == NULL)
2272 clear_evalarg(&local_evalarg, NULL);
2273 else
2274 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276
2277 return OK;
2278}
2279
2280/*
2281 * Handle second level expression:
2282 * expr3 && expr3 && expr3 logical AND
2283 *
2284 * "arg" must point to the first non-white of the expression.
2285 * "arg" is advanced to the next non-white after the recognized expression.
2286 *
2287 * Return OK or FAIL.
2288 */
2289 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002290eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002292 char_u *p;
2293 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
2295 /*
2296 * Get the first variable.
2297 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002298 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 return FAIL;
2300
2301 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002302 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002304 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002305 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002307 evalarg_T *evalarg_used = evalarg;
2308 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002309 int orig_flags;
2310 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002311 long result = TRUE;
2312 typval_T var2;
2313 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002314 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002315
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002316 if (evalarg == NULL)
2317 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002318 CLEAR_FIELD(local_evalarg);
2319 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002320 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002321 orig_flags = evalarg_used->eval_flags;
2322 evaluate = orig_flags & EVAL_EVALUATE;
2323 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002324 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002325 if (vim9script)
2326 {
2327 result = tv2bool(rettv);
2328 }
2329 else
2330 {
2331 error = FALSE;
2332 if (tv_get_number_chk(rettv, &error) == 0)
2333 result = FALSE;
2334 clear_tv(rettv);
2335 if (error)
2336 return FAIL;
2337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
2339
2340 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002341 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002343 while (p[0] == '&' && p[1] == '&')
2344 {
2345 if (getnext)
2346 *arg = eval_next_line(evalarg_used);
2347
2348 /*
2349 * Get the second variable.
2350 */
2351 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2352 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002353 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002354 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002356
2357 /*
2358 * Compute the result.
2359 */
2360 if (evaluate && result)
2361 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002362 if (vim9script)
2363 {
2364 clear_tv(rettv);
2365 *rettv = var2;
2366 result = tv2bool(rettv);
2367 }
2368 else
2369 {
2370 if (tv_get_number_chk(&var2, &error) == 0)
2371 result = FALSE;
2372 clear_tv(&var2);
2373 if (error)
2374 return FAIL;
2375 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002376 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002377 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002378 {
2379 rettv->v_type = VAR_NUMBER;
2380 rettv->vval.v_number = result;
2381 }
2382
2383 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002385
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002386 if (evalarg == NULL)
2387 clear_evalarg(&local_evalarg, NULL);
2388 else
2389 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
2391
2392 return OK;
2393}
2394
2395/*
2396 * Handle third level expression:
2397 * var1 == var2
2398 * var1 =~ var2
2399 * var1 != var2
2400 * var1 !~ var2
2401 * var1 > var2
2402 * var1 >= var2
2403 * var1 < var2
2404 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002405 * var1 is var2
2406 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 *
2408 * "arg" must point to the first non-white of the expression.
2409 * "arg" is advanced to the next non-white after the recognized expression.
2410 *
2411 * Return OK or FAIL.
2412 */
2413 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002414eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002417 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002419 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422 /*
2423 * Get the first variable.
2424 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002425 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 return FAIL;
2427
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002428 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 switch (p[0])
2430 {
2431 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002432 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002434 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 break;
2436 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002437 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002439 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 break;
2441 case '>': if (p[1] != '=')
2442 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002443 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 len = 1;
2445 }
2446 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002447 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 break;
2449 case '<': if (p[1] != '=')
2450 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002451 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 len = 1;
2453 }
2454 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002455 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002457 case 'i': if (p[1] == 's')
2458 {
2459 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2460 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002461 i = p[len];
2462 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002463 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002464 }
2465 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 }
2467
2468 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002469 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002471 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002473 typval_T var2;
2474 int ic;
2475 int vim9script = in_vim9script();
2476
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002477 if (getnext)
2478 *arg = eval_next_line(evalarg);
2479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002480 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 if (p[len] == '?')
2482 {
2483 ic = TRUE;
2484 ++len;
2485 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002486 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 else if (p[len] == '#')
2488 {
2489 ic = FALSE;
2490 ++len;
2491 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002492 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002494 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
2496 /*
2497 * Get the second variable.
2498 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002499 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002500 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 return FAIL;
2504 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002505 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002506 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002507 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002508
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002509 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002510 {
2511 ret = FAIL;
2512 clear_tv(rettv);
2513 }
2514 else
2515 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002516 clear_tv(&var2);
2517 return ret;
2518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
2520
2521 return OK;
2522}
2523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 void
2525eval_addblob(typval_T *tv1, typval_T *tv2)
2526{
2527 blob_T *b1 = tv1->vval.v_blob;
2528 blob_T *b2 = tv2->vval.v_blob;
2529 blob_T *b = blob_alloc();
2530 int i;
2531
2532 if (b != NULL)
2533 {
2534 for (i = 0; i < blob_len(b1); i++)
2535 ga_append(&b->bv_ga, blob_get(b1, i));
2536 for (i = 0; i < blob_len(b2); i++)
2537 ga_append(&b->bv_ga, blob_get(b2, i));
2538
2539 clear_tv(tv1);
2540 rettv_blob_set(tv1, b);
2541 }
2542}
2543
2544 int
2545eval_addlist(typval_T *tv1, typval_T *tv2)
2546{
2547 typval_T var3;
2548
2549 // concatenate Lists
2550 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2551 {
2552 clear_tv(tv1);
2553 clear_tv(tv2);
2554 return FAIL;
2555 }
2556 clear_tv(tv1);
2557 *tv1 = var3;
2558 return OK;
2559}
2560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561/*
2562 * Handle fourth level expression:
2563 * + number addition
2564 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002565 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002566 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 *
2568 * "arg" must point to the first non-white of the expression.
2569 * "arg" is advanced to the next non-white after the recognized expression.
2570 *
2571 * Return OK or FAIL.
2572 */
2573 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002574eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 /*
2577 * Get the first variable.
2578 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 return FAIL;
2581
2582 /*
2583 * Repeat computing, until no '+', '-' or '.' is following.
2584 */
2585 for (;;)
2586 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002587 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002588 int getnext;
2589 char_u *p;
2590 int op;
2591 int concat;
2592 typval_T var2;
2593
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002594 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002595 p = eval_next_non_blank(*arg, evalarg, &getnext);
2596 op = *p;
2597 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002598 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002600
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002601 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002602 *arg = eval_next_line(evalarg);
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002603 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002604 if ((op != '+' || (rettv->v_type != VAR_LIST
2605 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002606#ifdef FEAT_FLOAT
2607 && (op == '.' || rettv->v_type != VAR_FLOAT)
2608#endif
2609 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002610 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002611 // For "list + ...", an illegal use of the first operand as
2612 // a number cannot be determined before evaluating the 2nd
2613 // operand: if this is also a list, all is ok.
2614 // For "something . ...", "something - ..." or "non-list + ...",
2615 // we know that the first operand needs to be a string or number
2616 // without evaluating the 2nd operand. So check before to avoid
2617 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002618 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002619 {
2620 clear_tv(rettv);
2621 return FAIL;
2622 }
2623 }
2624
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 /*
2626 * Get the second variable.
2627 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002628 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2629 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002630 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002631 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 return FAIL;
2635 }
2636
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002637 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
2639 /*
2640 * Compute the result.
2641 */
2642 if (op == '.')
2643 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2645 char_u *s1 = tv_get_string_buf(rettv, buf1);
2646 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2647
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002648 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002649 {
2650 clear_tv(rettv);
2651 clear_tv(&var2);
2652 return FAIL;
2653 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002654 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 clear_tv(rettv);
2656 rettv->v_type = VAR_STRING;
2657 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002659 else if (op == '+' && rettv->v_type == VAR_BLOB
2660 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002662 else if (op == '+' && rettv->v_type == VAR_LIST
2663 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002664 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002666 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 else
2669 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002670 int error = FALSE;
2671 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002672#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002673 float_T f1 = 0, f2 = 0;
2674
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002675 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002676 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002677 f1 = rettv->vval.v_float;
2678 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002679 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002680 else
2681#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002683 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002684 if (error)
2685 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002686 // This can only happen for "list + non-list". For
2687 // "non-list + ..." or "something - ...", we returned
2688 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002689 clear_tv(rettv);
2690 return FAIL;
2691 }
2692#ifdef FEAT_FLOAT
2693 if (var2.v_type == VAR_FLOAT)
2694 f1 = n1;
2695#endif
2696 }
2697#ifdef FEAT_FLOAT
2698 if (var2.v_type == VAR_FLOAT)
2699 {
2700 f2 = var2.vval.v_float;
2701 n2 = 0;
2702 }
2703 else
2704#endif
2705 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002706 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002707 if (error)
2708 {
2709 clear_tv(rettv);
2710 clear_tv(&var2);
2711 return FAIL;
2712 }
2713#ifdef FEAT_FLOAT
2714 if (rettv->v_type == VAR_FLOAT)
2715 f2 = n2;
2716#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002718 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002719
2720#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002721 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002722 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2723 {
2724 if (op == '+')
2725 f1 = f1 + f2;
2726 else
2727 f1 = f1 - f2;
2728 rettv->v_type = VAR_FLOAT;
2729 rettv->vval.v_float = f1;
2730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002732#endif
2733 {
2734 if (op == '+')
2735 n1 = n1 + n2;
2736 else
2737 n1 = n1 - n2;
2738 rettv->v_type = VAR_NUMBER;
2739 rettv->vval.v_number = n1;
2740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002742 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 }
2744 }
2745 return OK;
2746}
2747
2748/*
2749 * Handle fifth level expression:
2750 * * number multiplication
2751 * / number division
2752 * % number modulo
2753 *
2754 * "arg" must point to the first non-white of the expression.
2755 * "arg" is advanced to the next non-white after the recognized expression.
2756 *
2757 * Return OK or FAIL.
2758 */
2759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002760eval6(
2761 char_u **arg,
2762 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002763 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002764 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002766#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002767 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
2770 /*
2771 * Get the first variable.
2772 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002773 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 return FAIL;
2775
2776 /*
2777 * Repeat computing, until no '*', '/' or '%' is following.
2778 */
2779 for (;;)
2780 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002781 int evaluate;
2782 int getnext;
2783 typval_T var2;
2784 int op;
2785 varnumber_T n1, n2;
2786#ifdef FEAT_FLOAT
2787 float_T f1, f2;
2788#endif
2789 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002790
2791 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002792 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002794
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002795 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002796 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002798#ifdef FEAT_FLOAT
2799 f1 = 0;
2800 f2 = 0;
2801#endif
2802 error = FALSE;
2803 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002804 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002806#ifdef FEAT_FLOAT
2807 if (rettv->v_type == VAR_FLOAT)
2808 {
2809 f1 = rettv->vval.v_float;
2810 use_float = TRUE;
2811 n1 = 0;
2812 }
2813 else
2814#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002815 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002816 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002817 if (error)
2818 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
2820 else
2821 n1 = 0;
2822
2823 /*
2824 * Get the second variable.
2825 */
2826 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002827 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 return FAIL;
2829
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002830 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002832#ifdef FEAT_FLOAT
2833 if (var2.v_type == VAR_FLOAT)
2834 {
2835 if (!use_float)
2836 {
2837 f1 = n1;
2838 use_float = TRUE;
2839 }
2840 f2 = var2.vval.v_float;
2841 n2 = 0;
2842 }
2843 else
2844#endif
2845 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002846 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002847 clear_tv(&var2);
2848 if (error)
2849 return FAIL;
2850#ifdef FEAT_FLOAT
2851 if (use_float)
2852 f2 = n2;
2853#endif
2854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
2856 /*
2857 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002858 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002860#ifdef FEAT_FLOAT
2861 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002863 if (op == '*')
2864 f1 = f1 * f2;
2865 else if (op == '/')
2866 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002867# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002868 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002869 if (f2 == 0.0)
2870 {
2871 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002872 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002873 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002874 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002875 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002876 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002877 }
2878 else
2879 f1 = f1 / f2;
2880# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002881 // We rely on the floating point library to handle divide
2882 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002883 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002884# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002887 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002889 return FAIL;
2890 }
2891 rettv->v_type = VAR_FLOAT;
2892 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
2894 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002897 if (op == '*')
2898 n1 = n1 * n2;
2899 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002900 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002902 n1 = num_modulus(n1, n2);
2903
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002904 rettv->v_type = VAR_NUMBER;
2905 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
2908 }
2909
2910 return OK;
2911}
2912
2913/*
2914 * Handle sixth level expression:
2915 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002916 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002917 * "string" string constant
2918 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 * &option-name option value
2920 * @r register contents
2921 * identifier variable value
2922 * function() function call
2923 * $VAR environment variable
2924 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002925 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002927 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002928 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 *
2930 * Also handle:
2931 * ! in front logical NOT
2932 * - in front unary minus
2933 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002934 * trailing [] subscript in String or List
2935 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002936 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 *
2938 * "arg" must point to the first non-white of the expression.
2939 * "arg" is advanced to the next non-white after the recognized expression.
2940 *
2941 * Return OK or FAIL.
2942 */
2943 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002944eval7(
2945 char_u **arg,
2946 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002947 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002950 int evaluate = evalarg != NULL
2951 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 int len;
2953 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 char_u *start_leader, *end_leader;
2955 int ret = OK;
2956 char_u *alias;
2957
2958 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002960 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002962 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
2964 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002965 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 */
2967 start_leader = *arg;
2968 while (**arg == '!' || **arg == '-' || **arg == '+')
2969 *arg = skipwhite(*arg + 1);
2970 end_leader = *arg;
2971
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002972 if (**arg == '.' && (!isdigit(*(*arg + 1))
2973#ifdef FEAT_FLOAT
2974 || current_sctx.sc_version < 2
2975#endif
2976 ))
2977 {
2978 semsg(_(e_invexpr2), *arg);
2979 ++*arg;
2980 return FAIL;
2981 }
2982
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 switch (**arg)
2984 {
2985 /*
2986 * Number constant.
2987 */
2988 case '0':
2989 case '1':
2990 case '2':
2991 case '3':
2992 case '4':
2993 case '5':
2994 case '6':
2995 case '7':
2996 case '8':
2997 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002998 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002999
3000 // Apply prefixed "-" and "+" now. Matters especially when
3001 // "->" follows.
3002 if (ret == OK && evaluate && end_leader > start_leader)
3003 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 break;
3005
3006 /*
3007 * String constant: "string".
3008 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003009 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 break;
3011
3012 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003013 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003015 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003016 break;
3017
3018 /*
3019 * List: [expr, expr]
3020 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003021 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 break;
3023
3024 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003025 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003026 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003027 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003028 {
3029 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003030 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003031 }
3032 else
3033 ret = NOTDONE;
3034 break;
3035
3036 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003037 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003038 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003039 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003040 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003041 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003042 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003043 break;
3044
3045 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003046 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003048 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 break;
3050
3051 /*
3052 * Environment variable: $VAR.
3053 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003054 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 break;
3056
3057 /*
3058 * Register contents: @r.
3059 */
3060 case '@': ++*arg;
3061 if (evaluate)
3062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003063 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003064 rettv->vval.v_string = get_reg_contents(**arg,
3065 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 }
3067 if (**arg != NUL)
3068 ++*arg;
3069 break;
3070
3071 /*
3072 * nested expression: (expression).
3073 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003074 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003075 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003076 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003077
Bram Moolenaar9215f012020-06-27 21:18:00 +02003078 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003079 if (**arg == ')')
3080 ++*arg;
3081 else if (ret == OK)
3082 {
3083 emsg(_(e_missing_close));
3084 clear_tv(rettv);
3085 ret = FAIL;
3086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 }
3088 break;
3089
Bram Moolenaar8c711452005-01-14 21:53:12 +00003090 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 break;
3092 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003093
3094 if (ret == NOTDONE)
3095 {
3096 /*
3097 * Must be a variable or function name.
3098 * Can also be a curly-braces kind of name: {expr}.
3099 */
3100 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003101 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003102 if (alias != NULL)
3103 s = alias;
3104
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003105 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003106 ret = FAIL;
3107 else
3108 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003109 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3110
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003111 if (**arg == '(')
3112 // "name(..." recursive!
Bram Moolenaare6b53242020-07-01 17:28:33 +02003113 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003114 else if (flags & EVAL_CONSTANT)
3115 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003116 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003117 {
3118 // get the value of "true", "false" or a variable
3119 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3120 {
3121 rettv->v_type = VAR_BOOL;
3122 rettv->vval.v_number = VVAL_TRUE;
3123 }
3124 else if (len == 5 && in_vim9script()
3125 && STRNCMP(s, "false", 4) == 0)
3126 {
3127 rettv->v_type = VAR_BOOL;
3128 rettv->vval.v_number = VVAL_FALSE;
3129 }
3130 else
3131 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3132 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003134 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003135 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003136 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003138 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003139 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003140 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003141 }
3142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 *arg = skipwhite(*arg);
3144
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003145 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3146 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003147 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003148 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 /*
3151 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3152 */
3153 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003154 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003155 return ret;
3156}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003157
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003158/*
3159 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003160 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003161 * Adjusts "end_leaderp" until it is at "start_leader".
3162 */
3163 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003164eval7_leader(
3165 typval_T *rettv,
3166 int numeric_only,
3167 char_u *start_leader,
3168 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003169{
3170 char_u *end_leader = *end_leaderp;
3171 int ret = OK;
3172 int error = FALSE;
3173 varnumber_T val = 0;
3174#ifdef FEAT_FLOAT
3175 float_T f = 0.0;
3176
3177 if (rettv->v_type == VAR_FLOAT)
3178 f = rettv->vval.v_float;
3179 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003181 val = tv_get_number_chk(rettv, &error);
3182 if (error)
3183 {
3184 clear_tv(rettv);
3185 ret = FAIL;
3186 }
3187 else
3188 {
3189 while (end_leader > start_leader)
3190 {
3191 --end_leader;
3192 if (*end_leader == '!')
3193 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003194 if (numeric_only)
3195 {
3196 ++end_leader;
3197 break;
3198 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003199#ifdef FEAT_FLOAT
3200 if (rettv->v_type == VAR_FLOAT)
3201 f = !f;
3202 else
3203#endif
3204 val = !val;
3205 }
3206 else if (*end_leader == '-')
3207 {
3208#ifdef FEAT_FLOAT
3209 if (rettv->v_type == VAR_FLOAT)
3210 f = -f;
3211 else
3212#endif
3213 val = -val;
3214 }
3215 }
3216#ifdef FEAT_FLOAT
3217 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003219 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003220 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003222 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003223#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003224 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003225 clear_tv(rettv);
3226 rettv->v_type = VAR_NUMBER;
3227 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003230 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 return ret;
3232}
3233
3234/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003235 * Call the function referred to in "rettv".
3236 */
3237 static int
3238call_func_rettv(
3239 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003240 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003241 typval_T *rettv,
3242 int evaluate,
3243 dict_T *selfdict,
3244 typval_T *basetv)
3245{
3246 partial_T *pt = NULL;
3247 funcexe_T funcexe;
3248 typval_T functv;
3249 char_u *s;
3250 int ret;
3251
3252 // need to copy the funcref so that we can clear rettv
3253 if (evaluate)
3254 {
3255 functv = *rettv;
3256 rettv->v_type = VAR_UNKNOWN;
3257
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003258 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003259 if (functv.v_type == VAR_PARTIAL)
3260 {
3261 pt = functv.vval.v_partial;
3262 s = partial_name(pt);
3263 }
3264 else
3265 s = functv.vval.v_string;
3266 }
3267 else
3268 s = (char_u *)"";
3269
Bram Moolenaara80faa82020-04-12 19:37:17 +02003270 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003271 funcexe.firstline = curwin->w_cursor.lnum;
3272 funcexe.lastline = curwin->w_cursor.lnum;
3273 funcexe.evaluate = evaluate;
3274 funcexe.partial = pt;
3275 funcexe.selfdict = selfdict;
3276 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003277 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003278
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003279 // Clear the funcref afterwards, so that deleting it while
3280 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003281 if (evaluate)
3282 clear_tv(&functv);
3283
3284 return ret;
3285}
3286
3287/*
3288 * Evaluate "->method()".
3289 * "*arg" points to the '-'.
3290 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3291 */
3292 static int
3293eval_lambda(
3294 char_u **arg,
3295 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003296 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003297 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003298{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003299 int evaluate = evalarg != NULL
3300 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003301 typval_T base = *rettv;
3302 int ret;
3303
3304 // Skip over the ->.
3305 *arg += 2;
3306 rettv->v_type = VAR_UNKNOWN;
3307
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003308 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003309 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003310 return FAIL;
3311 else if (**arg != '(')
3312 {
3313 if (verbose)
3314 {
3315 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003316 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003317 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003319 }
3320 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003321 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003322 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003323 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003324 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003325
3326 // Clear the funcref afterwards, so that deleting it while
3327 // evaluating the arguments is possible (see test55).
3328 if (evaluate)
3329 clear_tv(&base);
3330
3331 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003332}
3333
3334/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003335 * Evaluate "->method()".
3336 * "*arg" points to the '-'.
3337 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3338 */
3339 static int
3340eval_method(
3341 char_u **arg,
3342 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003343 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003344 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003345{
3346 char_u *name;
3347 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003348 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003349 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003350 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003351 int evaluate = evalarg != NULL
3352 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003353
3354 // Skip over the ->.
3355 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003356 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003357
Bram Moolenaarac92e252019-08-03 21:58:38 +02003358 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003359 len = get_name_len(arg, &alias, evaluate, TRUE);
3360 if (alias != NULL)
3361 name = alias;
3362
3363 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003364 {
3365 if (verbose)
3366 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003367 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003368 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003369 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003370 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003371 if (**arg != '(')
3372 {
3373 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003375 ret = FAIL;
3376 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003377 else if (VIM_ISWHITE((*arg)[-1]))
3378 {
3379 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003380 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003381 ret = FAIL;
3382 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003383 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003384 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003385 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003386 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003387
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003388 // Clear the funcref afterwards, so that deleting it while
3389 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003390 if (evaluate)
3391 clear_tv(&base);
3392
Bram Moolenaarac92e252019-08-03 21:58:38 +02003393 return ret;
3394}
3395
3396/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003397 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3398 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003399 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3400 */
3401 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003402eval_index(
3403 char_u **arg,
3404 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003405 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003406 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003407{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003408 int evaluate = evalarg != NULL
3409 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003410 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003411 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003412 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003413 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003414 long len = -1;
3415 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003416 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003417 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003418
Bram Moolenaara03f2332016-02-06 18:09:59 +01003419 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003420 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003421 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003422 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003423 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003424 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003425 return FAIL;
3426 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003427#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003428 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003429 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003430 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003431#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003432 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003433 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003434 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003435 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003436 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003437 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003438 return FAIL;
3439 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003440 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003442 if (evaluate)
3443 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003444 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003445
3446 case VAR_STRING:
3447 case VAR_NUMBER:
3448 case VAR_LIST:
3449 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003450 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003451 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003452 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003453
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003454 init_tv(&var1);
3455 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003456 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003457 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003458 /*
3459 * dict.name
3460 */
3461 key = *arg + 1;
3462 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3463 ;
3464 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003465 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003466 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003467 }
3468 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003469 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003470 /*
3471 * something[idx]
3472 *
3473 * Get the (first) variable from inside the [].
3474 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003475 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003476 if (**arg == ':')
3477 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003478 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003479 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003480 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003481 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003482 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003483 clear_tv(&var1);
3484 return FAIL;
3485 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003486
3487 /*
3488 * Get the second variable from inside the [:].
3489 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003490 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003491 if (**arg == ':')
3492 {
3493 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003494 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003495 if (**arg == ']')
3496 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003497 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003499 if (!empty1)
3500 clear_tv(&var1);
3501 return FAIL;
3502 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003503 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003504 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003505 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003506 if (!empty1)
3507 clear_tv(&var1);
3508 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003509 return FAIL;
3510 }
3511 }
3512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003513 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003514 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003515 if (**arg != ']')
3516 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003517 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003518 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003519 clear_tv(&var1);
3520 if (range)
3521 clear_tv(&var2);
3522 return FAIL;
3523 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003524 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003525 }
3526
3527 if (evaluate)
3528 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003529 n1 = 0;
3530 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003532 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003534 }
3535 if (range)
3536 {
3537 if (empty2)
3538 n2 = -1;
3539 else
3540 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003541 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003542 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003543 }
3544 }
3545
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003546 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003548 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003549 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003551 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003552 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003553 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003554 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003555 case VAR_SPECIAL:
3556 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003557 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003558 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003559
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003560 case VAR_NUMBER:
3561 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003562 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003563 len = (long)STRLEN(s);
3564 if (range)
3565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003566 // The resulting variable is a substring. If the indexes
3567 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003568 if (n1 < 0)
3569 {
3570 n1 = len + n1;
3571 if (n1 < 0)
3572 n1 = 0;
3573 }
3574 if (n2 < 0)
3575 n2 = len + n2;
3576 else if (n2 >= len)
3577 n2 = len;
3578 if (n1 >= len || n2 < 0 || n1 > n2)
3579 s = NULL;
3580 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003581 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003582 }
3583 else
3584 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003585 // The resulting variable is a string of a single
3586 // character. If the index is too big or negative the
3587 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003588 if (n1 >= len || n1 < 0)
3589 s = NULL;
3590 else
3591 s = vim_strnsave(s + n1, 1);
3592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003593 clear_tv(rettv);
3594 rettv->v_type = VAR_STRING;
3595 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003596 break;
3597
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003598 case VAR_BLOB:
3599 len = blob_len(rettv->vval.v_blob);
3600 if (range)
3601 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003602 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003603 // are out of range the result is empty.
3604 if (n1 < 0)
3605 {
3606 n1 = len + n1;
3607 if (n1 < 0)
3608 n1 = 0;
3609 }
3610 if (n2 < 0)
3611 n2 = len + n2;
3612 else if (n2 >= len)
3613 n2 = len - 1;
3614 if (n1 >= len || n2 < 0 || n1 > n2)
3615 {
3616 clear_tv(rettv);
3617 rettv->v_type = VAR_BLOB;
3618 rettv->vval.v_blob = NULL;
3619 }
3620 else
3621 {
3622 blob_T *blob = blob_alloc();
3623
3624 if (blob != NULL)
3625 {
3626 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3627 {
3628 blob_free(blob);
3629 return FAIL;
3630 }
3631 blob->bv_ga.ga_len = n2 - n1 + 1;
3632 for (i = n1; i <= n2; i++)
3633 blob_set(blob, i - n1,
3634 blob_get(rettv->vval.v_blob, i));
3635
3636 clear_tv(rettv);
3637 rettv_blob_set(rettv, blob);
3638 }
3639 }
3640 }
3641 else
3642 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003643 // The resulting variable is a byte value.
3644 // If the index is too big or negative that is an error.
3645 if (n1 < 0)
3646 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003647 if (n1 < len && n1 >= 0)
3648 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003649 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003650
3651 clear_tv(rettv);
3652 rettv->v_type = VAR_NUMBER;
3653 rettv->vval.v_number = v;
3654 }
3655 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003656 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003657 }
3658 break;
3659
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003660 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003661 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003662 if (n1 < 0)
3663 n1 = len + n1;
3664 if (!empty1 && (n1 < 0 || n1 >= len))
3665 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003666 // For a range we allow invalid values and return an empty
3667 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003668 if (!range)
3669 {
3670 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003671 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003672 return FAIL;
3673 }
3674 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003675 }
3676 if (range)
3677 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003678 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003679
3680 if (n2 < 0)
3681 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003682 else if (n2 >= len)
3683 n2 = len - 1;
3684 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003685 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003686 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003687 if (l == NULL)
3688 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003689 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003690 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003691 }
3692 else
3693 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003694 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003695 clear_tv(rettv);
3696 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003697 }
3698 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003699
3700 case VAR_DICT:
3701 if (range)
3702 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003703 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003704 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003705 if (len == -1)
3706 clear_tv(&var1);
3707 return FAIL;
3708 }
3709 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003710 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003711
3712 if (len == -1)
3713 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003714 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003715 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003716 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003717 clear_tv(&var1);
3718 return FAIL;
3719 }
3720 }
3721
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003722 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003723
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003724 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003725 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003726 if (len == -1)
3727 clear_tv(&var1);
3728 if (item == NULL)
3729 return FAIL;
3730
3731 copy_tv(&item->di_tv, &var1);
3732 clear_tv(rettv);
3733 *rettv = var1;
3734 }
3735 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003736 }
3737 }
3738
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003739 return OK;
3740}
3741
3742/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003743 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003744 */
3745 char_u *
3746partial_name(partial_T *pt)
3747{
3748 if (pt->pt_name != NULL)
3749 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003750 if (pt->pt_func != NULL)
3751 return pt->pt_func->uf_name;
3752 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003753}
3754
Bram Moolenaarddecc252016-04-06 22:59:37 +02003755 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003756partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003757{
3758 int i;
3759
3760 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003761 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003762 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003763 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003764 if (pt->pt_name != NULL)
3765 {
3766 func_unref(pt->pt_name);
3767 vim_free(pt->pt_name);
3768 }
3769 else
3770 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003771
3772 if (pt->pt_funcstack != NULL)
3773 {
3774 // Decrease the reference count for the context of a closure. If down
3775 // to zero free it and clear the variables on the stack.
3776 if (--pt->pt_funcstack->fs_refcount == 0)
3777 {
3778 garray_T *gap = &pt->pt_funcstack->fs_ga;
3779 typval_T *stack = gap->ga_data;
3780
3781 for (i = 0; i < gap->ga_len; ++i)
3782 clear_tv(stack + i);
3783 ga_clear(gap);
3784 vim_free(pt->pt_funcstack);
3785 }
3786 pt->pt_funcstack = NULL;
3787 }
3788
Bram Moolenaarddecc252016-04-06 22:59:37 +02003789 vim_free(pt);
3790}
3791
3792/*
3793 * Unreference a closure: decrement the reference count and free it when it
3794 * becomes zero.
3795 */
3796 void
3797partial_unref(partial_T *pt)
3798{
3799 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003800 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003801}
3802
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003803/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003804 * Return the next (unique) copy ID.
3805 * Used for serializing nested structures.
3806 */
3807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003808get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003809{
3810 current_copyID += COPYID_INC;
3811 return current_copyID;
3812}
3813
3814/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003815 * Garbage collection for lists and dictionaries.
3816 *
3817 * We use reference counts to be able to free most items right away when they
3818 * are no longer used. But for composite items it's possible that it becomes
3819 * unused while the reference count is > 0: When there is a recursive
3820 * reference. Example:
3821 * :let l = [1, 2, 3]
3822 * :let d = {9: l}
3823 * :let l[1] = d
3824 *
3825 * Since this is quite unusual we handle this with garbage collection: every
3826 * once in a while find out which lists and dicts are not referenced from any
3827 * variable.
3828 *
3829 * Here is a good reference text about garbage collection (refers to Python
3830 * but it applies to all reference-counting mechanisms):
3831 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003832 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003833
3834/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003835 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003836 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003837 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003838 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003839 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003840garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003841{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003842 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003843 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003844 buf_T *buf;
3845 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003846 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003847 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003848
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003849 if (!testing)
3850 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003851 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003852 want_garbage_collect = FALSE;
3853 may_garbage_collect = FALSE;
3854 garbage_collect_at_exit = FALSE;
3855 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003856
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003857 // The execution stack can grow big, limit the size.
3858 if (exestack.ga_maxlen - exestack.ga_len > 500)
3859 {
3860 size_t new_len;
3861 char_u *pp;
3862 int n;
3863
3864 // Keep 150% of the current size, with a minimum of the growth size.
3865 n = exestack.ga_len / 2;
3866 if (n < exestack.ga_growsize)
3867 n = exestack.ga_growsize;
3868
3869 // Don't make it bigger though.
3870 if (exestack.ga_len + n < exestack.ga_maxlen)
3871 {
3872 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3873 pp = vim_realloc(exestack.ga_data, new_len);
3874 if (pp == NULL)
3875 return FAIL;
3876 exestack.ga_maxlen = exestack.ga_len + n;
3877 exestack.ga_data = pp;
3878 }
3879 }
3880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003881 // We advance by two because we add one for items referenced through
3882 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003883 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003884
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003885 /*
3886 * 1. Go through all accessible variables and mark all lists and dicts
3887 * with copyID.
3888 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003890 // Don't free variables in the previous_funccal list unless they are only
3891 // referenced through previous_funccal. This must be first, because if
3892 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003893 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003895 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003896 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003898 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003899 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003900 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3901 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003902
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003903 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003904 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003905 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3906 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003907 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003908 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3909 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003910#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003911 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003912 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3913 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003914 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003915 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003916 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3917 NULL, NULL);
3918#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003919
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003920 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003921 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003922 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3923 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003924 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003925 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003927 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003928 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003930 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003931 abort = abort || set_ref_in_functions(copyID);
3932
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003933 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003934 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003936 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003937 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003938
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003939 // callbacks in buffers
3940 abort = abort || set_ref_in_buffers(copyID);
3941
Bram Moolenaar1dced572012-04-05 16:54:08 +02003942#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003943 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003944#endif
3945
Bram Moolenaardb913952012-06-29 12:54:53 +02003946#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003947 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003948#endif
3949
3950#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003951 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003952#endif
3953
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003954#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003955 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003956 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003957#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003958#ifdef FEAT_NETBEANS_INTG
3959 abort = abort || set_ref_in_nb_channel(copyID);
3960#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003961
Bram Moolenaare3188e22016-05-31 21:13:04 +02003962#ifdef FEAT_TIMERS
3963 abort = abort || set_ref_in_timer(copyID);
3964#endif
3965
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003966#ifdef FEAT_QUICKFIX
3967 abort = abort || set_ref_in_quickfix(copyID);
3968#endif
3969
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003970#ifdef FEAT_TERMINAL
3971 abort = abort || set_ref_in_term(copyID);
3972#endif
3973
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003974#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003975 abort = abort || set_ref_in_popups(copyID);
3976#endif
3977
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003978 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003979 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003980 /*
3981 * 2. Free lists and dictionaries that are not referenced.
3982 */
3983 did_free = free_unref_items(copyID);
3984
3985 /*
3986 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003987 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003988 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003989 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003990 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003991 else if (p_verbose > 0)
3992 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003993 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003994 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003995
3996 return did_free;
3997}
3998
3999/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004000 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004001 */
4002 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004003free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004004{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004005 int did_free = FALSE;
4006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004007 // Let all "free" functions know that we are here. This means no
4008 // dictionaries, lists, channels or jobs are to be freed, because we will
4009 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004010 in_free_unref_items = TRUE;
4011
4012 /*
4013 * PASS 1: free the contents of the items. We don't free the items
4014 * themselves yet, so that it is possible to decrement refcount counters
4015 */
4016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004017 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004018 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004019
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004020 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004021 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004022
4023#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004024 // Go through the list of jobs and free items without the copyID. This
4025 // must happen before doing channels, because jobs refer to channels, but
4026 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004027 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004029 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004030 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4031#endif
4032
4033 /*
4034 * PASS 2: free the items themselves.
4035 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004036 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004037 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004038
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004039#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004040 // Go through the list of jobs and free items without the copyID. This
4041 // must happen before doing channels, because jobs refer to channels, but
4042 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004043 free_unused_jobs(copyID, COPYID_MASK);
4044
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004045 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004046 free_unused_channels(copyID, COPYID_MASK);
4047#endif
4048
4049 in_free_unref_items = FALSE;
4050
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004051 return did_free;
4052}
4053
4054/*
4055 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004056 * "list_stack" is used to add lists to be marked. Can be NULL.
4057 *
4058 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004059 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004060 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004061set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004062{
4063 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004064 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004065 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004066 hashtab_T *cur_ht;
4067 ht_stack_T *ht_stack = NULL;
4068 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004069
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004070 cur_ht = ht;
4071 for (;;)
4072 {
4073 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004074 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004075 // Mark each item in the hashtab. If the item contains a hashtab
4076 // it is added to ht_stack, if it contains a list it is added to
4077 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004078 todo = (int)cur_ht->ht_used;
4079 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4080 if (!HASHITEM_EMPTY(hi))
4081 {
4082 --todo;
4083 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4084 &ht_stack, list_stack);
4085 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004086 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004087
4088 if (ht_stack == NULL)
4089 break;
4090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004091 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004092 cur_ht = ht_stack->ht;
4093 tempitem = ht_stack;
4094 ht_stack = ht_stack->prev;
4095 free(tempitem);
4096 }
4097
4098 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004099}
4100
4101/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004102 * Mark a dict and its items with "copyID".
4103 * Returns TRUE if setting references failed somehow.
4104 */
4105 int
4106set_ref_in_dict(dict_T *d, int copyID)
4107{
4108 if (d != NULL && d->dv_copyID != copyID)
4109 {
4110 d->dv_copyID = copyID;
4111 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4112 }
4113 return FALSE;
4114}
4115
4116/*
4117 * Mark a list and its items with "copyID".
4118 * Returns TRUE if setting references failed somehow.
4119 */
4120 int
4121set_ref_in_list(list_T *ll, int copyID)
4122{
4123 if (ll != NULL && ll->lv_copyID != copyID)
4124 {
4125 ll->lv_copyID = copyID;
4126 return set_ref_in_list_items(ll, copyID, NULL);
4127 }
4128 return FALSE;
4129}
4130
4131/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004132 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004133 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4134 *
4135 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004136 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004137 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004138set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004139{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004140 listitem_T *li;
4141 int abort = FALSE;
4142 list_T *cur_l;
4143 list_stack_T *list_stack = NULL;
4144 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004145
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004146 cur_l = l;
4147 for (;;)
4148 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004149 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004150 // Mark each item in the list. If the item contains a hashtab
4151 // it is added to ht_stack, if it contains a list it is added to
4152 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004153 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4154 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4155 ht_stack, &list_stack);
4156 if (list_stack == NULL)
4157 break;
4158
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004159 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004160 cur_l = list_stack->list;
4161 tempitem = list_stack;
4162 list_stack = list_stack->prev;
4163 free(tempitem);
4164 }
4165
4166 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004167}
4168
4169/*
4170 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004171 * "list_stack" is used to add lists to be marked. Can be NULL.
4172 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4173 *
4174 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004175 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004176 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004177set_ref_in_item(
4178 typval_T *tv,
4179 int copyID,
4180 ht_stack_T **ht_stack,
4181 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004182{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004183 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004184
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004185 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004186 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004187 dict_T *dd = tv->vval.v_dict;
4188
Bram Moolenaara03f2332016-02-06 18:09:59 +01004189 if (dd != NULL && dd->dv_copyID != copyID)
4190 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004191 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004192 dd->dv_copyID = copyID;
4193 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004194 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004195 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4196 }
4197 else
4198 {
4199 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4200 if (newitem == NULL)
4201 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004202 else
4203 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004204 newitem->ht = &dd->dv_hashtab;
4205 newitem->prev = *ht_stack;
4206 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004207 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004208 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004209 }
4210 }
4211 else if (tv->v_type == VAR_LIST)
4212 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004213 list_T *ll = tv->vval.v_list;
4214
Bram Moolenaara03f2332016-02-06 18:09:59 +01004215 if (ll != NULL && ll->lv_copyID != copyID)
4216 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004217 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004218 ll->lv_copyID = copyID;
4219 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004220 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004221 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004222 }
4223 else
4224 {
4225 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004226 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004227 if (newitem == NULL)
4228 abort = TRUE;
4229 else
4230 {
4231 newitem->list = ll;
4232 newitem->prev = *list_stack;
4233 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004234 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004235 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004236 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004237 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004238 else if (tv->v_type == VAR_FUNC)
4239 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004240 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004241 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004242 else if (tv->v_type == VAR_PARTIAL)
4243 {
4244 partial_T *pt = tv->vval.v_partial;
4245 int i;
4246
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004247 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004248 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004249 // Didn't see this partial yet.
4250 pt->pt_copyID = copyID;
4251
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004252 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004253
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004254 if (pt->pt_dict != NULL)
4255 {
4256 typval_T dtv;
4257
4258 dtv.v_type = VAR_DICT;
4259 dtv.vval.v_dict = pt->pt_dict;
4260 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4261 }
4262
4263 for (i = 0; i < pt->pt_argc; ++i)
4264 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4265 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004266 if (pt->pt_funcstack != NULL)
4267 {
4268 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4269
4270 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4271 abort = abort || set_ref_in_item(stack + i, copyID,
4272 ht_stack, list_stack);
4273 }
4274
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004275 }
4276 }
4277#ifdef FEAT_JOB_CHANNEL
4278 else if (tv->v_type == VAR_JOB)
4279 {
4280 job_T *job = tv->vval.v_job;
4281 typval_T dtv;
4282
4283 if (job != NULL && job->jv_copyID != copyID)
4284 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004285 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004286 if (job->jv_channel != NULL)
4287 {
4288 dtv.v_type = VAR_CHANNEL;
4289 dtv.vval.v_channel = job->jv_channel;
4290 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4291 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004292 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004293 {
4294 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004295 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004296 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4297 }
4298 }
4299 }
4300 else if (tv->v_type == VAR_CHANNEL)
4301 {
4302 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004303 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004304 typval_T dtv;
4305 jsonq_T *jq;
4306 cbq_T *cq;
4307
4308 if (ch != NULL && ch->ch_copyID != copyID)
4309 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004310 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004311 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004312 {
4313 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4314 jq = jq->jq_next)
4315 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4316 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4317 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004318 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004319 {
4320 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004321 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004322 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4323 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004324 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004325 {
4326 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004327 dtv.vval.v_partial =
4328 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004329 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4330 }
4331 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004332 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004333 {
4334 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004335 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004336 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4337 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004338 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004339 {
4340 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004341 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004342 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4343 }
4344 }
4345 }
4346#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004347 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004348}
4349
Bram Moolenaar8c711452005-01-14 21:53:12 +00004350/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004351 * Return a string with the string representation of a variable.
4352 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004353 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004354 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004355 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4356 * stings as "string()", otherwise does not put quotes around strings, as
4357 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004358 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4359 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004360 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004361 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004362 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004363echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004364 typval_T *tv,
4365 char_u **tofree,
4366 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004367 int copyID,
4368 int echo_style,
4369 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004370 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004371{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004372 static int recurse = 0;
4373 char_u *r = NULL;
4374
Bram Moolenaar33570922005-01-25 22:26:29 +00004375 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004376 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004377 if (!did_echo_string_emsg)
4378 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004379 // Only give this message once for a recursive call to avoid
4380 // flooding the user with errors. And stop iterating over lists
4381 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004382 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004383 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004384 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004385 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004386 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004387 }
4388 ++recurse;
4389
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004390 switch (tv->v_type)
4391 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004392 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004393 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004394 {
4395 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004396 r = tv->vval.v_string;
4397 if (r == NULL)
4398 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004399 }
4400 else
4401 {
4402 *tofree = string_quote(tv->vval.v_string, FALSE);
4403 r = *tofree;
4404 }
4405 break;
4406
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004407 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004408 if (echo_style)
4409 {
4410 *tofree = NULL;
4411 r = tv->vval.v_string;
4412 }
4413 else
4414 {
4415 *tofree = string_quote(tv->vval.v_string, TRUE);
4416 r = *tofree;
4417 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004418 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004419
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004420 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004421 {
4422 partial_T *pt = tv->vval.v_partial;
4423 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004424 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004425 garray_T ga;
4426 int i;
4427 char_u *tf;
4428
4429 ga_init2(&ga, 1, 100);
4430 ga_concat(&ga, (char_u *)"function(");
4431 if (fname != NULL)
4432 {
4433 ga_concat(&ga, fname);
4434 vim_free(fname);
4435 }
4436 if (pt != NULL && pt->pt_argc > 0)
4437 {
4438 ga_concat(&ga, (char_u *)", [");
4439 for (i = 0; i < pt->pt_argc; ++i)
4440 {
4441 if (i > 0)
4442 ga_concat(&ga, (char_u *)", ");
4443 ga_concat(&ga,
4444 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4445 vim_free(tf);
4446 }
4447 ga_concat(&ga, (char_u *)"]");
4448 }
4449 if (pt != NULL && pt->pt_dict != NULL)
4450 {
4451 typval_T dtv;
4452
4453 ga_concat(&ga, (char_u *)", ");
4454 dtv.v_type = VAR_DICT;
4455 dtv.vval.v_dict = pt->pt_dict;
4456 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4457 vim_free(tf);
4458 }
4459 ga_concat(&ga, (char_u *)")");
4460
4461 *tofree = ga.ga_data;
4462 r = *tofree;
4463 break;
4464 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004465
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004466 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004467 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004468 break;
4469
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004470 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004471 if (tv->vval.v_list == NULL)
4472 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004473 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004474 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004475 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004476 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004477 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4478 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004479 {
4480 *tofree = NULL;
4481 r = (char_u *)"[...]";
4482 }
4483 else
4484 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004485 int old_copyID = tv->vval.v_list->lv_copyID;
4486
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004487 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004488 *tofree = list2string(tv, copyID, restore_copyID);
4489 if (restore_copyID)
4490 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004491 r = *tofree;
4492 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004493 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004494
Bram Moolenaar8c711452005-01-14 21:53:12 +00004495 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004496 if (tv->vval.v_dict == NULL)
4497 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004498 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004499 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004500 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004501 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004502 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4503 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004504 {
4505 *tofree = NULL;
4506 r = (char_u *)"{...}";
4507 }
4508 else
4509 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004510 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004511
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004512 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004513 *tofree = dict2string(tv, copyID, restore_copyID);
4514 if (restore_copyID)
4515 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004516 r = *tofree;
4517 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004518 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004519
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004520 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004521 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004522 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004524 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004525 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004526 break;
4527
Bram Moolenaar835dc632016-02-07 14:27:38 +01004528 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004529 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004530 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004531 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004532 if (composite_val)
4533 {
4534 *tofree = string_quote(r, FALSE);
4535 r = *tofree;
4536 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004537 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004538
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004539 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004540#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004541 *tofree = NULL;
4542 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4543 r = numbuf;
4544 break;
4545#endif
4546
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004547 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004548 case VAR_SPECIAL:
4549 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004550 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004551 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004552 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004553
Bram Moolenaar8502c702014-06-17 12:51:16 +02004554 if (--recurse == 0)
4555 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004556 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004557}
4558
4559/*
4560 * Return a string with the string representation of a variable.
4561 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4562 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004563 * Does not put quotes around strings, as ":echo" displays values.
4564 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4565 * May return NULL.
4566 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004567 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004568echo_string(
4569 typval_T *tv,
4570 char_u **tofree,
4571 char_u *numbuf,
4572 int copyID)
4573{
4574 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4575}
4576
4577/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004578 * Return string "str" in ' quotes, doubling ' characters.
4579 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004580 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004581 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004582 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004583string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004584{
Bram Moolenaar33570922005-01-25 22:26:29 +00004585 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004586 char_u *p, *r, *s;
4587
Bram Moolenaar33570922005-01-25 22:26:29 +00004588 len = (function ? 13 : 3);
4589 if (str != NULL)
4590 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004591 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004592 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004593 if (*p == '\'')
4594 ++len;
4595 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004596 s = r = alloc(len);
4597 if (r != NULL)
4598 {
4599 if (function)
4600 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004601 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004602 r += 10;
4603 }
4604 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004605 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004606 if (str != NULL)
4607 for (p = str; *p != NUL; )
4608 {
4609 if (*p == '\'')
4610 *r++ = '\'';
4611 MB_COPY_CHAR(p, r);
4612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004613 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004614 if (function)
4615 *r++ = ')';
4616 *r++ = NUL;
4617 }
4618 return s;
4619}
4620
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004621#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004622/*
4623 * Convert the string "text" to a floating point number.
4624 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4625 * this always uses a decimal point.
4626 * Returns the length of the text that was consumed.
4627 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004629string2float(
4630 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004631 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632{
4633 char *s = (char *)text;
4634 float_T f;
4635
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004636 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004637 if (STRNICMP(text, "inf", 3) == 0)
4638 {
4639 *value = INFINITY;
4640 return 3;
4641 }
4642 if (STRNICMP(text, "-inf", 3) == 0)
4643 {
4644 *value = -INFINITY;
4645 return 4;
4646 }
4647 if (STRNICMP(text, "nan", 3) == 0)
4648 {
4649 *value = NAN;
4650 return 3;
4651 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004652 f = strtod(s, &s);
4653 *value = f;
4654 return (int)((char_u *)s - text);
4655}
4656#endif
4657
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004660 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004662 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004663var2fpos(
4664 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004665 int dollar_lnum, // TRUE when $ is last line
4666 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004668 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004670 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004672 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004673 if (varp->v_type == VAR_LIST)
4674 {
4675 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004676 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004677 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004678 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004679
4680 l = varp->vval.v_list;
4681 if (l == NULL)
4682 return NULL;
4683
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004684 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004685 pos.lnum = list_find_nr(l, 0L, &error);
4686 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004687 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004688
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004689 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004690 pos.col = list_find_nr(l, 1L, &error);
4691 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004692 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004693 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004694
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004695 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004696 li = list_find(l, 1L);
4697 if (li != NULL && li->li_tv.v_type == VAR_STRING
4698 && li->li_tv.vval.v_string != NULL
4699 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4700 pos.col = len + 1;
4701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004702 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004703 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004704 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004705 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004706
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004707 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004708 pos.coladd = list_find_nr(l, 2L, &error);
4709 if (error)
4710 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004711
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004712 return &pos;
4713 }
4714
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004715 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004716 if (name == NULL)
4717 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004718 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004720 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004721 {
4722 if (VIsual_active)
4723 return &VIsual;
4724 return &curwin->w_cursor;
4725 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004726 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004728 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4730 return NULL;
4731 return pp;
4732 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004733
Bram Moolenaara5525202006-03-02 22:52:09 +00004734 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004735
Bram Moolenaar477933c2007-07-17 14:32:23 +00004736 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004737 {
4738 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004739 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004740 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004741 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004742 // In silent Ex mode topline is zero, but that's not a valid line
4743 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004744 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004745 return &pos;
4746 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004747 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004748 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004749 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004750 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004751 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004752 return &pos;
4753 }
4754 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004755 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004757 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
4759 pos.lnum = curbuf->b_ml.ml_line_count;
4760 pos.col = 0;
4761 }
4762 else
4763 {
4764 pos.lnum = curwin->w_cursor.lnum;
4765 pos.col = (colnr_T)STRLEN(ml_get_curline());
4766 }
4767 return &pos;
4768 }
4769 return NULL;
4770}
4771
4772/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004773 * Convert list in "arg" into a position and optional file number.
4774 * When "fnump" is NULL there is no file number, only 3 items.
4775 * Note that the column is passed on as-is, the caller may want to decrement
4776 * it to use 1 for the first column.
4777 * Return FAIL when conversion is not possible, doesn't check the position for
4778 * validity.
4779 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004781list2fpos(
4782 typval_T *arg,
4783 pos_T *posp,
4784 int *fnump,
4785 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004786{
4787 list_T *l = arg->vval.v_list;
4788 long i = 0;
4789 long n;
4790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004791 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4792 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004793 if (arg->v_type != VAR_LIST
4794 || l == NULL
4795 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004796 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004797 return FAIL;
4798
4799 if (fnump != NULL)
4800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004801 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004802 if (n < 0)
4803 return FAIL;
4804 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004805 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004806 *fnump = n;
4807 }
4808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004809 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004810 if (n < 0)
4811 return FAIL;
4812 posp->lnum = n;
4813
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004814 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004815 if (n < 0)
4816 return FAIL;
4817 posp->col = n;
4818
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004819 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004820 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004821 posp->coladd = 0;
4822 else
4823 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004824
Bram Moolenaar493c1782014-05-28 14:34:46 +02004825 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004826 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004827
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004828 return OK;
4829}
4830
4831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 * Get the length of an environment variable name.
4833 * Advance "arg" to the first character after the name.
4834 * Return 0 for error.
4835 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004837get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
4839 char_u *p;
4840 int len;
4841
4842 for (p = *arg; vim_isIDc(*p); ++p)
4843 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004844 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 return 0;
4846
4847 len = (int)(p - *arg);
4848 *arg = p;
4849 return len;
4850}
4851
4852/*
4853 * Get the length of the name of a function or internal variable.
4854 * "arg" is advanced to the first non-white character after the name.
4855 * Return 0 if something is wrong.
4856 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004857 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004858get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859{
4860 char_u *p;
4861 int len;
4862
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004863 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004865 {
4866 if (*p == ':')
4867 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004868 // "s:" is start of "s:var", but "n:" is not and can be used in
4869 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004870 len = (int)(p - *arg);
4871 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4872 || len > 1)
4873 break;
4874 }
4875 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004876 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 return 0;
4878
4879 len = (int)(p - *arg);
4880 *arg = skipwhite(p);
4881
4882 return len;
4883}
4884
4885/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004886 * Get the length of the name of a variable or function.
4887 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004889 * Return -1 if curly braces expansion failed.
4890 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 * If the name contains 'magic' {}'s, expand them and return the
4892 * expanded name in an allocated string via 'alias' - caller must free.
4893 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004894 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004895get_name_len(
4896 char_u **arg,
4897 char_u **alias,
4898 int evaluate,
4899 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900{
4901 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 char_u *p;
4903 char_u *expr_start;
4904 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004906 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
4908 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4909 && (*arg)[2] == (int)KE_SNR)
4910 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004911 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 *arg += 3;
4913 return get_id_len(arg) + 3;
4914 }
4915 len = eval_fname_script(*arg);
4916 if (len > 0)
4917 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004918 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 *arg += len;
4920 }
4921
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004923 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004925 p = find_name_end(*arg, &expr_start, &expr_end,
4926 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (expr_start != NULL)
4928 {
4929 char_u *temp_string;
4930
4931 if (!evaluate)
4932 {
4933 len += (int)(p - *arg);
4934 *arg = skipwhite(p);
4935 return len;
4936 }
4937
4938 /*
4939 * Include any <SID> etc in the expanded string:
4940 * Thus the -len here.
4941 */
4942 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4943 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004944 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 *alias = temp_string;
4946 *arg = skipwhite(p);
4947 return (int)STRLEN(temp_string);
4948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
4950 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004951 // Only give an error when there is something, otherwise it will be
4952 // reported at a higher level.
4953 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004954 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
4956 return len;
4957}
4958
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004959/*
4960 * Find the end of a variable or function name, taking care of magic braces.
4961 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4962 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004963 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 * Return a pointer to just after the name. Equal to "arg" if there is no
4965 * valid name.
4966 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004967 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004968find_name_end(
4969 char_u *arg,
4970 char_u **expr_start,
4971 char_u **expr_end,
4972 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004974 int mb_nest = 0;
4975 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004977 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004978 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004980 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004982 *expr_start = NULL;
4983 *expr_end = NULL;
4984 }
4985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004987 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4988 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004989 return arg;
4990
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004991 for (p = arg; *p != NUL
4992 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004993 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004994 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004996 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004997 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004998 if (*p == '\'')
4999 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005000 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005001 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005002 ;
5003 if (*p == NUL)
5004 break;
5005 }
5006 else if (*p == '"')
5007 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005008 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005009 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005010 if (*p == '\\' && p[1] != NUL)
5011 ++p;
5012 if (*p == NUL)
5013 break;
5014 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005015 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5016 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005017 // "s:" is start of "s:var", but "n:" is not and can be used in
5018 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005019 len = (int)(p - arg);
5020 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005021 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005022 break;
5023 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005024
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005025 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005027 if (*p == '[')
5028 ++br_nest;
5029 else if (*p == ']')
5030 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005032
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005033 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005035 if (*p == '{')
5036 {
5037 mb_nest++;
5038 if (expr_start != NULL && *expr_start == NULL)
5039 *expr_start = p;
5040 }
5041 else if (*p == '}')
5042 {
5043 mb_nest--;
5044 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5045 *expr_end = p;
5046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049
5050 return p;
5051}
5052
5053/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005054 * Expands out the 'magic' {}'s in a variable/function name.
5055 * Note that this can call itself recursively, to deal with
5056 * constructs like foo{bar}{baz}{bam}
5057 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5058 * "in_start" ^
5059 * "expr_start" ^
5060 * "expr_end" ^
5061 * "in_end" ^
5062 *
5063 * Returns a new allocated string, which the caller must free.
5064 * Returns NULL for failure.
5065 */
5066 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005067make_expanded_name(
5068 char_u *in_start,
5069 char_u *expr_start,
5070 char_u *expr_end,
5071 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005072{
5073 char_u c1;
5074 char_u *retval = NULL;
5075 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005076
5077 if (expr_end == NULL || in_end == NULL)
5078 return NULL;
5079 *expr_start = NUL;
5080 *expr_end = NUL;
5081 c1 = *in_end;
5082 *in_end = NUL;
5083
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005084 temp_result = eval_to_string(expr_start + 1, FALSE);
5085 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005086 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005087 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5088 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005089 if (retval != NULL)
5090 {
5091 STRCPY(retval, in_start);
5092 STRCAT(retval, temp_result);
5093 STRCAT(retval, expr_end + 1);
5094 }
5095 }
5096 vim_free(temp_result);
5097
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005098 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005099 *expr_start = '{';
5100 *expr_end = '}';
5101
5102 if (retval != NULL)
5103 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005104 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005105 if (expr_start != NULL)
5106 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005107 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005108 temp_result = make_expanded_name(retval, expr_start,
5109 expr_end, temp_result);
5110 vim_free(retval);
5111 retval = temp_result;
5112 }
5113 }
5114
5115 return retval;
5116}
5117
5118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005120 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005122 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005123eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005125 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5126}
5127
5128/*
5129 * Return TRUE if character "c" can be used as the first character in a
5130 * variable or function name (excluding '{' and '}').
5131 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005132 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005133eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005134{
5135 return (ASCII_ISALPHA(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
5168 && ((rettv->v_type == VAR_DICT && *p == '.'
5169 && ASCII_ISALPHA(p[1]))
5170 || (*p == '-' && p[1] == '>'
5171 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005172 {
5173 *arg = eval_next_line(evalarg);
5174 check_white = FALSE;
5175 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005176
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005177 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5178 || rettv->v_type == VAR_PARTIAL))
5179 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005180 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005181 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5182 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005183
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005184 // Stop the expression evaluation when immediately aborting on
5185 // error, or when an interrupt occurred or an exception was thrown
5186 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005187 if (aborting())
5188 {
5189 if (ret == OK)
5190 clear_tv(rettv);
5191 ret = FAIL;
5192 }
5193 dict_unref(selfdict);
5194 selfdict = NULL;
5195 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005196 else if (**arg == '-' && (*arg)[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005197 {
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}