blob: 571e96f70b7a9a0206cfb55a32f213e607d554fb [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
3191/*
3192 * Handle sixth level expression:
3193 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003194 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003195 * "string" string constant
3196 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 * &option-name option value
3198 * @r register contents
3199 * identifier variable value
3200 * function() function call
3201 * $VAR environment variable
3202 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003203 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003205 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003206 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 *
3208 * Also handle:
3209 * ! in front logical NOT
3210 * - in front unary minus
3211 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003212 * trailing [] subscript in String or List
3213 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003214 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 *
3216 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003217 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 *
3219 * Return OK or FAIL.
3220 */
3221 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222eval7(
3223 char_u **arg,
3224 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003225 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003228 int evaluate = evalarg != NULL
3229 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 int len;
3231 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 char_u *start_leader, *end_leader;
3233 int ret = OK;
3234 char_u *alias;
3235
3236 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003237 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003238 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003240 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
3242 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003243 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 */
3245 start_leader = *arg;
3246 while (**arg == '!' || **arg == '-' || **arg == '+')
3247 *arg = skipwhite(*arg + 1);
3248 end_leader = *arg;
3249
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003250 if (**arg == '.' && (!isdigit(*(*arg + 1))
3251#ifdef FEAT_FLOAT
3252 || current_sctx.sc_version < 2
3253#endif
3254 ))
3255 {
3256 semsg(_(e_invexpr2), *arg);
3257 ++*arg;
3258 return FAIL;
3259 }
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 switch (**arg)
3262 {
3263 /*
3264 * Number constant.
3265 */
3266 case '0':
3267 case '1':
3268 case '2':
3269 case '3':
3270 case '4':
3271 case '5':
3272 case '6':
3273 case '7':
3274 case '8':
3275 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003276 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003277
3278 // Apply prefixed "-" and "+" now. Matters especially when
3279 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003280 if (ret == OK && evaluate && end_leader > start_leader
3281 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003282 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 break;
3284
3285 /*
3286 * String constant: "string".
3287 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003288 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 break;
3290
3291 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003292 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003294 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003295 break;
3296
3297 /*
3298 * List: [expr, expr]
3299 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003300 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 break;
3302
3303 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003304 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003305 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003306 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003307 {
3308 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003309 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003310 }
3311 else
3312 ret = NOTDONE;
3313 break;
3314
3315 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003316 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003317 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003318 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003319 case '{': if (in_vim9script())
3320 ret = NOTDONE;
3321 else
3322 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003323 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003324 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003325 break;
3326
3327 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003328 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003330 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 break;
3332
3333 /*
3334 * Environment variable: $VAR.
3335 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003336 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 break;
3338
3339 /*
3340 * Register contents: @r.
3341 */
3342 case '@': ++*arg;
3343 if (evaluate)
3344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003345 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003346 rettv->vval.v_string = get_reg_contents(**arg,
3347 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 }
3349 if (**arg != NUL)
3350 ++*arg;
3351 break;
3352
3353 /*
3354 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003355 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003357 case '(': ret = NOTDONE;
3358 if (in_vim9script())
3359 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
3360 if (ret == NOTDONE)
3361 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003362 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003363 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003364
Bram Moolenaar9215f012020-06-27 21:18:00 +02003365 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003366 if (**arg == ')')
3367 ++*arg;
3368 else if (ret == OK)
3369 {
3370 emsg(_(e_missing_close));
3371 clear_tv(rettv);
3372 ret = FAIL;
3373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
3375 break;
3376
Bram Moolenaar8c711452005-01-14 21:53:12 +00003377 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 break;
3379 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003380
3381 if (ret == NOTDONE)
3382 {
3383 /*
3384 * Must be a variable or function name.
3385 * Can also be a curly-braces kind of name: {expr}.
3386 */
3387 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003388 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003389 if (alias != NULL)
3390 s = alias;
3391
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003392 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003393 ret = FAIL;
3394 else
3395 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003396 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3397
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003398 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3399 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003400 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003401 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003402 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003403 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003404 else if (flags & EVAL_CONSTANT)
3405 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003406 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003407 {
3408 // get the value of "true", "false" or a variable
3409 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3410 {
3411 rettv->v_type = VAR_BOOL;
3412 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003413 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003414 }
3415 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003416 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003417 {
3418 rettv->v_type = VAR_BOOL;
3419 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003420 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003421 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003422 else if (len == 4 && in_vim9script()
3423 && STRNCMP(s, "null", 4) == 0)
3424 {
3425 rettv->v_type = VAR_SPECIAL;
3426 rettv->vval.v_number = VVAL_NULL;
3427 ret = OK;
3428 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003429 else
3430 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3431 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003432 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003433 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003434 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003435 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003436 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003437 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003438 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003439 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003440 }
3441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003442 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3443 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003444 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003445 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 /*
3448 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3449 */
3450 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003451 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003452 return ret;
3453}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003454
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003455/*
3456 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003457 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003458 * Adjusts "end_leaderp" until it is at "start_leader".
3459 */
3460 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003461eval7_leader(
3462 typval_T *rettv,
3463 int numeric_only,
3464 char_u *start_leader,
3465 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003466{
3467 char_u *end_leader = *end_leaderp;
3468 int ret = OK;
3469 int error = FALSE;
3470 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003471 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003472#ifdef FEAT_FLOAT
3473 float_T f = 0.0;
3474
3475 if (rettv->v_type == VAR_FLOAT)
3476 f = rettv->vval.v_float;
3477 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003478#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003479 {
3480 while (VIM_ISWHITE(end_leader[-1]))
3481 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003482 if (in_vim9script() && end_leader[-1] == '!')
3483 val = tv2bool(rettv);
3484 else
3485 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003486 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003487 if (error)
3488 {
3489 clear_tv(rettv);
3490 ret = FAIL;
3491 }
3492 else
3493 {
3494 while (end_leader > start_leader)
3495 {
3496 --end_leader;
3497 if (*end_leader == '!')
3498 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003499 if (numeric_only)
3500 {
3501 ++end_leader;
3502 break;
3503 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003504#ifdef FEAT_FLOAT
3505 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003506 {
3507 if (in_vim9script())
3508 {
3509 rettv->v_type = VAR_BOOL;
3510 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3511 }
3512 else
3513 f = !f;
3514 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003515 else
3516#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003517 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003518 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003519 type = VAR_BOOL;
3520 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003521 }
3522 else if (*end_leader == '-')
3523 {
3524#ifdef FEAT_FLOAT
3525 if (rettv->v_type == VAR_FLOAT)
3526 f = -f;
3527 else
3528#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003529 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003530 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003531 type = VAR_NUMBER;
3532 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003533 }
3534 }
3535#ifdef FEAT_FLOAT
3536 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003538 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003539 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003541 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003542#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003543 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003544 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003545 if (in_vim9script())
3546 rettv->v_type = type;
3547 else
3548 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003549 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003552 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 return ret;
3554}
3555
3556/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003557 * Call the function referred to in "rettv".
3558 */
3559 static int
3560call_func_rettv(
3561 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003562 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003563 typval_T *rettv,
3564 int evaluate,
3565 dict_T *selfdict,
3566 typval_T *basetv)
3567{
3568 partial_T *pt = NULL;
3569 funcexe_T funcexe;
3570 typval_T functv;
3571 char_u *s;
3572 int ret;
3573
3574 // need to copy the funcref so that we can clear rettv
3575 if (evaluate)
3576 {
3577 functv = *rettv;
3578 rettv->v_type = VAR_UNKNOWN;
3579
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003580 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003581 if (functv.v_type == VAR_PARTIAL)
3582 {
3583 pt = functv.vval.v_partial;
3584 s = partial_name(pt);
3585 }
3586 else
3587 s = functv.vval.v_string;
3588 }
3589 else
3590 s = (char_u *)"";
3591
Bram Moolenaara80faa82020-04-12 19:37:17 +02003592 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003593 funcexe.firstline = curwin->w_cursor.lnum;
3594 funcexe.lastline = curwin->w_cursor.lnum;
3595 funcexe.evaluate = evaluate;
3596 funcexe.partial = pt;
3597 funcexe.selfdict = selfdict;
3598 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003599 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003600
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003601 // Clear the funcref afterwards, so that deleting it while
3602 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003603 if (evaluate)
3604 clear_tv(&functv);
3605
3606 return ret;
3607}
3608
3609/*
3610 * Evaluate "->method()".
3611 * "*arg" points to the '-'.
3612 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3613 */
3614 static int
3615eval_lambda(
3616 char_u **arg,
3617 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003618 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003619 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003620{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003621 int evaluate = evalarg != NULL
3622 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003623 typval_T base = *rettv;
3624 int ret;
3625
3626 // Skip over the ->.
3627 *arg += 2;
3628 rettv->v_type = VAR_UNKNOWN;
3629
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003630 if (**arg == '{')
3631 {
3632 // ->{lambda}()
3633 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3634 }
3635 else
3636 {
3637 // ->(lambda)()
3638 ++*arg;
3639 ret = eval1(arg, rettv, evalarg);
3640 *arg = skipwhite_and_linebreak(*arg, evalarg);
3641 if (**arg != ')')
3642 {
3643 emsg(_(e_missing_close));
3644 ret = FAIL;
3645 }
3646 ++*arg;
3647 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003648 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003649 return FAIL;
3650 else if (**arg != '(')
3651 {
3652 if (verbose)
3653 {
3654 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003655 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003656 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003658 }
3659 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003660 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003661 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003662 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003663 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003664
3665 // Clear the funcref afterwards, so that deleting it while
3666 // evaluating the arguments is possible (see test55).
3667 if (evaluate)
3668 clear_tv(&base);
3669
3670 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003671}
3672
3673/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003674 * Evaluate "->method()".
3675 * "*arg" points to the '-'.
3676 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3677 */
3678 static int
3679eval_method(
3680 char_u **arg,
3681 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003682 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003683 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003684{
3685 char_u *name;
3686 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003687 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003688 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003689 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003690 int evaluate = evalarg != NULL
3691 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003692
3693 // Skip over the ->.
3694 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003695 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003696
Bram Moolenaarac92e252019-08-03 21:58:38 +02003697 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003698 len = get_name_len(arg, &alias, evaluate, TRUE);
3699 if (alias != NULL)
3700 name = alias;
3701
3702 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003703 {
3704 if (verbose)
3705 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003706 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003707 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003708 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003709 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003710 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003711 if (**arg != '(')
3712 {
3713 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003715 ret = FAIL;
3716 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003717 else if (VIM_ISWHITE((*arg)[-1]))
3718 {
3719 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003720 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003721 ret = FAIL;
3722 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003723 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003724 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003725 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003726 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003727
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003728 // Clear the funcref afterwards, so that deleting it while
3729 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003730 if (evaluate)
3731 clear_tv(&base);
3732
Bram Moolenaarac92e252019-08-03 21:58:38 +02003733 return ret;
3734}
3735
3736/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003737 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3738 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003739 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3740 */
3741 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003742eval_index(
3743 char_u **arg,
3744 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003745 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003746 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003747{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003748 int evaluate = evalarg != NULL
3749 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003750 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003751 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003752 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003753 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003754 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003755 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003756
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003757 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3758 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003759
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003760 init_tv(&var1);
3761 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003762 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003763 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003764 /*
3765 * dict.name
3766 */
3767 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003768 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003769 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003770 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003771 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003772 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003773 }
3774 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003775 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003776 /*
3777 * something[idx]
3778 *
3779 * Get the (first) variable from inside the [].
3780 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003781 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003782 if (**arg == ':')
3783 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003784 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003785 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003786 else if (vim9 && **arg == ':')
3787 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003788 semsg(_(e_white_space_required_before_and_after_str_at_str),
3789 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003790 clear_tv(&var1);
3791 return FAIL;
3792 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003793 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003794 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003795 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003796 clear_tv(&var1);
3797 return FAIL;
3798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003799
3800 /*
3801 * Get the second variable from inside the [:].
3802 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003803 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003804 if (**arg == ':')
3805 {
3806 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003807 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003808 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003809 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003810 semsg(_(e_white_space_required_before_and_after_str_at_str),
3811 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003812 if (!empty1)
3813 clear_tv(&var1);
3814 return FAIL;
3815 }
3816 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003817 if (**arg == ']')
3818 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003819 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003821 if (!empty1)
3822 clear_tv(&var1);
3823 return FAIL;
3824 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003825 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003826 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003827 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003828 if (!empty1)
3829 clear_tv(&var1);
3830 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003831 return FAIL;
3832 }
3833 }
3834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003835 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003836 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003837 if (**arg != ']')
3838 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003839 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003840 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003841 clear_tv(&var1);
3842 if (range)
3843 clear_tv(&var2);
3844 return FAIL;
3845 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003846 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003847 }
3848
3849 if (evaluate)
3850 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003851 int res = eval_index_inner(rettv, range,
3852 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3853 key, keylen, verbose);
3854 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003855 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003856 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003857 clear_tv(&var2);
3858 return res;
3859 }
3860 return OK;
3861}
3862
3863/*
3864 * Check if "rettv" can have an [index] or [sli:ce]
3865 */
3866 int
3867check_can_index(typval_T *rettv, int evaluate, int verbose)
3868{
3869 switch (rettv->v_type)
3870 {
3871 case VAR_FUNC:
3872 case VAR_PARTIAL:
3873 if (verbose)
3874 emsg(_("E695: Cannot index a Funcref"));
3875 return FAIL;
3876 case VAR_FLOAT:
3877#ifdef FEAT_FLOAT
3878 if (verbose)
3879 emsg(_(e_float_as_string));
3880 return FAIL;
3881#endif
3882 case VAR_BOOL:
3883 case VAR_SPECIAL:
3884 case VAR_JOB:
3885 case VAR_CHANNEL:
3886 if (verbose)
3887 emsg(_(e_cannot_index_special_variable));
3888 return FAIL;
3889 case VAR_UNKNOWN:
3890 case VAR_ANY:
3891 case VAR_VOID:
3892 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003893 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003894 emsg(_(e_cannot_index_special_variable));
3895 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003896 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003897 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003898
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003899 case VAR_STRING:
3900 case VAR_LIST:
3901 case VAR_DICT:
3902 case VAR_BLOB:
3903 break;
3904 case VAR_NUMBER:
3905 if (in_vim9script())
3906 emsg(_(e_cannot_index_number));
3907 break;
3908 }
3909 return OK;
3910}
3911
3912/*
3913 * Apply index or range to "rettv".
3914 * "var1" is the first index, NULL for [:expr].
3915 * "var2" is the second index, NULL for [expr] and [expr: ]
3916 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3917 */
3918 int
3919eval_index_inner(
3920 typval_T *rettv,
3921 int is_range,
3922 typval_T *var1,
3923 typval_T *var2,
3924 char_u *key,
3925 int keylen,
3926 int verbose)
3927{
3928 long n1, n2 = 0;
3929 long len;
3930
3931 n1 = 0;
3932 if (var1 != NULL && rettv->v_type != VAR_DICT)
3933 n1 = tv_get_number(var1);
3934
3935 if (is_range)
3936 {
3937 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003938 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003939 if (verbose)
3940 emsg(_(e_cannot_slice_dictionary));
3941 return FAIL;
3942 }
3943 if (var2 == NULL)
3944 n2 = -1;
3945 else
3946 n2 = tv_get_number(var2);
3947 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003948
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003949 switch (rettv->v_type)
3950 {
3951 case VAR_UNKNOWN:
3952 case VAR_ANY:
3953 case VAR_VOID:
3954 case VAR_FUNC:
3955 case VAR_PARTIAL:
3956 case VAR_FLOAT:
3957 case VAR_BOOL:
3958 case VAR_SPECIAL:
3959 case VAR_JOB:
3960 case VAR_CHANNEL:
3961 break; // not evaluating, skipping over subscript
3962
3963 case VAR_NUMBER:
3964 case VAR_STRING:
3965 {
3966 char_u *s = tv_get_string(rettv);
3967
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003968 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003969 if (in_vim9script())
3970 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003971 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003972 s = string_slice(s, n1, n2);
3973 else
3974 s = char_from_string(s, n1);
3975 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003976 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003977 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003978 // The resulting variable is a substring. If the indexes
3979 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003980 if (n1 < 0)
3981 {
3982 n1 = len + n1;
3983 if (n1 < 0)
3984 n1 = 0;
3985 }
3986 if (n2 < 0)
3987 n2 = len + n2;
3988 else if (n2 >= len)
3989 n2 = len;
3990 if (n1 >= len || n2 < 0 || n1 > n2)
3991 s = NULL;
3992 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003993 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003994 }
3995 else
3996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003997 // The resulting variable is a string of a single
3998 // character. If the index is too big or negative the
3999 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004000 if (n1 >= len || n1 < 0)
4001 s = NULL;
4002 else
4003 s = vim_strnsave(s + n1, 1);
4004 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004005 clear_tv(rettv);
4006 rettv->v_type = VAR_STRING;
4007 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004008 }
4009 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004010
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004011 case VAR_BLOB:
4012 len = blob_len(rettv->vval.v_blob);
4013 if (is_range)
4014 {
4015 // The resulting variable is a sub-blob. If the indexes
4016 // are out of range the result is empty.
4017 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004018 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004019 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004020 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004021 n1 = 0;
4022 }
4023 if (n2 < 0)
4024 n2 = len + n2;
4025 else if (n2 >= len)
4026 n2 = len - 1;
4027 if (n1 >= len || n2 < 0 || n1 > n2)
4028 {
4029 clear_tv(rettv);
4030 rettv->v_type = VAR_BLOB;
4031 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004032 }
4033 else
4034 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004035 blob_T *blob = blob_alloc();
4036 long i;
4037
4038 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004039 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004040 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004041 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004042 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004043 return FAIL;
4044 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004045 blob->bv_ga.ga_len = n2 - n1 + 1;
4046 for (i = n1; i <= n2; i++)
4047 blob_set(blob, i - n1,
4048 blob_get(rettv->vval.v_blob, i));
4049
4050 clear_tv(rettv);
4051 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004052 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004053 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004054 }
4055 else
4056 {
4057 // The resulting variable is a byte value.
4058 // If the index is too big or negative that is an error.
4059 if (n1 < 0)
4060 n1 = len + n1;
4061 if (n1 < len && n1 >= 0)
4062 {
4063 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004064
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004065 clear_tv(rettv);
4066 rettv->v_type = VAR_NUMBER;
4067 rettv->vval.v_number = v;
4068 }
4069 else
4070 semsg(_(e_blobidx), n1);
4071 }
4072 break;
4073
4074 case VAR_LIST:
4075 if (var1 == NULL)
4076 n1 = 0;
4077 if (var2 == NULL)
4078 n2 = -1;
4079 if (list_slice_or_index(rettv->vval.v_list,
4080 is_range, n1, n2, rettv, verbose) == FAIL)
4081 return FAIL;
4082 break;
4083
4084 case VAR_DICT:
4085 {
4086 dictitem_T *item;
4087 typval_T tmp;
4088
4089 if (key == NULL)
4090 {
4091 key = tv_get_string_chk(var1);
4092 if (key == NULL)
4093 return FAIL;
4094 }
4095
4096 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4097
4098 if (item == NULL && verbose)
4099 semsg(_(e_dictkey), key);
4100 if (item == NULL)
4101 return FAIL;
4102
4103 copy_tv(&item->di_tv, &tmp);
4104 clear_tv(rettv);
4105 *rettv = tmp;
4106 }
4107 break;
4108 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004109 return OK;
4110}
4111
4112/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004113 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004114 */
4115 char_u *
4116partial_name(partial_T *pt)
4117{
4118 if (pt->pt_name != NULL)
4119 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004120 if (pt->pt_func != NULL)
4121 return pt->pt_func->uf_name;
4122 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004123}
4124
Bram Moolenaarddecc252016-04-06 22:59:37 +02004125 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004126partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004127{
4128 int i;
4129
4130 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004131 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004132 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004133 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004134 if (pt->pt_name != NULL)
4135 {
4136 func_unref(pt->pt_name);
4137 vim_free(pt->pt_name);
4138 }
4139 else
4140 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004141
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004142 // Decrease the reference count for the context of a closure. If down
4143 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004144 if (pt->pt_funcstack != NULL)
4145 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004146 --pt->pt_funcstack->fs_refcount;
4147 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004148 }
4149
Bram Moolenaarddecc252016-04-06 22:59:37 +02004150 vim_free(pt);
4151}
4152
4153/*
4154 * Unreference a closure: decrement the reference count and free it when it
4155 * becomes zero.
4156 */
4157 void
4158partial_unref(partial_T *pt)
4159{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004160 if (pt != NULL)
4161 {
4162 if (--pt->pt_refcount <= 0)
4163 partial_free(pt);
4164
4165 // If the reference count goes down to one, the funcstack may be the
4166 // only reference and can be freed if no other partials reference it.
4167 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4168 funcstack_check_refcount(pt->pt_funcstack);
4169 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004170}
4171
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004172/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004173 * Return the next (unique) copy ID.
4174 * Used for serializing nested structures.
4175 */
4176 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004177get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004178{
4179 current_copyID += COPYID_INC;
4180 return current_copyID;
4181}
4182
4183/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004184 * Garbage collection for lists and dictionaries.
4185 *
4186 * We use reference counts to be able to free most items right away when they
4187 * are no longer used. But for composite items it's possible that it becomes
4188 * unused while the reference count is > 0: When there is a recursive
4189 * reference. Example:
4190 * :let l = [1, 2, 3]
4191 * :let d = {9: l}
4192 * :let l[1] = d
4193 *
4194 * Since this is quite unusual we handle this with garbage collection: every
4195 * once in a while find out which lists and dicts are not referenced from any
4196 * variable.
4197 *
4198 * Here is a good reference text about garbage collection (refers to Python
4199 * but it applies to all reference-counting mechanisms):
4200 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004201 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004202
4203/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004204 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004205 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004206 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004207 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004208 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004209garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004210{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004211 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004212 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004213 buf_T *buf;
4214 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004215 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004216 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004217
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004218 if (!testing)
4219 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004220 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004221 want_garbage_collect = FALSE;
4222 may_garbage_collect = FALSE;
4223 garbage_collect_at_exit = FALSE;
4224 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004225
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004226 // The execution stack can grow big, limit the size.
4227 if (exestack.ga_maxlen - exestack.ga_len > 500)
4228 {
4229 size_t new_len;
4230 char_u *pp;
4231 int n;
4232
4233 // Keep 150% of the current size, with a minimum of the growth size.
4234 n = exestack.ga_len / 2;
4235 if (n < exestack.ga_growsize)
4236 n = exestack.ga_growsize;
4237
4238 // Don't make it bigger though.
4239 if (exestack.ga_len + n < exestack.ga_maxlen)
4240 {
4241 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4242 pp = vim_realloc(exestack.ga_data, new_len);
4243 if (pp == NULL)
4244 return FAIL;
4245 exestack.ga_maxlen = exestack.ga_len + n;
4246 exestack.ga_data = pp;
4247 }
4248 }
4249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004250 // We advance by two because we add one for items referenced through
4251 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004252 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004253
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004254 /*
4255 * 1. Go through all accessible variables and mark all lists and dicts
4256 * with copyID.
4257 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004258
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004259 // Don't free variables in the previous_funccal list unless they are only
4260 // referenced through previous_funccal. This must be first, because if
4261 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004262 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004263
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004264 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004265 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004266
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004267 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004268 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004269 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4270 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004271
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004272 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004273 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004274 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4275 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004276 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004277 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4278 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004279#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004280 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004281 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4282 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004283 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004284 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004285 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4286 NULL, NULL);
4287#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004288
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004289 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004290 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004291 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4292 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004293 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004294 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004295
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004296 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004297 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004298
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004299 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004300 abort = abort || set_ref_in_functions(copyID);
4301
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004302 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004303 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004304
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004305 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004306 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004307
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004308 // callbacks in buffers
4309 abort = abort || set_ref_in_buffers(copyID);
4310
Bram Moolenaar1dced572012-04-05 16:54:08 +02004311#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004312 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004313#endif
4314
Bram Moolenaardb913952012-06-29 12:54:53 +02004315#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004316 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004317#endif
4318
4319#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004320 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004321#endif
4322
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004323#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004324 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004325 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004326#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004327#ifdef FEAT_NETBEANS_INTG
4328 abort = abort || set_ref_in_nb_channel(copyID);
4329#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004330
Bram Moolenaare3188e22016-05-31 21:13:04 +02004331#ifdef FEAT_TIMERS
4332 abort = abort || set_ref_in_timer(copyID);
4333#endif
4334
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004335#ifdef FEAT_QUICKFIX
4336 abort = abort || set_ref_in_quickfix(copyID);
4337#endif
4338
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004339#ifdef FEAT_TERMINAL
4340 abort = abort || set_ref_in_term(copyID);
4341#endif
4342
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004343#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004344 abort = abort || set_ref_in_popups(copyID);
4345#endif
4346
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004347 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004348 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004349 /*
4350 * 2. Free lists and dictionaries that are not referenced.
4351 */
4352 did_free = free_unref_items(copyID);
4353
4354 /*
4355 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004356 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004357 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004358 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004359 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004360 else if (p_verbose > 0)
4361 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004362 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004363 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004364
4365 return did_free;
4366}
4367
4368/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004369 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004370 */
4371 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004372free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004373{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004374 int did_free = FALSE;
4375
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004376 // Let all "free" functions know that we are here. This means no
4377 // dictionaries, lists, channels or jobs are to be freed, because we will
4378 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004379 in_free_unref_items = TRUE;
4380
4381 /*
4382 * PASS 1: free the contents of the items. We don't free the items
4383 * themselves yet, so that it is possible to decrement refcount counters
4384 */
4385
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004386 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004387 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004388
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004389 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004390 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004391
4392#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004393 // Go through the list of jobs and free items without the copyID. This
4394 // must happen before doing channels, because jobs refer to channels, but
4395 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004396 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4397
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004398 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004399 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4400#endif
4401
4402 /*
4403 * PASS 2: free the items themselves.
4404 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004405 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004406 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004407
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004408#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004409 // Go through the list of jobs and free items without the copyID. This
4410 // must happen before doing channels, because jobs refer to channels, but
4411 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004412 free_unused_jobs(copyID, COPYID_MASK);
4413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004414 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004415 free_unused_channels(copyID, COPYID_MASK);
4416#endif
4417
4418 in_free_unref_items = FALSE;
4419
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004420 return did_free;
4421}
4422
4423/*
4424 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004425 * "list_stack" is used to add lists to be marked. Can be NULL.
4426 *
4427 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004428 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004430set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004431{
4432 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004433 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004434 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004435 hashtab_T *cur_ht;
4436 ht_stack_T *ht_stack = NULL;
4437 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004438
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004439 cur_ht = ht;
4440 for (;;)
4441 {
4442 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004443 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004444 // Mark each item in the hashtab. If the item contains a hashtab
4445 // it is added to ht_stack, if it contains a list it is added to
4446 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004447 todo = (int)cur_ht->ht_used;
4448 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4449 if (!HASHITEM_EMPTY(hi))
4450 {
4451 --todo;
4452 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4453 &ht_stack, list_stack);
4454 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004455 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004456
4457 if (ht_stack == NULL)
4458 break;
4459
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004460 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004461 cur_ht = ht_stack->ht;
4462 tempitem = ht_stack;
4463 ht_stack = ht_stack->prev;
4464 free(tempitem);
4465 }
4466
4467 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004468}
4469
4470/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004471 * Mark a dict and its items with "copyID".
4472 * Returns TRUE if setting references failed somehow.
4473 */
4474 int
4475set_ref_in_dict(dict_T *d, int copyID)
4476{
4477 if (d != NULL && d->dv_copyID != copyID)
4478 {
4479 d->dv_copyID = copyID;
4480 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4481 }
4482 return FALSE;
4483}
4484
4485/*
4486 * Mark a list and its items with "copyID".
4487 * Returns TRUE if setting references failed somehow.
4488 */
4489 int
4490set_ref_in_list(list_T *ll, int copyID)
4491{
4492 if (ll != NULL && ll->lv_copyID != copyID)
4493 {
4494 ll->lv_copyID = copyID;
4495 return set_ref_in_list_items(ll, copyID, NULL);
4496 }
4497 return FALSE;
4498}
4499
4500/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004501 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004502 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4503 *
4504 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004505 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004506 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004507set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004508{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004509 listitem_T *li;
4510 int abort = FALSE;
4511 list_T *cur_l;
4512 list_stack_T *list_stack = NULL;
4513 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004514
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004515 cur_l = l;
4516 for (;;)
4517 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004518 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004519 // Mark each item in the list. If the item contains a hashtab
4520 // it is added to ht_stack, if it contains a list it is added to
4521 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004522 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4523 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4524 ht_stack, &list_stack);
4525 if (list_stack == NULL)
4526 break;
4527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004528 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004529 cur_l = list_stack->list;
4530 tempitem = list_stack;
4531 list_stack = list_stack->prev;
4532 free(tempitem);
4533 }
4534
4535 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004536}
4537
4538/*
4539 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004540 * "list_stack" is used to add lists to be marked. Can be NULL.
4541 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4542 *
4543 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004544 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004546set_ref_in_item(
4547 typval_T *tv,
4548 int copyID,
4549 ht_stack_T **ht_stack,
4550 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004551{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004552 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004553
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004554 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004555 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004556 dict_T *dd = tv->vval.v_dict;
4557
Bram Moolenaara03f2332016-02-06 18:09:59 +01004558 if (dd != NULL && dd->dv_copyID != copyID)
4559 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004560 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004561 dd->dv_copyID = copyID;
4562 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004563 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004564 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4565 }
4566 else
4567 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004568 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4569
Bram Moolenaara03f2332016-02-06 18:09:59 +01004570 if (newitem == NULL)
4571 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004572 else
4573 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004574 newitem->ht = &dd->dv_hashtab;
4575 newitem->prev = *ht_stack;
4576 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004577 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004578 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004579 }
4580 }
4581 else if (tv->v_type == VAR_LIST)
4582 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004583 list_T *ll = tv->vval.v_list;
4584
Bram Moolenaara03f2332016-02-06 18:09:59 +01004585 if (ll != NULL && ll->lv_copyID != copyID)
4586 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004587 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004588 ll->lv_copyID = copyID;
4589 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004590 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004591 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004592 }
4593 else
4594 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004595 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4596
Bram Moolenaara03f2332016-02-06 18:09:59 +01004597 if (newitem == NULL)
4598 abort = TRUE;
4599 else
4600 {
4601 newitem->list = ll;
4602 newitem->prev = *list_stack;
4603 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004604 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004605 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004606 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004607 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004608 else if (tv->v_type == VAR_FUNC)
4609 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004610 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004611 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004612 else if (tv->v_type == VAR_PARTIAL)
4613 {
4614 partial_T *pt = tv->vval.v_partial;
4615 int i;
4616
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004617 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004618 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004619 // Didn't see this partial yet.
4620 pt->pt_copyID = copyID;
4621
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004622 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004623
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004624 if (pt->pt_dict != NULL)
4625 {
4626 typval_T dtv;
4627
4628 dtv.v_type = VAR_DICT;
4629 dtv.vval.v_dict = pt->pt_dict;
4630 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4631 }
4632
4633 for (i = 0; i < pt->pt_argc; ++i)
4634 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4635 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004636 if (pt->pt_funcstack != NULL)
4637 {
4638 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4639
4640 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4641 abort = abort || set_ref_in_item(stack + i, copyID,
4642 ht_stack, list_stack);
4643 }
4644
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004645 }
4646 }
4647#ifdef FEAT_JOB_CHANNEL
4648 else if (tv->v_type == VAR_JOB)
4649 {
4650 job_T *job = tv->vval.v_job;
4651 typval_T dtv;
4652
4653 if (job != NULL && job->jv_copyID != copyID)
4654 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004655 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004656 if (job->jv_channel != NULL)
4657 {
4658 dtv.v_type = VAR_CHANNEL;
4659 dtv.vval.v_channel = job->jv_channel;
4660 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4661 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004662 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004663 {
4664 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004665 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004666 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4667 }
4668 }
4669 }
4670 else if (tv->v_type == VAR_CHANNEL)
4671 {
4672 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004673 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004674 typval_T dtv;
4675 jsonq_T *jq;
4676 cbq_T *cq;
4677
4678 if (ch != NULL && ch->ch_copyID != copyID)
4679 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004680 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004681 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004682 {
4683 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4684 jq = jq->jq_next)
4685 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4686 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4687 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004688 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004689 {
4690 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004691 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004692 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4693 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004694 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004695 {
4696 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004697 dtv.vval.v_partial =
4698 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004699 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4700 }
4701 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004702 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004703 {
4704 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004705 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004706 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4707 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004708 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004709 {
4710 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004711 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004712 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4713 }
4714 }
4715 }
4716#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004717 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004718}
4719
Bram Moolenaar8c711452005-01-14 21:53:12 +00004720/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 * Return a string with the string representation of a variable.
4722 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004723 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004724 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004725 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004726 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004727 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004728 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4729 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004730 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004731 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004732 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004733echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004734 typval_T *tv,
4735 char_u **tofree,
4736 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004737 int copyID,
4738 int echo_style,
4739 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004740 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004741{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004742 static int recurse = 0;
4743 char_u *r = NULL;
4744
Bram Moolenaar33570922005-01-25 22:26:29 +00004745 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004746 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004747 if (!did_echo_string_emsg)
4748 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004749 // Only give this message once for a recursive call to avoid
4750 // flooding the user with errors. And stop iterating over lists
4751 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004752 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004753 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004754 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004755 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004756 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004757 }
4758 ++recurse;
4759
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760 switch (tv->v_type)
4761 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004762 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004763 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004764 {
4765 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004766 r = tv->vval.v_string;
4767 if (r == NULL)
4768 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004769 }
4770 else
4771 {
4772 *tofree = string_quote(tv->vval.v_string, FALSE);
4773 r = *tofree;
4774 }
4775 break;
4776
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004777 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004778 if (echo_style)
4779 {
4780 *tofree = NULL;
4781 r = tv->vval.v_string;
4782 }
4783 else
4784 {
4785 *tofree = string_quote(tv->vval.v_string, TRUE);
4786 r = *tofree;
4787 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004788 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004789
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004790 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004791 {
4792 partial_T *pt = tv->vval.v_partial;
4793 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004794 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004795 garray_T ga;
4796 int i;
4797 char_u *tf;
4798
4799 ga_init2(&ga, 1, 100);
4800 ga_concat(&ga, (char_u *)"function(");
4801 if (fname != NULL)
4802 {
4803 ga_concat(&ga, fname);
4804 vim_free(fname);
4805 }
4806 if (pt != NULL && pt->pt_argc > 0)
4807 {
4808 ga_concat(&ga, (char_u *)", [");
4809 for (i = 0; i < pt->pt_argc; ++i)
4810 {
4811 if (i > 0)
4812 ga_concat(&ga, (char_u *)", ");
4813 ga_concat(&ga,
4814 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4815 vim_free(tf);
4816 }
4817 ga_concat(&ga, (char_u *)"]");
4818 }
4819 if (pt != NULL && pt->pt_dict != NULL)
4820 {
4821 typval_T dtv;
4822
4823 ga_concat(&ga, (char_u *)", ");
4824 dtv.v_type = VAR_DICT;
4825 dtv.vval.v_dict = pt->pt_dict;
4826 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4827 vim_free(tf);
4828 }
4829 ga_concat(&ga, (char_u *)")");
4830
4831 *tofree = ga.ga_data;
4832 r = *tofree;
4833 break;
4834 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004835
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004836 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004837 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004838 break;
4839
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004840 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004841 if (tv->vval.v_list == NULL)
4842 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004843 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004844 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004845 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004846 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004847 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4848 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004849 {
4850 *tofree = NULL;
4851 r = (char_u *)"[...]";
4852 }
4853 else
4854 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004855 int old_copyID = tv->vval.v_list->lv_copyID;
4856
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004857 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004858 *tofree = list2string(tv, copyID, restore_copyID);
4859 if (restore_copyID)
4860 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004861 r = *tofree;
4862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004863 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004864
Bram Moolenaar8c711452005-01-14 21:53:12 +00004865 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004866 if (tv->vval.v_dict == NULL)
4867 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004868 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004869 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004870 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004871 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004872 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4873 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004874 {
4875 *tofree = NULL;
4876 r = (char_u *)"{...}";
4877 }
4878 else
4879 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004880 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004881
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004882 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004883 *tofree = dict2string(tv, copyID, restore_copyID);
4884 if (restore_copyID)
4885 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004886 r = *tofree;
4887 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004888 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004889
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004890 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004891 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004892 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004894 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004895 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004896 break;
4897
Bram Moolenaar835dc632016-02-07 14:27:38 +01004898 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004899 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004900 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004901 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004902 if (composite_val)
4903 {
4904 *tofree = string_quote(r, FALSE);
4905 r = *tofree;
4906 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004907 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004908
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004910#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911 *tofree = NULL;
4912 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4913 r = numbuf;
4914 break;
4915#endif
4916
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004917 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004918 case VAR_SPECIAL:
4919 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004920 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004921 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004922 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004923
Bram Moolenaar8502c702014-06-17 12:51:16 +02004924 if (--recurse == 0)
4925 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004926 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004927}
4928
4929/*
4930 * Return a string with the string representation of a variable.
4931 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4932 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004933 * Does not put quotes around strings, as ":echo" displays values.
4934 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4935 * May return NULL.
4936 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004937 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004938echo_string(
4939 typval_T *tv,
4940 char_u **tofree,
4941 char_u *numbuf,
4942 int copyID)
4943{
4944 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4945}
4946
4947/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004948 * Return string "str" in ' quotes, doubling ' characters.
4949 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004950 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004951 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004952 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004953string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004954{
Bram Moolenaar33570922005-01-25 22:26:29 +00004955 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004956 char_u *p, *r, *s;
4957
Bram Moolenaar33570922005-01-25 22:26:29 +00004958 len = (function ? 13 : 3);
4959 if (str != NULL)
4960 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004961 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004962 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004963 if (*p == '\'')
4964 ++len;
4965 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004966 s = r = alloc(len);
4967 if (r != NULL)
4968 {
4969 if (function)
4970 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004971 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004972 r += 10;
4973 }
4974 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004975 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004976 if (str != NULL)
4977 for (p = str; *p != NUL; )
4978 {
4979 if (*p == '\'')
4980 *r++ = '\'';
4981 MB_COPY_CHAR(p, r);
4982 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004983 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004984 if (function)
4985 *r++ = ')';
4986 *r++ = NUL;
4987 }
4988 return s;
4989}
4990
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004991#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992/*
4993 * Convert the string "text" to a floating point number.
4994 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4995 * this always uses a decimal point.
4996 * Returns the length of the text that was consumed.
4997 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004999string2float(
5000 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005001 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005002{
5003 char *s = (char *)text;
5004 float_T f;
5005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005006 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005007 if (STRNICMP(text, "inf", 3) == 0)
5008 {
5009 *value = INFINITY;
5010 return 3;
5011 }
5012 if (STRNICMP(text, "-inf", 3) == 0)
5013 {
5014 *value = -INFINITY;
5015 return 4;
5016 }
5017 if (STRNICMP(text, "nan", 3) == 0)
5018 {
5019 *value = NAN;
5020 return 3;
5021 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005022 f = strtod(s, &s);
5023 *value = f;
5024 return (int)((char_u *)s - text);
5025}
5026#endif
5027
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005030 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005032 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005033var2fpos(
5034 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005035 int dollar_lnum, // TRUE when $ is last line
5036 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005038 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005040 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005042 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005043 if (varp->v_type == VAR_LIST)
5044 {
5045 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005046 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005047 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005048 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005049
5050 l = varp->vval.v_list;
5051 if (l == NULL)
5052 return NULL;
5053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005054 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005055 pos.lnum = list_find_nr(l, 0L, &error);
5056 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005057 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005058 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005059
Bram Moolenaarec65d772020-08-20 22:29:12 +02005060 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005061 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005062 li = list_find(l, 1L);
5063 if (li != NULL && li->li_tv.v_type == VAR_STRING
5064 && li->li_tv.vval.v_string != NULL
5065 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005066 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005067 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005068 }
5069 else
5070 {
5071 pos.col = list_find_nr(l, 1L, &error);
5072 if (error)
5073 return NULL;
5074 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005075
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005076 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005077 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005078 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005079 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005080
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005081 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005082 pos.coladd = list_find_nr(l, 2L, &error);
5083 if (error)
5084 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005085
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005086 return &pos;
5087 }
5088
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005089 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005090 if (name == NULL)
5091 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005092 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005094 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005095 {
5096 if (VIsual_active)
5097 return &VIsual;
5098 return &curwin->w_cursor;
5099 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005100 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005102 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5104 return NULL;
5105 return pp;
5106 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005107
Bram Moolenaara5525202006-03-02 22:52:09 +00005108 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005109
Bram Moolenaar477933c2007-07-17 14:32:23 +00005110 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005111 {
5112 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005113 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005114 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005115 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // In silent Ex mode topline is zero, but that's not a valid line
5117 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005118 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005119 return &pos;
5120 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005121 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005122 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005123 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005124 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005125 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005126 return &pos;
5127 }
5128 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005129 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005131 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
5133 pos.lnum = curbuf->b_ml.ml_line_count;
5134 pos.col = 0;
5135 }
5136 else
5137 {
5138 pos.lnum = curwin->w_cursor.lnum;
5139 pos.col = (colnr_T)STRLEN(ml_get_curline());
5140 }
5141 return &pos;
5142 }
5143 return NULL;
5144}
5145
5146/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005147 * Convert list in "arg" into a position and optional file number.
5148 * When "fnump" is NULL there is no file number, only 3 items.
5149 * Note that the column is passed on as-is, the caller may want to decrement
5150 * it to use 1 for the first column.
5151 * Return FAIL when conversion is not possible, doesn't check the position for
5152 * validity.
5153 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005154 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005155list2fpos(
5156 typval_T *arg,
5157 pos_T *posp,
5158 int *fnump,
5159 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005160{
5161 list_T *l = arg->vval.v_list;
5162 long i = 0;
5163 long n;
5164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005165 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5166 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005167 if (arg->v_type != VAR_LIST
5168 || l == NULL
5169 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005170 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005171 return FAIL;
5172
5173 if (fnump != NULL)
5174 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005175 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005176 if (n < 0)
5177 return FAIL;
5178 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005179 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005180 *fnump = n;
5181 }
5182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005183 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005184 if (n < 0)
5185 return FAIL;
5186 posp->lnum = n;
5187
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005188 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005189 if (n < 0)
5190 return FAIL;
5191 posp->col = n;
5192
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005193 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005194 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005195 posp->coladd = 0;
5196 else
5197 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005198
Bram Moolenaar493c1782014-05-28 14:34:46 +02005199 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005200 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005201
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005202 return OK;
5203}
5204
5205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 * Get the length of an environment variable name.
5207 * Advance "arg" to the first character after the name.
5208 * Return 0 for error.
5209 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005210 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005211get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212{
5213 char_u *p;
5214 int len;
5215
5216 for (p = *arg; vim_isIDc(*p); ++p)
5217 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005218 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 return 0;
5220
5221 len = (int)(p - *arg);
5222 *arg = p;
5223 return len;
5224}
5225
5226/*
5227 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005228 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 * Return 0 if something is wrong.
5230 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005231 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005232get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233{
5234 char_u *p;
5235 int len;
5236
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005237 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005239 {
5240 if (*p == ':')
5241 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005242 // "s:" is start of "s:var", but "n:" is not and can be used in
5243 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005244 len = (int)(p - *arg);
5245 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5246 || len > 1)
5247 break;
5248 }
5249 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005250 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 return 0;
5252
5253 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005254 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
5256 return len;
5257}
5258
5259/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005260 * Get the length of the name of a variable or function.
5261 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005263 * Return -1 if curly braces expansion failed.
5264 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 * If the name contains 'magic' {}'s, expand them and return the
5266 * expanded name in an allocated string via 'alias' - caller must free.
5267 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005268 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005269get_name_len(
5270 char_u **arg,
5271 char_u **alias,
5272 int evaluate,
5273 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 char_u *p;
5277 char_u *expr_start;
5278 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005280 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
5282 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5283 && (*arg)[2] == (int)KE_SNR)
5284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005285 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 *arg += 3;
5287 return get_id_len(arg) + 3;
5288 }
5289 len = eval_fname_script(*arg);
5290 if (len > 0)
5291 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005292 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 *arg += len;
5294 }
5295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005297 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005299 p = find_name_end(*arg, &expr_start, &expr_end,
5300 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 if (expr_start != NULL)
5302 {
5303 char_u *temp_string;
5304
5305 if (!evaluate)
5306 {
5307 len += (int)(p - *arg);
5308 *arg = skipwhite(p);
5309 return len;
5310 }
5311
5312 /*
5313 * Include any <SID> etc in the expanded string:
5314 * Thus the -len here.
5315 */
5316 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5317 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005318 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 *alias = temp_string;
5320 *arg = skipwhite(p);
5321 return (int)STRLEN(temp_string);
5322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323
5324 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005325 // Only give an error when there is something, otherwise it will be
5326 // reported at a higher level.
5327 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005328 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 return len;
5331}
5332
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005333/*
5334 * Find the end of a variable or function name, taking care of magic braces.
5335 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5336 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005337 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005338 * Return a pointer to just after the name. Equal to "arg" if there is no
5339 * valid name.
5340 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005341 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005342find_name_end(
5343 char_u *arg,
5344 char_u **expr_start,
5345 char_u **expr_end,
5346 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005348 int mb_nest = 0;
5349 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005351 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005352 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005356 *expr_start = NULL;
5357 *expr_end = NULL;
5358 }
5359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005360 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005361 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5362 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005363 return arg;
5364
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005365 for (p = arg; *p != NUL
5366 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005367 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005368 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005369 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005371 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005373 if (*p == '\'')
5374 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005375 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005376 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005377 ;
5378 if (*p == NUL)
5379 break;
5380 }
5381 else if (*p == '"')
5382 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005384 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005385 if (*p == '\\' && p[1] != NUL)
5386 ++p;
5387 if (*p == NUL)
5388 break;
5389 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005390 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5391 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005392 // "s:" is start of "s:var", but "n:" is not and can be used in
5393 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005394 len = (int)(p - arg);
5395 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005396 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005397 break;
5398 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005399
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005400 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005402 if (*p == '[')
5403 ++br_nest;
5404 else if (*p == ']')
5405 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005407
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005408 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005410 if (*p == '{')
5411 {
5412 mb_nest++;
5413 if (expr_start != NULL && *expr_start == NULL)
5414 *expr_start = p;
5415 }
5416 else if (*p == '}')
5417 {
5418 mb_nest--;
5419 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5420 *expr_end = p;
5421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
5424
5425 return p;
5426}
5427
5428/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005429 * Expands out the 'magic' {}'s in a variable/function name.
5430 * Note that this can call itself recursively, to deal with
5431 * constructs like foo{bar}{baz}{bam}
5432 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5433 * "in_start" ^
5434 * "expr_start" ^
5435 * "expr_end" ^
5436 * "in_end" ^
5437 *
5438 * Returns a new allocated string, which the caller must free.
5439 * Returns NULL for failure.
5440 */
5441 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005442make_expanded_name(
5443 char_u *in_start,
5444 char_u *expr_start,
5445 char_u *expr_end,
5446 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005447{
5448 char_u c1;
5449 char_u *retval = NULL;
5450 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005451
5452 if (expr_end == NULL || in_end == NULL)
5453 return NULL;
5454 *expr_start = NUL;
5455 *expr_end = NUL;
5456 c1 = *in_end;
5457 *in_end = NUL;
5458
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005459 temp_result = eval_to_string(expr_start + 1, FALSE);
5460 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005461 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005462 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5463 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005464 if (retval != NULL)
5465 {
5466 STRCPY(retval, in_start);
5467 STRCAT(retval, temp_result);
5468 STRCAT(retval, expr_end + 1);
5469 }
5470 }
5471 vim_free(temp_result);
5472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005473 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005474 *expr_start = '{';
5475 *expr_end = '}';
5476
5477 if (retval != NULL)
5478 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005479 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005480 if (expr_start != NULL)
5481 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005482 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005483 temp_result = make_expanded_name(retval, expr_start,
5484 expr_end, temp_result);
5485 vim_free(retval);
5486 retval = temp_result;
5487 }
5488 }
5489
5490 return retval;
5491}
5492
5493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005495 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005498eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005500 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005501}
5502
5503/*
5504 * Return TRUE if character "c" can be used as the first character in a
5505 * variable or function name (excluding '{' and '}').
5506 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005507 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005508eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005509{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005510 return ASCII_ISALPHA(c) || c == '_';
5511}
5512
5513/*
5514 * Return TRUE if character "c" can be used as the first character of a
5515 * dictionary key.
5516 */
5517 int
5518eval_isdictc(int c)
5519{
5520 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521}
5522
5523/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005524 * Return the character "str[index]" where "index" is the character index. If
5525 * "index" is out of range NULL is returned.
5526 */
5527 char_u *
5528char_from_string(char_u *str, varnumber_T index)
5529{
5530 size_t nbyte = 0;
5531 varnumber_T nchar = index;
5532 size_t slen;
5533
5534 if (str == NULL || index < 0)
5535 return NULL;
5536 slen = STRLEN(str);
5537 while (nchar > 0 && nbyte < slen)
5538 {
5539 nbyte += MB_CPTR2LEN(str + nbyte);
5540 --nchar;
5541 }
5542 if (nbyte >= slen)
5543 return NULL;
5544 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5545}
5546
5547/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005548 * Get the byte index for character index "idx" in string "str" with length
5549 * "str_len".
5550 * If going over the end return "str_len".
5551 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005552 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005553 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005554 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005555char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5556{
5557 varnumber_T nchar = idx;
5558 size_t nbyte = 0;
5559
5560 if (nchar >= 0)
5561 {
5562 while (nchar > 0 && nbyte < str_len)
5563 {
5564 nbyte += MB_CPTR2LEN(str + nbyte);
5565 --nchar;
5566 }
5567 }
5568 else
5569 {
5570 nbyte = str_len;
5571 while (nchar < 0 && nbyte > 0)
5572 {
5573 --nbyte;
5574 nbyte -= mb_head_off(str, str + nbyte);
5575 ++nchar;
5576 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005577 if (nchar < 0)
5578 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005579 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005580 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005581}
5582
5583/*
5584 * Return the slice "str[first:last]" using character indexes.
5585 * Return NULL when the result is empty.
5586 */
5587 char_u *
5588string_slice(char_u *str, varnumber_T first, varnumber_T last)
5589{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005590 long start_byte, end_byte;
5591 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005592
5593 if (str == NULL)
5594 return NULL;
5595 slen = STRLEN(str);
5596 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005597 if (start_byte < 0)
5598 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005599 if (last == -1)
Bram Moolenaar25660542020-08-28 16:38:11 +02005600 end_byte = (long)slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005601 else
5602 {
5603 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005604 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005605 // end index is inclusive
5606 end_byte += MB_CPTR2LEN(str + end_byte);
5607 }
5608
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005609 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005610 return NULL;
5611 return vim_strnsave(str + start_byte, end_byte - start_byte);
5612}
5613
5614/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005615 * Handle:
5616 * - expr[expr], expr[expr:expr] subscript
5617 * - ".name" lookup
5618 * - function call with Funcref variable: func(expr)
5619 * - method call: var->method()
5620 *
5621 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005622 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005623 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005624handle_subscript(
5625 char_u **arg,
5626 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005627 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005628 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005629{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005630 int evaluate = evalarg != NULL
5631 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005632 int ret = OK;
5633 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005634 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005635 int getnext;
5636 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005637
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005638 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005639 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005640 // When at the end of the line and ".name" or "->{" or "->X" follows in
5641 // the next line then consume the line break.
5642 p = eval_next_non_blank(*arg, evalarg, &getnext);
5643 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005644 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005645 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005646 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005647 {
5648 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005649 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005650 check_white = FALSE;
5651 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005652
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005653 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5654 || rettv->v_type == VAR_PARTIAL))
5655 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005656 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005657 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5658 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005659
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005660 // Stop the expression evaluation when immediately aborting on
5661 // error, or when an interrupt occurred or an exception was thrown
5662 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005663 if (aborting())
5664 {
5665 if (ret == OK)
5666 clear_tv(rettv);
5667 ret = FAIL;
5668 }
5669 dict_unref(selfdict);
5670 selfdict = NULL;
5671 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005672 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005673 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005674 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005675 if (ret == OK)
5676 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005677 if (((*arg)[2] == '{' && !in_vim9script()) || (*arg)[2] == '(')
5678 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005679 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005680 else
5681 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005682 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005683 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005684 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005685 // "." is ".name" lookup when we found a dict or when evaluating and
5686 // scriptversion is at least 2, where string concatenation is "..".
5687 else if (**arg == '['
5688 || (**arg == '.' && (rettv->v_type == VAR_DICT
5689 || (!evaluate
5690 && (*arg)[1] != '.'
5691 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005692 {
5693 dict_unref(selfdict);
5694 if (rettv->v_type == VAR_DICT)
5695 {
5696 selfdict = rettv->vval.v_dict;
5697 if (selfdict != NULL)
5698 ++selfdict->dv_refcount;
5699 }
5700 else
5701 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005702 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005703 {
5704 clear_tv(rettv);
5705 ret = FAIL;
5706 }
5707 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005708 else
5709 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005710 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005712 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5713 // Don't do this when "Func" is already a partial that was bound
5714 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005715 if (selfdict != NULL
5716 && (rettv->v_type == VAR_FUNC
5717 || (rettv->v_type == VAR_PARTIAL
5718 && (rettv->vval.v_partial->pt_auto
5719 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005720 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005721
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005722 dict_unref(selfdict);
5723 return ret;
5724}
5725
5726/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005727 * Make a copy of an item.
5728 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005729 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5730 * reference to an already copied list/dict can be used.
5731 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005732 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005734item_copy(
5735 typval_T *from,
5736 typval_T *to,
5737 int deep,
5738 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005739{
5740 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005741 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005742
Bram Moolenaar33570922005-01-25 22:26:29 +00005743 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005745 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005746 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005747 }
5748 ++recurse;
5749
5750 switch (from->v_type)
5751 {
5752 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005753 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005754 case VAR_STRING:
5755 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005756 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005757 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005758 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005759 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005760 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005761 copy_tv(from, to);
5762 break;
5763 case VAR_LIST:
5764 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005765 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005766 if (from->vval.v_list == NULL)
5767 to->vval.v_list = NULL;
5768 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005770 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005771 to->vval.v_list = from->vval.v_list->lv_copylist;
5772 ++to->vval.v_list->lv_refcount;
5773 }
5774 else
5775 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5776 if (to->vval.v_list == NULL)
5777 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005778 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005779 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005780 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005781 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005782 case VAR_DICT:
5783 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005784 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005785 if (from->vval.v_dict == NULL)
5786 to->vval.v_dict = NULL;
5787 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5788 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005789 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005790 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5791 ++to->vval.v_dict->dv_refcount;
5792 }
5793 else
5794 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5795 if (to->vval.v_dict == NULL)
5796 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005797 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005798 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005799 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005800 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005801 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005802 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005803 }
5804 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005805 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005806}
5807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005808 void
5809echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5810{
5811 char_u *tofree;
5812 char_u numbuf[NUMBUFLEN];
5813 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5814
5815 if (*atstart)
5816 {
5817 *atstart = FALSE;
5818 // Call msg_start() after eval1(), evaluating the expression
5819 // may cause a message to appear.
5820 if (with_space)
5821 {
5822 // Mark the saved text as finishing the line, so that what
5823 // follows is displayed on a new line when scrolling back
5824 // at the more prompt.
5825 msg_sb_eol();
5826 msg_start();
5827 }
5828 }
5829 else if (with_space)
5830 msg_puts_attr(" ", echo_attr);
5831
5832 if (p != NULL)
5833 for ( ; *p != NUL && !got_int; ++p)
5834 {
5835 if (*p == '\n' || *p == '\r' || *p == TAB)
5836 {
5837 if (*p != TAB && *needclr)
5838 {
5839 // remove any text still there from the command
5840 msg_clr_eos();
5841 *needclr = FALSE;
5842 }
5843 msg_putchar_attr(*p, echo_attr);
5844 }
5845 else
5846 {
5847 if (has_mbyte)
5848 {
5849 int i = (*mb_ptr2len)(p);
5850
5851 (void)msg_outtrans_len_attr(p, i, echo_attr);
5852 p += i - 1;
5853 }
5854 else
5855 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5856 }
5857 }
5858 vim_free(tofree);
5859}
5860
Bram Moolenaare9a41262005-01-15 22:18:47 +00005861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 * ":echo expr1 ..." print each argument separated with a space, add a
5863 * newline at the end.
5864 * ":echon expr1 ..." print each argument plain.
5865 */
5866 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005867ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005870 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 char_u *p;
5872 int needclr = TRUE;
5873 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005874 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005875 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005876 evalarg_T evalarg;
5877
Bram Moolenaare707c882020-07-01 13:07:37 +02005878 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879
5880 if (eap->skip)
5881 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005882 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005884 // If eval1() causes an error message the text from the command may
5885 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005886 need_clr_eos = needclr;
5887
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005889 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 {
5891 /*
5892 * Report the invalid expression unless the expression evaluation
5893 * has been cancelled due to an aborting error, an interrupt, or an
5894 * exception.
5895 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005896 if (!aborting() && did_emsg == did_emsg_before
5897 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005898 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005899 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 break;
5901 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005902 need_clr_eos = FALSE;
5903
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005905 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005906
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 arg = skipwhite(arg);
5909 }
5910 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005911 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
5913 if (eap->skip)
5914 --emsg_skip;
5915 else
5916 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005917 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 if (needclr)
5919 msg_clr_eos();
5920 if (eap->cmdidx == CMD_echo)
5921 msg_end();
5922 }
5923}
5924
5925/*
5926 * ":echohl {name}".
5927 */
5928 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005929ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005931 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932}
5933
5934/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005935 * Returns the :echo attribute
5936 */
5937 int
5938get_echo_attr(void)
5939{
5940 return echo_attr;
5941}
5942
5943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 * ":execute expr1 ..." execute the result of an expression.
5945 * ":echomsg expr1 ..." Print a message
5946 * ":echoerr expr1 ..." Print an error
5947 * Each gets spaces around each argument and a newline at the end for
5948 * echo commands
5949 */
5950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005951ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
5953 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005954 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 int ret = OK;
5956 char_u *p;
5957 garray_T ga;
5958 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959
5960 ga_init2(&ga, 1, 80);
5961
5962 if (eap->skip)
5963 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005964 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005966 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005967 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969
5970 if (!eap->skip)
5971 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005972 char_u buf[NUMBUFLEN];
5973
5974 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005975 {
5976 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5977 {
5978 emsg(_(e_inval_string));
5979 p = NULL;
5980 }
5981 else
5982 p = tv_get_string_buf(&rettv, buf);
5983 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005984 else
5985 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005986 if (p == NULL)
5987 {
5988 clear_tv(&rettv);
5989 ret = FAIL;
5990 break;
5991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 len = (int)STRLEN(p);
5993 if (ga_grow(&ga, len + 2) == FAIL)
5994 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005995 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 ret = FAIL;
5997 break;
5998 }
5999 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 ga.ga_len += len;
6003 }
6004
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006005 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 arg = skipwhite(arg);
6007 }
6008
6009 if (ret != FAIL && ga.ga_data != NULL)
6010 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006011 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6012 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006013 // Mark the already saved text as finishing the line, so that what
6014 // follows is displayed on a new line when scrolling back at the
6015 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006016 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006017 }
6018
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006020 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006021 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006022 out_flush();
6023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 else if (eap->cmdidx == CMD_echoerr)
6025 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006026 int save_did_emsg = did_emsg;
6027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006028 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006029 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 if (!force_abort)
6031 did_emsg = save_did_emsg;
6032 }
6033 else if (eap->cmdidx == CMD_execute)
6034 do_cmdline((char_u *)ga.ga_data,
6035 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6036 }
6037
6038 ga_clear(&ga);
6039
6040 if (eap->skip)
6041 --emsg_skip;
6042
6043 eap->nextcmd = check_nextcmd(arg);
6044}
6045
6046/*
6047 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6048 * "arg" points to the "&" or '+' when called, to "option" when returning.
6049 * Returns NULL when no option name found. Otherwise pointer to the char
6050 * after the option name.
6051 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006052 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006053find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 char_u *p = *arg;
6056
6057 ++p;
6058 if (*p == 'g' && p[1] == ':')
6059 {
6060 *opt_flags = OPT_GLOBAL;
6061 p += 2;
6062 }
6063 else if (*p == 'l' && p[1] == ':')
6064 {
6065 *opt_flags = OPT_LOCAL;
6066 p += 2;
6067 }
6068 else
6069 *opt_flags = 0;
6070
6071 if (!ASCII_ISALPHA(*p))
6072 return NULL;
6073 *arg = p;
6074
6075 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006076 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 else
6078 while (ASCII_ISALPHA(*p))
6079 ++p;
6080 return p;
6081}
6082
6083/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006084 * Display script name where an item was last set.
6085 * Should only be invoked when 'verbose' is non-zero.
6086 */
6087 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006088last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006089{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006090 char_u *p;
6091
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006092 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006093 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006094 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006095 if (p != NULL)
6096 {
6097 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006098 msg_puts(_("\n\tLast set from "));
6099 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006100 if (script_ctx.sc_lnum > 0)
6101 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006102 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006103 msg_outnum((long)script_ctx.sc_lnum);
6104 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006105 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006106 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006107 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006108 }
6109}
6110
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006111#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
6113/*
6114 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006115 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 * "flags" can be "g" to do a global substitute.
6117 * Returns an allocated string, NULL for error.
6118 */
6119 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120do_string_sub(
6121 char_u *str,
6122 char_u *pat,
6123 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006124 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006125 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126{
6127 int sublen;
6128 regmatch_T regmatch;
6129 int i;
6130 int do_all;
6131 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006132 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 garray_T ga;
6134 char_u *ret;
6135 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006136 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006138 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006140 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141
6142 ga_init2(&ga, 1, 200);
6143
6144 do_all = (flags[0] == 'g');
6145
6146 regmatch.rm_ic = p_ic;
6147 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6148 if (regmatch.regprog != NULL)
6149 {
6150 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006151 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6153 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006154 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006155 if (regmatch.startp[0] == regmatch.endp[0])
6156 {
6157 if (zero_width == regmatch.startp[0])
6158 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006159 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006160 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006161 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6162 (size_t)i);
6163 ga.ga_len += i;
6164 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006165 continue;
6166 }
6167 zero_width = regmatch.startp[0];
6168 }
6169
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 /*
6171 * Get some space for a temporary buffer to do the substitution
6172 * into. It will contain:
6173 * - The text up to where the match is.
6174 * - The substituted text.
6175 * - The text after the match.
6176 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006177 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006178 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6180 {
6181 ga_clear(&ga);
6182 break;
6183 }
6184
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006185 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 i = (int)(regmatch.startp[0] - tail);
6187 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006188 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006189 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 + ga.ga_len + i, TRUE, TRUE, FALSE);
6191 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006192 tail = regmatch.endp[0];
6193 if (*tail == NUL)
6194 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 if (!do_all)
6196 break;
6197 }
6198
6199 if (ga.ga_data != NULL)
6200 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6201
Bram Moolenaar473de612013-06-08 18:19:48 +02006202 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 }
6204
6205 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6206 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006207 if (p_cpo == empty_option)
6208 p_cpo = save_cpo;
6209 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006210 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006211 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006212 // If it's still empty it was changed and restored, need to restore in
6213 // the complicated way.
6214 if (*p_cpo == NUL)
6215 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006216 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218
6219 return ret;
6220}