blob: 02cfd7d7dd5b91c2896a72765f91260079e8f041 [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 Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000044} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045
Bram Moolenaar48e697e2016-01-23 22:17:30 +010046static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020047static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
48static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
52static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020053static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020056static 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 +020057
Bram Moolenaare21c1582019-03-02 11:57:09 +010058/*
59 * Return "n1" divided by "n2", taking care of dividing by zero.
60 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020061 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010062num_divide(varnumber_T n1, varnumber_T n2)
63{
64 varnumber_T result;
65
66 if (n2 == 0) // give an error message?
67 {
68 if (n1 == 0)
69 result = VARNUM_MIN; // similar to NaN
70 else if (n1 < 0)
71 result = -VARNUM_MAX;
72 else
73 result = VARNUM_MAX;
74 }
75 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
83 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020084 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010085num_modulus(varnumber_T n1, varnumber_T n2)
86{
87 // Give an error when n2 is 0?
88 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaara1fa8922017-01-12 20:06:33 +010091#if defined(EBCDIC) || defined(PROTO)
92/*
93 * Compare struct fst by function name.
94 */
95 static int
96compare_func_name(const void *s1, const void *s2)
97{
98 struct fst *p1 = (struct fst *)s1;
99 struct fst *p2 = (struct fst *)s2;
100
101 return STRCMP(p1->f_name, p2->f_name);
102}
103
104/*
105 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100106 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100107 * On machines using EBCDIC we have to sort it.
108 */
109 static void
110sortFunctions(void)
111{
112 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
113
114 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
115}
116#endif
117
Bram Moolenaar33570922005-01-25 22:26:29 +0000118/*
119 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000120 */
121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100122eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000123{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200124 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200125 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000126
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200127#ifdef EBCDIC
128 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100129 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200130 */
131 sortFunctions();
132#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000133}
134
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000135#if defined(EXITFREE) || defined(PROTO)
136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000138{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200139 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100140 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200141 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000142
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200143 // autoloaded script names
144 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000145
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200146 // unreferenced lists and dicts
147 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200148
149 // functions not garbage collected
150 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151}
152#endif
153
Bram Moolenaare6b53242020-07-01 17:28:33 +0200154 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
156{
157 CLEAR_FIELD(*evalarg);
158 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
159 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
160 {
161 evalarg->eval_getline = eap->getline;
162 evalarg->eval_cookie = eap->cookie;
163 }
164}
165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Top level evaluation function, returning a boolean.
168 * Sets "error" to TRUE if there was an error.
169 * Return TRUE or FALSE.
170 */
171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100172eval_to_bool(
173 char_u *arg,
174 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200175 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100176 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
Bram Moolenaar33570922005-01-25 22:26:29 +0000178 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200179 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200180 evalarg_T evalarg;
181
Bram Moolenaar37c83712020-06-30 21:18:36 +0200182 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
184 if (skip)
185 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200186 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 else
189 {
190 *error = FALSE;
191 if (!skip)
192 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200193 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200194 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200195 else
196 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000197 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 }
199 }
200 if (skip)
201 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200202 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200204 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205}
206
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207/*
208 * Call eval1() and give an error message if not done at a lower level.
209 */
210 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100213 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 int ret;
215 int did_emsg_before = did_emsg;
216 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200219 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
220
221 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222 if (ret == FAIL)
223 {
224 // Report the invalid expression unless the expression evaluation has
225 // been cancelled due to an aborting error, an interrupt, or an
226 // exception, or we already gave a more specific error.
227 // Also check called_emsg for when using assert_fails().
228 if (!aborting() && did_emsg == did_emsg_before
229 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100230 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100231 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100233 return ret;
234}
235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200237 * Return whether a typval is a valid expression to pass to eval_expr_typval()
238 * or eval_expr_to_bool(). An empty string returns FALSE;
239 */
240 int
241eval_expr_valid_arg(typval_T *tv)
242{
243 return tv->v_type != VAR_UNKNOWN
244 && (tv->v_type != VAR_STRING
245 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
246}
247
248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 * Evaluate an expression, which can be a function, partial or string.
250 * Pass arguments "argv[argc]".
251 * Return the result in "rettv" and OK or FAIL.
252 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200253 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100254eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
255{
256 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100257 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200258 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100259
260 if (expr->v_type == VAR_FUNC)
261 {
262 s = expr->vval.v_string;
263 if (s == NULL || *s == NUL)
264 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200265 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200266 funcexe.evaluate = TRUE;
267 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100268 return FAIL;
269 }
270 else if (expr->v_type == VAR_PARTIAL)
271 {
272 partial_T *partial = expr->vval.v_partial;
273
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200274 if (partial == NULL)
275 return FAIL;
276
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200278 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200280 if (call_def_function(partial->pt_func, argc, argv,
281 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 return FAIL;
283 }
284 else
285 {
286 s = partial_name(partial);
287 if (s == NULL || *s == NUL)
288 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200289 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 funcexe.evaluate = TRUE;
291 funcexe.partial = partial;
292 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
293 return FAIL;
294 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 }
296 else
297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100298 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 if (s == NULL)
300 return FAIL;
301 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200302 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100303 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200304 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100305 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200306 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100307 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 return FAIL;
309 }
310 }
311 return OK;
312}
313
314/*
315 * Like eval_to_bool() but using a typval_T instead of a string.
316 * Works for string, funcref and partial.
317 */
318 int
319eval_expr_to_bool(typval_T *expr, int *error)
320{
321 typval_T rettv;
322 int res;
323
324 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
325 {
326 *error = TRUE;
327 return FALSE;
328 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200329 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100330 clear_tv(&rettv);
331 return res;
332}
333
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334/*
335 * Top level evaluation function, returning a string. If "skip" is TRUE,
336 * only parsing to "nextcmd" is done, without reporting errors. Return
337 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
338 */
339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100340eval_to_string_skip(
341 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200342 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100343 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
Bram Moolenaar33570922005-01-25 22:26:29 +0000345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200347 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar37c83712020-06-30 21:18:36 +0200349 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 if (skip)
351 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200352 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 retval = NULL;
354 else
355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100356 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 }
359 if (skip)
360 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200361 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363 return retval;
364}
365
366/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367 * Skip over an expression at "*pp".
368 * Return FAIL for an error, OK otherwise.
369 */
370 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200371skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000372{
Bram Moolenaar33570922005-01-25 22:26:29 +0000373 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000374
375 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200376 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000377}
378
379/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200380 * Skip over an expression at "*pp".
381 * If in Vim9 script and line breaks are encountered, the lines are
382 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200383 * "arg" is advanced to just after the expression.
384 * "start" is set to the start of the expression, "end" to just after the end.
385 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200386 * Return FAIL for an error, OK otherwise.
387 */
388 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200389skip_expr_concatenate(
390 char_u **arg,
391 char_u **start,
392 char_u **end,
393 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200394{
395 typval_T rettv;
396 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200397 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200398 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 int evaluate = evalarg == NULL
401 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200403 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 {
406 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200407 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 ++gap->ga_len;
410 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200411 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412
413 // Don't evaluate the expression.
414 if (evalarg != NULL)
415 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200416 *arg = skipwhite(*arg);
417 res = eval1(arg, &rettv, evalarg);
418 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 if (evalarg != NULL)
420 evalarg->eval_flags = save_flags;
421
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200422 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200425 if (evalarg->eval_ga.ga_len == 1)
426 {
427 // just one line, no need to concatenate
428 ga_clear(gap);
429 gap->ga_itemsize = 0;
430 }
431 else
432 {
433 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200435
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 // Line breaks encountered, concatenate all the lines.
437 *((char_u **)gap->ga_data) = *start;
438 p = ga_concat_strings(gap, "");
439
440 // free the lines only when using getsourceline()
441 if (evalarg->eval_cookie != NULL)
442 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200444 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200445 // Do not free the last line, "arg" points into it, free it
446 // later.
447 vim_free(evalarg->eval_tofree);
448 evalarg->eval_tofree =
449 ((char_u **)gap->ga_data)[gap->ga_len - 1];
450 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear_strings(gap);
452 }
453 else
454 ga_clear(gap);
455 gap->ga_itemsize = 0;
456 if (p == NULL)
457 return FAIL;
458 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200459 vim_free(evalarg->eval_tofree_lambda);
460 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200461 // Compute "end" relative to the end.
462 *end = *start + STRLEN(*start) - endoff;
463 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200464 }
465
466 return res;
467}
468
469/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200470 * Top level evaluation function, returning a string. Does not handle line
471 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000472 * When "convert" is TRUE convert a List into a sequence of lines and convert
473 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 * Return pointer to allocated memory, or NULL for failure.
475 */
476 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100477eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100478 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100479 int convert,
480 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
Bram Moolenaar33570922005-01-25 22:26:29 +0000482 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000484 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000485#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000486 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000487#endif
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100488 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100490 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
491 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 retval = NULL;
493 else
494 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000495 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000496 {
497 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000498 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200499 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200500 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200501 if (tv.vval.v_list->lv_len > 0)
502 ga_append(&ga, NL);
503 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000504 ga_append(&ga, NUL);
505 retval = (char_u *)ga.ga_data;
506 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000507#ifdef FEAT_FLOAT
508 else if (convert && tv.v_type == VAR_FLOAT)
509 {
510 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
511 retval = vim_strsave(numbuf);
512 }
513#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000514 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100515 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000516 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100518 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
520 return retval;
521}
522
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100523 char_u *
524eval_to_string(
525 char_u *arg,
526 int convert)
527{
528 return eval_to_string_eap(arg, convert, NULL);
529}
530
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000532 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200533 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200534 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 */
536 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100537eval_to_string_safe(
538 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100539 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
541 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200542 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200543 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200545 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200546 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000547 if (use_sandbox)
548 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200549 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200550 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000551 if (use_sandbox)
552 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200553 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200554 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200555 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 return retval;
557}
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559/*
560 * Top level evaluation function, returning a number.
561 * Evaluates "expr" silently.
562 * Returns -1 for an error.
563 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200564 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100565eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
Bram Moolenaar33570922005-01-25 22:26:29 +0000567 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200568 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000569 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571 ++emsg_off;
572
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200573 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 retval = -1;
575 else
576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100577 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000578 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580 --emsg_off;
581
582 return retval;
583}
584
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000585/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000586 * Top level evaluation function.
587 * Returns an allocated typval_T with the result.
588 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000589 */
590 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200591eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000592{
593 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200594 evalarg_T evalarg;
595
596 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000597
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200598 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200599 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100600 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000601
Bram Moolenaar37c83712020-06-30 21:18:36 +0200602 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000603 return tv;
604}
605
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100607 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200608 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
609 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000610 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100613call_vim_function(
614 char_u *func,
615 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200616 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200617 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000619 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200620 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100622 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200623 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200624 funcexe.firstline = curwin->w_cursor.lnum;
625 funcexe.lastline = curwin->w_cursor.lnum;
626 funcexe.evaluate = TRUE;
627 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000628 if (ret == FAIL)
629 clear_tv(rettv);
630
631 return ret;
632}
633
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100634/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100635 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100636 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200637 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
638 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100639 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200640 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641call_func_retnr(
642 char_u *func,
643 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200644 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100645{
646 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200647 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100648
Bram Moolenaarded27a12018-08-01 19:06:03 +0200649 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100650 return -1;
651
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100652 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100653 clear_tv(&rettv);
654 return retval;
655}
656
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000657/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100658 * Call Vim script function like call_func_retnr() and drop the result.
659 * Returns FAIL when calling the function fails.
660 */
661 int
662call_func_noret(
663 char_u *func,
664 int argc,
665 typval_T *argv)
666{
667 typval_T rettv;
668
669 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
670 return FAIL;
671 clear_tv(&rettv);
672 return OK;
673}
674
675/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100676 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100677 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000678 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000679 */
680 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100681call_func_retstr(
682 char_u *func,
683 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200684 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000685{
686 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000687 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000688
Bram Moolenaarded27a12018-08-01 19:06:03 +0200689 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000690 return NULL;
691
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100692 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000693 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 return retval;
695}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000696
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000697/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100698 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100699 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000701 */
702 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703call_func_retlist(
704 char_u *func,
705 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200706 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707{
708 typval_T rettv;
709
Bram Moolenaarded27a12018-08-01 19:06:03 +0200710 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000711 return NULL;
712
713 if (rettv.v_type != VAR_LIST)
714 {
715 clear_tv(&rettv);
716 return NULL;
717 }
718
719 return rettv.vval.v_list;
720}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#ifdef FEAT_FOLDING
723/*
724 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
725 * it in "*cp". Doesn't give error messages.
726 */
727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100728eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729{
Bram Moolenaar33570922005-01-25 22:26:29 +0000730 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200731 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000733 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
734 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
736 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000737 if (use_sandbox)
738 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200739 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200741 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 retval = 0;
743 else
744 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100745 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000746 if (tv.v_type == VAR_NUMBER)
747 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000748 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 retval = 0;
750 else
751 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100752 // If the result is a string, check if there is a non-digit before
753 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000754 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 if (!VIM_ISDIGIT(*s) && *s != '-')
756 *cp = *s++;
757 retval = atol((char *)s);
758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000759 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000762 if (use_sandbox)
763 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200764 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200765 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200767 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768}
769#endif
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 * Get an lval: variable, Dict item or List item that can be assigned a value
773 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
774 * "name.key", "name.key[expr]" etc.
775 * Indexing only works if "name" is an existing List or Dictionary.
776 * "name" points to the start of the name.
777 * If "rettv" is not NULL it points to the value to be assigned.
778 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
779 * wrong; must end in space or cmd separator.
780 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100781 * flags:
782 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100783 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100784 * GLV_NO_AUTOLOAD: do not use script autoloading
785 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000786 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000787 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000789 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200790 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100791get_lval(
792 char_u *name,
793 typval_T *rettv,
794 lval_T *lp,
795 int unlet,
796 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100797 int flags, // GLV_ values
798 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000800 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 char_u *expr_start, *expr_end;
802 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000803 dictitem_T *v;
804 typval_T var1;
805 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000807 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000808 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000809 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100810 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100811 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100813 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200814 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000815
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100816 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000817 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100818 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200820 lp->ll_name_end = find_name_end(name, NULL, NULL,
821 FNE_INCL_BR | fne_flags);
822 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 }
824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100825 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000826 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 if (expr_start != NULL)
829 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100830 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100831 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 && *p != '[' && *p != '.')
833 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200834 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835 return NULL;
836 }
837
838 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
839 if (lp->ll_exp_name == NULL)
840 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100841 // Report an invalid expression in braces, unless the
842 // expression evaluation has been cancelled due to an
843 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 if (!aborting() && !quiet)
845 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000846 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100847 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 return NULL;
849 }
850 }
851 lp->ll_name = lp->ll_exp_name;
852 }
853 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000855 lp->ll_name = name;
856
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200857 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200859 // "a: type" is declaring variable "a" with a type, not "a:".
860 if (p == name + 2 && p[-1] == ':')
861 {
862 --p;
863 lp->ll_name_end = p;
864 }
865 if (*p == ':')
866 {
867 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
868 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200870 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100871 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
872 if (lp->ll_type == NULL && !quiet)
873 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200874 lp->ll_name_end = tp;
875 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 }
877 }
878
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100879 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
881 return p;
882
883 cc = *p;
884 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100885 // Only pass &ht when we would write to the variable, it prevents autoload
886 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100887 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
888 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000889 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200890 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000891 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000892 if (v == NULL)
893 return NULL;
894
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100895 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
896 {
897 if (!quiet)
898 semsg(_(e_variable_already_declared), lp->ll_name);
899 return NULL;
900 }
901
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000902 /*
903 * Loop until no more [idx] or .key is following.
904 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000905 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100906 var1.v_type = VAR_UNKNOWN;
907 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000909 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000910 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200911 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100912 && !(lp->ll_tv->v_type == VAR_BLOB
913 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000914 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100916 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000918 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000919 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000921 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100922 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000923 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000924 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000925
Bram Moolenaar10c65862020-10-08 21:16:42 +0200926 if (in_vim9script() && lp->ll_valtype == NULL
927 && lp->ll_tv == &v->di_tv
928 && ht != NULL && ht == get_script_local_ht())
929 {
930 svar_T *sv = find_typval_in_script(lp->ll_tv);
931
932 // Vim9 script local variable: get the type
933 if (sv != NULL)
934 lp->ll_valtype = sv->sv_type;
935 }
936
Bram Moolenaar8c711452005-01-14 21:53:12 +0000937 len = -1;
938 if (*p == '.')
939 {
940 key = p + 1;
941 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
942 ;
943 if (len == 0)
944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000945 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100946 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000947 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000948 }
949 p = key + len;
950 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000951 else
952 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100953 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000954 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000955 if (*p == ':')
956 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000957 else
958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000959 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200960 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000961 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100962 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100964 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000965 clear_tv(&var1);
966 return NULL;
967 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200968 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000969 }
970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100971 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000972 if (*p == ':')
973 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000974 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200977 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100978 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000980 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100981 if (rettv != NULL
982 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100983 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100984 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100985 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000986 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100988 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100989 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 }
992 p = skipwhite(p + 1);
993 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000995 else
996 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200998 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200999 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001001 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001002 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001003 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001004 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001005 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001006 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001007 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001008 clear_tv(&var2);
1009 return NULL;
1010 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001011 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001013 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001014 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001016
Bram Moolenaar8c711452005-01-14 21:53:12 +00001017 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001018 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001020 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001021 clear_tv(&var1);
1022 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 }
1025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001026 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001027 ++p;
1028 }
1029
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001031 {
1032 if (len == -1)
1033 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001034 // "[key]": get key from "var1"
1035 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001036 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001037 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001038 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001039 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001040 }
1041 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001042 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001043
1044 // a NULL dict is equivalent with an empty dict
1045 if (lp->ll_tv->vval.v_dict == NULL)
1046 {
1047 lp->ll_tv->vval.v_dict = dict_alloc();
1048 if (lp->ll_tv->vval.v_dict == NULL)
1049 {
1050 clear_tv(&var1);
1051 return NULL;
1052 }
1053 ++lp->ll_tv->vval.v_dict->dv_refcount;
1054 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001055 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001056
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001057 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001059 // When assigning to a scope dictionary check that a function and
1060 // variable name is valid (only variable name unless it is l: or
1061 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001062 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001063 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001064 int prevval;
1065 int wrong;
1066
1067 if (len != -1)
1068 {
1069 prevval = key[len];
1070 key[len] = NUL;
1071 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001072 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001073 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001074 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1075 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001076 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001077 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001078 if (len != -1)
1079 key[len] = prevval;
1080 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001081 {
1082 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001083 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001084 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001085 }
1086
Bram Moolenaar10c65862020-10-08 21:16:42 +02001087 if (lp->ll_valtype != NULL)
1088 // use the type of the member
1089 lp->ll_valtype = lp->ll_valtype->tt_member;
1090
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001091 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001092 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001093 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001094 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001095 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001097 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001098 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001099 return NULL;
1100 }
1101
Bram Moolenaar31b81602019-02-10 22:14:27 +01001102 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001103 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001104 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001105 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001106 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001107 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001108 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001109 }
1110 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001112 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001114 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001115 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001116 p = NULL;
1117 break;
1118 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001119 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001120 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001121 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1122 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001123 {
1124 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001125 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001126 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001127
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001128 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001129 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001130 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001131 else if (lp->ll_tv->v_type == VAR_BLOB)
1132 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001133 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1134
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001135 /*
1136 * Get the number and item for the only or first index of the List.
1137 */
1138 if (empty1)
1139 lp->ll_n1 = 0;
1140 else
1141 // is number or string
1142 lp->ll_n1 = (long)tv_get_number(&var1);
1143 clear_tv(&var1);
1144
1145 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001146 || lp->ll_n1 > bloblen
1147 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001148 {
1149 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001150 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001151 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001152 return NULL;
1153 }
1154 if (lp->ll_range && !lp->ll_empty2)
1155 {
1156 lp->ll_n2 = (long)tv_get_number(&var2);
1157 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001158 if (lp->ll_n2 < 0
1159 || lp->ll_n2 >= bloblen
1160 || lp->ll_n2 < lp->ll_n1)
1161 {
1162 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001163 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001164 return NULL;
1165 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001166 }
1167 lp->ll_blob = lp->ll_tv->vval.v_blob;
1168 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001169 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001170 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001171 else
1172 {
1173 /*
1174 * Get the number and item for the only or first index of the List.
1175 */
1176 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001177 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001178 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001179 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001180 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001181 clear_tv(&var1);
1182
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001183 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001184 lp->ll_list = lp->ll_tv->vval.v_list;
1185 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1186 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001187 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001188 if (lp->ll_n1 < 0)
1189 {
1190 lp->ll_n1 = 0;
1191 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1192 }
1193 }
1194 if (lp->ll_li == NULL)
1195 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001196 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001197 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001200 }
1201
Bram Moolenaar10c65862020-10-08 21:16:42 +02001202 if (lp->ll_valtype != NULL)
1203 // use the type of the member
1204 lp->ll_valtype = lp->ll_valtype->tt_member;
1205
Bram Moolenaar8c711452005-01-14 21:53:12 +00001206 /*
1207 * May need to find the item or absolute index for the second
1208 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 * When no index given: "lp->ll_empty2" is TRUE.
1210 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001211 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001212 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001213 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001214 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001215 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001218 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001220 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001221 {
1222 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001225 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001227 }
1228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001229 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 if (lp->ll_n1 < 0)
1231 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1232 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001233 {
1234 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001235 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001237 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001238 }
1239
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001241 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001242 }
1243
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001244 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 return p;
1247}
1248
1249/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001250 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001252 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001253clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254{
1255 vim_free(lp->ll_exp_name);
1256 vim_free(lp->ll_newkey);
1257}
1258
1259/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001260 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001262 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1263 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001264 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001265 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001266set_var_lval(
1267 lval_T *lp,
1268 char_u *endp,
1269 typval_T *rettv,
1270 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001271 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001272 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001273{
1274 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001275 listitem_T *ri;
1276 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277
1278 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001279 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001280 cc = *endp;
1281 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001282 if (lp->ll_blob != NULL)
1283 {
1284 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001285
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001286 if (op != NULL && *op != '=')
1287 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001288 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001289 return;
1290 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001291 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001292 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001293
1294 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1295 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001296 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001297
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001298 if (lp->ll_empty2)
1299 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1300
1301 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001303 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 return;
1305 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001306 if (lp->ll_empty2)
1307 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001308
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001309 ir = 0;
1310 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1311 blob_set(lp->ll_blob, il,
1312 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001313 }
1314 else
1315 {
1316 val = (int)tv_get_number_chk(rettv, &error);
1317 if (!error)
1318 {
1319 garray_T *gap = &lp->ll_blob->bv_ga;
1320
1321 // Allow for appending a byte. Setting a byte beyond
1322 // the end is an error otherwise.
1323 if (lp->ll_n1 < gap->ga_len
1324 || (lp->ll_n1 == gap->ga_len
1325 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1326 {
1327 blob_set(lp->ll_blob, lp->ll_n1, val);
1328 if (lp->ll_n1 == gap->ga_len)
1329 ++gap->ga_len;
1330 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001331 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001332 }
1333 }
1334 }
1335 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001337 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001338
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001339 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001340 {
1341 emsg(_(e_cannot_mod));
1342 *endp = cc;
1343 return;
1344 }
1345
Bram Moolenaarff697e62019-02-12 22:28:33 +01001346 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001347 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001348 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001349 &tv, &di, TRUE, FALSE) == OK)
1350 {
1351 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001352 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1353 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001354 && tv_op(&tv, rettv, op) == OK)
1355 set_var(lp->ll_name, &tv, FALSE);
1356 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001359 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001360 {
1361 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001362 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001363 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001365 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001366 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001367 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001368 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001369 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001370 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001371 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001372 else if (lp->ll_range)
1373 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001374 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001375 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001376
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001377 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001378 {
1379 emsg(_("E996: Cannot lock a range"));
1380 return;
1381 }
1382
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001383 /*
1384 * Check whether any of the list items is locked
1385 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001386 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001387 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001388 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001389 return;
1390 ri = ri->li_next;
1391 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1392 break;
1393 ll_li = ll_li->li_next;
1394 ++ll_n1;
1395 }
1396
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001397 /*
1398 * Assign the List values to the list items.
1399 */
1400 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001401 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001402 if (op != NULL && *op != '=')
1403 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1404 else
1405 {
1406 clear_tv(&lp->ll_li->li_tv);
1407 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1408 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001409 ri = ri->li_next;
1410 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1411 break;
1412 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001413 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001414 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001415 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001416 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 ri = NULL;
1418 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001419 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001420 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 lp->ll_li = lp->ll_li->li_next;
1422 ++lp->ll_n1;
1423 }
1424 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001425 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001426 else if (lp->ll_empty2
1427 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001429 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 }
1431 else
1432 {
1433 /*
1434 * Assign to a List or Dictionary item.
1435 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001436 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001437 {
1438 emsg(_("E996: Cannot lock a list or dict"));
1439 return;
1440 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001441
1442 if (lp->ll_valtype != NULL
1443 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1444 return;
1445
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 if (lp->ll_newkey != NULL)
1447 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001448 if (op != NULL && *op != '=')
1449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001450 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001451 return;
1452 }
1453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001454 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001455 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001456 if (di == NULL)
1457 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001458 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1459 {
1460 vim_free(di);
1461 return;
1462 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001463 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001464 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465 else if (op != NULL && *op != '=')
1466 {
1467 tv_op(lp->ll_tv, rettv, op);
1468 return;
1469 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001470 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001472
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001473 /*
1474 * Assign the value to the variable or list item.
1475 */
1476 if (copy)
1477 copy_tv(rettv, lp->ll_tv);
1478 else
1479 {
1480 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001481 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001482 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483 }
1484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485}
1486
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001487/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001488 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1489 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490 * Returns OK or FAIL.
1491 */
1492 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001494{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001495 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496 char_u numbuf[NUMBUFLEN];
1497 char_u *s;
1498
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001499 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001500 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001501 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001502 {
1503 switch (tv1->v_type)
1504 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001505 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001506 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001508 case VAR_DICT:
1509 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001510 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001511 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001512 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001513 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001514 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 break;
1516
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001517 case VAR_BLOB:
1518 if (*op != '+' || tv2->v_type != VAR_BLOB)
1519 break;
1520 // BLOB += BLOB
1521 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1522 {
1523 blob_T *b1 = tv1->vval.v_blob;
1524 blob_T *b2 = tv2->vval.v_blob;
1525 int i, len = blob_len(b2);
1526 for (i = 0; i < len; i++)
1527 ga_append(&b1->bv_ga, blob_get(b2, i));
1528 }
1529 return OK;
1530
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001531 case VAR_LIST:
1532 if (*op != '+' || tv2->v_type != VAR_LIST)
1533 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001534 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001535 if (tv2->vval.v_list != NULL)
1536 {
1537 if (tv1->vval.v_list == NULL)
1538 {
1539 tv1->vval.v_list = tv2->vval.v_list;
1540 ++tv1->vval.v_list->lv_refcount;
1541 }
1542 else
1543 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1544 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001545 return OK;
1546
1547 case VAR_NUMBER:
1548 case VAR_STRING:
1549 if (tv2->v_type == VAR_LIST)
1550 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001551 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001552 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001553 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001554 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001555#ifdef FEAT_FLOAT
1556 if (tv2->v_type == VAR_FLOAT)
1557 {
1558 float_T f = n;
1559
Bram Moolenaarff697e62019-02-12 22:28:33 +01001560 if (*op == '%')
1561 break;
1562 switch (*op)
1563 {
1564 case '+': f += tv2->vval.v_float; break;
1565 case '-': f -= tv2->vval.v_float; break;
1566 case '*': f *= tv2->vval.v_float; break;
1567 case '/': f /= tv2->vval.v_float; break;
1568 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 clear_tv(tv1);
1570 tv1->v_type = VAR_FLOAT;
1571 tv1->vval.v_float = f;
1572 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001573 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001574#endif
1575 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001576 switch (*op)
1577 {
1578 case '+': n += tv_get_number(tv2); break;
1579 case '-': n -= tv_get_number(tv2); break;
1580 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001581 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1582 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001583 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001584 clear_tv(tv1);
1585 tv1->v_type = VAR_NUMBER;
1586 tv1->vval.v_number = n;
1587 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001588 }
1589 else
1590 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001591 if (tv2->v_type == VAR_FLOAT)
1592 break;
1593
Bram Moolenaarff697e62019-02-12 22:28:33 +01001594 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001595 s = tv_get_string(tv1);
1596 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001597 clear_tv(tv1);
1598 tv1->v_type = VAR_STRING;
1599 tv1->vval.v_string = s;
1600 }
1601 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001602
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001603 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001604#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001605 {
1606 float_T f;
1607
Bram Moolenaarff697e62019-02-12 22:28:33 +01001608 if (*op == '%' || *op == '.'
1609 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001610 && tv2->v_type != VAR_NUMBER
1611 && tv2->v_type != VAR_STRING))
1612 break;
1613 if (tv2->v_type == VAR_FLOAT)
1614 f = tv2->vval.v_float;
1615 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001616 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001617 switch (*op)
1618 {
1619 case '+': tv1->vval.v_float += f; break;
1620 case '-': tv1->vval.v_float -= f; break;
1621 case '*': tv1->vval.v_float *= f; break;
1622 case '/': tv1->vval.v_float /= f; break;
1623 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001625#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001626 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 }
1628 }
1629
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001630 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001631 return FAIL;
1632}
1633
1634/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635 * Evaluate the expression used in a ":for var in expr" command.
1636 * "arg" points to "var".
1637 * Set "*errp" to TRUE for an error, FALSE otherwise;
1638 * Return a pointer that holds the info. Null when there is an error.
1639 */
1640 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001641eval_for_line(
1642 char_u *arg,
1643 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001644 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001645 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001646{
Bram Moolenaar33570922005-01-25 22:26:29 +00001647 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001649 typval_T tv;
1650 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001651 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001653 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001655 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 if (fi == NULL)
1657 return NULL;
1658
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001659 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 if (expr == NULL)
1661 return fi;
1662
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001663 expr = skipwhite_and_linebreak(expr, evalarg);
1664 if (expr[0] != 'i' || expr[1] != 'n'
1665 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 return fi;
1669 }
1670
1671 if (skip)
1672 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001673 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1674 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 {
1676 *errp = FALSE;
1677 if (!skip)
1678 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001679 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001680 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001681 l = tv.vval.v_list;
1682 if (l == NULL)
1683 {
1684 // a null list is like an empty list: do nothing
1685 clear_tv(&tv);
1686 }
1687 else
1688 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001690 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001692 // No need to increment the refcount, it's already set for
1693 // the list being used in "tv".
1694 fi->fi_list = l;
1695 list_add_watch(l, &fi->fi_lw);
1696 fi->fi_lw.lw_item = l->lv_first;
1697 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001698 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001699 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001700 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001701 fi->fi_bi = 0;
1702 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001703 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001704 typval_T btv;
1705
1706 // Make a copy, so that the iteration still works when the
1707 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001709 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001710 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001711 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001712 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 else
1714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001715 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001716 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 }
1718 }
1719 }
1720 if (skip)
1721 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001722 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723
1724 return fi;
1725}
1726
1727/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001728 * Used when looping over a :for line, skip the "in expr" part.
1729 */
1730 void
1731skip_for_lines(void *fi_void, evalarg_T *evalarg)
1732{
1733 forinfo_T *fi = (forinfo_T *)fi_void;
1734 int i;
1735
1736 for (i = 0; i < fi->fi_break_count; ++i)
1737 eval_next_line(evalarg);
1738}
1739
1740/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741 * Use the first item in a ":for" list. Advance to the next.
1742 * Assign the values to the variable (list). "arg" points to the first one.
1743 * Return TRUE when a valid item was found, FALSE when at end of list or
1744 * something wrong.
1745 */
1746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001747next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001749 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 int result;
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001751 int flag = in_vim9script() ? ASSIGN_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001752 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001754 if (fi->fi_blob != NULL)
1755 {
1756 typval_T tv;
1757
1758 if (fi->fi_bi >= blob_len(fi->fi_blob))
1759 return FALSE;
1760 tv.v_type = VAR_NUMBER;
1761 tv.v_lock = VAR_FIXED;
1762 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1763 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001764 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001765 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001766 }
1767
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 item = fi->fi_lw.lw_item;
1769 if (item == NULL)
1770 result = FALSE;
1771 else
1772 {
1773 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001774 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001775 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776 }
1777 return result;
1778}
1779
1780/*
1781 * Free the structure used to store info used by ":for".
1782 */
1783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001784free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785{
Bram Moolenaar33570922005-01-25 22:26:29 +00001786 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001788 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001789 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001791 list_unref(fi->fi_list);
1792 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001793 if (fi != NULL && fi->fi_blob != NULL)
1794 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 vim_free(fi);
1796}
1797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001799set_context_for_expression(
1800 expand_T *xp,
1801 char_u *arg,
1802 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001804 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001808 if (cmdidx == CMD_let || cmdidx == CMD_var
1809 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 {
1811 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001812 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001814 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001815 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 {
1817 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001818 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001819 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 break;
1821 }
1822 return;
1823 }
1824 }
1825 else
1826 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1827 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 while ((xp->xp_pattern = vim_strpbrk(arg,
1829 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1830 {
1831 c = *xp->xp_pattern;
1832 if (c == '&')
1833 {
1834 c = xp->xp_pattern[1];
1835 if (c == '&')
1836 {
1837 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001838 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 }
1840 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001843 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1844 xp->xp_pattern += 2;
1845
1846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
1848 else if (c == '$')
1849 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001850 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 xp->xp_context = EXPAND_ENV_VARS;
1852 }
1853 else if (c == '=')
1854 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001855 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 xp->xp_context = EXPAND_EXPRESSION;
1857 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001858 else if (c == '#'
1859 && xp->xp_context == EXPAND_EXPRESSION)
1860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001861 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001862 break;
1863 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001864 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 && xp->xp_context == EXPAND_FUNCTIONS
1866 && vim_strchr(xp->xp_pattern, '(') == NULL)
1867 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001868 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 break;
1870 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001871 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001873 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
1875 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1876 if (c == '\\' && xp->xp_pattern[1] != NUL)
1877 ++xp->xp_pattern;
1878 xp->xp_context = EXPAND_NOTHING;
1879 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001880 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001882 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1884 /* skip */ ;
1885 xp->xp_context = EXPAND_NOTHING;
1886 }
1887 else if (c == '|')
1888 {
1889 if (xp->xp_pattern[1] == '|')
1890 {
1891 ++xp->xp_pattern;
1892 xp->xp_context = EXPAND_EXPRESSION;
1893 }
1894 else
1895 xp->xp_context = EXPAND_COMMANDS;
1896 }
1897 else
1898 xp->xp_context = EXPAND_EXPRESSION;
1899 }
1900 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001901 // Doesn't look like something valid, expand as an expression
1902 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 arg = xp->xp_pattern;
1905 if (*arg != NUL)
1906 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1907 /* skip */ ;
1908 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001909
1910 // ":exe one two" completes "two"
1911 if ((cmdidx == CMD_execute
1912 || cmdidx == CMD_echo
1913 || cmdidx == CMD_echon
1914 || cmdidx == CMD_echomsg)
1915 && xp->xp_context == EXPAND_EXPRESSION)
1916 {
1917 for (;;)
1918 {
1919 char_u *n = skiptowhite(arg);
1920
1921 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1922 break;
1923 arg = skipwhite(n);
1924 }
1925 }
1926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 xp->xp_pattern = arg;
1928}
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001931 * Return TRUE if "pat" matches "text".
1932 * Does not use 'cpo' and always uses 'magic'.
1933 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001934 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001935pattern_match(char_u *pat, char_u *text, int ic)
1936{
1937 int matches = FALSE;
1938 char_u *save_cpo;
1939 regmatch_T regmatch;
1940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001941 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001942 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001943 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001944 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1945 if (regmatch.regprog != NULL)
1946 {
1947 regmatch.rm_ic = ic;
1948 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1949 vim_regfree(regmatch.regprog);
1950 }
1951 p_cpo = save_cpo;
1952 return matches;
1953}
1954
1955/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001956 * Handle a name followed by "(". Both for just "name(arg)" and for
1957 * "expr->name(arg)".
1958 * Returns OK or FAIL.
1959 */
1960 static int
1961eval_func(
1962 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001963 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001964 char_u *name,
1965 int name_len,
1966 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001967 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001968 typval_T *basetv) // "expr" for "expr->name(arg)"
1969{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001970 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001971 char_u *s = name;
1972 int len = name_len;
1973 partial_T *partial;
1974 int ret = OK;
1975
1976 if (!evaluate)
1977 check_vars(s, len);
1978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001979 // If "s" is the name of a variable of type VAR_FUNC
1980 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001981 s = deref_func_name(s, &len, &partial, !evaluate);
1982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001983 // Need to make a copy, in case evaluating the arguments makes
1984 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001985 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001986 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001987 ret = FAIL;
1988 else
1989 {
1990 funcexe_T funcexe;
1991
1992 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001993 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001994 funcexe.firstline = curwin->w_cursor.lnum;
1995 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001996 funcexe.evaluate = evaluate;
1997 funcexe.partial = partial;
1998 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001999 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002000 }
2001 vim_free(s);
2002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002003 // If evaluate is FALSE rettv->v_type was not set in
2004 // get_func_tv, but it's needed in handle_subscript() to parse
2005 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002006 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2007 {
2008 rettv->vval.v_string = NULL;
2009 rettv->v_type = VAR_FUNC;
2010 }
2011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002012 // Stop the expression evaluation when immediately
2013 // aborting on error, or when an interrupt occurred or
2014 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002015 if (evaluate && aborting())
2016 {
2017 if (ret == OK)
2018 clear_tv(rettv);
2019 ret = FAIL;
2020 }
2021 return ret;
2022}
2023
2024/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002025 * Get the next line source line without advancing. But do skip over comment
2026 * lines.
2027 */
2028 static char_u *
2029getline_peek_skip_comments(evalarg_T *evalarg)
2030{
2031 for (;;)
2032 {
2033 char_u *next = getline_peek(evalarg->eval_getline,
2034 evalarg->eval_cookie);
2035 char_u *p;
2036
2037 if (next == NULL)
2038 break;
2039 p = skipwhite(next);
2040 if (*p != NUL && !vim9_comment_start(p))
2041 return next;
2042 (void)eval_next_line(evalarg);
2043 }
2044 return NULL;
2045}
2046
2047/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002048 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2049 * comment) and there is a next line, return the next line (skipping blanks)
2050 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002051 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2052 * "arg" must point somewhere inside a line, not at the start.
2053 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002054 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002055eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2056{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002057 char_u *p = skipwhite(arg);
2058
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002059 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002060 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002061 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002062 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002063 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002064 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002065 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002066
2067 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002068 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002069 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002070 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002071
Bram Moolenaar9d489562020-07-30 20:08:50 +02002072 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002073 {
2074 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002075 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002076 }
2077 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002078 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002079}
2080
2081/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002082 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002083 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002084 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002085eval_next_line(evalarg_T *evalarg)
2086{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002087 garray_T *gap = &evalarg->eval_ga;
2088 char_u *line;
2089
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002090 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002091 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2092 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002093 else
2094 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002095 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002096 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2097 {
2098 // Going to concatenate the lines after parsing.
2099 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2100 ++gap->ga_len;
2101 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002102 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002103 {
2104 vim_free(evalarg->eval_tofree);
2105 evalarg->eval_tofree = line;
2106 }
2107 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002108}
2109
Bram Moolenaare6b53242020-07-01 17:28:33 +02002110/*
2111 * Call eval_next_non_blank() and get the next line if needed.
2112 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002113 char_u *
2114skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2115{
2116 int getnext;
2117 char_u *p = skipwhite(arg);
2118
Bram Moolenaar962d7212020-07-04 14:15:00 +02002119 if (evalarg == NULL)
2120 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002121 eval_next_non_blank(p, evalarg, &getnext);
2122 if (getnext)
2123 return eval_next_line(evalarg);
2124 return p;
2125}
2126
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002127/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002128 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002129 */
2130 void
2131clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2132{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002133 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002134 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002135 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002136 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002137 if (eap != NULL)
2138 {
2139 // We may need to keep the original command line, e.g. for
2140 // ":let" it has the variable names. But we may also need the
2141 // new one, "nextcmd" points into it. Keep both.
2142 vim_free(eap->cmdline_tofree);
2143 eap->cmdline_tofree = *eap->cmdlinep;
2144 *eap->cmdlinep = evalarg->eval_tofree;
2145 }
2146 else
2147 vim_free(evalarg->eval_tofree);
2148 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002149 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002150
2151 vim_free(evalarg->eval_tofree_lambda);
2152 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002153 }
2154}
2155
2156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2160 */
2161
2162/*
2163 * Handle zero level expression.
2164 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002166 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002167 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 * Return OK or FAIL.
2169 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002170 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002171eval0(
2172 char_u *arg,
2173 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002174 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
2177 int ret;
2178 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002179 int did_emsg_before = did_emsg;
2180 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002181 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
2183 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002184 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002185 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002186
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002187 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
2189 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 /*
2192 * Report the invalid expression unless the expression evaluation has
2193 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002194 * exception, or we already gave a more specific error.
2195 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002197 if (!aborting()
2198 && did_emsg == did_emsg_before
2199 && called_emsg == called_emsg_before
2200 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002201 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002202
2203 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002204 // a next command to avoid more errors, unless "|" is following, which
2205 // could only be a command separator.
2206 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2207 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002208 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002210
2211 if (eap != NULL)
2212 eap->nextcmd = check_nextcmd(p);
2213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 return ret;
2215}
2216
2217/*
2218 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002219 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002220 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 *
2222 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002223 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002225 * Note: "rettv.v_lock" is not set.
2226 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 * Return OK or FAIL.
2228 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002229 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002230eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002232 char_u *p;
2233 int getnext;
2234
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002235 CLEAR_POINTER(rettv);
2236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 /*
2238 * Get the first variable.
2239 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002240 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 return FAIL;
2242
Bram Moolenaar793648f2020-06-26 21:28:25 +02002243 p = eval_next_non_blank(*arg, evalarg, &getnext);
2244 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002246 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002247 int result;
2248 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002249 evalarg_T *evalarg_used = evalarg;
2250 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002251 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002252 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002253 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002254
2255 if (evalarg == NULL)
2256 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002257 CLEAR_FIELD(local_evalarg);
2258 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002259 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002260 orig_flags = evalarg_used->eval_flags;
2261 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002262
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002263 if (getnext)
2264 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002265 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002266 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002267 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002268 {
2269 error_white_both(p, 1);
2270 clear_tv(rettv);
2271 return FAIL;
2272 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002273 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002274 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002277 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002279 int error = FALSE;
2280
Bram Moolenaar13106602020-10-04 16:06:05 +02002281 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002282 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002283 else if (vim9script)
2284 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002285 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002287 if (error || !op_falsy || !result)
2288 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002289 if (error)
2290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292
2293 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002294 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002296 if (op_falsy)
2297 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002298 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002299 {
2300 error_white_both(p, 1);
2301 clear_tv(rettv);
2302 return FAIL;
2303 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002304 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002305 evalarg_used->eval_flags = (op_falsy ? !result : result)
2306 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2307 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002308 {
2309 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002311 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002312 if (!op_falsy || !result)
2313 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002315 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002317 /*
2318 * Check for the ":".
2319 */
2320 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2321 if (*p != ':')
2322 {
2323 emsg(_(e_missing_colon));
2324 if (evaluate && result)
2325 clear_tv(rettv);
2326 evalarg_used->eval_flags = orig_flags;
2327 return FAIL;
2328 }
2329 if (getnext)
2330 *arg = eval_next_line(evalarg_used);
2331 else
2332 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002333 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002334 {
2335 error_white_both(p, 1);
2336 clear_tv(rettv);
2337 evalarg_used->eval_flags = orig_flags;
2338 return FAIL;
2339 }
2340 *arg = p;
2341 }
2342
2343 /*
2344 * Get the third variable. Recursive!
2345 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002346 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002347 {
2348 error_white_both(p, 1);
2349 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002350 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002351 return FAIL;
2352 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002353 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2354 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002355 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002356 if (eval1(arg, &var2, evalarg_used) == FAIL)
2357 {
2358 if (evaluate && result)
2359 clear_tv(rettv);
2360 evalarg_used->eval_flags = orig_flags;
2361 return FAIL;
2362 }
2363 if (evaluate && !result)
2364 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002366
2367 if (evalarg == NULL)
2368 clear_evalarg(&local_evalarg, NULL);
2369 else
2370 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
2372
2373 return OK;
2374}
2375
2376/*
2377 * Handle first level expression:
2378 * expr2 || expr2 || expr2 logical OR
2379 *
2380 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002381 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 *
2383 * Return OK or FAIL.
2384 */
2385 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002386eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002388 char_u *p;
2389 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
2391 /*
2392 * Get the first variable.
2393 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002394 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 return FAIL;
2396
2397 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002398 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002400 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002401 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002403 evalarg_T *evalarg_used = evalarg;
2404 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002405 int evaluate;
2406 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002407 long result = FALSE;
2408 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002409 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002410 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002411
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002412 if (evalarg == NULL)
2413 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002414 CLEAR_FIELD(local_evalarg);
2415 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002416 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002417 orig_flags = evalarg_used->eval_flags;
2418 evaluate = orig_flags & EVAL_EVALUATE;
2419 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002420 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002421 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002422 result = tv_get_bool_chk(rettv, &error);
2423 else if (tv_get_number_chk(rettv, &error) != 0)
2424 result = TRUE;
2425 clear_tv(rettv);
2426 if (error)
2427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429
2430 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002431 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002433 while (p[0] == '|' && p[1] == '|')
2434 {
2435 if (getnext)
2436 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002437 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002438 {
2439 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2440 {
2441 error_white_both(p, 2);
2442 clear_tv(rettv);
2443 return FAIL;
2444 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002445 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002446 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002447
2448 /*
2449 * Get the second variable.
2450 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002451 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2452 {
2453 error_white_both(p, 2);
2454 clear_tv(rettv);
2455 return FAIL;
2456 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002457 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2458 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002459 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002460 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002462
2463 /*
2464 * Compute the result.
2465 */
2466 if (evaluate && !result)
2467 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002468 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002469 result = tv_get_bool_chk(&var2, &error);
2470 else if (tv_get_number_chk(&var2, &error) != 0)
2471 result = TRUE;
2472 clear_tv(&var2);
2473 if (error)
2474 return FAIL;
2475 }
2476 if (evaluate)
2477 {
2478 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002479 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002480 rettv->v_type = VAR_BOOL;
2481 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002482 }
2483 else
2484 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002485 rettv->v_type = VAR_NUMBER;
2486 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002487 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002488 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002489
2490 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002492
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002493 if (evalarg == NULL)
2494 clear_evalarg(&local_evalarg, NULL);
2495 else
2496 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 }
2498
2499 return OK;
2500}
2501
2502/*
2503 * Handle second level expression:
2504 * expr3 && expr3 && expr3 logical AND
2505 *
2506 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002507 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 *
2509 * Return OK or FAIL.
2510 */
2511 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002512eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002514 char_u *p;
2515 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
2517 /*
2518 * Get the first variable.
2519 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002520 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 return FAIL;
2522
2523 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002524 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002526 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002527 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002529 evalarg_T *evalarg_used = evalarg;
2530 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002531 int orig_flags;
2532 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002533 long result = TRUE;
2534 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002535 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002536 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002537
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002538 if (evalarg == NULL)
2539 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002540 CLEAR_FIELD(local_evalarg);
2541 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002542 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002543 orig_flags = evalarg_used->eval_flags;
2544 evaluate = orig_flags & EVAL_EVALUATE;
2545 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002546 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002547 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002548 result = tv_get_bool_chk(rettv, &error);
2549 else if (tv_get_number_chk(rettv, &error) == 0)
2550 result = FALSE;
2551 clear_tv(rettv);
2552 if (error)
2553 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
2555
2556 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002557 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002559 while (p[0] == '&' && p[1] == '&')
2560 {
2561 if (getnext)
2562 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002563 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002564 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002565 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002566 {
2567 error_white_both(p, 2);
2568 clear_tv(rettv);
2569 return FAIL;
2570 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002571 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002572 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002573
2574 /*
2575 * Get the second variable.
2576 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002577 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2578 {
2579 error_white_both(p, 2);
2580 clear_tv(rettv);
2581 return FAIL;
2582 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002583 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2584 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002585 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002586 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002587 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002588 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002589
2590 /*
2591 * Compute the result.
2592 */
2593 if (evaluate && result)
2594 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002595 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002596 result = tv_get_bool_chk(&var2, &error);
2597 else if (tv_get_number_chk(&var2, &error) == 0)
2598 result = FALSE;
2599 clear_tv(&var2);
2600 if (error)
2601 return FAIL;
2602 }
2603 if (evaluate)
2604 {
2605 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002606 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002607 rettv->v_type = VAR_BOOL;
2608 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002609 }
2610 else
2611 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002612 rettv->v_type = VAR_NUMBER;
2613 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002614 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002615 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002616
2617 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002619
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620 if (evalarg == NULL)
2621 clear_evalarg(&local_evalarg, NULL);
2622 else
2623 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625
2626 return OK;
2627}
2628
2629/*
2630 * Handle third level expression:
2631 * var1 == var2
2632 * var1 =~ var2
2633 * var1 != var2
2634 * var1 !~ var2
2635 * var1 > var2
2636 * var1 >= var2
2637 * var1 < var2
2638 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002639 * var1 is var2
2640 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 *
2642 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002643 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 *
2645 * Return OK or FAIL.
2646 */
2647 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002648eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002651 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002652 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002654 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
2656 /*
2657 * Get the first variable.
2658 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002659 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 return FAIL;
2661
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002662 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002663 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
2665 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002666 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002668 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002670 typval_T var2;
2671 int ic;
2672 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002673 int evaluate = evalarg == NULL
2674 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002675
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002676 if (getnext)
2677 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002678 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2679 {
2680 error_white_both(p, len);
2681 clear_tv(rettv);
2682 return FAIL;
2683 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002684
Bram Moolenaar696ba232020-07-29 21:20:41 +02002685 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2686 {
2687 semsg(_(e_invexpr2), p);
2688 clear_tv(rettv);
2689 return FAIL;
2690 }
2691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002692 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (p[len] == '?')
2694 {
2695 ic = TRUE;
2696 ++len;
2697 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002698 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 else if (p[len] == '#')
2700 {
2701 ic = FALSE;
2702 ++len;
2703 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002704 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002706 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
2708 /*
2709 * Get the second variable.
2710 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002711 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2712 {
2713 error_white_both(p, 1);
2714 clear_tv(rettv);
2715 return FAIL;
2716 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002717 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002718 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002720 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 return FAIL;
2722 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002723 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002724 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002725 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002726
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002727 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002728 {
2729 ret = FAIL;
2730 clear_tv(rettv);
2731 }
2732 else
2733 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002734 clear_tv(&var2);
2735 return ret;
2736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 }
2738
2739 return OK;
2740}
2741
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002742/*
2743 * Make a copy of blob "tv1" and append blob "tv2".
2744 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 void
2746eval_addblob(typval_T *tv1, typval_T *tv2)
2747{
2748 blob_T *b1 = tv1->vval.v_blob;
2749 blob_T *b2 = tv2->vval.v_blob;
2750 blob_T *b = blob_alloc();
2751 int i;
2752
2753 if (b != NULL)
2754 {
2755 for (i = 0; i < blob_len(b1); i++)
2756 ga_append(&b->bv_ga, blob_get(b1, i));
2757 for (i = 0; i < blob_len(b2); i++)
2758 ga_append(&b->bv_ga, blob_get(b2, i));
2759
2760 clear_tv(tv1);
2761 rettv_blob_set(tv1, b);
2762 }
2763}
2764
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002765/*
2766 * Make a copy of list "tv1" and append list "tv2".
2767 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 int
2769eval_addlist(typval_T *tv1, typval_T *tv2)
2770{
2771 typval_T var3;
2772
2773 // concatenate Lists
2774 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2775 {
2776 clear_tv(tv1);
2777 clear_tv(tv2);
2778 return FAIL;
2779 }
2780 clear_tv(tv1);
2781 *tv1 = var3;
2782 return OK;
2783}
2784
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785/*
2786 * Handle fourth level expression:
2787 * + number addition
2788 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002789 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002790 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 *
2792 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002793 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 *
2795 * Return OK or FAIL.
2796 */
2797 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002798eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 /*
2801 * Get the first variable.
2802 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002803 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 return FAIL;
2805
2806 /*
2807 * Repeat computing, until no '+', '-' or '.' is following.
2808 */
2809 for (;;)
2810 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002811 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002812 int getnext;
2813 char_u *p;
2814 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002815 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002816 int concat;
2817 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002818 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002819
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002820 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002821 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002822 p = eval_next_non_blank(*arg, evalarg, &getnext);
2823 op = *p;
2824 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002825 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002827
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002828 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002829 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002830 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002831 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002832 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002833 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002834 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002835 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002836 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002837 clear_tv(rettv);
2838 return FAIL;
2839 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002840 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002841 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002842 if ((op != '+' || (rettv->v_type != VAR_LIST
2843 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002844#ifdef FEAT_FLOAT
2845 && (op == '.' || rettv->v_type != VAR_FLOAT)
2846#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002847 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002848 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002849 int error = FALSE;
2850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002851 // For "list + ...", an illegal use of the first operand as
2852 // a number cannot be determined before evaluating the 2nd
2853 // operand: if this is also a list, all is ok.
2854 // For "something . ...", "something - ..." or "non-list + ...",
2855 // we know that the first operand needs to be a string or number
2856 // without evaluating the 2nd operand. So check before to avoid
2857 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002858 if (op != '.')
2859 tv_get_number_chk(rettv, &error);
2860 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002861 {
2862 clear_tv(rettv);
2863 return FAIL;
2864 }
2865 }
2866
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 /*
2868 * Get the second variable.
2869 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002870 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002871 {
2872 error_white_both(p, oplen);
2873 clear_tv(rettv);
2874 return FAIL;
2875 }
2876 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002877 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002879 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 return FAIL;
2881 }
2882
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002883 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 /*
2886 * Compute the result.
2887 */
2888 if (op == '.')
2889 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002890 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2891 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002892 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002893
Bram Moolenaar2e086612020-08-25 22:37:48 +02002894 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002895 || var2.v_type == VAR_CHANNEL
2896 || var2.v_type == VAR_JOB))
2897 emsg(_(e_inval_string));
2898#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002899 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002900 {
2901 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2902 var2.vval.v_float);
2903 s2 = buf2;
2904 }
2905#endif
2906 else
2907 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002908 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002909 {
2910 clear_tv(rettv);
2911 clear_tv(&var2);
2912 return FAIL;
2913 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002914 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002915 clear_tv(rettv);
2916 rettv->v_type = VAR_STRING;
2917 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002919 else if (op == '+' && rettv->v_type == VAR_BLOB
2920 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002922 else if (op == '+' && rettv->v_type == VAR_LIST
2923 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002924 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002926 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 else
2929 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002930 int error = FALSE;
2931 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002932#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002933 float_T f1 = 0, f2 = 0;
2934
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002935 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002936 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002937 f1 = rettv->vval.v_float;
2938 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002939 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002940 else
2941#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002942 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002943 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002944 if (error)
2945 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002946 // This can only happen for "list + non-list". For
2947 // "non-list + ..." or "something - ...", we returned
2948 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002949 clear_tv(rettv);
2950 return FAIL;
2951 }
2952#ifdef FEAT_FLOAT
2953 if (var2.v_type == VAR_FLOAT)
2954 f1 = n1;
2955#endif
2956 }
2957#ifdef FEAT_FLOAT
2958 if (var2.v_type == VAR_FLOAT)
2959 {
2960 f2 = var2.vval.v_float;
2961 n2 = 0;
2962 }
2963 else
2964#endif
2965 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002966 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002967 if (error)
2968 {
2969 clear_tv(rettv);
2970 clear_tv(&var2);
2971 return FAIL;
2972 }
2973#ifdef FEAT_FLOAT
2974 if (rettv->v_type == VAR_FLOAT)
2975 f2 = n2;
2976#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002979
2980#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002981 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002982 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2983 {
2984 if (op == '+')
2985 f1 = f1 + f2;
2986 else
2987 f1 = f1 - f2;
2988 rettv->v_type = VAR_FLOAT;
2989 rettv->vval.v_float = f1;
2990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002992#endif
2993 {
2994 if (op == '+')
2995 n1 = n1 + n2;
2996 else
2997 n1 = n1 - n2;
2998 rettv->v_type = VAR_NUMBER;
2999 rettv->vval.v_number = n1;
3000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003002 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004 }
3005 return OK;
3006}
3007
3008/*
3009 * Handle fifth level expression:
3010 * * number multiplication
3011 * / number division
3012 * % number modulo
3013 *
3014 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003015 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 *
3017 * Return OK or FAIL.
3018 */
3019 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003020eval6(
3021 char_u **arg,
3022 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003023 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003024 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003026#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003027 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030 /*
3031 * Get the first variable.
3032 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003033 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 return FAIL;
3035
3036 /*
3037 * Repeat computing, until no '*', '/' or '%' is following.
3038 */
3039 for (;;)
3040 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003041 int evaluate;
3042 int getnext;
3043 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003044 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003045 int op;
3046 varnumber_T n1, n2;
3047#ifdef FEAT_FLOAT
3048 float_T f1, f2;
3049#endif
3050 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003051
Bram Moolenaar9d489562020-07-30 20:08:50 +02003052 p = eval_next_non_blank(*arg, evalarg, &getnext);
3053 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003054 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003056
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003057 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003058 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003059 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003060 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003061 {
3062 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3063 {
3064 error_white_both(p, 1);
3065 clear_tv(rettv);
3066 return FAIL;
3067 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003068 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003071#ifdef FEAT_FLOAT
3072 f1 = 0;
3073 f2 = 0;
3074#endif
3075 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003076 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078#ifdef FEAT_FLOAT
3079 if (rettv->v_type == VAR_FLOAT)
3080 {
3081 f1 = rettv->vval.v_float;
3082 use_float = TRUE;
3083 n1 = 0;
3084 }
3085 else
3086#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003087 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003088 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003089 if (error)
3090 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
3092 else
3093 n1 = 0;
3094
3095 /*
3096 * Get the second variable.
3097 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003098 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3099 {
3100 error_white_both(p, 1);
3101 clear_tv(rettv);
3102 return FAIL;
3103 }
3104 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003105 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 return FAIL;
3107
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003108 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003110#ifdef FEAT_FLOAT
3111 if (var2.v_type == VAR_FLOAT)
3112 {
3113 if (!use_float)
3114 {
3115 f1 = n1;
3116 use_float = TRUE;
3117 }
3118 f2 = var2.vval.v_float;
3119 n2 = 0;
3120 }
3121 else
3122#endif
3123 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003124 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 clear_tv(&var2);
3126 if (error)
3127 return FAIL;
3128#ifdef FEAT_FLOAT
3129 if (use_float)
3130 f2 = n2;
3131#endif
3132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
3134 /*
3135 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003138#ifdef FEAT_FLOAT
3139 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141 if (op == '*')
3142 f1 = f1 * f2;
3143 else if (op == '/')
3144 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003145# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003146 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003147 if (f2 == 0.0)
3148 {
3149 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003150 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003151 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003152 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003153 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003154 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003155 }
3156 else
3157 f1 = f1 / f2;
3158# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003159 // We rely on the floating point library to handle divide
3160 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003161 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003162# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003167 return FAIL;
3168 }
3169 rettv->v_type = VAR_FLOAT;
3170 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
3172 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003175 if (op == '*')
3176 n1 = n1 * n2;
3177 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003178 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003180 n1 = num_modulus(n1, n2);
3181
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182 rettv->v_type = VAR_NUMBER;
3183 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186 }
3187
3188 return OK;
3189}
3190
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003191 int
3192eval_leader(char_u **arg, int vim9)
3193{
3194 char_u *s = *arg;
3195 char_u *p = *arg;
3196
3197 while (*p == '!' || *p == '-' || *p == '+')
3198 {
3199 char_u *n = skipwhite(p + 1);
3200
3201 // ++, --, -+ and +- are not accepted in Vim9 script
3202 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3203 {
3204 semsg(_(e_invexpr2), s);
3205 return FAIL;
3206 }
3207 p = n;
3208 }
3209 *arg = p;
3210 return OK;
3211}
3212
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213/*
3214 * Handle sixth level expression:
3215 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003216 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003217 * "string" string constant
3218 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 * &option-name option value
3220 * @r register contents
3221 * identifier variable value
3222 * function() function call
3223 * $VAR environment variable
3224 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003225 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003227 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003228 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 *
3230 * Also handle:
3231 * ! in front logical NOT
3232 * - in front unary minus
3233 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003234 * trailing [] subscript in String or List
3235 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003236 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 *
3238 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003239 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 *
3241 * Return OK or FAIL.
3242 */
3243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003244eval7(
3245 char_u **arg,
3246 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003247 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003250 int evaluate = evalarg != NULL
3251 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 int len;
3253 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 char_u *start_leader, *end_leader;
3255 int ret = OK;
3256 char_u *alias;
3257
3258 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003259 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003260 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003262 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263
3264 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003265 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 */
3267 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003268 if (eval_leader(arg, in_vim9script()) == FAIL)
3269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 end_leader = *arg;
3271
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003272 if (**arg == '.' && (!isdigit(*(*arg + 1))
3273#ifdef FEAT_FLOAT
3274 || current_sctx.sc_version < 2
3275#endif
3276 ))
3277 {
3278 semsg(_(e_invexpr2), *arg);
3279 ++*arg;
3280 return FAIL;
3281 }
3282
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 switch (**arg)
3284 {
3285 /*
3286 * Number constant.
3287 */
3288 case '0':
3289 case '1':
3290 case '2':
3291 case '3':
3292 case '4':
3293 case '5':
3294 case '6':
3295 case '7':
3296 case '8':
3297 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003298 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003299
3300 // Apply prefixed "-" and "+" now. Matters especially when
3301 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003302 if (ret == OK && evaluate && end_leader > start_leader
3303 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003304 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 break;
3306
3307 /*
3308 * String constant: "string".
3309 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003310 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 break;
3312
3313 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003314 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003316 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003317 break;
3318
3319 /*
3320 * List: [expr, expr]
3321 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003322 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 break;
3324
3325 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003326 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003327 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003328 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003329 {
3330 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003331 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003332 }
3333 else
3334 ret = NOTDONE;
3335 break;
3336
3337 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003338 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003339 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003340 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003341 case '{': if (in_vim9script())
3342 ret = NOTDONE;
3343 else
3344 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003345 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003346 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003347 break;
3348
3349 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003350 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003352 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 break;
3354
3355 /*
3356 * Environment variable: $VAR.
3357 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003358 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 break;
3360
3361 /*
3362 * Register contents: @r.
3363 */
3364 case '@': ++*arg;
3365 if (evaluate)
3366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003367 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003368 rettv->vval.v_string = get_reg_contents(**arg,
3369 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 }
3371 if (**arg != NUL)
3372 ++*arg;
3373 break;
3374
3375 /*
3376 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003377 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003379 case '(': ret = NOTDONE;
3380 if (in_vim9script())
3381 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
3382 if (ret == NOTDONE)
3383 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003384 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003385 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003386
Bram Moolenaar9215f012020-06-27 21:18:00 +02003387 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003388 if (**arg == ')')
3389 ++*arg;
3390 else if (ret == OK)
3391 {
3392 emsg(_(e_missing_close));
3393 clear_tv(rettv);
3394 ret = FAIL;
3395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
3397 break;
3398
Bram Moolenaar8c711452005-01-14 21:53:12 +00003399 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 break;
3401 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003402
3403 if (ret == NOTDONE)
3404 {
3405 /*
3406 * Must be a variable or function name.
3407 * Can also be a curly-braces kind of name: {expr}.
3408 */
3409 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003410 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003411 if (alias != NULL)
3412 s = alias;
3413
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003414 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003415 ret = FAIL;
3416 else
3417 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003418 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3419
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003420 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3421 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003422 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003423 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003424 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003425 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003426 else if (flags & EVAL_CONSTANT)
3427 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003428 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003429 {
3430 // get the value of "true", "false" or a variable
3431 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3432 {
3433 rettv->v_type = VAR_BOOL;
3434 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003435 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003436 }
3437 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003438 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003439 {
3440 rettv->v_type = VAR_BOOL;
3441 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003442 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003443 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003444 else if (len == 4 && in_vim9script()
3445 && STRNCMP(s, "null", 4) == 0)
3446 {
3447 rettv->v_type = VAR_SPECIAL;
3448 rettv->vval.v_number = VVAL_NULL;
3449 ret = OK;
3450 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003451 else
3452 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3453 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003454 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003455 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003456 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003457 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003458 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003459 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003460 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003461 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003462 }
3463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003464 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3465 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003466 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003467 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
3469 /*
3470 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3471 */
3472 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003473 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003474 return ret;
3475}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003476
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003477/*
3478 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003479 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003480 * Adjusts "end_leaderp" until it is at "start_leader".
3481 */
3482 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003483eval7_leader(
3484 typval_T *rettv,
3485 int numeric_only,
3486 char_u *start_leader,
3487 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003488{
3489 char_u *end_leader = *end_leaderp;
3490 int ret = OK;
3491 int error = FALSE;
3492 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003493 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003494#ifdef FEAT_FLOAT
3495 float_T f = 0.0;
3496
3497 if (rettv->v_type == VAR_FLOAT)
3498 f = rettv->vval.v_float;
3499 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003500#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003501 {
3502 while (VIM_ISWHITE(end_leader[-1]))
3503 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003504 if (in_vim9script() && end_leader[-1] == '!')
3505 val = tv2bool(rettv);
3506 else
3507 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003508 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003509 if (error)
3510 {
3511 clear_tv(rettv);
3512 ret = FAIL;
3513 }
3514 else
3515 {
3516 while (end_leader > start_leader)
3517 {
3518 --end_leader;
3519 if (*end_leader == '!')
3520 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003521 if (numeric_only)
3522 {
3523 ++end_leader;
3524 break;
3525 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003526#ifdef FEAT_FLOAT
3527 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003528 {
3529 if (in_vim9script())
3530 {
3531 rettv->v_type = VAR_BOOL;
3532 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3533 }
3534 else
3535 f = !f;
3536 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003537 else
3538#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003539 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003540 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003541 type = VAR_BOOL;
3542 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003543 }
3544 else if (*end_leader == '-')
3545 {
3546#ifdef FEAT_FLOAT
3547 if (rettv->v_type == VAR_FLOAT)
3548 f = -f;
3549 else
3550#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003551 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003552 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003553 type = VAR_NUMBER;
3554 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003555 }
3556 }
3557#ifdef FEAT_FLOAT
3558 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003560 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003561 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003563 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003564#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003565 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003566 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003567 if (in_vim9script())
3568 rettv->v_type = type;
3569 else
3570 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003571 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003574 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 return ret;
3576}
3577
3578/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003579 * Call the function referred to in "rettv".
3580 */
3581 static int
3582call_func_rettv(
3583 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003584 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003585 typval_T *rettv,
3586 int evaluate,
3587 dict_T *selfdict,
3588 typval_T *basetv)
3589{
3590 partial_T *pt = NULL;
3591 funcexe_T funcexe;
3592 typval_T functv;
3593 char_u *s;
3594 int ret;
3595
3596 // need to copy the funcref so that we can clear rettv
3597 if (evaluate)
3598 {
3599 functv = *rettv;
3600 rettv->v_type = VAR_UNKNOWN;
3601
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003602 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003603 if (functv.v_type == VAR_PARTIAL)
3604 {
3605 pt = functv.vval.v_partial;
3606 s = partial_name(pt);
3607 }
3608 else
3609 s = functv.vval.v_string;
3610 }
3611 else
3612 s = (char_u *)"";
3613
Bram Moolenaara80faa82020-04-12 19:37:17 +02003614 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003615 funcexe.firstline = curwin->w_cursor.lnum;
3616 funcexe.lastline = curwin->w_cursor.lnum;
3617 funcexe.evaluate = evaluate;
3618 funcexe.partial = pt;
3619 funcexe.selfdict = selfdict;
3620 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003621 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003623 // Clear the funcref afterwards, so that deleting it while
3624 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003625 if (evaluate)
3626 clear_tv(&functv);
3627
3628 return ret;
3629}
3630
3631/*
3632 * Evaluate "->method()".
3633 * "*arg" points to the '-'.
3634 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3635 */
3636 static int
3637eval_lambda(
3638 char_u **arg,
3639 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003640 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003641 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003642{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003643 int evaluate = evalarg != NULL
3644 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003645 typval_T base = *rettv;
3646 int ret;
3647
3648 // Skip over the ->.
3649 *arg += 2;
3650 rettv->v_type = VAR_UNKNOWN;
3651
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003652 if (**arg == '{')
3653 {
3654 // ->{lambda}()
3655 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3656 }
3657 else
3658 {
3659 // ->(lambda)()
3660 ++*arg;
3661 ret = eval1(arg, rettv, evalarg);
3662 *arg = skipwhite_and_linebreak(*arg, evalarg);
3663 if (**arg != ')')
3664 {
3665 emsg(_(e_missing_close));
3666 ret = FAIL;
3667 }
3668 ++*arg;
3669 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003670 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003671 return FAIL;
3672 else if (**arg != '(')
3673 {
3674 if (verbose)
3675 {
3676 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003677 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003678 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003680 }
3681 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003682 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003683 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003684 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003685 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003686
3687 // Clear the funcref afterwards, so that deleting it while
3688 // evaluating the arguments is possible (see test55).
3689 if (evaluate)
3690 clear_tv(&base);
3691
3692 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003693}
3694
3695/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003696 * Evaluate "->method()".
3697 * "*arg" points to the '-'.
3698 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3699 */
3700 static int
3701eval_method(
3702 char_u **arg,
3703 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003704 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003705 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003706{
3707 char_u *name;
3708 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003709 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003710 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003711 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003712 int evaluate = evalarg != NULL
3713 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003714
3715 // Skip over the ->.
3716 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003717 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003718
Bram Moolenaarac92e252019-08-03 21:58:38 +02003719 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003720 len = get_name_len(arg, &alias, evaluate, TRUE);
3721 if (alias != NULL)
3722 name = alias;
3723
3724 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003725 {
3726 if (verbose)
3727 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003728 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003729 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003730 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003731 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003732 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003733 if (**arg != '(')
3734 {
3735 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003737 ret = FAIL;
3738 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003739 else if (VIM_ISWHITE((*arg)[-1]))
3740 {
3741 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003742 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003743 ret = FAIL;
3744 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003745 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003746 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003747 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003748 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003749
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003750 // Clear the funcref afterwards, so that deleting it while
3751 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003752 if (evaluate)
3753 clear_tv(&base);
3754
Bram Moolenaarac92e252019-08-03 21:58:38 +02003755 return ret;
3756}
3757
3758/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003759 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3760 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003761 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3762 */
3763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003764eval_index(
3765 char_u **arg,
3766 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003767 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003768 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003769{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003770 int evaluate = evalarg != NULL
3771 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003772 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003773 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003774 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003775 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003776 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003777 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003778
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003779 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3780 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003781
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003782 init_tv(&var1);
3783 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003784 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003785 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003786 /*
3787 * dict.name
3788 */
3789 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003790 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003791 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003792 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003793 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003794 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003795 }
3796 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003797 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003798 /*
3799 * something[idx]
3800 *
3801 * Get the (first) variable from inside the [].
3802 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003803 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003804 if (**arg == ':')
3805 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003806 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003807 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003808 else if (vim9 && **arg == ':')
3809 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003810 semsg(_(e_white_space_required_before_and_after_str_at_str),
3811 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003812 clear_tv(&var1);
3813 return FAIL;
3814 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003815 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003816 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003817 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003818 clear_tv(&var1);
3819 return FAIL;
3820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003821
3822 /*
3823 * Get the second variable from inside the [:].
3824 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003825 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003826 if (**arg == ':')
3827 {
3828 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003829 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003830 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003831 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003832 semsg(_(e_white_space_required_before_and_after_str_at_str),
3833 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003834 if (!empty1)
3835 clear_tv(&var1);
3836 return FAIL;
3837 }
3838 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003839 if (**arg == ']')
3840 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003841 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003843 if (!empty1)
3844 clear_tv(&var1);
3845 return FAIL;
3846 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003847 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003848 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003849 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003850 if (!empty1)
3851 clear_tv(&var1);
3852 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003853 return FAIL;
3854 }
3855 }
3856
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003857 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003858 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003859 if (**arg != ']')
3860 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003861 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003862 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003863 clear_tv(&var1);
3864 if (range)
3865 clear_tv(&var2);
3866 return FAIL;
3867 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003868 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003869 }
3870
3871 if (evaluate)
3872 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003873 int res = eval_index_inner(rettv, range,
3874 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3875 key, keylen, verbose);
3876 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003877 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003878 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003879 clear_tv(&var2);
3880 return res;
3881 }
3882 return OK;
3883}
3884
3885/*
3886 * Check if "rettv" can have an [index] or [sli:ce]
3887 */
3888 int
3889check_can_index(typval_T *rettv, int evaluate, int verbose)
3890{
3891 switch (rettv->v_type)
3892 {
3893 case VAR_FUNC:
3894 case VAR_PARTIAL:
3895 if (verbose)
3896 emsg(_("E695: Cannot index a Funcref"));
3897 return FAIL;
3898 case VAR_FLOAT:
3899#ifdef FEAT_FLOAT
3900 if (verbose)
3901 emsg(_(e_float_as_string));
3902 return FAIL;
3903#endif
3904 case VAR_BOOL:
3905 case VAR_SPECIAL:
3906 case VAR_JOB:
3907 case VAR_CHANNEL:
3908 if (verbose)
3909 emsg(_(e_cannot_index_special_variable));
3910 return FAIL;
3911 case VAR_UNKNOWN:
3912 case VAR_ANY:
3913 case VAR_VOID:
3914 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003915 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003916 emsg(_(e_cannot_index_special_variable));
3917 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003918 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003919 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003920
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003921 case VAR_STRING:
3922 case VAR_LIST:
3923 case VAR_DICT:
3924 case VAR_BLOB:
3925 break;
3926 case VAR_NUMBER:
3927 if (in_vim9script())
3928 emsg(_(e_cannot_index_number));
3929 break;
3930 }
3931 return OK;
3932}
3933
3934/*
3935 * Apply index or range to "rettv".
3936 * "var1" is the first index, NULL for [:expr].
3937 * "var2" is the second index, NULL for [expr] and [expr: ]
3938 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3939 */
3940 int
3941eval_index_inner(
3942 typval_T *rettv,
3943 int is_range,
3944 typval_T *var1,
3945 typval_T *var2,
3946 char_u *key,
3947 int keylen,
3948 int verbose)
3949{
3950 long n1, n2 = 0;
3951 long len;
3952
3953 n1 = 0;
3954 if (var1 != NULL && rettv->v_type != VAR_DICT)
3955 n1 = tv_get_number(var1);
3956
3957 if (is_range)
3958 {
3959 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003960 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003961 if (verbose)
3962 emsg(_(e_cannot_slice_dictionary));
3963 return FAIL;
3964 }
3965 if (var2 == NULL)
3966 n2 = -1;
3967 else
3968 n2 = tv_get_number(var2);
3969 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003970
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003971 switch (rettv->v_type)
3972 {
3973 case VAR_UNKNOWN:
3974 case VAR_ANY:
3975 case VAR_VOID:
3976 case VAR_FUNC:
3977 case VAR_PARTIAL:
3978 case VAR_FLOAT:
3979 case VAR_BOOL:
3980 case VAR_SPECIAL:
3981 case VAR_JOB:
3982 case VAR_CHANNEL:
3983 break; // not evaluating, skipping over subscript
3984
3985 case VAR_NUMBER:
3986 case VAR_STRING:
3987 {
3988 char_u *s = tv_get_string(rettv);
3989
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003990 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003991 if (in_vim9script())
3992 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003993 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003994 s = string_slice(s, n1, n2);
3995 else
3996 s = char_from_string(s, n1);
3997 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003998 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003999 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004000 // The resulting variable is a substring. If the indexes
4001 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004002 if (n1 < 0)
4003 {
4004 n1 = len + n1;
4005 if (n1 < 0)
4006 n1 = 0;
4007 }
4008 if (n2 < 0)
4009 n2 = len + n2;
4010 else if (n2 >= len)
4011 n2 = len;
4012 if (n1 >= len || n2 < 0 || n1 > n2)
4013 s = NULL;
4014 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004015 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004016 }
4017 else
4018 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004019 // The resulting variable is a string of a single
4020 // character. If the index is too big or negative the
4021 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004022 if (n1 >= len || n1 < 0)
4023 s = NULL;
4024 else
4025 s = vim_strnsave(s + n1, 1);
4026 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 clear_tv(rettv);
4028 rettv->v_type = VAR_STRING;
4029 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004030 }
4031 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004032
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004033 case VAR_BLOB:
4034 len = blob_len(rettv->vval.v_blob);
4035 if (is_range)
4036 {
4037 // The resulting variable is a sub-blob. If the indexes
4038 // are out of range the result is empty.
4039 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004040 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004041 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004042 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004043 n1 = 0;
4044 }
4045 if (n2 < 0)
4046 n2 = len + n2;
4047 else if (n2 >= len)
4048 n2 = len - 1;
4049 if (n1 >= len || n2 < 0 || n1 > n2)
4050 {
4051 clear_tv(rettv);
4052 rettv->v_type = VAR_BLOB;
4053 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004054 }
4055 else
4056 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004057 blob_T *blob = blob_alloc();
4058 long i;
4059
4060 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004061 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004062 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004063 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004064 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004065 return FAIL;
4066 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004067 blob->bv_ga.ga_len = n2 - n1 + 1;
4068 for (i = n1; i <= n2; i++)
4069 blob_set(blob, i - n1,
4070 blob_get(rettv->vval.v_blob, i));
4071
4072 clear_tv(rettv);
4073 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004074 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004075 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004076 }
4077 else
4078 {
4079 // The resulting variable is a byte value.
4080 // If the index is too big or negative that is an error.
4081 if (n1 < 0)
4082 n1 = len + n1;
4083 if (n1 < len && n1 >= 0)
4084 {
4085 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004086
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004087 clear_tv(rettv);
4088 rettv->v_type = VAR_NUMBER;
4089 rettv->vval.v_number = v;
4090 }
4091 else
4092 semsg(_(e_blobidx), n1);
4093 }
4094 break;
4095
4096 case VAR_LIST:
4097 if (var1 == NULL)
4098 n1 = 0;
4099 if (var2 == NULL)
4100 n2 = -1;
4101 if (list_slice_or_index(rettv->vval.v_list,
4102 is_range, n1, n2, rettv, verbose) == FAIL)
4103 return FAIL;
4104 break;
4105
4106 case VAR_DICT:
4107 {
4108 dictitem_T *item;
4109 typval_T tmp;
4110
4111 if (key == NULL)
4112 {
4113 key = tv_get_string_chk(var1);
4114 if (key == NULL)
4115 return FAIL;
4116 }
4117
4118 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4119
4120 if (item == NULL && verbose)
4121 semsg(_(e_dictkey), key);
4122 if (item == NULL)
4123 return FAIL;
4124
4125 copy_tv(&item->di_tv, &tmp);
4126 clear_tv(rettv);
4127 *rettv = tmp;
4128 }
4129 break;
4130 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004131 return OK;
4132}
4133
4134/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004135 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004136 */
4137 char_u *
4138partial_name(partial_T *pt)
4139{
4140 if (pt->pt_name != NULL)
4141 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004142 if (pt->pt_func != NULL)
4143 return pt->pt_func->uf_name;
4144 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004145}
4146
Bram Moolenaarddecc252016-04-06 22:59:37 +02004147 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004148partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004149{
4150 int i;
4151
4152 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004153 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004154 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004155 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004156 if (pt->pt_name != NULL)
4157 {
4158 func_unref(pt->pt_name);
4159 vim_free(pt->pt_name);
4160 }
4161 else
4162 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004163
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004164 // Decrease the reference count for the context of a closure. If down
4165 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004166 if (pt->pt_funcstack != NULL)
4167 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004168 --pt->pt_funcstack->fs_refcount;
4169 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004170 }
4171
Bram Moolenaarddecc252016-04-06 22:59:37 +02004172 vim_free(pt);
4173}
4174
4175/*
4176 * Unreference a closure: decrement the reference count and free it when it
4177 * becomes zero.
4178 */
4179 void
4180partial_unref(partial_T *pt)
4181{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004182 if (pt != NULL)
4183 {
4184 if (--pt->pt_refcount <= 0)
4185 partial_free(pt);
4186
4187 // If the reference count goes down to one, the funcstack may be the
4188 // only reference and can be freed if no other partials reference it.
4189 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4190 funcstack_check_refcount(pt->pt_funcstack);
4191 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004192}
4193
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004194/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004195 * Return the next (unique) copy ID.
4196 * Used for serializing nested structures.
4197 */
4198 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004199get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004200{
4201 current_copyID += COPYID_INC;
4202 return current_copyID;
4203}
4204
4205/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004206 * Garbage collection for lists and dictionaries.
4207 *
4208 * We use reference counts to be able to free most items right away when they
4209 * are no longer used. But for composite items it's possible that it becomes
4210 * unused while the reference count is > 0: When there is a recursive
4211 * reference. Example:
4212 * :let l = [1, 2, 3]
4213 * :let d = {9: l}
4214 * :let l[1] = d
4215 *
4216 * Since this is quite unusual we handle this with garbage collection: every
4217 * once in a while find out which lists and dicts are not referenced from any
4218 * variable.
4219 *
4220 * Here is a good reference text about garbage collection (refers to Python
4221 * but it applies to all reference-counting mechanisms):
4222 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004223 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004224
4225/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004226 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004227 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004228 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004229 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004230 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004231garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004232{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004233 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004234 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004235 buf_T *buf;
4236 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004237 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004238 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004239
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004240 if (!testing)
4241 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004242 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004243 want_garbage_collect = FALSE;
4244 may_garbage_collect = FALSE;
4245 garbage_collect_at_exit = FALSE;
4246 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004247
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004248 // The execution stack can grow big, limit the size.
4249 if (exestack.ga_maxlen - exestack.ga_len > 500)
4250 {
4251 size_t new_len;
4252 char_u *pp;
4253 int n;
4254
4255 // Keep 150% of the current size, with a minimum of the growth size.
4256 n = exestack.ga_len / 2;
4257 if (n < exestack.ga_growsize)
4258 n = exestack.ga_growsize;
4259
4260 // Don't make it bigger though.
4261 if (exestack.ga_len + n < exestack.ga_maxlen)
4262 {
4263 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4264 pp = vim_realloc(exestack.ga_data, new_len);
4265 if (pp == NULL)
4266 return FAIL;
4267 exestack.ga_maxlen = exestack.ga_len + n;
4268 exestack.ga_data = pp;
4269 }
4270 }
4271
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004272 // We advance by two because we add one for items referenced through
4273 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004274 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004275
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004276 /*
4277 * 1. Go through all accessible variables and mark all lists and dicts
4278 * with copyID.
4279 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004280
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004281 // Don't free variables in the previous_funccal list unless they are only
4282 // referenced through previous_funccal. This must be first, because if
4283 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004284 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004285
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004286 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004287 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004288
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004289 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004290 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004291 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4292 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004293
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004294 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004295 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004296 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4297 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004298 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004299 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4300 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004301#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004302 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004303 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4304 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004305 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004306 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004307 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4308 NULL, NULL);
4309#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004310
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004311 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004312 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004313 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4314 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004315 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004316 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004317
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004318 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004319 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004320
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004321 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004322 abort = abort || set_ref_in_functions(copyID);
4323
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004324 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004325 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004326
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004327 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004328 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004329
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004330 // callbacks in buffers
4331 abort = abort || set_ref_in_buffers(copyID);
4332
Bram Moolenaar1dced572012-04-05 16:54:08 +02004333#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004334 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004335#endif
4336
Bram Moolenaardb913952012-06-29 12:54:53 +02004337#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004338 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004339#endif
4340
4341#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004342 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004343#endif
4344
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004345#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004346 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004347 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004348#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004349#ifdef FEAT_NETBEANS_INTG
4350 abort = abort || set_ref_in_nb_channel(copyID);
4351#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004352
Bram Moolenaare3188e22016-05-31 21:13:04 +02004353#ifdef FEAT_TIMERS
4354 abort = abort || set_ref_in_timer(copyID);
4355#endif
4356
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004357#ifdef FEAT_QUICKFIX
4358 abort = abort || set_ref_in_quickfix(copyID);
4359#endif
4360
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004361#ifdef FEAT_TERMINAL
4362 abort = abort || set_ref_in_term(copyID);
4363#endif
4364
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004365#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004366 abort = abort || set_ref_in_popups(copyID);
4367#endif
4368
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004369 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004370 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004371 /*
4372 * 2. Free lists and dictionaries that are not referenced.
4373 */
4374 did_free = free_unref_items(copyID);
4375
4376 /*
4377 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004378 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004379 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004380 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004381 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004382 else if (p_verbose > 0)
4383 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004384 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004385 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004386
4387 return did_free;
4388}
4389
4390/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004391 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004392 */
4393 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004394free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004395{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004396 int did_free = FALSE;
4397
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004398 // Let all "free" functions know that we are here. This means no
4399 // dictionaries, lists, channels or jobs are to be freed, because we will
4400 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004401 in_free_unref_items = TRUE;
4402
4403 /*
4404 * PASS 1: free the contents of the items. We don't free the items
4405 * themselves yet, so that it is possible to decrement refcount counters
4406 */
4407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004408 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004409 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004410
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004411 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004412 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004413
4414#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004415 // Go through the list of jobs and free items without the copyID. This
4416 // must happen before doing channels, because jobs refer to channels, but
4417 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004418 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004420 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004421 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4422#endif
4423
4424 /*
4425 * PASS 2: free the items themselves.
4426 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004427 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004428 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004429
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004430#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004431 // Go through the list of jobs and free items without the copyID. This
4432 // must happen before doing channels, because jobs refer to channels, but
4433 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004434 free_unused_jobs(copyID, COPYID_MASK);
4435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004436 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004437 free_unused_channels(copyID, COPYID_MASK);
4438#endif
4439
4440 in_free_unref_items = FALSE;
4441
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004442 return did_free;
4443}
4444
4445/*
4446 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004447 * "list_stack" is used to add lists to be marked. Can be NULL.
4448 *
4449 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004450 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004451 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004452set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004453{
4454 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004455 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004456 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004457 hashtab_T *cur_ht;
4458 ht_stack_T *ht_stack = NULL;
4459 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004460
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004461 cur_ht = ht;
4462 for (;;)
4463 {
4464 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004466 // Mark each item in the hashtab. If the item contains a hashtab
4467 // it is added to ht_stack, if it contains a list it is added to
4468 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004469 todo = (int)cur_ht->ht_used;
4470 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4471 if (!HASHITEM_EMPTY(hi))
4472 {
4473 --todo;
4474 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4475 &ht_stack, list_stack);
4476 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004477 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004478
4479 if (ht_stack == NULL)
4480 break;
4481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004482 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004483 cur_ht = ht_stack->ht;
4484 tempitem = ht_stack;
4485 ht_stack = ht_stack->prev;
4486 free(tempitem);
4487 }
4488
4489 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004490}
4491
4492/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004493 * Mark a dict and its items with "copyID".
4494 * Returns TRUE if setting references failed somehow.
4495 */
4496 int
4497set_ref_in_dict(dict_T *d, int copyID)
4498{
4499 if (d != NULL && d->dv_copyID != copyID)
4500 {
4501 d->dv_copyID = copyID;
4502 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4503 }
4504 return FALSE;
4505}
4506
4507/*
4508 * Mark a list and its items with "copyID".
4509 * Returns TRUE if setting references failed somehow.
4510 */
4511 int
4512set_ref_in_list(list_T *ll, int copyID)
4513{
4514 if (ll != NULL && ll->lv_copyID != copyID)
4515 {
4516 ll->lv_copyID = copyID;
4517 return set_ref_in_list_items(ll, copyID, NULL);
4518 }
4519 return FALSE;
4520}
4521
4522/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004523 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004524 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4525 *
4526 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004527 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004528 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004529set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004530{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004531 listitem_T *li;
4532 int abort = FALSE;
4533 list_T *cur_l;
4534 list_stack_T *list_stack = NULL;
4535 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004536
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004537 cur_l = l;
4538 for (;;)
4539 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004540 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004541 // Mark each item in the list. If the item contains a hashtab
4542 // it is added to ht_stack, if it contains a list it is added to
4543 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004544 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4545 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4546 ht_stack, &list_stack);
4547 if (list_stack == NULL)
4548 break;
4549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004550 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004551 cur_l = list_stack->list;
4552 tempitem = list_stack;
4553 list_stack = list_stack->prev;
4554 free(tempitem);
4555 }
4556
4557 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004558}
4559
4560/*
4561 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004562 * "list_stack" is used to add lists to be marked. Can be NULL.
4563 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4564 *
4565 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004566 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004568set_ref_in_item(
4569 typval_T *tv,
4570 int copyID,
4571 ht_stack_T **ht_stack,
4572 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004573{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004574 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004575
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004576 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004577 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004578 dict_T *dd = tv->vval.v_dict;
4579
Bram Moolenaara03f2332016-02-06 18:09:59 +01004580 if (dd != NULL && dd->dv_copyID != copyID)
4581 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004582 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004583 dd->dv_copyID = copyID;
4584 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004585 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004586 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4587 }
4588 else
4589 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004590 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4591
Bram Moolenaara03f2332016-02-06 18:09:59 +01004592 if (newitem == NULL)
4593 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004594 else
4595 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004596 newitem->ht = &dd->dv_hashtab;
4597 newitem->prev = *ht_stack;
4598 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004599 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004600 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004601 }
4602 }
4603 else if (tv->v_type == VAR_LIST)
4604 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004605 list_T *ll = tv->vval.v_list;
4606
Bram Moolenaara03f2332016-02-06 18:09:59 +01004607 if (ll != NULL && ll->lv_copyID != copyID)
4608 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004609 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004610 ll->lv_copyID = copyID;
4611 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004612 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004613 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004614 }
4615 else
4616 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004617 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4618
Bram Moolenaara03f2332016-02-06 18:09:59 +01004619 if (newitem == NULL)
4620 abort = TRUE;
4621 else
4622 {
4623 newitem->list = ll;
4624 newitem->prev = *list_stack;
4625 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004626 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004627 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004628 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004629 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004630 else if (tv->v_type == VAR_FUNC)
4631 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004632 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004633 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004634 else if (tv->v_type == VAR_PARTIAL)
4635 {
4636 partial_T *pt = tv->vval.v_partial;
4637 int i;
4638
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004639 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004640 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004641 // Didn't see this partial yet.
4642 pt->pt_copyID = copyID;
4643
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004644 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004645
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004646 if (pt->pt_dict != NULL)
4647 {
4648 typval_T dtv;
4649
4650 dtv.v_type = VAR_DICT;
4651 dtv.vval.v_dict = pt->pt_dict;
4652 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4653 }
4654
4655 for (i = 0; i < pt->pt_argc; ++i)
4656 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4657 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004658 if (pt->pt_funcstack != NULL)
4659 {
4660 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4661
4662 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4663 abort = abort || set_ref_in_item(stack + i, copyID,
4664 ht_stack, list_stack);
4665 }
4666
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004667 }
4668 }
4669#ifdef FEAT_JOB_CHANNEL
4670 else if (tv->v_type == VAR_JOB)
4671 {
4672 job_T *job = tv->vval.v_job;
4673 typval_T dtv;
4674
4675 if (job != NULL && job->jv_copyID != copyID)
4676 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004677 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004678 if (job->jv_channel != NULL)
4679 {
4680 dtv.v_type = VAR_CHANNEL;
4681 dtv.vval.v_channel = job->jv_channel;
4682 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4683 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004684 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004685 {
4686 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004687 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004688 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4689 }
4690 }
4691 }
4692 else if (tv->v_type == VAR_CHANNEL)
4693 {
4694 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004695 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004696 typval_T dtv;
4697 jsonq_T *jq;
4698 cbq_T *cq;
4699
4700 if (ch != NULL && ch->ch_copyID != copyID)
4701 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004702 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004703 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004704 {
4705 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4706 jq = jq->jq_next)
4707 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4708 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4709 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004710 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004711 {
4712 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004713 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004714 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4715 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004716 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004717 {
4718 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004719 dtv.vval.v_partial =
4720 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004721 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4722 }
4723 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004724 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004725 {
4726 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004727 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004728 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4729 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004730 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004731 {
4732 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004733 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004734 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4735 }
4736 }
4737 }
4738#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004739 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004740}
4741
Bram Moolenaar8c711452005-01-14 21:53:12 +00004742/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 * Return a string with the string representation of a variable.
4744 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004745 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004746 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004747 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004748 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004749 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004750 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4751 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004752 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004754 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004755echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004756 typval_T *tv,
4757 char_u **tofree,
4758 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004759 int copyID,
4760 int echo_style,
4761 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004762 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004763{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004764 static int recurse = 0;
4765 char_u *r = NULL;
4766
Bram Moolenaar33570922005-01-25 22:26:29 +00004767 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004768 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004769 if (!did_echo_string_emsg)
4770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004771 // Only give this message once for a recursive call to avoid
4772 // flooding the user with errors. And stop iterating over lists
4773 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004774 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004775 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004777 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004778 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004779 }
4780 ++recurse;
4781
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 switch (tv->v_type)
4783 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004784 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004785 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004786 {
4787 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004788 r = tv->vval.v_string;
4789 if (r == NULL)
4790 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004791 }
4792 else
4793 {
4794 *tofree = string_quote(tv->vval.v_string, FALSE);
4795 r = *tofree;
4796 }
4797 break;
4798
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004799 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004800 if (echo_style)
4801 {
4802 *tofree = NULL;
4803 r = tv->vval.v_string;
4804 }
4805 else
4806 {
4807 *tofree = string_quote(tv->vval.v_string, TRUE);
4808 r = *tofree;
4809 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004810 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004811
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004812 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004813 {
4814 partial_T *pt = tv->vval.v_partial;
4815 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004816 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004817 garray_T ga;
4818 int i;
4819 char_u *tf;
4820
4821 ga_init2(&ga, 1, 100);
4822 ga_concat(&ga, (char_u *)"function(");
4823 if (fname != NULL)
4824 {
4825 ga_concat(&ga, fname);
4826 vim_free(fname);
4827 }
4828 if (pt != NULL && pt->pt_argc > 0)
4829 {
4830 ga_concat(&ga, (char_u *)", [");
4831 for (i = 0; i < pt->pt_argc; ++i)
4832 {
4833 if (i > 0)
4834 ga_concat(&ga, (char_u *)", ");
4835 ga_concat(&ga,
4836 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4837 vim_free(tf);
4838 }
4839 ga_concat(&ga, (char_u *)"]");
4840 }
4841 if (pt != NULL && pt->pt_dict != NULL)
4842 {
4843 typval_T dtv;
4844
4845 ga_concat(&ga, (char_u *)", ");
4846 dtv.v_type = VAR_DICT;
4847 dtv.vval.v_dict = pt->pt_dict;
4848 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4849 vim_free(tf);
4850 }
4851 ga_concat(&ga, (char_u *)")");
4852
4853 *tofree = ga.ga_data;
4854 r = *tofree;
4855 break;
4856 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004857
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004858 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004859 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004860 break;
4861
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004862 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004863 if (tv->vval.v_list == NULL)
4864 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004865 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004866 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004867 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004868 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004869 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4870 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004871 {
4872 *tofree = NULL;
4873 r = (char_u *)"[...]";
4874 }
4875 else
4876 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004877 int old_copyID = tv->vval.v_list->lv_copyID;
4878
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004879 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004880 *tofree = list2string(tv, copyID, restore_copyID);
4881 if (restore_copyID)
4882 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004883 r = *tofree;
4884 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004885 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004886
Bram Moolenaar8c711452005-01-14 21:53:12 +00004887 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004888 if (tv->vval.v_dict == NULL)
4889 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004890 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004891 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004892 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004893 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004894 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4895 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004896 {
4897 *tofree = NULL;
4898 r = (char_u *)"{...}";
4899 }
4900 else
4901 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004902 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004903
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004904 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004905 *tofree = dict2string(tv, copyID, restore_copyID);
4906 if (restore_copyID)
4907 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004908 r = *tofree;
4909 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004910 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004911
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004913 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004914 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004916 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004917 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004918 break;
4919
Bram Moolenaar835dc632016-02-07 14:27:38 +01004920 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004921 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004922 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004923 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004924 if (composite_val)
4925 {
4926 *tofree = string_quote(r, FALSE);
4927 r = *tofree;
4928 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004929 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004930
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004932#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004933 *tofree = NULL;
4934 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4935 r = numbuf;
4936 break;
4937#endif
4938
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004939 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004940 case VAR_SPECIAL:
4941 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004942 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004943 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004944 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004945
Bram Moolenaar8502c702014-06-17 12:51:16 +02004946 if (--recurse == 0)
4947 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004948 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004949}
4950
4951/*
4952 * Return a string with the string representation of a variable.
4953 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4954 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004955 * Does not put quotes around strings, as ":echo" displays values.
4956 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4957 * May return NULL.
4958 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004959 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004960echo_string(
4961 typval_T *tv,
4962 char_u **tofree,
4963 char_u *numbuf,
4964 int copyID)
4965{
4966 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4967}
4968
4969/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004970 * Return string "str" in ' quotes, doubling ' characters.
4971 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004973 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004974 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004975string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004976{
Bram Moolenaar33570922005-01-25 22:26:29 +00004977 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004978 char_u *p, *r, *s;
4979
Bram Moolenaar33570922005-01-25 22:26:29 +00004980 len = (function ? 13 : 3);
4981 if (str != NULL)
4982 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004983 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004984 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004985 if (*p == '\'')
4986 ++len;
4987 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004988 s = r = alloc(len);
4989 if (r != NULL)
4990 {
4991 if (function)
4992 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004994 r += 10;
4995 }
4996 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004997 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004998 if (str != NULL)
4999 for (p = str; *p != NUL; )
5000 {
5001 if (*p == '\'')
5002 *r++ = '\'';
5003 MB_COPY_CHAR(p, r);
5004 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005005 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005006 if (function)
5007 *r++ = ')';
5008 *r++ = NUL;
5009 }
5010 return s;
5011}
5012
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005013#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014/*
5015 * Convert the string "text" to a floating point number.
5016 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5017 * this always uses a decimal point.
5018 * Returns the length of the text that was consumed.
5019 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005020 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005021string2float(
5022 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005023 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005024{
5025 char *s = (char *)text;
5026 float_T f;
5027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005028 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005029 if (STRNICMP(text, "inf", 3) == 0)
5030 {
5031 *value = INFINITY;
5032 return 3;
5033 }
5034 if (STRNICMP(text, "-inf", 3) == 0)
5035 {
5036 *value = -INFINITY;
5037 return 4;
5038 }
5039 if (STRNICMP(text, "nan", 3) == 0)
5040 {
5041 *value = NAN;
5042 return 3;
5043 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044 f = strtod(s, &s);
5045 *value = f;
5046 return (int)((char_u *)s - text);
5047}
5048#endif
5049
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005052 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005054 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005055var2fpos(
5056 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005057 int dollar_lnum, // TRUE when $ is last line
5058 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005060 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005062 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005064 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005065 if (varp->v_type == VAR_LIST)
5066 {
5067 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005068 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005069 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005070 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005071
5072 l = varp->vval.v_list;
5073 if (l == NULL)
5074 return NULL;
5075
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005076 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005077 pos.lnum = list_find_nr(l, 0L, &error);
5078 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005079 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005080 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005081
Bram Moolenaarec65d772020-08-20 22:29:12 +02005082 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005083 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005084 li = list_find(l, 1L);
5085 if (li != NULL && li->li_tv.v_type == VAR_STRING
5086 && li->li_tv.vval.v_string != NULL
5087 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005088 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005089 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005090 }
5091 else
5092 {
5093 pos.col = list_find_nr(l, 1L, &error);
5094 if (error)
5095 return NULL;
5096 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005097
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005098 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005099 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005100 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005101 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005103 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005104 pos.coladd = list_find_nr(l, 2L, &error);
5105 if (error)
5106 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005107
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005108 return &pos;
5109 }
5110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005111 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005112 if (name == NULL)
5113 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005114 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005117 {
5118 if (VIsual_active)
5119 return &VIsual;
5120 return &curwin->w_cursor;
5121 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005122 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005124 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5126 return NULL;
5127 return pp;
5128 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005129
Bram Moolenaara5525202006-03-02 22:52:09 +00005130 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005131
Bram Moolenaar477933c2007-07-17 14:32:23 +00005132 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005133 {
5134 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005135 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005136 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005137 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005138 // In silent Ex mode topline is zero, but that's not a valid line
5139 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005140 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005141 return &pos;
5142 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005143 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005144 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005145 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005146 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005147 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005148 return &pos;
5149 }
5150 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005151 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005153 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
5155 pos.lnum = curbuf->b_ml.ml_line_count;
5156 pos.col = 0;
5157 }
5158 else
5159 {
5160 pos.lnum = curwin->w_cursor.lnum;
5161 pos.col = (colnr_T)STRLEN(ml_get_curline());
5162 }
5163 return &pos;
5164 }
5165 return NULL;
5166}
5167
5168/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005169 * Convert list in "arg" into a position and optional file number.
5170 * When "fnump" is NULL there is no file number, only 3 items.
5171 * Note that the column is passed on as-is, the caller may want to decrement
5172 * it to use 1 for the first column.
5173 * Return FAIL when conversion is not possible, doesn't check the position for
5174 * validity.
5175 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005176 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005177list2fpos(
5178 typval_T *arg,
5179 pos_T *posp,
5180 int *fnump,
5181 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005182{
5183 list_T *l = arg->vval.v_list;
5184 long i = 0;
5185 long n;
5186
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005187 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5188 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005189 if (arg->v_type != VAR_LIST
5190 || l == NULL
5191 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005192 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005193 return FAIL;
5194
5195 if (fnump != NULL)
5196 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005197 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005198 if (n < 0)
5199 return FAIL;
5200 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005201 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005202 *fnump = n;
5203 }
5204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005205 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005206 if (n < 0)
5207 return FAIL;
5208 posp->lnum = n;
5209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005210 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005211 if (n < 0)
5212 return FAIL;
5213 posp->col = n;
5214
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005215 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005216 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005217 posp->coladd = 0;
5218 else
5219 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005220
Bram Moolenaar493c1782014-05-28 14:34:46 +02005221 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005222 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005223
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005224 return OK;
5225}
5226
5227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 * Get the length of an environment variable name.
5229 * Advance "arg" to the first character after the name.
5230 * Return 0 for error.
5231 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005232 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005233get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234{
5235 char_u *p;
5236 int len;
5237
5238 for (p = *arg; vim_isIDc(*p); ++p)
5239 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005240 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 return 0;
5242
5243 len = (int)(p - *arg);
5244 *arg = p;
5245 return len;
5246}
5247
5248/*
5249 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005250 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 * Return 0 if something is wrong.
5252 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005253 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005254get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
5256 char_u *p;
5257 int len;
5258
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005259 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005261 {
5262 if (*p == ':')
5263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005264 // "s:" is start of "s:var", but "n:" is not and can be used in
5265 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005266 len = (int)(p - *arg);
5267 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5268 || len > 1)
5269 break;
5270 }
5271 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005272 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 return 0;
5274
5275 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005276 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 return len;
5279}
5280
5281/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005282 * Get the length of the name of a variable or function.
5283 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005285 * Return -1 if curly braces expansion failed.
5286 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 * If the name contains 'magic' {}'s, expand them and return the
5288 * expanded name in an allocated string via 'alias' - caller must free.
5289 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005291get_name_len(
5292 char_u **arg,
5293 char_u **alias,
5294 int evaluate,
5295 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
5297 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 char_u *p;
5299 char_u *expr_start;
5300 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005302 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
5304 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5305 && (*arg)[2] == (int)KE_SNR)
5306 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005307 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 *arg += 3;
5309 return get_id_len(arg) + 3;
5310 }
5311 len = eval_fname_script(*arg);
5312 if (len > 0)
5313 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005314 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 *arg += len;
5316 }
5317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005319 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005321 p = find_name_end(*arg, &expr_start, &expr_end,
5322 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 if (expr_start != NULL)
5324 {
5325 char_u *temp_string;
5326
5327 if (!evaluate)
5328 {
5329 len += (int)(p - *arg);
5330 *arg = skipwhite(p);
5331 return len;
5332 }
5333
5334 /*
5335 * Include any <SID> etc in the expanded string:
5336 * Thus the -len here.
5337 */
5338 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5339 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005340 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 *alias = temp_string;
5342 *arg = skipwhite(p);
5343 return (int)STRLEN(temp_string);
5344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
5346 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005347 // Only give an error when there is something, otherwise it will be
5348 // reported at a higher level.
5349 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005350 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
5352 return len;
5353}
5354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005355/*
5356 * Find the end of a variable or function name, taking care of magic braces.
5357 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5358 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005359 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005360 * Return a pointer to just after the name. Equal to "arg" if there is no
5361 * valid name.
5362 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005363 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005364find_name_end(
5365 char_u *arg,
5366 char_u **expr_start,
5367 char_u **expr_end,
5368 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 int mb_nest = 0;
5371 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005373 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005374 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005376 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005378 *expr_start = NULL;
5379 *expr_end = NULL;
5380 }
5381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005382 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005383 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5384 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005385 return arg;
5386
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005387 for (p = arg; *p != NUL
5388 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005389 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005390 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005391 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005392 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005393 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005394 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005395 if (*p == '\'')
5396 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005397 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005398 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005399 ;
5400 if (*p == NUL)
5401 break;
5402 }
5403 else if (*p == '"')
5404 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005405 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005406 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005407 if (*p == '\\' && p[1] != NUL)
5408 ++p;
5409 if (*p == NUL)
5410 break;
5411 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005412 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5413 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 // "s:" is start of "s:var", but "n:" is not and can be used in
5415 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005416 len = (int)(p - arg);
5417 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005418 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005419 break;
5420 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005421
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005422 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005424 if (*p == '[')
5425 ++br_nest;
5426 else if (*p == ']')
5427 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005429
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005430 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005432 if (*p == '{')
5433 {
5434 mb_nest++;
5435 if (expr_start != NULL && *expr_start == NULL)
5436 *expr_start = p;
5437 }
5438 else if (*p == '}')
5439 {
5440 mb_nest--;
5441 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5442 *expr_end = p;
5443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
5446
5447 return p;
5448}
5449
5450/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005451 * Expands out the 'magic' {}'s in a variable/function name.
5452 * Note that this can call itself recursively, to deal with
5453 * constructs like foo{bar}{baz}{bam}
5454 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5455 * "in_start" ^
5456 * "expr_start" ^
5457 * "expr_end" ^
5458 * "in_end" ^
5459 *
5460 * Returns a new allocated string, which the caller must free.
5461 * Returns NULL for failure.
5462 */
5463 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005464make_expanded_name(
5465 char_u *in_start,
5466 char_u *expr_start,
5467 char_u *expr_end,
5468 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005469{
5470 char_u c1;
5471 char_u *retval = NULL;
5472 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005473
5474 if (expr_end == NULL || in_end == NULL)
5475 return NULL;
5476 *expr_start = NUL;
5477 *expr_end = NUL;
5478 c1 = *in_end;
5479 *in_end = NUL;
5480
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005481 temp_result = eval_to_string(expr_start + 1, FALSE);
5482 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005483 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005484 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5485 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005486 if (retval != NULL)
5487 {
5488 STRCPY(retval, in_start);
5489 STRCAT(retval, temp_result);
5490 STRCAT(retval, expr_end + 1);
5491 }
5492 }
5493 vim_free(temp_result);
5494
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005495 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005496 *expr_start = '{';
5497 *expr_end = '}';
5498
5499 if (retval != NULL)
5500 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005501 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005502 if (expr_start != NULL)
5503 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005504 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005505 temp_result = make_expanded_name(retval, expr_start,
5506 expr_end, temp_result);
5507 vim_free(retval);
5508 retval = temp_result;
5509 }
5510 }
5511
5512 return retval;
5513}
5514
5515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005517 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005520eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005522 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005523}
5524
5525/*
5526 * Return TRUE if character "c" can be used as the first character in a
5527 * variable or function name (excluding '{' and '}').
5528 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005529 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005530eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005531{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005532 return ASCII_ISALPHA(c) || c == '_';
5533}
5534
5535/*
5536 * Return TRUE if character "c" can be used as the first character of a
5537 * dictionary key.
5538 */
5539 int
5540eval_isdictc(int c)
5541{
5542 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543}
5544
5545/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005546 * Return the character "str[index]" where "index" is the character index. If
5547 * "index" is out of range NULL is returned.
5548 */
5549 char_u *
5550char_from_string(char_u *str, varnumber_T index)
5551{
5552 size_t nbyte = 0;
5553 varnumber_T nchar = index;
5554 size_t slen;
5555
5556 if (str == NULL || index < 0)
5557 return NULL;
5558 slen = STRLEN(str);
5559 while (nchar > 0 && nbyte < slen)
5560 {
5561 nbyte += MB_CPTR2LEN(str + nbyte);
5562 --nchar;
5563 }
5564 if (nbyte >= slen)
5565 return NULL;
5566 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5567}
5568
5569/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005570 * Get the byte index for character index "idx" in string "str" with length
5571 * "str_len".
5572 * If going over the end return "str_len".
5573 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005574 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005575 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005576 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005577char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5578{
5579 varnumber_T nchar = idx;
5580 size_t nbyte = 0;
5581
5582 if (nchar >= 0)
5583 {
5584 while (nchar > 0 && nbyte < str_len)
5585 {
5586 nbyte += MB_CPTR2LEN(str + nbyte);
5587 --nchar;
5588 }
5589 }
5590 else
5591 {
5592 nbyte = str_len;
5593 while (nchar < 0 && nbyte > 0)
5594 {
5595 --nbyte;
5596 nbyte -= mb_head_off(str, str + nbyte);
5597 ++nchar;
5598 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005599 if (nchar < 0)
5600 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005601 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005602 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005603}
5604
5605/*
5606 * Return the slice "str[first:last]" using character indexes.
5607 * Return NULL when the result is empty.
5608 */
5609 char_u *
5610string_slice(char_u *str, varnumber_T first, varnumber_T last)
5611{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005612 long start_byte, end_byte;
5613 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005614
5615 if (str == NULL)
5616 return NULL;
5617 slen = STRLEN(str);
5618 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005619 if (start_byte < 0)
5620 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005621 if (last == -1)
Bram Moolenaar25660542020-08-28 16:38:11 +02005622 end_byte = (long)slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005623 else
5624 {
5625 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005626 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005627 // end index is inclusive
5628 end_byte += MB_CPTR2LEN(str + end_byte);
5629 }
5630
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005631 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005632 return NULL;
5633 return vim_strnsave(str + start_byte, end_byte - start_byte);
5634}
5635
5636/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005637 * Handle:
5638 * - expr[expr], expr[expr:expr] subscript
5639 * - ".name" lookup
5640 * - function call with Funcref variable: func(expr)
5641 * - method call: var->method()
5642 *
5643 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005644 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005645 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005646handle_subscript(
5647 char_u **arg,
5648 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005649 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005650 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005651{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005652 int evaluate = evalarg != NULL
5653 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005654 int ret = OK;
5655 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005656 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005657 int getnext;
5658 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005659
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005660 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005661 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005662 // When at the end of the line and ".name" or "->{" or "->X" follows in
5663 // the next line then consume the line break.
5664 p = eval_next_non_blank(*arg, evalarg, &getnext);
5665 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005666 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005667 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005668 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005669 {
5670 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005671 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005672 check_white = FALSE;
5673 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005674
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005675 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5676 || rettv->v_type == VAR_PARTIAL))
5677 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005678 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005679 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5680 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005681
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005682 // Stop the expression evaluation when immediately aborting on
5683 // error, or when an interrupt occurred or an exception was thrown
5684 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005685 if (aborting())
5686 {
5687 if (ret == OK)
5688 clear_tv(rettv);
5689 ret = FAIL;
5690 }
5691 dict_unref(selfdict);
5692 selfdict = NULL;
5693 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005694 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005695 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005696 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005697 if (ret == OK)
5698 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005699 if (((*arg)[2] == '{' && !in_vim9script()) || (*arg)[2] == '(')
5700 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005701 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005702 else
5703 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005704 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005705 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005706 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005707 // "." is ".name" lookup when we found a dict or when evaluating and
5708 // scriptversion is at least 2, where string concatenation is "..".
5709 else if (**arg == '['
5710 || (**arg == '.' && (rettv->v_type == VAR_DICT
5711 || (!evaluate
5712 && (*arg)[1] != '.'
5713 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005714 {
5715 dict_unref(selfdict);
5716 if (rettv->v_type == VAR_DICT)
5717 {
5718 selfdict = rettv->vval.v_dict;
5719 if (selfdict != NULL)
5720 ++selfdict->dv_refcount;
5721 }
5722 else
5723 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005724 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005725 {
5726 clear_tv(rettv);
5727 ret = FAIL;
5728 }
5729 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005730 else
5731 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005732 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005733
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005734 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5735 // Don't do this when "Func" is already a partial that was bound
5736 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005737 if (selfdict != NULL
5738 && (rettv->v_type == VAR_FUNC
5739 || (rettv->v_type == VAR_PARTIAL
5740 && (rettv->vval.v_partial->pt_auto
5741 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005742 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005743
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005744 dict_unref(selfdict);
5745 return ret;
5746}
5747
5748/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005749 * Make a copy of an item.
5750 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005751 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5752 * reference to an already copied list/dict can be used.
5753 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005754 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005755 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005756item_copy(
5757 typval_T *from,
5758 typval_T *to,
5759 int deep,
5760 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005761{
5762 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005763 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005764
Bram Moolenaar33570922005-01-25 22:26:29 +00005765 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005766 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005767 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005768 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005769 }
5770 ++recurse;
5771
5772 switch (from->v_type)
5773 {
5774 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005775 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005776 case VAR_STRING:
5777 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005778 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005779 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005780 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005781 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005782 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005783 copy_tv(from, to);
5784 break;
5785 case VAR_LIST:
5786 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005787 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005788 if (from->vval.v_list == NULL)
5789 to->vval.v_list = NULL;
5790 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5791 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005792 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005793 to->vval.v_list = from->vval.v_list->lv_copylist;
5794 ++to->vval.v_list->lv_refcount;
5795 }
5796 else
5797 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5798 if (to->vval.v_list == NULL)
5799 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005800 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005801 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005802 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005803 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005804 case VAR_DICT:
5805 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005806 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005807 if (from->vval.v_dict == NULL)
5808 to->vval.v_dict = NULL;
5809 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5810 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005811 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005812 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5813 ++to->vval.v_dict->dv_refcount;
5814 }
5815 else
5816 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5817 if (to->vval.v_dict == NULL)
5818 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005819 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005820 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005821 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005822 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005823 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005824 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005825 }
5826 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005827 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005828}
5829
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005830 void
5831echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5832{
5833 char_u *tofree;
5834 char_u numbuf[NUMBUFLEN];
5835 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5836
5837 if (*atstart)
5838 {
5839 *atstart = FALSE;
5840 // Call msg_start() after eval1(), evaluating the expression
5841 // may cause a message to appear.
5842 if (with_space)
5843 {
5844 // Mark the saved text as finishing the line, so that what
5845 // follows is displayed on a new line when scrolling back
5846 // at the more prompt.
5847 msg_sb_eol();
5848 msg_start();
5849 }
5850 }
5851 else if (with_space)
5852 msg_puts_attr(" ", echo_attr);
5853
5854 if (p != NULL)
5855 for ( ; *p != NUL && !got_int; ++p)
5856 {
5857 if (*p == '\n' || *p == '\r' || *p == TAB)
5858 {
5859 if (*p != TAB && *needclr)
5860 {
5861 // remove any text still there from the command
5862 msg_clr_eos();
5863 *needclr = FALSE;
5864 }
5865 msg_putchar_attr(*p, echo_attr);
5866 }
5867 else
5868 {
5869 if (has_mbyte)
5870 {
5871 int i = (*mb_ptr2len)(p);
5872
5873 (void)msg_outtrans_len_attr(p, i, echo_attr);
5874 p += i - 1;
5875 }
5876 else
5877 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5878 }
5879 }
5880 vim_free(tofree);
5881}
5882
Bram Moolenaare9a41262005-01-15 22:18:47 +00005883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 * ":echo expr1 ..." print each argument separated with a space, add a
5885 * newline at the end.
5886 * ":echon expr1 ..." print each argument plain.
5887 */
5888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005889ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890{
5891 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005892 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 char_u *p;
5894 int needclr = TRUE;
5895 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005896 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005897 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005898 evalarg_T evalarg;
5899
Bram Moolenaare707c882020-07-01 13:07:37 +02005900 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
5902 if (eap->skip)
5903 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005904 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005906 // If eval1() causes an error message the text from the command may
5907 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005908 need_clr_eos = needclr;
5909
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005911 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {
5913 /*
5914 * Report the invalid expression unless the expression evaluation
5915 * has been cancelled due to an aborting error, an interrupt, or an
5916 * exception.
5917 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005918 if (!aborting() && did_emsg == did_emsg_before
5919 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005920 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005921 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 break;
5923 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005924 need_clr_eos = FALSE;
5925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005927 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005928
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005929 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 arg = skipwhite(arg);
5931 }
5932 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005933 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934
5935 if (eap->skip)
5936 --emsg_skip;
5937 else
5938 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005939 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 if (needclr)
5941 msg_clr_eos();
5942 if (eap->cmdidx == CMD_echo)
5943 msg_end();
5944 }
5945}
5946
5947/*
5948 * ":echohl {name}".
5949 */
5950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005951ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005953 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954}
5955
5956/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005957 * Returns the :echo attribute
5958 */
5959 int
5960get_echo_attr(void)
5961{
5962 return echo_attr;
5963}
5964
5965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 * ":execute expr1 ..." execute the result of an expression.
5967 * ":echomsg expr1 ..." Print a message
5968 * ":echoerr expr1 ..." Print an error
5969 * Each gets spaces around each argument and a newline at the end for
5970 * echo commands
5971 */
5972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005973ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
5975 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 int ret = OK;
5978 char_u *p;
5979 garray_T ga;
5980 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981
5982 ga_init2(&ga, 1, 80);
5983
5984 if (eap->skip)
5985 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005986 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005988 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005989 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991
5992 if (!eap->skip)
5993 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005994 char_u buf[NUMBUFLEN];
5995
5996 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005997 {
5998 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5999 {
6000 emsg(_(e_inval_string));
6001 p = NULL;
6002 }
6003 else
6004 p = tv_get_string_buf(&rettv, buf);
6005 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006006 else
6007 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006008 if (p == NULL)
6009 {
6010 clear_tv(&rettv);
6011 ret = FAIL;
6012 break;
6013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 len = (int)STRLEN(p);
6015 if (ga_grow(&ga, len + 2) == FAIL)
6016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006017 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 ret = FAIL;
6019 break;
6020 }
6021 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 ga.ga_len += len;
6025 }
6026
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006027 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 arg = skipwhite(arg);
6029 }
6030
6031 if (ret != FAIL && ga.ga_data != NULL)
6032 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006033 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6034 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006035 // Mark the already saved text as finishing the line, so that what
6036 // follows is displayed on a new line when scrolling back at the
6037 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006038 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006039 }
6040
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006042 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006043 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006044 out_flush();
6045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 else if (eap->cmdidx == CMD_echoerr)
6047 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006048 int save_did_emsg = did_emsg;
6049
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006050 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006051 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 if (!force_abort)
6053 did_emsg = save_did_emsg;
6054 }
6055 else if (eap->cmdidx == CMD_execute)
6056 do_cmdline((char_u *)ga.ga_data,
6057 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6058 }
6059
6060 ga_clear(&ga);
6061
6062 if (eap->skip)
6063 --emsg_skip;
6064
6065 eap->nextcmd = check_nextcmd(arg);
6066}
6067
6068/*
6069 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6070 * "arg" points to the "&" or '+' when called, to "option" when returning.
6071 * Returns NULL when no option name found. Otherwise pointer to the char
6072 * after the option name.
6073 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006074 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
6077 char_u *p = *arg;
6078
6079 ++p;
6080 if (*p == 'g' && p[1] == ':')
6081 {
6082 *opt_flags = OPT_GLOBAL;
6083 p += 2;
6084 }
6085 else if (*p == 'l' && p[1] == ':')
6086 {
6087 *opt_flags = OPT_LOCAL;
6088 p += 2;
6089 }
6090 else
6091 *opt_flags = 0;
6092
6093 if (!ASCII_ISALPHA(*p))
6094 return NULL;
6095 *arg = p;
6096
6097 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006098 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 else
6100 while (ASCII_ISALPHA(*p))
6101 ++p;
6102 return p;
6103}
6104
6105/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006106 * Display script name where an item was last set.
6107 * Should only be invoked when 'verbose' is non-zero.
6108 */
6109 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006110last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006111{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006112 char_u *p;
6113
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006114 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006115 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006116 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006117 if (p != NULL)
6118 {
6119 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006120 msg_puts(_("\n\tLast set from "));
6121 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006122 if (script_ctx.sc_lnum > 0)
6123 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006124 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006125 msg_outnum((long)script_ctx.sc_lnum);
6126 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006127 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006128 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006129 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006130 }
6131}
6132
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006133#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134
6135/*
6136 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006137 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 * "flags" can be "g" to do a global substitute.
6139 * Returns an allocated string, NULL for error.
6140 */
6141 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006142do_string_sub(
6143 char_u *str,
6144 char_u *pat,
6145 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006146 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006147 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 int sublen;
6150 regmatch_T regmatch;
6151 int i;
6152 int do_all;
6153 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006154 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 garray_T ga;
6156 char_u *ret;
6157 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006158 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006160 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006162 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163
6164 ga_init2(&ga, 1, 200);
6165
6166 do_all = (flags[0] == 'g');
6167
6168 regmatch.rm_ic = p_ic;
6169 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6170 if (regmatch.regprog != NULL)
6171 {
6172 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006173 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6175 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006176 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006177 if (regmatch.startp[0] == regmatch.endp[0])
6178 {
6179 if (zero_width == regmatch.startp[0])
6180 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006181 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006182 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006183 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6184 (size_t)i);
6185 ga.ga_len += i;
6186 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006187 continue;
6188 }
6189 zero_width = regmatch.startp[0];
6190 }
6191
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 /*
6193 * Get some space for a temporary buffer to do the substitution
6194 * into. It will contain:
6195 * - The text up to where the match is.
6196 * - The substituted text.
6197 * - The text after the match.
6198 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006199 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006200 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6202 {
6203 ga_clear(&ga);
6204 break;
6205 }
6206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006207 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 i = (int)(regmatch.startp[0] - tail);
6209 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006210 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006211 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 + ga.ga_len + i, TRUE, TRUE, FALSE);
6213 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006214 tail = regmatch.endp[0];
6215 if (*tail == NUL)
6216 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if (!do_all)
6218 break;
6219 }
6220
6221 if (ga.ga_data != NULL)
6222 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6223
Bram Moolenaar473de612013-06-08 18:19:48 +02006224 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 }
6226
6227 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6228 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006229 if (p_cpo == empty_option)
6230 p_cpo = save_cpo;
6231 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006232 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006233 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006234 // If it's still empty it was changed and restored, need to restore in
6235 // the complicated way.
6236 if (*p_cpo == NUL)
6237 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006238 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240
6241 return ret;
6242}