blob: 3c4a8c562d659e40460f10f6e22ed76ba719c562 [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())
194 retval = tv2bool(&tv);
195 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 Moolenaard155d7a2018-12-21 16:04:21 +0100329 res = (tv_get_number_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 Moolenaar7454a062016-01-30 15:14:10 +0100371skip_expr(char_u **pp)
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 Moolenaar5409f5d2020-06-24 18:37:35 +0200376 return eval1(pp, &rettv, NULL);
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 Moolenaare40fbc22020-06-27 18:06:45 +0200398 garray_T *gap = &evalarg->eval_ga;
399 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
400
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200401 if (vim9script
402 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403 {
404 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200405 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200407 ++gap->ga_len;
408 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200409 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410
411 // Don't evaluate the expression.
412 if (evalarg != NULL)
413 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200414 *arg = skipwhite(*arg);
415 res = eval1(arg, &rettv, evalarg);
416 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200417 if (evalarg != NULL)
418 evalarg->eval_flags = save_flags;
419
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200420 if (vim9script
421 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 if (evalarg->eval_ga.ga_len == 1)
424 {
425 // just one line, no need to concatenate
426 ga_clear(gap);
427 gap->ga_itemsize = 0;
428 }
429 else
430 {
431 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200432 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200433
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200434 // Line breaks encountered, concatenate all the lines.
435 *((char_u **)gap->ga_data) = *start;
436 p = ga_concat_strings(gap, "");
437
438 // free the lines only when using getsourceline()
439 if (evalarg->eval_cookie != NULL)
440 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200441 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200442 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the last line, "arg" points into it, free it
444 // later.
445 vim_free(evalarg->eval_tofree);
446 evalarg->eval_tofree =
447 ((char_u **)gap->ga_data)[gap->ga_len - 1];
448 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200449 ga_clear_strings(gap);
450 }
451 else
452 ga_clear(gap);
453 gap->ga_itemsize = 0;
454 if (p == NULL)
455 return FAIL;
456 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200457 vim_free(evalarg->eval_tofree_lambda);
458 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 // Compute "end" relative to the end.
460 *end = *start + STRLEN(*start) - endoff;
461 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200462 }
463
464 return res;
465}
466
467/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200468 * Top level evaluation function, returning a string. Does not handle line
469 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000470 * When "convert" is TRUE convert a List into a sequence of lines and convert
471 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 * Return pointer to allocated memory, or NULL for failure.
473 */
474 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100475eval_to_string(
476 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100477 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478{
Bram Moolenaar33570922005-01-25 22:26:29 +0000479 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000481 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000482#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000483 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200486 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 retval = NULL;
488 else
489 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000490 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000491 {
492 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000493 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200494 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200495 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200496 if (tv.vval.v_list->lv_len > 0)
497 ga_append(&ga, NL);
498 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000499 ga_append(&ga, NUL);
500 retval = (char_u *)ga.ga_data;
501 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000502#ifdef FEAT_FLOAT
503 else if (convert && tv.v_type == VAR_FLOAT)
504 {
505 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
506 retval = vim_strsave(numbuf);
507 }
508#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000509 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100510 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000511 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200513 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514
515 return retval;
516}
517
518/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000519 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200520 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200521 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 */
523 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100524eval_to_string_safe(
525 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100526 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
528 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200529 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200530 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200532 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200533 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000534 if (use_sandbox)
535 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200536 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200537 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000538 if (use_sandbox)
539 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200540 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200541 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200542 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 return retval;
544}
545
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546/*
547 * Top level evaluation function, returning a number.
548 * Evaluates "expr" silently.
549 * Returns -1 for an error.
550 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200551 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100552eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaar33570922005-01-25 22:26:29 +0000554 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200555 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000556 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
558 ++emsg_off;
559
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200560 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 retval = -1;
562 else
563 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100564 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000565 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 }
567 --emsg_off;
568
569 return retval;
570}
571
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000572/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000573 * Top level evaluation function.
574 * Returns an allocated typval_T with the result.
575 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000576 */
577 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200578eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000579{
580 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200581 evalarg_T evalarg;
582
583 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000584
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200585 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200586 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100587 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000588
Bram Moolenaar37c83712020-06-30 21:18:36 +0200589 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000590 return tv;
591}
592
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100594 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200595 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
596 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000597 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200599 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100600call_vim_function(
601 char_u *func,
602 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200603 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200604 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000606 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200607 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100609 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200610 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200611 funcexe.firstline = curwin->w_cursor.lnum;
612 funcexe.lastline = curwin->w_cursor.lnum;
613 funcexe.evaluate = TRUE;
614 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000615 if (ret == FAIL)
616 clear_tv(rettv);
617
618 return ret;
619}
620
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100621/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100622 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100623 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200624 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
625 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100626 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200627 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100628call_func_retnr(
629 char_u *func,
630 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200631 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100632{
633 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200634 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100635
Bram Moolenaarded27a12018-08-01 19:06:03 +0200636 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100637 return -1;
638
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100639 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100640 clear_tv(&rettv);
641 return retval;
642}
643
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000644/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100645 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000646 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200647 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
648 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000649 */
650 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100651call_func_retstr(
652 char_u *func,
653 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200654 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000655{
656 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000657 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000658
Bram Moolenaarded27a12018-08-01 19:06:03 +0200659 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000660 return NULL;
661
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100662 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000663 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 return retval;
665}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000666
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000667/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100668 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200669 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
670 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000671 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000672 */
673 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100674call_func_retlist(
675 char_u *func,
676 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200677 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000678{
679 typval_T rettv;
680
Bram Moolenaarded27a12018-08-01 19:06:03 +0200681 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000682 return NULL;
683
684 if (rettv.v_type != VAR_LIST)
685 {
686 clear_tv(&rettv);
687 return NULL;
688 }
689
690 return rettv.vval.v_list;
691}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#ifdef FEAT_FOLDING
694/*
695 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
696 * it in "*cp". Doesn't give error messages.
697 */
698 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100699eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
Bram Moolenaar33570922005-01-25 22:26:29 +0000701 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200702 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000704 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
705 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706
707 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000708 if (use_sandbox)
709 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200710 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200712 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 retval = 0;
714 else
715 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100716 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000717 if (tv.v_type == VAR_NUMBER)
718 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000719 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 retval = 0;
721 else
722 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100723 // If the result is a string, check if there is a non-digit before
724 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000725 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 if (!VIM_ISDIGIT(*s) && *s != '-')
727 *cp = *s++;
728 retval = atol((char *)s);
729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000730 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 }
732 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000733 if (use_sandbox)
734 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200735 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200736 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200738 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739}
740#endif
741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000743 * Get an lval: variable, Dict item or List item that can be assigned a value
744 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
745 * "name.key", "name.key[expr]" etc.
746 * Indexing only works if "name" is an existing List or Dictionary.
747 * "name" points to the start of the name.
748 * If "rettv" is not NULL it points to the value to be assigned.
749 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
750 * wrong; must end in space or cmd separator.
751 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100752 * flags:
753 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100754 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100755 * GLV_NO_AUTOLOAD: do not use script autoloading
756 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000757 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000758 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000759 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000760 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200761 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100762get_lval(
763 char_u *name,
764 typval_T *rettv,
765 lval_T *lp,
766 int unlet,
767 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100768 int flags, // GLV_ values
769 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000770{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000771 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 char_u *expr_start, *expr_end;
773 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000774 dictitem_T *v;
775 typval_T var1;
776 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000777 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000778 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000779 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000780 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000781 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100782 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000783
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100784 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200785 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000786
787 if (skip)
788 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100789 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000790 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200791 lp->ll_name_end = find_name_end(name, NULL, NULL,
792 FNE_INCL_BR | fne_flags);
793 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000794 }
795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100796 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000797 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000799 if (expr_start != NULL)
800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100801 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100802 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000803 && *p != '[' && *p != '.')
804 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200805 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 return NULL;
807 }
808
809 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
810 if (lp->ll_exp_name == NULL)
811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100812 // Report an invalid expression in braces, unless the
813 // expression evaluation has been cancelled due to an
814 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000815 if (!aborting() && !quiet)
816 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000817 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100818 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 return NULL;
820 }
821 }
822 lp->ll_name = lp->ll_exp_name;
823 }
824 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000826 lp->ll_name = name;
827
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200828 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200830 // "a: type" is declaring variable "a" with a type, not "a:".
831 if (p == name + 2 && p[-1] == ':')
832 {
833 --p;
834 lp->ll_name_end = p;
835 }
836 if (*p == ':')
837 {
838 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
839 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200841 // parse the type after the name
842 lp->ll_type = parse_type(&tp, &si->sn_type_list);
843 lp->ll_name_end = tp;
844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 }
846 }
847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100848 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000849 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
850 return p;
851
852 cc = *p;
853 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100854 // Only pass &ht when we would write to the variable, it prevents autoload
855 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100856 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
857 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200859 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000860 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000861 if (v == NULL)
862 return NULL;
863
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000864 /*
865 * Loop until no more [idx] or .key is following.
866 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100868 var1.v_type = VAR_UNKNOWN;
869 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000871 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
873 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100874 && lp->ll_tv->vval.v_dict != NULL)
875 && !(lp->ll_tv->v_type == VAR_BLOB
876 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100879 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000881 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000883 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000884 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100885 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000887 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000888
Bram Moolenaar8c711452005-01-14 21:53:12 +0000889 len = -1;
890 if (*p == '.')
891 {
892 key = p + 1;
893 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
894 ;
895 if (len == 0)
896 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100898 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000900 }
901 p = key + len;
902 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000903 else
904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000906 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000907 if (*p == ':')
908 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000909 else
910 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000911 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200912 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100914 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000915 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100916 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000917 clear_tv(&var1);
918 return NULL;
919 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200920 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000921 }
922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100923 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000924 if (*p == ':')
925 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000926 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000928 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200929 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100930 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000931 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000932 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100933 if (rettv != NULL
934 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100935 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100936 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100937 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000938 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000939 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100940 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100941 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000943 }
944 p = skipwhite(p + 1);
945 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000946 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000947 else
948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200950 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200951 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000952 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100953 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000955 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100956 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100958 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100959 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000960 clear_tv(&var2);
961 return NULL;
962 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000963 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000964 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000965 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000966 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000968
Bram Moolenaar8c711452005-01-14 21:53:12 +0000969 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000970 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000971 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100972 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100973 clear_tv(&var1);
974 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000975 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000976 }
977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100978 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000979 ++p;
980 }
981
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000983 {
984 if (len == -1)
985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100986 // "[key]": get key from "var1"
987 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200988 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000991 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 }
993 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000994 lp->ll_list = NULL;
995 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000996 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100998 // When assigning to a scope dictionary check that a function and
999 // variable name is valid (only variable name unless it is l: or
1000 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001001 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001002 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001003 int prevval;
1004 int wrong;
1005
1006 if (len != -1)
1007 {
1008 prevval = key[len];
1009 key[len] = NUL;
1010 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001011 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001012 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001013 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1014 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001015 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001016 || !valid_varname(key);
1017 if (len != -1)
1018 key[len] = prevval;
1019 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001020 {
1021 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001022 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001023 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001024 }
1025
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001026 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001027 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001028 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001029 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001030 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001032 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001033 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001034 return NULL;
1035 }
1036
Bram Moolenaar31b81602019-02-10 22:14:27 +01001037 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001039 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001041 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001042 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001043 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001044 }
1045 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001047 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001049 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001051 p = NULL;
1052 break;
1053 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001054 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001055 else if ((flags & GLV_READ_ONLY) == 0
1056 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001057 {
1058 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001059 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001060 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001061
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001062 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001063 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001064 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001065 else if (lp->ll_tv->v_type == VAR_BLOB)
1066 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001067 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1068
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001069 /*
1070 * Get the number and item for the only or first index of the List.
1071 */
1072 if (empty1)
1073 lp->ll_n1 = 0;
1074 else
1075 // is number or string
1076 lp->ll_n1 = (long)tv_get_number(&var1);
1077 clear_tv(&var1);
1078
1079 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001080 || lp->ll_n1 > bloblen
1081 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001082 {
1083 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001084 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001085 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001086 return NULL;
1087 }
1088 if (lp->ll_range && !lp->ll_empty2)
1089 {
1090 lp->ll_n2 = (long)tv_get_number(&var2);
1091 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001092 if (lp->ll_n2 < 0
1093 || lp->ll_n2 >= bloblen
1094 || lp->ll_n2 < lp->ll_n1)
1095 {
1096 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001097 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001098 return NULL;
1099 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001100 }
1101 lp->ll_blob = lp->ll_tv->vval.v_blob;
1102 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001103 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001104 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001105 else
1106 {
1107 /*
1108 * Get the number and item for the only or first index of the List.
1109 */
1110 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001112 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001113 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001114 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001115 clear_tv(&var1);
1116
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001117 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001118 lp->ll_list = lp->ll_tv->vval.v_list;
1119 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1120 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001121 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001122 if (lp->ll_n1 < 0)
1123 {
1124 lp->ll_n1 = 0;
1125 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1126 }
1127 }
1128 if (lp->ll_li == NULL)
1129 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001130 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001131 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001132 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001134 }
1135
1136 /*
1137 * May need to find the item or absolute index for the second
1138 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 * When no index given: "lp->ll_empty2" is TRUE.
1140 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001144 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001145 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001146 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001147 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001148 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001150 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001151 {
1152 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001153 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001155 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001156 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001157 }
1158
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001159 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 if (lp->ll_n1 < 0)
1161 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1162 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001163 {
1164 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001165 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001167 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001168 }
1169
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 }
1173
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001174 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 return p;
1177}
1178
1179/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001180 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001183clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001184{
1185 vim_free(lp->ll_exp_name);
1186 vim_free(lp->ll_newkey);
1187}
1188
1189/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001190 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001191 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001192 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1193 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001196set_var_lval(
1197 lval_T *lp,
1198 char_u *endp,
1199 typval_T *rettv,
1200 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001202 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001203{
1204 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001205 listitem_T *ri;
1206 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207
1208 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001210 cc = *endp;
1211 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001212 if (lp->ll_blob != NULL)
1213 {
1214 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001215
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001216 if (op != NULL && *op != '=')
1217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001218 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001219 return;
1220 }
1221
1222 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1223 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001224 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001225
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001226 if (lp->ll_empty2)
1227 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1228
1229 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001231 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001232 return;
1233 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001234 if (lp->ll_empty2)
1235 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001236
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001237 ir = 0;
1238 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1239 blob_set(lp->ll_blob, il,
1240 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001241 }
1242 else
1243 {
1244 val = (int)tv_get_number_chk(rettv, &error);
1245 if (!error)
1246 {
1247 garray_T *gap = &lp->ll_blob->bv_ga;
1248
1249 // Allow for appending a byte. Setting a byte beyond
1250 // the end is an error otherwise.
1251 if (lp->ll_n1 < gap->ga_len
1252 || (lp->ll_n1 == gap->ga_len
1253 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1254 {
1255 blob_set(lp->ll_blob, lp->ll_n1, val);
1256 if (lp->ll_n1 == gap->ga_len)
1257 ++gap->ga_len;
1258 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001259 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001260 }
1261 }
1262 }
1263 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001264 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001265 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001268 {
1269 emsg(_(e_cannot_mod));
1270 *endp = cc;
1271 return;
1272 }
1273
Bram Moolenaarff697e62019-02-12 22:28:33 +01001274 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001275 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001276 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001277 &tv, &di, TRUE, FALSE) == OK)
1278 {
1279 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001280 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1281 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001282 && tv_op(&tv, rettv, op) == OK)
1283 set_var(lp->ll_name, &tv, FALSE);
1284 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001285 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001286 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001287 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001288 {
1289 if (lp->ll_type != NULL
1290 && check_typval_type(lp->ll_type, rettv) == FAIL)
1291 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001293 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001294 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001296 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001297 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001298 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001299 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300 else if (lp->ll_range)
1301 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001302 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001303 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001306 {
1307 emsg(_("E996: Cannot lock a range"));
1308 return;
1309 }
1310
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001311 /*
1312 * Check whether any of the list items is locked
1313 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001314 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001315 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001316 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001317 return;
1318 ri = ri->li_next;
1319 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1320 break;
1321 ll_li = ll_li->li_next;
1322 ++ll_n1;
1323 }
1324
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 /*
1326 * Assign the List values to the list items.
1327 */
1328 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001329 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001330 if (op != NULL && *op != '=')
1331 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1332 else
1333 {
1334 clear_tv(&lp->ll_li->li_tv);
1335 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1336 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001337 ri = ri->li_next;
1338 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1339 break;
1340 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001341 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001342 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001343 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001344 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001345 ri = NULL;
1346 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001347 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001348 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 lp->ll_li = lp->ll_li->li_next;
1350 ++lp->ll_n1;
1351 }
1352 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001353 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001354 else if (lp->ll_empty2
1355 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001356 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001357 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001358 }
1359 else
1360 {
1361 /*
1362 * Assign to a List or Dictionary item.
1363 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001365 {
1366 emsg(_("E996: Cannot lock a list or dict"));
1367 return;
1368 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001369 if (lp->ll_newkey != NULL)
1370 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001371 if (op != NULL && *op != '=')
1372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001373 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001374 return;
1375 }
1376
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001377 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001378 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001379 if (di == NULL)
1380 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001381 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1382 {
1383 vim_free(di);
1384 return;
1385 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001386 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001387 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001388 else if (op != NULL && *op != '=')
1389 {
1390 tv_op(lp->ll_tv, rettv, op);
1391 return;
1392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001394 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001395
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001396 /*
1397 * Assign the value to the variable or list item.
1398 */
1399 if (copy)
1400 copy_tv(rettv, lp->ll_tv);
1401 else
1402 {
1403 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001404 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001405 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001406 }
1407 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001408}
1409
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001410/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001411 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1412 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001413 * Returns OK or FAIL.
1414 */
1415 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001416tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001417{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001418 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001419 char_u numbuf[NUMBUFLEN];
1420 char_u *s;
1421
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001422 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001423 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001424 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001425 {
1426 switch (tv1->v_type)
1427 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001428 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001429 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001431 case VAR_DICT:
1432 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001433 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001434 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001435 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001436 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001437 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001438 break;
1439
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001440 case VAR_BLOB:
1441 if (*op != '+' || tv2->v_type != VAR_BLOB)
1442 break;
1443 // BLOB += BLOB
1444 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1445 {
1446 blob_T *b1 = tv1->vval.v_blob;
1447 blob_T *b2 = tv2->vval.v_blob;
1448 int i, len = blob_len(b2);
1449 for (i = 0; i < len; i++)
1450 ga_append(&b1->bv_ga, blob_get(b2, i));
1451 }
1452 return OK;
1453
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001454 case VAR_LIST:
1455 if (*op != '+' || tv2->v_type != VAR_LIST)
1456 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001457 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1459 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1460 return OK;
1461
1462 case VAR_NUMBER:
1463 case VAR_STRING:
1464 if (tv2->v_type == VAR_LIST)
1465 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001466 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001467 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001468 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001469 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001470#ifdef FEAT_FLOAT
1471 if (tv2->v_type == VAR_FLOAT)
1472 {
1473 float_T f = n;
1474
Bram Moolenaarff697e62019-02-12 22:28:33 +01001475 if (*op == '%')
1476 break;
1477 switch (*op)
1478 {
1479 case '+': f += tv2->vval.v_float; break;
1480 case '-': f -= tv2->vval.v_float; break;
1481 case '*': f *= tv2->vval.v_float; break;
1482 case '/': f /= tv2->vval.v_float; break;
1483 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001484 clear_tv(tv1);
1485 tv1->v_type = VAR_FLOAT;
1486 tv1->vval.v_float = f;
1487 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001488 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001489#endif
1490 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001491 switch (*op)
1492 {
1493 case '+': n += tv_get_number(tv2); break;
1494 case '-': n -= tv_get_number(tv2); break;
1495 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001496 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1497 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001498 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001499 clear_tv(tv1);
1500 tv1->v_type = VAR_NUMBER;
1501 tv1->vval.v_number = n;
1502 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001503 }
1504 else
1505 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001506 if (tv2->v_type == VAR_FLOAT)
1507 break;
1508
Bram Moolenaarff697e62019-02-12 22:28:33 +01001509 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001510 s = tv_get_string(tv1);
1511 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001512 clear_tv(tv1);
1513 tv1->v_type = VAR_STRING;
1514 tv1->vval.v_string = s;
1515 }
1516 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001517
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001518 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001519#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001520 {
1521 float_T f;
1522
Bram Moolenaarff697e62019-02-12 22:28:33 +01001523 if (*op == '%' || *op == '.'
1524 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001525 && tv2->v_type != VAR_NUMBER
1526 && tv2->v_type != VAR_STRING))
1527 break;
1528 if (tv2->v_type == VAR_FLOAT)
1529 f = tv2->vval.v_float;
1530 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001531 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001532 switch (*op)
1533 {
1534 case '+': tv1->vval.v_float += f; break;
1535 case '-': tv1->vval.v_float -= f; break;
1536 case '*': tv1->vval.v_float *= f; break;
1537 case '/': tv1->vval.v_float /= f; break;
1538 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001539 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001540#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001541 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001542 }
1543 }
1544
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001545 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001546 return FAIL;
1547}
1548
1549/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001550 * Evaluate the expression used in a ":for var in expr" command.
1551 * "arg" points to "var".
1552 * Set "*errp" to TRUE for an error, FALSE otherwise;
1553 * Return a pointer that holds the info. Null when there is an error.
1554 */
1555 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001556eval_for_line(
1557 char_u *arg,
1558 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001559 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001560 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561{
Bram Moolenaar33570922005-01-25 22:26:29 +00001562 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001564 typval_T tv;
1565 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001566 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001567
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001568 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001569
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001570 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001571 if (fi == NULL)
1572 return NULL;
1573
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001574 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001575 if (expr == NULL)
1576 return fi;
1577
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001578 expr = skipwhite_and_linebreak(expr, evalarg);
1579 if (expr[0] != 'i' || expr[1] != 'n'
1580 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001581 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001583 return fi;
1584 }
1585
1586 if (skip)
1587 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001588 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1589 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001590 {
1591 *errp = FALSE;
1592 if (!skip)
1593 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001594 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001595 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001596 l = tv.vval.v_list;
1597 if (l == NULL)
1598 {
1599 // a null list is like an empty list: do nothing
1600 clear_tv(&tv);
1601 }
1602 else
1603 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001605 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001607 // No need to increment the refcount, it's already set for
1608 // the list being used in "tv".
1609 fi->fi_list = l;
1610 list_add_watch(l, &fi->fi_lw);
1611 fi->fi_lw.lw_item = l->lv_first;
1612 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001613 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001614 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001615 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001616 fi->fi_bi = 0;
1617 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001618 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001619 typval_T btv;
1620
1621 // Make a copy, so that the iteration still works when the
1622 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001624 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001625 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001626 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001627 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001628 else
1629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001630 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001631 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 }
1633 }
1634 }
1635 if (skip)
1636 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001637 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638
1639 return fi;
1640}
1641
1642/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001643 * Used when looping over a :for line, skip the "in expr" part.
1644 */
1645 void
1646skip_for_lines(void *fi_void, evalarg_T *evalarg)
1647{
1648 forinfo_T *fi = (forinfo_T *)fi_void;
1649 int i;
1650
1651 for (i = 0; i < fi->fi_break_count; ++i)
1652 eval_next_line(evalarg);
1653}
1654
1655/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 * Use the first item in a ":for" list. Advance to the next.
1657 * Assign the values to the variable (list). "arg" points to the first one.
1658 * Return TRUE when a valid item was found, FALSE when at end of list or
1659 * something wrong.
1660 */
1661 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001662next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001664 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665 int result;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001666 int flag = in_vim9script() ? LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001667 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001669 if (fi->fi_blob != NULL)
1670 {
1671 typval_T tv;
1672
1673 if (fi->fi_bi >= blob_len(fi->fi_blob))
1674 return FALSE;
1675 tv.v_type = VAR_NUMBER;
1676 tv.v_lock = VAR_FIXED;
1677 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1678 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001679 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001680 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001681 }
1682
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683 item = fi->fi_lw.lw_item;
1684 if (item == NULL)
1685 result = FALSE;
1686 else
1687 {
1688 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001689 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001690 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 }
1692 return result;
1693}
1694
1695/*
1696 * Free the structure used to store info used by ":for".
1697 */
1698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001699free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700{
Bram Moolenaar33570922005-01-25 22:26:29 +00001701 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001703 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001704 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001706 list_unref(fi->fi_list);
1707 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001708 if (fi != NULL && fi->fi_blob != NULL)
1709 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 vim_free(fi);
1711}
1712
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001714set_context_for_expression(
1715 expand_T *xp,
1716 char_u *arg,
1717 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 int got_eq = FALSE;
1720 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001721 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001723 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 {
1725 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001726 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001728 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001729 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730 {
1731 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001732 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001733 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001734 break;
1735 }
1736 return;
1737 }
1738 }
1739 else
1740 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1741 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 while ((xp->xp_pattern = vim_strpbrk(arg,
1743 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1744 {
1745 c = *xp->xp_pattern;
1746 if (c == '&')
1747 {
1748 c = xp->xp_pattern[1];
1749 if (c == '&')
1750 {
1751 ++xp->xp_pattern;
1752 xp->xp_context = cmdidx != CMD_let || got_eq
1753 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1754 }
1755 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001758 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1759 xp->xp_pattern += 2;
1760
1761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 }
1763 else if (c == '$')
1764 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001765 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 xp->xp_context = EXPAND_ENV_VARS;
1767 }
1768 else if (c == '=')
1769 {
1770 got_eq = TRUE;
1771 xp->xp_context = EXPAND_EXPRESSION;
1772 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001773 else if (c == '#'
1774 && xp->xp_context == EXPAND_EXPRESSION)
1775 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001776 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001777 break;
1778 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001779 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 && xp->xp_context == EXPAND_FUNCTIONS
1781 && vim_strchr(xp->xp_pattern, '(') == NULL)
1782 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001783 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 break;
1785 }
1786 else if (cmdidx != CMD_let || got_eq)
1787 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001788 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 {
1790 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1791 if (c == '\\' && xp->xp_pattern[1] != NUL)
1792 ++xp->xp_pattern;
1793 xp->xp_context = EXPAND_NOTHING;
1794 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001795 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001797 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1799 /* skip */ ;
1800 xp->xp_context = EXPAND_NOTHING;
1801 }
1802 else if (c == '|')
1803 {
1804 if (xp->xp_pattern[1] == '|')
1805 {
1806 ++xp->xp_pattern;
1807 xp->xp_context = EXPAND_EXPRESSION;
1808 }
1809 else
1810 xp->xp_context = EXPAND_COMMANDS;
1811 }
1812 else
1813 xp->xp_context = EXPAND_EXPRESSION;
1814 }
1815 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001816 // Doesn't look like something valid, expand as an expression
1817 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 arg = xp->xp_pattern;
1820 if (*arg != NUL)
1821 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1822 /* skip */ ;
1823 }
1824 xp->xp_pattern = arg;
1825}
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001828 * Return TRUE if "pat" matches "text".
1829 * Does not use 'cpo' and always uses 'magic'.
1830 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001831 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001832pattern_match(char_u *pat, char_u *text, int ic)
1833{
1834 int matches = FALSE;
1835 char_u *save_cpo;
1836 regmatch_T regmatch;
1837
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001838 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001839 save_cpo = p_cpo;
1840 p_cpo = (char_u *)"";
1841 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1842 if (regmatch.regprog != NULL)
1843 {
1844 regmatch.rm_ic = ic;
1845 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1846 vim_regfree(regmatch.regprog);
1847 }
1848 p_cpo = save_cpo;
1849 return matches;
1850}
1851
1852/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001853 * Handle a name followed by "(". Both for just "name(arg)" and for
1854 * "expr->name(arg)".
1855 * Returns OK or FAIL.
1856 */
1857 static int
1858eval_func(
1859 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001860 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001861 char_u *name,
1862 int name_len,
1863 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001864 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001865 typval_T *basetv) // "expr" for "expr->name(arg)"
1866{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001867 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001868 char_u *s = name;
1869 int len = name_len;
1870 partial_T *partial;
1871 int ret = OK;
1872
1873 if (!evaluate)
1874 check_vars(s, len);
1875
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001876 // If "s" is the name of a variable of type VAR_FUNC
1877 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001878 s = deref_func_name(s, &len, &partial, !evaluate);
1879
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001880 // Need to make a copy, in case evaluating the arguments makes
1881 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001882 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001883 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001884 ret = FAIL;
1885 else
1886 {
1887 funcexe_T funcexe;
1888
1889 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001890 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001891 funcexe.firstline = curwin->w_cursor.lnum;
1892 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001893 funcexe.evaluate = evaluate;
1894 funcexe.partial = partial;
1895 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001896 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001897 }
1898 vim_free(s);
1899
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001900 // If evaluate is FALSE rettv->v_type was not set in
1901 // get_func_tv, but it's needed in handle_subscript() to parse
1902 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001903 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1904 {
1905 rettv->vval.v_string = NULL;
1906 rettv->v_type = VAR_FUNC;
1907 }
1908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001909 // Stop the expression evaluation when immediately
1910 // aborting on error, or when an interrupt occurred or
1911 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001912 if (evaluate && aborting())
1913 {
1914 if (ret == OK)
1915 clear_tv(rettv);
1916 ret = FAIL;
1917 }
1918 return ret;
1919}
1920
1921/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001922 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1923 * comment) and there is a next line, return the next line (skipping blanks)
1924 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001925 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1926 * "arg" must point somewhere inside a line, not at the start.
1927 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001928 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001929eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1930{
Bram Moolenaar9d489562020-07-30 20:08:50 +02001931 char_u *p = skipwhite(arg);
1932
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001933 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001934 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001935 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001936 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001937 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001938 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02001939 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001940
1941 if (evalarg->eval_cookie != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001942 next = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001943 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001944 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001945
Bram Moolenaar9d489562020-07-30 20:08:50 +02001946 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001947 {
1948 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001949 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001950 }
1951 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02001952 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001953}
1954
1955/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001956 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001957 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001958 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001959eval_next_line(evalarg_T *evalarg)
1960{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001961 garray_T *gap = &evalarg->eval_ga;
1962 char_u *line;
1963
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001964 if (evalarg->eval_cookie != NULL)
1965 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
1966 else
1967 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001968 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001969 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1970 {
1971 // Going to concatenate the lines after parsing.
1972 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1973 ++gap->ga_len;
1974 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001975 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001976 {
1977 vim_free(evalarg->eval_tofree);
1978 evalarg->eval_tofree = line;
1979 }
1980 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001981}
1982
Bram Moolenaare6b53242020-07-01 17:28:33 +02001983/*
1984 * Call eval_next_non_blank() and get the next line if needed.
1985 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001986 char_u *
1987skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1988{
1989 int getnext;
1990 char_u *p = skipwhite(arg);
1991
Bram Moolenaar962d7212020-07-04 14:15:00 +02001992 if (evalarg == NULL)
1993 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02001994 eval_next_non_blank(p, evalarg, &getnext);
1995 if (getnext)
1996 return eval_next_line(evalarg);
1997 return p;
1998}
1999
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002000/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002001 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002002 */
2003 void
2004clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2005{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002006 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002007 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002008 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002009 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002010 if (eap != NULL)
2011 {
2012 // We may need to keep the original command line, e.g. for
2013 // ":let" it has the variable names. But we may also need the
2014 // new one, "nextcmd" points into it. Keep both.
2015 vim_free(eap->cmdline_tofree);
2016 eap->cmdline_tofree = *eap->cmdlinep;
2017 *eap->cmdlinep = evalarg->eval_tofree;
2018 }
2019 else
2020 vim_free(evalarg->eval_tofree);
2021 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002022 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002023
2024 vim_free(evalarg->eval_tofree_lambda);
2025 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002026 }
2027}
2028
2029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002031 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2033 */
2034
2035/*
2036 * Handle zero level expression.
2037 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002038 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002039 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002040 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 * Return OK or FAIL.
2042 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002043 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002044eval0(
2045 char_u *arg,
2046 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002047 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002048 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049{
2050 int ret;
2051 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002052 int did_emsg_before = did_emsg;
2053 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002054 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
2056 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002057 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002058 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002059
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002060 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
2062 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 /*
2065 * Report the invalid expression unless the expression evaluation has
2066 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002067 * exception, or we already gave a more specific error.
2068 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002070 if (!aborting()
2071 && did_emsg == did_emsg_before
2072 && called_emsg == called_emsg_before
2073 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002074 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 ret = FAIL;
2076 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002077
2078 if (eap != NULL)
2079 eap->nextcmd = check_nextcmd(p);
2080
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 return ret;
2082}
2083
2084/*
2085 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002086 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 *
2088 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002089 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002091 * Note: "rettv.v_lock" is not set.
2092 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 * Return OK or FAIL.
2094 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002095 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002096eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002098 char_u *p;
2099 int getnext;
2100
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 /*
2102 * Get the first variable.
2103 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002104 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 return FAIL;
2106
Bram Moolenaar793648f2020-06-26 21:28:25 +02002107 p = eval_next_non_blank(*arg, evalarg, &getnext);
2108 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002110 int result;
2111 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002112 evalarg_T *evalarg_used = evalarg;
2113 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002114 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002115 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116
2117 if (evalarg == NULL)
2118 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002119 CLEAR_FIELD(local_evalarg);
2120 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002121 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002122 orig_flags = evalarg_used->eval_flags;
2123 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002124
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002125 if (getnext)
2126 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002127 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002128 {
2129 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2130 {
2131 error_white_both(p, 1);
2132 clear_tv(rettv);
2133 return FAIL;
2134 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002135 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002136 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002137
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002139 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002141 int error = FALSE;
2142
Bram Moolenaar56acb092020-08-16 14:48:19 +02002143 if (in_vim9script())
2144 result = tv2bool(rettv);
2145 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002148 if (error)
2149 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151
2152 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002153 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002155 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2156 {
2157 error_white_both(p, 1);
2158 clear_tv(rettv);
2159 return FAIL;
2160 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002161 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2162 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002163 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002164 if (eval1(arg, rettv, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 return FAIL;
2166
2167 /*
2168 * Check for the ":".
2169 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002170 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar793648f2020-06-26 21:28:25 +02002171 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 return FAIL;
2177 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002178 if (getnext)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002179 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002180 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002181 {
2182 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2183 {
2184 error_white_both(p, 1);
2185 clear_tv(rettv);
2186 return FAIL;
2187 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002188 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
2191 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002192 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002194 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2195 {
2196 error_white_both(p, 1);
2197 clear_tv(rettv);
2198 return FAIL;
2199 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002200 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2201 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002202 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002203 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
2205 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 return FAIL;
2208 }
2209 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 *rettv = var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002211
2212 if (evalarg == NULL)
2213 clear_evalarg(&local_evalarg, NULL);
2214 else
2215 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217
2218 return OK;
2219}
2220
2221/*
2222 * Handle first level expression:
2223 * expr2 || expr2 || expr2 logical OR
2224 *
2225 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002226 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 *
2228 * Return OK or FAIL.
2229 */
2230 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002231eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002233 char_u *p;
2234 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
2236 /*
2237 * Get the first variable.
2238 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002239 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 return FAIL;
2241
2242 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002243 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002245 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002246 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002248 evalarg_T *evalarg_used = evalarg;
2249 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002250 int evaluate;
2251 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002252 long result = FALSE;
2253 typval_T var2;
2254 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002255 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002256
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002257 if (evalarg == NULL)
2258 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002259 CLEAR_FIELD(local_evalarg);
2260 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002261 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002262 orig_flags = evalarg_used->eval_flags;
2263 evaluate = orig_flags & EVAL_EVALUATE;
2264 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002265 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002266 if (vim9script)
2267 {
2268 result = tv2bool(rettv);
2269 }
2270 else
2271 {
2272 error = FALSE;
2273 if (tv_get_number_chk(rettv, &error) != 0)
2274 result = TRUE;
2275 clear_tv(rettv);
2276 if (error)
2277 return FAIL;
2278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280
2281 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002282 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002284 while (p[0] == '|' && p[1] == '|')
2285 {
2286 if (getnext)
2287 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002288 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002289 {
2290 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2291 {
2292 error_white_both(p, 2);
2293 clear_tv(rettv);
2294 return FAIL;
2295 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002296 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002297 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002298
2299 /*
2300 * Get the second variable.
2301 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002302 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2303 {
2304 error_white_both(p, 2);
2305 clear_tv(rettv);
2306 return FAIL;
2307 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002308 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2309 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002310 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002311 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002312 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002313
2314 /*
2315 * Compute the result.
2316 */
2317 if (evaluate && !result)
2318 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002319 if (vim9script)
2320 {
2321 clear_tv(rettv);
2322 *rettv = var2;
2323 result = tv2bool(rettv);
2324 }
2325 else
2326 {
2327 if (tv_get_number_chk(&var2, &error) != 0)
2328 result = TRUE;
2329 clear_tv(&var2);
2330 if (error)
2331 return FAIL;
2332 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002333 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002334 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002335 {
2336 rettv->v_type = VAR_NUMBER;
2337 rettv->vval.v_number = result;
2338 }
2339
2340 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002342
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002343 if (evalarg == NULL)
2344 clear_evalarg(&local_evalarg, NULL);
2345 else
2346 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
2348
2349 return OK;
2350}
2351
2352/*
2353 * Handle second level expression:
2354 * expr3 && expr3 && expr3 logical AND
2355 *
2356 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002357 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 *
2359 * Return OK or FAIL.
2360 */
2361 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002362eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002364 char_u *p;
2365 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
2367 /*
2368 * Get the first variable.
2369 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002370 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 return FAIL;
2372
2373 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002374 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002376 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002377 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002379 evalarg_T *evalarg_used = evalarg;
2380 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002381 int orig_flags;
2382 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002383 long result = TRUE;
2384 typval_T var2;
2385 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002386 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002387
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002388 if (evalarg == NULL)
2389 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002390 CLEAR_FIELD(local_evalarg);
2391 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002392 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002393 orig_flags = evalarg_used->eval_flags;
2394 evaluate = orig_flags & EVAL_EVALUATE;
2395 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002396 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002397 if (vim9script)
2398 {
2399 result = tv2bool(rettv);
2400 }
2401 else
2402 {
2403 error = FALSE;
2404 if (tv_get_number_chk(rettv, &error) == 0)
2405 result = FALSE;
2406 clear_tv(rettv);
2407 if (error)
2408 return FAIL;
2409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
2411
2412 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002413 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002415 while (p[0] == '&' && p[1] == '&')
2416 {
2417 if (getnext)
2418 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002419 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002420 {
2421 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2422 {
2423 error_white_both(p, 2);
2424 clear_tv(rettv);
2425 return FAIL;
2426 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002427 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002428 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002429
2430 /*
2431 * Get the second variable.
2432 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002433 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2434 {
2435 error_white_both(p, 2);
2436 clear_tv(rettv);
2437 return FAIL;
2438 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002439 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2440 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002441 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002442 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002443 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002444
2445 /*
2446 * Compute the result.
2447 */
2448 if (evaluate && result)
2449 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002450 if (vim9script)
2451 {
2452 clear_tv(rettv);
2453 *rettv = var2;
2454 result = tv2bool(rettv);
2455 }
2456 else
2457 {
2458 if (tv_get_number_chk(&var2, &error) == 0)
2459 result = FALSE;
2460 clear_tv(&var2);
2461 if (error)
2462 return FAIL;
2463 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002464 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002465 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002466 {
2467 rettv->v_type = VAR_NUMBER;
2468 rettv->vval.v_number = result;
2469 }
2470
2471 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002473
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002474 if (evalarg == NULL)
2475 clear_evalarg(&local_evalarg, NULL);
2476 else
2477 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 }
2479
2480 return OK;
2481}
2482
2483/*
2484 * Handle third level expression:
2485 * var1 == var2
2486 * var1 =~ var2
2487 * var1 != var2
2488 * var1 !~ var2
2489 * var1 > var2
2490 * var1 >= var2
2491 * var1 < var2
2492 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002493 * var1 is var2
2494 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 *
2496 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002497 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 *
2499 * Return OK or FAIL.
2500 */
2501 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002502eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002505 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002506 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002508 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510 /*
2511 * Get the first variable.
2512 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002513 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 return FAIL;
2515
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002516 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002517 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
2519 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002520 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002522 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002524 typval_T var2;
2525 int ic;
2526 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002527 int evaluate = evalarg == NULL
2528 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002529
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002530 if (getnext)
2531 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002532 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2533 {
2534 error_white_both(p, len);
2535 clear_tv(rettv);
2536 return FAIL;
2537 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002538
Bram Moolenaar696ba232020-07-29 21:20:41 +02002539 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2540 {
2541 semsg(_(e_invexpr2), p);
2542 clear_tv(rettv);
2543 return FAIL;
2544 }
2545
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002546 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (p[len] == '?')
2548 {
2549 ic = TRUE;
2550 ++len;
2551 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002552 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 else if (p[len] == '#')
2554 {
2555 ic = FALSE;
2556 ++len;
2557 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002558 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002560 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
2562 /*
2563 * Get the second variable.
2564 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002565 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2566 {
2567 error_white_both(p, 1);
2568 clear_tv(rettv);
2569 return FAIL;
2570 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002571 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002572 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 return FAIL;
2576 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002577 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002578 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002579 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002580
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002581 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002582 {
2583 ret = FAIL;
2584 clear_tv(rettv);
2585 }
2586 else
2587 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002588 clear_tv(&var2);
2589 return ret;
2590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592
2593 return OK;
2594}
2595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 void
2597eval_addblob(typval_T *tv1, typval_T *tv2)
2598{
2599 blob_T *b1 = tv1->vval.v_blob;
2600 blob_T *b2 = tv2->vval.v_blob;
2601 blob_T *b = blob_alloc();
2602 int i;
2603
2604 if (b != NULL)
2605 {
2606 for (i = 0; i < blob_len(b1); i++)
2607 ga_append(&b->bv_ga, blob_get(b1, i));
2608 for (i = 0; i < blob_len(b2); i++)
2609 ga_append(&b->bv_ga, blob_get(b2, i));
2610
2611 clear_tv(tv1);
2612 rettv_blob_set(tv1, b);
2613 }
2614}
2615
2616 int
2617eval_addlist(typval_T *tv1, typval_T *tv2)
2618{
2619 typval_T var3;
2620
2621 // concatenate Lists
2622 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2623 {
2624 clear_tv(tv1);
2625 clear_tv(tv2);
2626 return FAIL;
2627 }
2628 clear_tv(tv1);
2629 *tv1 = var3;
2630 return OK;
2631}
2632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633/*
2634 * Handle fourth level expression:
2635 * + number addition
2636 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002637 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002638 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 *
2640 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002641 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 *
2643 * Return OK or FAIL.
2644 */
2645 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002646eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 /*
2649 * Get the first variable.
2650 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002651 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 return FAIL;
2653
2654 /*
2655 * Repeat computing, until no '+', '-' or '.' is following.
2656 */
2657 for (;;)
2658 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002659 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002660 int getnext;
2661 char_u *p;
2662 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002663 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002664 int concat;
2665 typval_T var2;
2666
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002667 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002668 p = eval_next_non_blank(*arg, evalarg, &getnext);
2669 op = *p;
2670 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002671 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002673
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002674 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002675 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002676 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002677 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002678 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002679 {
2680 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2681 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002682 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002683 clear_tv(rettv);
2684 return FAIL;
2685 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002686 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002687 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002688 if ((op != '+' || (rettv->v_type != VAR_LIST
2689 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002690#ifdef FEAT_FLOAT
2691 && (op == '.' || rettv->v_type != VAR_FLOAT)
2692#endif
2693 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002695 // For "list + ...", an illegal use of the first operand as
2696 // a number cannot be determined before evaluating the 2nd
2697 // operand: if this is also a list, all is ok.
2698 // For "something . ...", "something - ..." or "non-list + ...",
2699 // we know that the first operand needs to be a string or number
2700 // without evaluating the 2nd operand. So check before to avoid
2701 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002702 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002703 {
2704 clear_tv(rettv);
2705 return FAIL;
2706 }
2707 }
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 /*
2710 * Get the second variable.
2711 */
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002712 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[oplen]))
2713 {
2714 error_white_both(p, oplen);
2715 clear_tv(rettv);
2716 return FAIL;
2717 }
2718 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002719 if (eval6(arg, &var2, evalarg, !in_vim9script() && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002721 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 return FAIL;
2723 }
2724
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002725 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
2727 /*
2728 * Compute the result.
2729 */
2730 if (op == '.')
2731 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002732 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2733 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002734 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002735
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002736 if (in_vim9script() && (var2.v_type == VAR_VOID
2737 || var2.v_type == VAR_CHANNEL
2738 || var2.v_type == VAR_JOB))
2739 emsg(_(e_inval_string));
2740#ifdef FEAT_FLOAT
2741 else if (var2.v_type == VAR_FLOAT)
2742 {
2743 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2744 var2.vval.v_float);
2745 s2 = buf2;
2746 }
2747#endif
2748 else
2749 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002750 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002751 {
2752 clear_tv(rettv);
2753 clear_tv(&var2);
2754 return FAIL;
2755 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002756 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002757 clear_tv(rettv);
2758 rettv->v_type = VAR_STRING;
2759 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002761 else if (op == '+' && rettv->v_type == VAR_BLOB
2762 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002764 else if (op == '+' && rettv->v_type == VAR_LIST
2765 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002766 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002768 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 else
2771 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002772 int error = FALSE;
2773 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002774#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002775 float_T f1 = 0, f2 = 0;
2776
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002777 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002778 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002779 f1 = rettv->vval.v_float;
2780 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002781 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002782 else
2783#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002785 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002786 if (error)
2787 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002788 // This can only happen for "list + non-list". For
2789 // "non-list + ..." or "something - ...", we returned
2790 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002791 clear_tv(rettv);
2792 return FAIL;
2793 }
2794#ifdef FEAT_FLOAT
2795 if (var2.v_type == VAR_FLOAT)
2796 f1 = n1;
2797#endif
2798 }
2799#ifdef FEAT_FLOAT
2800 if (var2.v_type == VAR_FLOAT)
2801 {
2802 f2 = var2.vval.v_float;
2803 n2 = 0;
2804 }
2805 else
2806#endif
2807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002808 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002809 if (error)
2810 {
2811 clear_tv(rettv);
2812 clear_tv(&var2);
2813 return FAIL;
2814 }
2815#ifdef FEAT_FLOAT
2816 if (rettv->v_type == VAR_FLOAT)
2817 f2 = n2;
2818#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002819 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002820 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002821
2822#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002823 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002824 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2825 {
2826 if (op == '+')
2827 f1 = f1 + f2;
2828 else
2829 f1 = f1 - f2;
2830 rettv->v_type = VAR_FLOAT;
2831 rettv->vval.v_float = f1;
2832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002834#endif
2835 {
2836 if (op == '+')
2837 n1 = n1 + n2;
2838 else
2839 n1 = n1 - n2;
2840 rettv->v_type = VAR_NUMBER;
2841 rettv->vval.v_number = n1;
2842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002844 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
2846 }
2847 return OK;
2848}
2849
2850/*
2851 * Handle fifth level expression:
2852 * * number multiplication
2853 * / number division
2854 * % number modulo
2855 *
2856 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002857 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 *
2859 * Return OK or FAIL.
2860 */
2861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002862eval6(
2863 char_u **arg,
2864 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002865 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002866 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002868#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002869 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
2872 /*
2873 * Get the first variable.
2874 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002875 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 return FAIL;
2877
2878 /*
2879 * Repeat computing, until no '*', '/' or '%' is following.
2880 */
2881 for (;;)
2882 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002883 int evaluate;
2884 int getnext;
2885 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002886 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002887 int op;
2888 varnumber_T n1, n2;
2889#ifdef FEAT_FLOAT
2890 float_T f1, f2;
2891#endif
2892 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002893
Bram Moolenaar9d489562020-07-30 20:08:50 +02002894 p = eval_next_non_blank(*arg, evalarg, &getnext);
2895 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002896 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002898
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002899 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002900 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002901 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002902 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002903 {
2904 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2905 {
2906 error_white_both(p, 1);
2907 clear_tv(rettv);
2908 return FAIL;
2909 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002910 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002913#ifdef FEAT_FLOAT
2914 f1 = 0;
2915 f2 = 0;
2916#endif
2917 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002918 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002920#ifdef FEAT_FLOAT
2921 if (rettv->v_type == VAR_FLOAT)
2922 {
2923 f1 = rettv->vval.v_float;
2924 use_float = TRUE;
2925 n1 = 0;
2926 }
2927 else
2928#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002929 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002931 if (error)
2932 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934 else
2935 n1 = 0;
2936
2937 /*
2938 * Get the second variable.
2939 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002940 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2941 {
2942 error_white_both(p, 1);
2943 clear_tv(rettv);
2944 return FAIL;
2945 }
2946 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002947 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 return FAIL;
2949
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002950 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002952#ifdef FEAT_FLOAT
2953 if (var2.v_type == VAR_FLOAT)
2954 {
2955 if (!use_float)
2956 {
2957 f1 = n1;
2958 use_float = TRUE;
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 clear_tv(&var2);
2968 if (error)
2969 return FAIL;
2970#ifdef FEAT_FLOAT
2971 if (use_float)
2972 f2 = n2;
2973#endif
2974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
2976 /*
2977 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002978 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002980#ifdef FEAT_FLOAT
2981 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002983 if (op == '*')
2984 f1 = f1 * f2;
2985 else if (op == '/')
2986 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002987# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002988 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002989 if (f2 == 0.0)
2990 {
2991 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002992 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002993 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002994 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002995 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002996 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002997 }
2998 else
2999 f1 = f1 / f2;
3000# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003001 // We rely on the floating point library to handle divide
3002 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003003 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003004# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003007 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003009 return FAIL;
3010 }
3011 rettv->v_type = VAR_FLOAT;
3012 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
3014 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003017 if (op == '*')
3018 n1 = n1 * n2;
3019 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003020 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003022 n1 = num_modulus(n1, n2);
3023
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024 rettv->v_type = VAR_NUMBER;
3025 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 }
3028 }
3029
3030 return OK;
3031}
3032
3033/*
3034 * Handle sixth level expression:
3035 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003036 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003037 * "string" string constant
3038 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 * &option-name option value
3040 * @r register contents
3041 * identifier variable value
3042 * function() function call
3043 * $VAR environment variable
3044 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003045 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003047 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003048 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 *
3050 * Also handle:
3051 * ! in front logical NOT
3052 * - in front unary minus
3053 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003054 * trailing [] subscript in String or List
3055 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003056 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 *
3058 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003059 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 *
3061 * Return OK or FAIL.
3062 */
3063 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003064eval7(
3065 char_u **arg,
3066 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003067 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003070 int evaluate = evalarg != NULL
3071 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 int len;
3073 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 char_u *start_leader, *end_leader;
3075 int ret = OK;
3076 char_u *alias;
3077
3078 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003079 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003080 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003082 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
3084 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003085 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 */
3087 start_leader = *arg;
3088 while (**arg == '!' || **arg == '-' || **arg == '+')
3089 *arg = skipwhite(*arg + 1);
3090 end_leader = *arg;
3091
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003092 if (**arg == '.' && (!isdigit(*(*arg + 1))
3093#ifdef FEAT_FLOAT
3094 || current_sctx.sc_version < 2
3095#endif
3096 ))
3097 {
3098 semsg(_(e_invexpr2), *arg);
3099 ++*arg;
3100 return FAIL;
3101 }
3102
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 switch (**arg)
3104 {
3105 /*
3106 * Number constant.
3107 */
3108 case '0':
3109 case '1':
3110 case '2':
3111 case '3':
3112 case '4':
3113 case '5':
3114 case '6':
3115 case '7':
3116 case '8':
3117 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003118 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003119
3120 // Apply prefixed "-" and "+" now. Matters especially when
3121 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003122 if (ret == OK && evaluate && end_leader > start_leader
3123 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003124 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 break;
3126
3127 /*
3128 * String constant: "string".
3129 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003130 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 break;
3132
3133 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003134 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003136 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003137 break;
3138
3139 /*
3140 * List: [expr, expr]
3141 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003142 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 break;
3144
3145 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003146 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003147 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003148 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003149 {
3150 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003151 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003152 }
3153 else
3154 ret = NOTDONE;
3155 break;
3156
3157 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003158 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003159 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003160 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003161 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003162 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003163 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003164 break;
3165
3166 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003167 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003169 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 break;
3171
3172 /*
3173 * Environment variable: $VAR.
3174 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003175 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 break;
3177
3178 /*
3179 * Register contents: @r.
3180 */
3181 case '@': ++*arg;
3182 if (evaluate)
3183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003184 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003185 rettv->vval.v_string = get_reg_contents(**arg,
3186 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 }
3188 if (**arg != NUL)
3189 ++*arg;
3190 break;
3191
3192 /*
3193 * nested expression: (expression).
3194 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003195 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003196 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003197 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003198
Bram Moolenaar9215f012020-06-27 21:18:00 +02003199 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003200 if (**arg == ')')
3201 ++*arg;
3202 else if (ret == OK)
3203 {
3204 emsg(_(e_missing_close));
3205 clear_tv(rettv);
3206 ret = FAIL;
3207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 }
3209 break;
3210
Bram Moolenaar8c711452005-01-14 21:53:12 +00003211 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 break;
3213 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003214
3215 if (ret == NOTDONE)
3216 {
3217 /*
3218 * Must be a variable or function name.
3219 * Can also be a curly-braces kind of name: {expr}.
3220 */
3221 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003222 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003223 if (alias != NULL)
3224 s = alias;
3225
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003226 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003227 ret = FAIL;
3228 else
3229 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003230 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3231
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003232 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3233 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003234 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003235 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003236 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003237 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003238 else if (flags & EVAL_CONSTANT)
3239 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003240 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003241 {
3242 // get the value of "true", "false" or a variable
3243 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3244 {
3245 rettv->v_type = VAR_BOOL;
3246 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003247 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003248 }
3249 else if (len == 5 && in_vim9script()
3250 && STRNCMP(s, "false", 4) == 0)
3251 {
3252 rettv->v_type = VAR_BOOL;
3253 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003254 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003255 }
3256 else
3257 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3258 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003259 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003260 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003261 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003262 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003263 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003264 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003265 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003266 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003267 }
3268
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003269 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3270 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003271 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003272 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
3274 /*
3275 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3276 */
3277 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003278 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003279 return ret;
3280}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003281
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003282/*
3283 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003284 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003285 * Adjusts "end_leaderp" until it is at "start_leader".
3286 */
3287 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003288eval7_leader(
3289 typval_T *rettv,
3290 int numeric_only,
3291 char_u *start_leader,
3292 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003293{
3294 char_u *end_leader = *end_leaderp;
3295 int ret = OK;
3296 int error = FALSE;
3297 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003298 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003299#ifdef FEAT_FLOAT
3300 float_T f = 0.0;
3301
3302 if (rettv->v_type == VAR_FLOAT)
3303 f = rettv->vval.v_float;
3304 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003305#endif
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003306 if (in_vim9script() && end_leader[-1] == '!')
3307 val = tv2bool(rettv);
3308 else
3309 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003310 if (error)
3311 {
3312 clear_tv(rettv);
3313 ret = FAIL;
3314 }
3315 else
3316 {
3317 while (end_leader > start_leader)
3318 {
3319 --end_leader;
3320 if (*end_leader == '!')
3321 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003322 if (numeric_only)
3323 {
3324 ++end_leader;
3325 break;
3326 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003327#ifdef FEAT_FLOAT
3328 if (rettv->v_type == VAR_FLOAT)
3329 f = !f;
3330 else
3331#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003332 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003333 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003334 type = VAR_BOOL;
3335 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003336 }
3337 else if (*end_leader == '-')
3338 {
3339#ifdef FEAT_FLOAT
3340 if (rettv->v_type == VAR_FLOAT)
3341 f = -f;
3342 else
3343#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003344 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003345 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003346 type = VAR_NUMBER;
3347 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003348 }
3349 }
3350#ifdef FEAT_FLOAT
3351 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003353 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003354 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003356 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003357#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003358 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003359 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003360 if (in_vim9script())
3361 rettv->v_type = type;
3362 else
3363 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003364 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003367 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 return ret;
3369}
3370
3371/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003372 * Call the function referred to in "rettv".
3373 */
3374 static int
3375call_func_rettv(
3376 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003377 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003378 typval_T *rettv,
3379 int evaluate,
3380 dict_T *selfdict,
3381 typval_T *basetv)
3382{
3383 partial_T *pt = NULL;
3384 funcexe_T funcexe;
3385 typval_T functv;
3386 char_u *s;
3387 int ret;
3388
3389 // need to copy the funcref so that we can clear rettv
3390 if (evaluate)
3391 {
3392 functv = *rettv;
3393 rettv->v_type = VAR_UNKNOWN;
3394
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003395 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003396 if (functv.v_type == VAR_PARTIAL)
3397 {
3398 pt = functv.vval.v_partial;
3399 s = partial_name(pt);
3400 }
3401 else
3402 s = functv.vval.v_string;
3403 }
3404 else
3405 s = (char_u *)"";
3406
Bram Moolenaara80faa82020-04-12 19:37:17 +02003407 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003408 funcexe.firstline = curwin->w_cursor.lnum;
3409 funcexe.lastline = curwin->w_cursor.lnum;
3410 funcexe.evaluate = evaluate;
3411 funcexe.partial = pt;
3412 funcexe.selfdict = selfdict;
3413 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003414 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003416 // Clear the funcref afterwards, so that deleting it while
3417 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003418 if (evaluate)
3419 clear_tv(&functv);
3420
3421 return ret;
3422}
3423
3424/*
3425 * Evaluate "->method()".
3426 * "*arg" points to the '-'.
3427 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3428 */
3429 static int
3430eval_lambda(
3431 char_u **arg,
3432 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003433 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003434 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003435{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003436 int evaluate = evalarg != NULL
3437 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003438 typval_T base = *rettv;
3439 int ret;
3440
3441 // Skip over the ->.
3442 *arg += 2;
3443 rettv->v_type = VAR_UNKNOWN;
3444
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003445 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003446 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003447 return FAIL;
3448 else if (**arg != '(')
3449 {
3450 if (verbose)
3451 {
3452 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003453 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003454 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003456 }
3457 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003458 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003459 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003460 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003461 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003462
3463 // Clear the funcref afterwards, so that deleting it while
3464 // evaluating the arguments is possible (see test55).
3465 if (evaluate)
3466 clear_tv(&base);
3467
3468 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003469}
3470
3471/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003472 * Evaluate "->method()".
3473 * "*arg" points to the '-'.
3474 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3475 */
3476 static int
3477eval_method(
3478 char_u **arg,
3479 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003480 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003481 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003482{
3483 char_u *name;
3484 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003485 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003486 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003487 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003488 int evaluate = evalarg != NULL
3489 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003490
3491 // Skip over the ->.
3492 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003493 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003494
Bram Moolenaarac92e252019-08-03 21:58:38 +02003495 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003496 len = get_name_len(arg, &alias, evaluate, TRUE);
3497 if (alias != NULL)
3498 name = alias;
3499
3500 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003501 {
3502 if (verbose)
3503 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003504 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003505 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003506 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003507 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003508 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003509 if (**arg != '(')
3510 {
3511 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003513 ret = FAIL;
3514 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003515 else if (VIM_ISWHITE((*arg)[-1]))
3516 {
3517 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003518 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003519 ret = FAIL;
3520 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003521 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003522 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003523 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003524 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003525
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003526 // Clear the funcref afterwards, so that deleting it while
3527 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003528 if (evaluate)
3529 clear_tv(&base);
3530
Bram Moolenaarac92e252019-08-03 21:58:38 +02003531 return ret;
3532}
3533
3534/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003535 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3536 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003537 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3538 */
3539 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003540eval_index(
3541 char_u **arg,
3542 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003543 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003544 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003545{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003546 int evaluate = evalarg != NULL
3547 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003549 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003550 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003551 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003552 int keylen = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003553
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003554 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3555 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003556
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003557 init_tv(&var1);
3558 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003559 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003560 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003561 /*
3562 * dict.name
3563 */
3564 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003565 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003566 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003567 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003568 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003569 *arg = skipwhite(key + keylen);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003570 }
3571 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003572 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003573 /*
3574 * something[idx]
3575 *
3576 * Get the (first) variable from inside the [].
3577 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003578 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003579 if (**arg == ':')
3580 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003581 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003582 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003583 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003584 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003585 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003586 clear_tv(&var1);
3587 return FAIL;
3588 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589
3590 /*
3591 * Get the second variable from inside the [:].
3592 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003593 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003594 if (**arg == ':')
3595 {
3596 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003597 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003598 if (**arg == ']')
3599 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003600 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003602 if (!empty1)
3603 clear_tv(&var1);
3604 return FAIL;
3605 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003606 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003607 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003608 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003609 if (!empty1)
3610 clear_tv(&var1);
3611 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003612 return FAIL;
3613 }
3614 }
3615
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003616 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003617 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003618 if (**arg != ']')
3619 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003620 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003621 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 clear_tv(&var1);
3623 if (range)
3624 clear_tv(&var2);
3625 return FAIL;
3626 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003627 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003628 }
3629
3630 if (evaluate)
3631 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003632 int res = eval_index_inner(rettv, range,
3633 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3634 key, keylen, verbose);
3635 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003636 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003637 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003638 clear_tv(&var2);
3639 return res;
3640 }
3641 return OK;
3642}
3643
3644/*
3645 * Check if "rettv" can have an [index] or [sli:ce]
3646 */
3647 int
3648check_can_index(typval_T *rettv, int evaluate, int verbose)
3649{
3650 switch (rettv->v_type)
3651 {
3652 case VAR_FUNC:
3653 case VAR_PARTIAL:
3654 if (verbose)
3655 emsg(_("E695: Cannot index a Funcref"));
3656 return FAIL;
3657 case VAR_FLOAT:
3658#ifdef FEAT_FLOAT
3659 if (verbose)
3660 emsg(_(e_float_as_string));
3661 return FAIL;
3662#endif
3663 case VAR_BOOL:
3664 case VAR_SPECIAL:
3665 case VAR_JOB:
3666 case VAR_CHANNEL:
3667 if (verbose)
3668 emsg(_(e_cannot_index_special_variable));
3669 return FAIL;
3670 case VAR_UNKNOWN:
3671 case VAR_ANY:
3672 case VAR_VOID:
3673 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003674 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003675 emsg(_(e_cannot_index_special_variable));
3676 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003677 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003678 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003679
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003680 case VAR_STRING:
3681 case VAR_LIST:
3682 case VAR_DICT:
3683 case VAR_BLOB:
3684 break;
3685 case VAR_NUMBER:
3686 if (in_vim9script())
3687 emsg(_(e_cannot_index_number));
3688 break;
3689 }
3690 return OK;
3691}
3692
3693/*
3694 * Apply index or range to "rettv".
3695 * "var1" is the first index, NULL for [:expr].
3696 * "var2" is the second index, NULL for [expr] and [expr: ]
3697 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3698 */
3699 int
3700eval_index_inner(
3701 typval_T *rettv,
3702 int is_range,
3703 typval_T *var1,
3704 typval_T *var2,
3705 char_u *key,
3706 int keylen,
3707 int verbose)
3708{
3709 long n1, n2 = 0;
3710 long len;
3711
3712 n1 = 0;
3713 if (var1 != NULL && rettv->v_type != VAR_DICT)
3714 n1 = tv_get_number(var1);
3715
3716 if (is_range)
3717 {
3718 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003719 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003720 if (verbose)
3721 emsg(_(e_cannot_slice_dictionary));
3722 return FAIL;
3723 }
3724 if (var2 == NULL)
3725 n2 = -1;
3726 else
3727 n2 = tv_get_number(var2);
3728 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003729
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003730 switch (rettv->v_type)
3731 {
3732 case VAR_UNKNOWN:
3733 case VAR_ANY:
3734 case VAR_VOID:
3735 case VAR_FUNC:
3736 case VAR_PARTIAL:
3737 case VAR_FLOAT:
3738 case VAR_BOOL:
3739 case VAR_SPECIAL:
3740 case VAR_JOB:
3741 case VAR_CHANNEL:
3742 break; // not evaluating, skipping over subscript
3743
3744 case VAR_NUMBER:
3745 case VAR_STRING:
3746 {
3747 char_u *s = tv_get_string(rettv);
3748
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003749 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003750 if (in_vim9script())
3751 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003752 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003753 s = string_slice(s, n1, n2);
3754 else
3755 s = char_from_string(s, n1);
3756 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003757 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003758 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003759 // The resulting variable is a substring. If the indexes
3760 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003761 if (n1 < 0)
3762 {
3763 n1 = len + n1;
3764 if (n1 < 0)
3765 n1 = 0;
3766 }
3767 if (n2 < 0)
3768 n2 = len + n2;
3769 else if (n2 >= len)
3770 n2 = len;
3771 if (n1 >= len || n2 < 0 || n1 > n2)
3772 s = NULL;
3773 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003774 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003775 }
3776 else
3777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003778 // The resulting variable is a string of a single
3779 // character. If the index is too big or negative the
3780 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003781 if (n1 >= len || n1 < 0)
3782 s = NULL;
3783 else
3784 s = vim_strnsave(s + n1, 1);
3785 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003786 clear_tv(rettv);
3787 rettv->v_type = VAR_STRING;
3788 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003789 }
3790 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003791
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003792 case VAR_BLOB:
3793 len = blob_len(rettv->vval.v_blob);
3794 if (is_range)
3795 {
3796 // The resulting variable is a sub-blob. If the indexes
3797 // are out of range the result is empty.
3798 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003799 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003800 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003801 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003802 n1 = 0;
3803 }
3804 if (n2 < 0)
3805 n2 = len + n2;
3806 else if (n2 >= len)
3807 n2 = len - 1;
3808 if (n1 >= len || n2 < 0 || n1 > n2)
3809 {
3810 clear_tv(rettv);
3811 rettv->v_type = VAR_BLOB;
3812 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003813 }
3814 else
3815 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003816 blob_T *blob = blob_alloc();
3817 long i;
3818
3819 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003820 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003821 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003822 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003823 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003824 return FAIL;
3825 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003826 blob->bv_ga.ga_len = n2 - n1 + 1;
3827 for (i = n1; i <= n2; i++)
3828 blob_set(blob, i - n1,
3829 blob_get(rettv->vval.v_blob, i));
3830
3831 clear_tv(rettv);
3832 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003833 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003834 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003835 }
3836 else
3837 {
3838 // The resulting variable is a byte value.
3839 // If the index is too big or negative that is an error.
3840 if (n1 < 0)
3841 n1 = len + n1;
3842 if (n1 < len && n1 >= 0)
3843 {
3844 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003845
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003846 clear_tv(rettv);
3847 rettv->v_type = VAR_NUMBER;
3848 rettv->vval.v_number = v;
3849 }
3850 else
3851 semsg(_(e_blobidx), n1);
3852 }
3853 break;
3854
3855 case VAR_LIST:
3856 if (var1 == NULL)
3857 n1 = 0;
3858 if (var2 == NULL)
3859 n2 = -1;
3860 if (list_slice_or_index(rettv->vval.v_list,
3861 is_range, n1, n2, rettv, verbose) == FAIL)
3862 return FAIL;
3863 break;
3864
3865 case VAR_DICT:
3866 {
3867 dictitem_T *item;
3868 typval_T tmp;
3869
3870 if (key == NULL)
3871 {
3872 key = tv_get_string_chk(var1);
3873 if (key == NULL)
3874 return FAIL;
3875 }
3876
3877 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
3878
3879 if (item == NULL && verbose)
3880 semsg(_(e_dictkey), key);
3881 if (item == NULL)
3882 return FAIL;
3883
3884 copy_tv(&item->di_tv, &tmp);
3885 clear_tv(rettv);
3886 *rettv = tmp;
3887 }
3888 break;
3889 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003890 return OK;
3891}
3892
3893/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003894 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003895 */
3896 char_u *
3897partial_name(partial_T *pt)
3898{
3899 if (pt->pt_name != NULL)
3900 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003901 if (pt->pt_func != NULL)
3902 return pt->pt_func->uf_name;
3903 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003904}
3905
Bram Moolenaarddecc252016-04-06 22:59:37 +02003906 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003907partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003908{
3909 int i;
3910
3911 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003912 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003913 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003914 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003915 if (pt->pt_name != NULL)
3916 {
3917 func_unref(pt->pt_name);
3918 vim_free(pt->pt_name);
3919 }
3920 else
3921 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003922
3923 if (pt->pt_funcstack != NULL)
3924 {
3925 // Decrease the reference count for the context of a closure. If down
3926 // to zero free it and clear the variables on the stack.
3927 if (--pt->pt_funcstack->fs_refcount == 0)
3928 {
3929 garray_T *gap = &pt->pt_funcstack->fs_ga;
3930 typval_T *stack = gap->ga_data;
3931
3932 for (i = 0; i < gap->ga_len; ++i)
3933 clear_tv(stack + i);
3934 ga_clear(gap);
3935 vim_free(pt->pt_funcstack);
3936 }
3937 pt->pt_funcstack = NULL;
3938 }
3939
Bram Moolenaarddecc252016-04-06 22:59:37 +02003940 vim_free(pt);
3941}
3942
3943/*
3944 * Unreference a closure: decrement the reference count and free it when it
3945 * becomes zero.
3946 */
3947 void
3948partial_unref(partial_T *pt)
3949{
3950 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003951 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003952}
3953
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003954/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003955 * Return the next (unique) copy ID.
3956 * Used for serializing nested structures.
3957 */
3958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003959get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003960{
3961 current_copyID += COPYID_INC;
3962 return current_copyID;
3963}
3964
3965/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003966 * Garbage collection for lists and dictionaries.
3967 *
3968 * We use reference counts to be able to free most items right away when they
3969 * are no longer used. But for composite items it's possible that it becomes
3970 * unused while the reference count is > 0: When there is a recursive
3971 * reference. Example:
3972 * :let l = [1, 2, 3]
3973 * :let d = {9: l}
3974 * :let l[1] = d
3975 *
3976 * Since this is quite unusual we handle this with garbage collection: every
3977 * once in a while find out which lists and dicts are not referenced from any
3978 * variable.
3979 *
3980 * Here is a good reference text about garbage collection (refers to Python
3981 * but it applies to all reference-counting mechanisms):
3982 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003983 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003984
3985/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003986 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003987 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003988 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003989 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003990 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003991garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003992{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003993 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003994 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003995 buf_T *buf;
3996 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003997 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003998 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003999
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004000 if (!testing)
4001 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004002 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004003 want_garbage_collect = FALSE;
4004 may_garbage_collect = FALSE;
4005 garbage_collect_at_exit = FALSE;
4006 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004007
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004008 // The execution stack can grow big, limit the size.
4009 if (exestack.ga_maxlen - exestack.ga_len > 500)
4010 {
4011 size_t new_len;
4012 char_u *pp;
4013 int n;
4014
4015 // Keep 150% of the current size, with a minimum of the growth size.
4016 n = exestack.ga_len / 2;
4017 if (n < exestack.ga_growsize)
4018 n = exestack.ga_growsize;
4019
4020 // Don't make it bigger though.
4021 if (exestack.ga_len + n < exestack.ga_maxlen)
4022 {
4023 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4024 pp = vim_realloc(exestack.ga_data, new_len);
4025 if (pp == NULL)
4026 return FAIL;
4027 exestack.ga_maxlen = exestack.ga_len + n;
4028 exestack.ga_data = pp;
4029 }
4030 }
4031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004032 // We advance by two because we add one for items referenced through
4033 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004034 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004035
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004036 /*
4037 * 1. Go through all accessible variables and mark all lists and dicts
4038 * with copyID.
4039 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004041 // Don't free variables in the previous_funccal list unless they are only
4042 // referenced through previous_funccal. This must be first, because if
4043 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004044 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004045
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004046 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004047 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004049 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004050 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004051 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4052 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004054 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004055 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004056 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4057 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004058 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004059 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4060 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004061#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004062 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004063 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4064 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004065 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004066 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004067 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4068 NULL, NULL);
4069#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004070
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004071 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004072 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004073 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4074 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004075 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004076 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004078 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004080
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004081 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004082 abort = abort || set_ref_in_functions(copyID);
4083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004084 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004085 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004087 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004088 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004089
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004090 // callbacks in buffers
4091 abort = abort || set_ref_in_buffers(copyID);
4092
Bram Moolenaar1dced572012-04-05 16:54:08 +02004093#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004094 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004095#endif
4096
Bram Moolenaardb913952012-06-29 12:54:53 +02004097#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004098 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004099#endif
4100
4101#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004102 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004103#endif
4104
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004105#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004106 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004107 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004108#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004109#ifdef FEAT_NETBEANS_INTG
4110 abort = abort || set_ref_in_nb_channel(copyID);
4111#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004112
Bram Moolenaare3188e22016-05-31 21:13:04 +02004113#ifdef FEAT_TIMERS
4114 abort = abort || set_ref_in_timer(copyID);
4115#endif
4116
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004117#ifdef FEAT_QUICKFIX
4118 abort = abort || set_ref_in_quickfix(copyID);
4119#endif
4120
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004121#ifdef FEAT_TERMINAL
4122 abort = abort || set_ref_in_term(copyID);
4123#endif
4124
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004125#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004126 abort = abort || set_ref_in_popups(copyID);
4127#endif
4128
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004129 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004130 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004131 /*
4132 * 2. Free lists and dictionaries that are not referenced.
4133 */
4134 did_free = free_unref_items(copyID);
4135
4136 /*
4137 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004138 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004139 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004141 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004142 else if (p_verbose > 0)
4143 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004144 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004145 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004146
4147 return did_free;
4148}
4149
4150/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004151 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004152 */
4153 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004154free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004155{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004156 int did_free = FALSE;
4157
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004158 // Let all "free" functions know that we are here. This means no
4159 // dictionaries, lists, channels or jobs are to be freed, because we will
4160 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004161 in_free_unref_items = TRUE;
4162
4163 /*
4164 * PASS 1: free the contents of the items. We don't free the items
4165 * themselves yet, so that it is possible to decrement refcount counters
4166 */
4167
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004168 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004169 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004170
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004171 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004172 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004173
4174#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004175 // Go through the list of jobs and free items without the copyID. This
4176 // must happen before doing channels, because jobs refer to channels, but
4177 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004178 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4179
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004180 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004181 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4182#endif
4183
4184 /*
4185 * PASS 2: free the items themselves.
4186 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004187 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004188 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004189
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004190#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004191 // Go through the list of jobs and free items without the copyID. This
4192 // must happen before doing channels, because jobs refer to channels, but
4193 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004194 free_unused_jobs(copyID, COPYID_MASK);
4195
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004196 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004197 free_unused_channels(copyID, COPYID_MASK);
4198#endif
4199
4200 in_free_unref_items = FALSE;
4201
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004202 return did_free;
4203}
4204
4205/*
4206 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004207 * "list_stack" is used to add lists to be marked. Can be NULL.
4208 *
4209 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004210 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004211 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004212set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004213{
4214 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004215 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004216 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004217 hashtab_T *cur_ht;
4218 ht_stack_T *ht_stack = NULL;
4219 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004220
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004221 cur_ht = ht;
4222 for (;;)
4223 {
4224 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004225 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004226 // Mark each item in the hashtab. If the item contains a hashtab
4227 // it is added to ht_stack, if it contains a list it is added to
4228 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004229 todo = (int)cur_ht->ht_used;
4230 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4231 if (!HASHITEM_EMPTY(hi))
4232 {
4233 --todo;
4234 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4235 &ht_stack, list_stack);
4236 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004237 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004238
4239 if (ht_stack == NULL)
4240 break;
4241
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004242 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004243 cur_ht = ht_stack->ht;
4244 tempitem = ht_stack;
4245 ht_stack = ht_stack->prev;
4246 free(tempitem);
4247 }
4248
4249 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004250}
4251
4252/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004253 * Mark a dict and its items with "copyID".
4254 * Returns TRUE if setting references failed somehow.
4255 */
4256 int
4257set_ref_in_dict(dict_T *d, int copyID)
4258{
4259 if (d != NULL && d->dv_copyID != copyID)
4260 {
4261 d->dv_copyID = copyID;
4262 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4263 }
4264 return FALSE;
4265}
4266
4267/*
4268 * Mark a list and its items with "copyID".
4269 * Returns TRUE if setting references failed somehow.
4270 */
4271 int
4272set_ref_in_list(list_T *ll, int copyID)
4273{
4274 if (ll != NULL && ll->lv_copyID != copyID)
4275 {
4276 ll->lv_copyID = copyID;
4277 return set_ref_in_list_items(ll, copyID, NULL);
4278 }
4279 return FALSE;
4280}
4281
4282/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004283 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004284 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4285 *
4286 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004287 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004288 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004289set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004290{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004291 listitem_T *li;
4292 int abort = FALSE;
4293 list_T *cur_l;
4294 list_stack_T *list_stack = NULL;
4295 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004296
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004297 cur_l = l;
4298 for (;;)
4299 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004300 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004301 // Mark each item in the list. If the item contains a hashtab
4302 // it is added to ht_stack, if it contains a list it is added to
4303 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004304 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4305 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4306 ht_stack, &list_stack);
4307 if (list_stack == NULL)
4308 break;
4309
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004310 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004311 cur_l = list_stack->list;
4312 tempitem = list_stack;
4313 list_stack = list_stack->prev;
4314 free(tempitem);
4315 }
4316
4317 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004318}
4319
4320/*
4321 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004322 * "list_stack" is used to add lists to be marked. Can be NULL.
4323 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4324 *
4325 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004326 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004327 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004328set_ref_in_item(
4329 typval_T *tv,
4330 int copyID,
4331 ht_stack_T **ht_stack,
4332 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004333{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004334 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004335
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004336 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004337 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004338 dict_T *dd = tv->vval.v_dict;
4339
Bram Moolenaara03f2332016-02-06 18:09:59 +01004340 if (dd != NULL && dd->dv_copyID != copyID)
4341 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004342 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004343 dd->dv_copyID = copyID;
4344 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004345 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004346 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4347 }
4348 else
4349 {
4350 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4351 if (newitem == NULL)
4352 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004353 else
4354 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004355 newitem->ht = &dd->dv_hashtab;
4356 newitem->prev = *ht_stack;
4357 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004358 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004359 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004360 }
4361 }
4362 else if (tv->v_type == VAR_LIST)
4363 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004364 list_T *ll = tv->vval.v_list;
4365
Bram Moolenaara03f2332016-02-06 18:09:59 +01004366 if (ll != NULL && ll->lv_copyID != copyID)
4367 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004369 ll->lv_copyID = copyID;
4370 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004371 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004372 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004373 }
4374 else
4375 {
4376 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004377 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004378 if (newitem == NULL)
4379 abort = TRUE;
4380 else
4381 {
4382 newitem->list = ll;
4383 newitem->prev = *list_stack;
4384 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004385 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004386 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004387 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004388 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004389 else if (tv->v_type == VAR_FUNC)
4390 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004391 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004392 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004393 else if (tv->v_type == VAR_PARTIAL)
4394 {
4395 partial_T *pt = tv->vval.v_partial;
4396 int i;
4397
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004398 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004399 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004400 // Didn't see this partial yet.
4401 pt->pt_copyID = copyID;
4402
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004403 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004404
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004405 if (pt->pt_dict != NULL)
4406 {
4407 typval_T dtv;
4408
4409 dtv.v_type = VAR_DICT;
4410 dtv.vval.v_dict = pt->pt_dict;
4411 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4412 }
4413
4414 for (i = 0; i < pt->pt_argc; ++i)
4415 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4416 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004417 if (pt->pt_funcstack != NULL)
4418 {
4419 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4420
4421 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4422 abort = abort || set_ref_in_item(stack + i, copyID,
4423 ht_stack, list_stack);
4424 }
4425
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004426 }
4427 }
4428#ifdef FEAT_JOB_CHANNEL
4429 else if (tv->v_type == VAR_JOB)
4430 {
4431 job_T *job = tv->vval.v_job;
4432 typval_T dtv;
4433
4434 if (job != NULL && job->jv_copyID != copyID)
4435 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004436 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004437 if (job->jv_channel != NULL)
4438 {
4439 dtv.v_type = VAR_CHANNEL;
4440 dtv.vval.v_channel = job->jv_channel;
4441 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4442 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004443 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004444 {
4445 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004446 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004447 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4448 }
4449 }
4450 }
4451 else if (tv->v_type == VAR_CHANNEL)
4452 {
4453 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004454 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004455 typval_T dtv;
4456 jsonq_T *jq;
4457 cbq_T *cq;
4458
4459 if (ch != NULL && ch->ch_copyID != copyID)
4460 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004461 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004462 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004463 {
4464 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4465 jq = jq->jq_next)
4466 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4467 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4468 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004469 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004470 {
4471 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004472 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004473 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4474 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004475 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004476 {
4477 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004478 dtv.vval.v_partial =
4479 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004480 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4481 }
4482 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004483 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004484 {
4485 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004486 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004487 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4488 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004489 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004490 {
4491 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004492 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004493 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4494 }
4495 }
4496 }
4497#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004498 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004499}
4500
Bram Moolenaar8c711452005-01-14 21:53:12 +00004501/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004502 * Return a string with the string representation of a variable.
4503 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004504 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004505 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004506 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4507 * stings as "string()", otherwise does not put quotes around strings, as
4508 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004509 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4510 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004511 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004512 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004513 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004514echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004515 typval_T *tv,
4516 char_u **tofree,
4517 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004518 int copyID,
4519 int echo_style,
4520 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004521 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004522{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004523 static int recurse = 0;
4524 char_u *r = NULL;
4525
Bram Moolenaar33570922005-01-25 22:26:29 +00004526 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004527 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004528 if (!did_echo_string_emsg)
4529 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004530 // Only give this message once for a recursive call to avoid
4531 // flooding the user with errors. And stop iterating over lists
4532 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004533 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004534 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004535 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004536 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004537 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004538 }
4539 ++recurse;
4540
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004541 switch (tv->v_type)
4542 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004543 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004544 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004545 {
4546 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004547 r = tv->vval.v_string;
4548 if (r == NULL)
4549 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004550 }
4551 else
4552 {
4553 *tofree = string_quote(tv->vval.v_string, FALSE);
4554 r = *tofree;
4555 }
4556 break;
4557
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004558 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004559 if (echo_style)
4560 {
4561 *tofree = NULL;
4562 r = tv->vval.v_string;
4563 }
4564 else
4565 {
4566 *tofree = string_quote(tv->vval.v_string, TRUE);
4567 r = *tofree;
4568 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004569 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004570
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004571 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004572 {
4573 partial_T *pt = tv->vval.v_partial;
4574 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004575 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004576 garray_T ga;
4577 int i;
4578 char_u *tf;
4579
4580 ga_init2(&ga, 1, 100);
4581 ga_concat(&ga, (char_u *)"function(");
4582 if (fname != NULL)
4583 {
4584 ga_concat(&ga, fname);
4585 vim_free(fname);
4586 }
4587 if (pt != NULL && pt->pt_argc > 0)
4588 {
4589 ga_concat(&ga, (char_u *)", [");
4590 for (i = 0; i < pt->pt_argc; ++i)
4591 {
4592 if (i > 0)
4593 ga_concat(&ga, (char_u *)", ");
4594 ga_concat(&ga,
4595 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4596 vim_free(tf);
4597 }
4598 ga_concat(&ga, (char_u *)"]");
4599 }
4600 if (pt != NULL && pt->pt_dict != NULL)
4601 {
4602 typval_T dtv;
4603
4604 ga_concat(&ga, (char_u *)", ");
4605 dtv.v_type = VAR_DICT;
4606 dtv.vval.v_dict = pt->pt_dict;
4607 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4608 vim_free(tf);
4609 }
4610 ga_concat(&ga, (char_u *)")");
4611
4612 *tofree = ga.ga_data;
4613 r = *tofree;
4614 break;
4615 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004616
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004617 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004618 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004619 break;
4620
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004622 if (tv->vval.v_list == NULL)
4623 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004624 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004625 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004626 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004627 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004628 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4629 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004630 {
4631 *tofree = NULL;
4632 r = (char_u *)"[...]";
4633 }
4634 else
4635 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004636 int old_copyID = tv->vval.v_list->lv_copyID;
4637
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004638 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004639 *tofree = list2string(tv, copyID, restore_copyID);
4640 if (restore_copyID)
4641 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004642 r = *tofree;
4643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004644 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004645
Bram Moolenaar8c711452005-01-14 21:53:12 +00004646 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004647 if (tv->vval.v_dict == NULL)
4648 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004649 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004650 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004651 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004652 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004653 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4654 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004655 {
4656 *tofree = NULL;
4657 r = (char_u *)"{...}";
4658 }
4659 else
4660 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004661 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004662
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004663 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004664 *tofree = dict2string(tv, copyID, restore_copyID);
4665 if (restore_copyID)
4666 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004667 r = *tofree;
4668 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004669 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004670
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004672 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004673 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004675 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004676 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004677 break;
4678
Bram Moolenaar835dc632016-02-07 14:27:38 +01004679 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004680 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004681 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004682 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004683 if (composite_val)
4684 {
4685 *tofree = string_quote(r, FALSE);
4686 r = *tofree;
4687 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004688 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004689
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004690 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004691#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004692 *tofree = NULL;
4693 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4694 r = numbuf;
4695 break;
4696#endif
4697
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004698 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004699 case VAR_SPECIAL:
4700 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004701 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004702 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004703 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004704
Bram Moolenaar8502c702014-06-17 12:51:16 +02004705 if (--recurse == 0)
4706 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004707 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004708}
4709
4710/*
4711 * Return a string with the string representation of a variable.
4712 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4713 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004714 * Does not put quotes around strings, as ":echo" displays values.
4715 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4716 * May return NULL.
4717 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004718 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004719echo_string(
4720 typval_T *tv,
4721 char_u **tofree,
4722 char_u *numbuf,
4723 int copyID)
4724{
4725 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4726}
4727
4728/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004729 * Return string "str" in ' quotes, doubling ' characters.
4730 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004731 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004732 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004733 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004734string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004735{
Bram Moolenaar33570922005-01-25 22:26:29 +00004736 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004737 char_u *p, *r, *s;
4738
Bram Moolenaar33570922005-01-25 22:26:29 +00004739 len = (function ? 13 : 3);
4740 if (str != NULL)
4741 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004742 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004743 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004744 if (*p == '\'')
4745 ++len;
4746 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004747 s = r = alloc(len);
4748 if (r != NULL)
4749 {
4750 if (function)
4751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004753 r += 10;
4754 }
4755 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004756 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004757 if (str != NULL)
4758 for (p = str; *p != NUL; )
4759 {
4760 if (*p == '\'')
4761 *r++ = '\'';
4762 MB_COPY_CHAR(p, r);
4763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004764 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004765 if (function)
4766 *r++ = ')';
4767 *r++ = NUL;
4768 }
4769 return s;
4770}
4771
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004772#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004773/*
4774 * Convert the string "text" to a floating point number.
4775 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4776 * this always uses a decimal point.
4777 * Returns the length of the text that was consumed.
4778 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004780string2float(
4781 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004782 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783{
4784 char *s = (char *)text;
4785 float_T f;
4786
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004787 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004788 if (STRNICMP(text, "inf", 3) == 0)
4789 {
4790 *value = INFINITY;
4791 return 3;
4792 }
4793 if (STRNICMP(text, "-inf", 3) == 0)
4794 {
4795 *value = -INFINITY;
4796 return 4;
4797 }
4798 if (STRNICMP(text, "nan", 3) == 0)
4799 {
4800 *value = NAN;
4801 return 3;
4802 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004803 f = strtod(s, &s);
4804 *value = f;
4805 return (int)((char_u *)s - text);
4806}
4807#endif
4808
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004811 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004813 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004814var2fpos(
4815 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004816 int dollar_lnum, // TRUE when $ is last line
4817 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004819 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004821 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004823 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004824 if (varp->v_type == VAR_LIST)
4825 {
4826 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004827 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004828 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004829 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004830
4831 l = varp->vval.v_list;
4832 if (l == NULL)
4833 return NULL;
4834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004835 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004836 pos.lnum = list_find_nr(l, 0L, &error);
4837 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004838 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004840 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004841 pos.col = list_find_nr(l, 1L, &error);
4842 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004843 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004844 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004845
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004846 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004847 li = list_find(l, 1L);
4848 if (li != NULL && li->li_tv.v_type == VAR_STRING
4849 && li->li_tv.vval.v_string != NULL
4850 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4851 pos.col = len + 1;
4852
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004853 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004854 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004855 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004856 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004857
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004858 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004859 pos.coladd = list_find_nr(l, 2L, &error);
4860 if (error)
4861 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004862
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004863 return &pos;
4864 }
4865
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004866 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004867 if (name == NULL)
4868 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004869 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004871 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004872 {
4873 if (VIsual_active)
4874 return &VIsual;
4875 return &curwin->w_cursor;
4876 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004877 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004879 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4881 return NULL;
4882 return pp;
4883 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004884
Bram Moolenaara5525202006-03-02 22:52:09 +00004885 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004886
Bram Moolenaar477933c2007-07-17 14:32:23 +00004887 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004888 {
4889 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004890 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004891 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004892 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004893 // In silent Ex mode topline is zero, but that's not a valid line
4894 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004895 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004896 return &pos;
4897 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004898 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004899 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004900 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004901 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004902 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004903 return &pos;
4904 }
4905 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004906 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004908 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
4910 pos.lnum = curbuf->b_ml.ml_line_count;
4911 pos.col = 0;
4912 }
4913 else
4914 {
4915 pos.lnum = curwin->w_cursor.lnum;
4916 pos.col = (colnr_T)STRLEN(ml_get_curline());
4917 }
4918 return &pos;
4919 }
4920 return NULL;
4921}
4922
4923/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004924 * Convert list in "arg" into a position and optional file number.
4925 * When "fnump" is NULL there is no file number, only 3 items.
4926 * Note that the column is passed on as-is, the caller may want to decrement
4927 * it to use 1 for the first column.
4928 * Return FAIL when conversion is not possible, doesn't check the position for
4929 * validity.
4930 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004931 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004932list2fpos(
4933 typval_T *arg,
4934 pos_T *posp,
4935 int *fnump,
4936 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004937{
4938 list_T *l = arg->vval.v_list;
4939 long i = 0;
4940 long n;
4941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004942 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4943 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004944 if (arg->v_type != VAR_LIST
4945 || l == NULL
4946 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004947 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004948 return FAIL;
4949
4950 if (fnump != NULL)
4951 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004952 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004953 if (n < 0)
4954 return FAIL;
4955 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004956 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004957 *fnump = n;
4958 }
4959
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004960 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004961 if (n < 0)
4962 return FAIL;
4963 posp->lnum = n;
4964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004965 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004966 if (n < 0)
4967 return FAIL;
4968 posp->col = n;
4969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004970 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004971 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004972 posp->coladd = 0;
4973 else
4974 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004975
Bram Moolenaar493c1782014-05-28 14:34:46 +02004976 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004977 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004978
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004979 return OK;
4980}
4981
4982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 * Get the length of an environment variable name.
4984 * Advance "arg" to the first character after the name.
4985 * Return 0 for error.
4986 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004988get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
4990 char_u *p;
4991 int len;
4992
4993 for (p = *arg; vim_isIDc(*p); ++p)
4994 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004995 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 return 0;
4997
4998 len = (int)(p - *arg);
4999 *arg = p;
5000 return len;
5001}
5002
5003/*
5004 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005005 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 * Return 0 if something is wrong.
5007 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005008 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005009get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010{
5011 char_u *p;
5012 int len;
5013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005014 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005016 {
5017 if (*p == ':')
5018 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005019 // "s:" is start of "s:var", but "n:" is not and can be used in
5020 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005021 len = (int)(p - *arg);
5022 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5023 || len > 1)
5024 break;
5025 }
5026 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005027 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 return 0;
5029
5030 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005031 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
5033 return len;
5034}
5035
5036/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005037 * Get the length of the name of a variable or function.
5038 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005040 * Return -1 if curly braces expansion failed.
5041 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 * If the name contains 'magic' {}'s, expand them and return the
5043 * expanded name in an allocated string via 'alias' - caller must free.
5044 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005045 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005046get_name_len(
5047 char_u **arg,
5048 char_u **alias,
5049 int evaluate,
5050 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
5052 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 char_u *p;
5054 char_u *expr_start;
5055 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005057 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
5059 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5060 && (*arg)[2] == (int)KE_SNR)
5061 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005062 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 *arg += 3;
5064 return get_id_len(arg) + 3;
5065 }
5066 len = eval_fname_script(*arg);
5067 if (len > 0)
5068 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005069 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 *arg += len;
5071 }
5072
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005076 p = find_name_end(*arg, &expr_start, &expr_end,
5077 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 if (expr_start != NULL)
5079 {
5080 char_u *temp_string;
5081
5082 if (!evaluate)
5083 {
5084 len += (int)(p - *arg);
5085 *arg = skipwhite(p);
5086 return len;
5087 }
5088
5089 /*
5090 * Include any <SID> etc in the expanded string:
5091 * Thus the -len here.
5092 */
5093 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5094 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005095 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 *alias = temp_string;
5097 *arg = skipwhite(p);
5098 return (int)STRLEN(temp_string);
5099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
5101 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005102 // Only give an error when there is something, otherwise it will be
5103 // reported at a higher level.
5104 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005105 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
5107 return len;
5108}
5109
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005110/*
5111 * Find the end of a variable or function name, taking care of magic braces.
5112 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5113 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005114 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005115 * Return a pointer to just after the name. Equal to "arg" if there is no
5116 * valid name.
5117 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005118 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005119find_name_end(
5120 char_u *arg,
5121 char_u **expr_start,
5122 char_u **expr_end,
5123 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005125 int mb_nest = 0;
5126 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005128 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005129 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005131 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005133 *expr_start = NULL;
5134 *expr_end = NULL;
5135 }
5136
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005137 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005138 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5139 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005140 return arg;
5141
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142 for (p = arg; *p != NUL
5143 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005144 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005145 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005146 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005147 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005148 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005150 if (*p == '\'')
5151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005152 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005153 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005154 ;
5155 if (*p == NUL)
5156 break;
5157 }
5158 else if (*p == '"')
5159 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005160 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005161 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005162 if (*p == '\\' && p[1] != NUL)
5163 ++p;
5164 if (*p == NUL)
5165 break;
5166 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005167 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5168 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005169 // "s:" is start of "s:var", but "n:" is not and can be used in
5170 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005171 len = (int)(p - arg);
5172 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005173 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005174 break;
5175 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005177 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 if (*p == '[')
5180 ++br_nest;
5181 else if (*p == ']')
5182 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005184
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005185 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 if (*p == '{')
5188 {
5189 mb_nest++;
5190 if (expr_start != NULL && *expr_start == NULL)
5191 *expr_start = p;
5192 }
5193 else if (*p == '}')
5194 {
5195 mb_nest--;
5196 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5197 *expr_end = p;
5198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201
5202 return p;
5203}
5204
5205/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005206 * Expands out the 'magic' {}'s in a variable/function name.
5207 * Note that this can call itself recursively, to deal with
5208 * constructs like foo{bar}{baz}{bam}
5209 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5210 * "in_start" ^
5211 * "expr_start" ^
5212 * "expr_end" ^
5213 * "in_end" ^
5214 *
5215 * Returns a new allocated string, which the caller must free.
5216 * Returns NULL for failure.
5217 */
5218 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005219make_expanded_name(
5220 char_u *in_start,
5221 char_u *expr_start,
5222 char_u *expr_end,
5223 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005224{
5225 char_u c1;
5226 char_u *retval = NULL;
5227 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228
5229 if (expr_end == NULL || in_end == NULL)
5230 return NULL;
5231 *expr_start = NUL;
5232 *expr_end = NUL;
5233 c1 = *in_end;
5234 *in_end = NUL;
5235
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005236 temp_result = eval_to_string(expr_start + 1, FALSE);
5237 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005238 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005239 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5240 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005241 if (retval != NULL)
5242 {
5243 STRCPY(retval, in_start);
5244 STRCAT(retval, temp_result);
5245 STRCAT(retval, expr_end + 1);
5246 }
5247 }
5248 vim_free(temp_result);
5249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005250 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005251 *expr_start = '{';
5252 *expr_end = '}';
5253
5254 if (retval != NULL)
5255 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005256 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005257 if (expr_start != NULL)
5258 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005259 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005260 temp_result = make_expanded_name(retval, expr_start,
5261 expr_end, temp_result);
5262 vim_free(retval);
5263 retval = temp_result;
5264 }
5265 }
5266
5267 return retval;
5268}
5269
5270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005272 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005274 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005275eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005277 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005278}
5279
5280/*
5281 * Return TRUE if character "c" can be used as the first character in a
5282 * variable or function name (excluding '{' and '}').
5283 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005284 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005285eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005286{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005287 return ASCII_ISALPHA(c) || c == '_';
5288}
5289
5290/*
5291 * Return TRUE if character "c" can be used as the first character of a
5292 * dictionary key.
5293 */
5294 int
5295eval_isdictc(int c)
5296{
5297 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298}
5299
5300/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005301 * Return the character "str[index]" where "index" is the character index. If
5302 * "index" is out of range NULL is returned.
5303 */
5304 char_u *
5305char_from_string(char_u *str, varnumber_T index)
5306{
5307 size_t nbyte = 0;
5308 varnumber_T nchar = index;
5309 size_t slen;
5310
5311 if (str == NULL || index < 0)
5312 return NULL;
5313 slen = STRLEN(str);
5314 while (nchar > 0 && nbyte < slen)
5315 {
5316 nbyte += MB_CPTR2LEN(str + nbyte);
5317 --nchar;
5318 }
5319 if (nbyte >= slen)
5320 return NULL;
5321 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5322}
5323
5324/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005325 * Get the byte index for character index "idx" in string "str" with length
5326 * "str_len".
5327 * If going over the end return "str_len".
5328 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005329 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005330 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005331 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005332char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5333{
5334 varnumber_T nchar = idx;
5335 size_t nbyte = 0;
5336
5337 if (nchar >= 0)
5338 {
5339 while (nchar > 0 && nbyte < str_len)
5340 {
5341 nbyte += MB_CPTR2LEN(str + nbyte);
5342 --nchar;
5343 }
5344 }
5345 else
5346 {
5347 nbyte = str_len;
5348 while (nchar < 0 && nbyte > 0)
5349 {
5350 --nbyte;
5351 nbyte -= mb_head_off(str, str + nbyte);
5352 ++nchar;
5353 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005354 if (nchar < 0)
5355 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005356 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005357 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005358}
5359
5360/*
5361 * Return the slice "str[first:last]" using character indexes.
5362 * Return NULL when the result is empty.
5363 */
5364 char_u *
5365string_slice(char_u *str, varnumber_T first, varnumber_T last)
5366{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005367 long start_byte, end_byte;
5368 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005369
5370 if (str == NULL)
5371 return NULL;
5372 slen = STRLEN(str);
5373 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005374 if (start_byte < 0)
5375 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005376 if (last == -1)
5377 end_byte = slen;
5378 else
5379 {
5380 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005381 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005382 // end index is inclusive
5383 end_byte += MB_CPTR2LEN(str + end_byte);
5384 }
5385
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005386 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005387 return NULL;
5388 return vim_strnsave(str + start_byte, end_byte - start_byte);
5389}
5390
5391/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005392 * Handle:
5393 * - expr[expr], expr[expr:expr] subscript
5394 * - ".name" lookup
5395 * - function call with Funcref variable: func(expr)
5396 * - method call: var->method()
5397 *
5398 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005399 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005401handle_subscript(
5402 char_u **arg,
5403 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005404 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005405 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005406{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005407 int evaluate = evalarg != NULL
5408 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005409 int ret = OK;
5410 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005411 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005412 int getnext;
5413 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005414
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005415 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005416 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005417 // When at the end of the line and ".name" or "->{" or "->X" follows in
5418 // the next line then consume the line break.
5419 p = eval_next_non_blank(*arg, evalarg, &getnext);
5420 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005421 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005422 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005423 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005424 {
5425 *arg = eval_next_line(evalarg);
5426 check_white = FALSE;
5427 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005428
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005429 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5430 || rettv->v_type == VAR_PARTIAL))
5431 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005432 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005433 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5434 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005435
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005436 // Stop the expression evaluation when immediately aborting on
5437 // error, or when an interrupt occurred or an exception was thrown
5438 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005439 if (aborting())
5440 {
5441 if (ret == OK)
5442 clear_tv(rettv);
5443 ret = FAIL;
5444 }
5445 dict_unref(selfdict);
5446 selfdict = NULL;
5447 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005448 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005449 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005450 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005451 if (ret == OK)
5452 {
5453 if ((*arg)[2] == '{')
5454 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005455 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005456 else
5457 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005458 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005459 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005460 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005461 // "." is ".name" lookup when we found a dict or when evaluating and
5462 // scriptversion is at least 2, where string concatenation is "..".
5463 else if (**arg == '['
5464 || (**arg == '.' && (rettv->v_type == VAR_DICT
5465 || (!evaluate
5466 && (*arg)[1] != '.'
5467 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005468 {
5469 dict_unref(selfdict);
5470 if (rettv->v_type == VAR_DICT)
5471 {
5472 selfdict = rettv->vval.v_dict;
5473 if (selfdict != NULL)
5474 ++selfdict->dv_refcount;
5475 }
5476 else
5477 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005478 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005479 {
5480 clear_tv(rettv);
5481 ret = FAIL;
5482 }
5483 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005484 else
5485 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005486 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005488 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5489 // Don't do this when "Func" is already a partial that was bound
5490 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005491 if (selfdict != NULL
5492 && (rettv->v_type == VAR_FUNC
5493 || (rettv->v_type == VAR_PARTIAL
5494 && (rettv->vval.v_partial->pt_auto
5495 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005496 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005497
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005498 dict_unref(selfdict);
5499 return ret;
5500}
5501
5502/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005503 * Make a copy of an item.
5504 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005505 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5506 * reference to an already copied list/dict can be used.
5507 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005508 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005510item_copy(
5511 typval_T *from,
5512 typval_T *to,
5513 int deep,
5514 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005515{
5516 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005517 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005518
Bram Moolenaar33570922005-01-25 22:26:29 +00005519 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005520 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005521 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005522 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005523 }
5524 ++recurse;
5525
5526 switch (from->v_type)
5527 {
5528 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005529 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005530 case VAR_STRING:
5531 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005532 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005533 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005534 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005535 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005536 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005537 copy_tv(from, to);
5538 break;
5539 case VAR_LIST:
5540 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005541 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005542 if (from->vval.v_list == NULL)
5543 to->vval.v_list = NULL;
5544 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5545 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005546 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005547 to->vval.v_list = from->vval.v_list->lv_copylist;
5548 ++to->vval.v_list->lv_refcount;
5549 }
5550 else
5551 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5552 if (to->vval.v_list == NULL)
5553 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005554 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005555 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005557 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005558 case VAR_DICT:
5559 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005560 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005561 if (from->vval.v_dict == NULL)
5562 to->vval.v_dict = NULL;
5563 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5564 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005565 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005566 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5567 ++to->vval.v_dict->dv_refcount;
5568 }
5569 else
5570 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5571 if (to->vval.v_dict == NULL)
5572 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005573 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005574 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005575 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005576 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005577 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005578 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005579 }
5580 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005581 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005582}
5583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 void
5585echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5586{
5587 char_u *tofree;
5588 char_u numbuf[NUMBUFLEN];
5589 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5590
5591 if (*atstart)
5592 {
5593 *atstart = FALSE;
5594 // Call msg_start() after eval1(), evaluating the expression
5595 // may cause a message to appear.
5596 if (with_space)
5597 {
5598 // Mark the saved text as finishing the line, so that what
5599 // follows is displayed on a new line when scrolling back
5600 // at the more prompt.
5601 msg_sb_eol();
5602 msg_start();
5603 }
5604 }
5605 else if (with_space)
5606 msg_puts_attr(" ", echo_attr);
5607
5608 if (p != NULL)
5609 for ( ; *p != NUL && !got_int; ++p)
5610 {
5611 if (*p == '\n' || *p == '\r' || *p == TAB)
5612 {
5613 if (*p != TAB && *needclr)
5614 {
5615 // remove any text still there from the command
5616 msg_clr_eos();
5617 *needclr = FALSE;
5618 }
5619 msg_putchar_attr(*p, echo_attr);
5620 }
5621 else
5622 {
5623 if (has_mbyte)
5624 {
5625 int i = (*mb_ptr2len)(p);
5626
5627 (void)msg_outtrans_len_attr(p, i, echo_attr);
5628 p += i - 1;
5629 }
5630 else
5631 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5632 }
5633 }
5634 vim_free(tofree);
5635}
5636
Bram Moolenaare9a41262005-01-15 22:18:47 +00005637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 * ":echo expr1 ..." print each argument separated with a space, add a
5639 * newline at the end.
5640 * ":echon expr1 ..." print each argument plain.
5641 */
5642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005643ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644{
5645 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005646 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 char_u *p;
5648 int needclr = TRUE;
5649 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005650 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005651 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005652 evalarg_T evalarg;
5653
Bram Moolenaare707c882020-07-01 13:07:37 +02005654 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655
5656 if (eap->skip)
5657 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005658 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005660 // If eval1() causes an error message the text from the command may
5661 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005662 need_clr_eos = needclr;
5663
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005665 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
5667 /*
5668 * Report the invalid expression unless the expression evaluation
5669 * has been cancelled due to an aborting error, an interrupt, or an
5670 * exception.
5671 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005672 if (!aborting() && did_emsg == did_emsg_before
5673 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005674 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005675 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 break;
5677 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005678 need_clr_eos = FALSE;
5679
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005681 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 arg = skipwhite(arg);
5685 }
5686 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005687 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688
5689 if (eap->skip)
5690 --emsg_skip;
5691 else
5692 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005693 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 if (needclr)
5695 msg_clr_eos();
5696 if (eap->cmdidx == CMD_echo)
5697 msg_end();
5698 }
5699}
5700
5701/*
5702 * ":echohl {name}".
5703 */
5704 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005705ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005707 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708}
5709
5710/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005711 * Returns the :echo attribute
5712 */
5713 int
5714get_echo_attr(void)
5715{
5716 return echo_attr;
5717}
5718
5719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 * ":execute expr1 ..." execute the result of an expression.
5721 * ":echomsg expr1 ..." Print a message
5722 * ":echoerr expr1 ..." Print an error
5723 * Each gets spaces around each argument and a newline at the end for
5724 * echo commands
5725 */
5726 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005727ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
5729 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005730 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 int ret = OK;
5732 char_u *p;
5733 garray_T ga;
5734 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
5736 ga_init2(&ga, 1, 80);
5737
5738 if (eap->skip)
5739 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005740 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005742 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005743 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
5746 if (!eap->skip)
5747 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005748 char_u buf[NUMBUFLEN];
5749
5750 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005751 {
5752 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5753 {
5754 emsg(_(e_inval_string));
5755 p = NULL;
5756 }
5757 else
5758 p = tv_get_string_buf(&rettv, buf);
5759 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005760 else
5761 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005762 if (p == NULL)
5763 {
5764 clear_tv(&rettv);
5765 ret = FAIL;
5766 break;
5767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 len = (int)STRLEN(p);
5769 if (ga_grow(&ga, len + 2) == FAIL)
5770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005771 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 ret = FAIL;
5773 break;
5774 }
5775 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 ga.ga_len += len;
5779 }
5780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005781 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 arg = skipwhite(arg);
5783 }
5784
5785 if (ret != FAIL && ga.ga_data != NULL)
5786 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005787 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5788 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005789 // Mark the already saved text as finishing the line, so that what
5790 // follows is displayed on a new line when scrolling back at the
5791 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005792 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005793 }
5794
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005796 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005797 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005798 out_flush();
5799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 else if (eap->cmdidx == CMD_echoerr)
5801 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005802 int save_did_emsg = did_emsg;
5803
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005804 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005805 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 if (!force_abort)
5807 did_emsg = save_did_emsg;
5808 }
5809 else if (eap->cmdidx == CMD_execute)
5810 do_cmdline((char_u *)ga.ga_data,
5811 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5812 }
5813
5814 ga_clear(&ga);
5815
5816 if (eap->skip)
5817 --emsg_skip;
5818
5819 eap->nextcmd = check_nextcmd(arg);
5820}
5821
5822/*
5823 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5824 * "arg" points to the "&" or '+' when called, to "option" when returning.
5825 * Returns NULL when no option name found. Otherwise pointer to the char
5826 * after the option name.
5827 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005828 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005829find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
5831 char_u *p = *arg;
5832
5833 ++p;
5834 if (*p == 'g' && p[1] == ':')
5835 {
5836 *opt_flags = OPT_GLOBAL;
5837 p += 2;
5838 }
5839 else if (*p == 'l' && p[1] == ':')
5840 {
5841 *opt_flags = OPT_LOCAL;
5842 p += 2;
5843 }
5844 else
5845 *opt_flags = 0;
5846
5847 if (!ASCII_ISALPHA(*p))
5848 return NULL;
5849 *arg = p;
5850
5851 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005852 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 else
5854 while (ASCII_ISALPHA(*p))
5855 ++p;
5856 return p;
5857}
5858
5859/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005860 * Display script name where an item was last set.
5861 * Should only be invoked when 'verbose' is non-zero.
5862 */
5863 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005864last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005865{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005866 char_u *p;
5867
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005868 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005869 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005870 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005871 if (p != NULL)
5872 {
5873 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005874 msg_puts(_("\n\tLast set from "));
5875 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005876 if (script_ctx.sc_lnum > 0)
5877 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005878 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005879 msg_outnum((long)script_ctx.sc_lnum);
5880 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005881 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005882 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005883 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005884 }
5885}
5886
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005887#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888
5889/*
5890 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005891 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 * "flags" can be "g" to do a global substitute.
5893 * Returns an allocated string, NULL for error.
5894 */
5895 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005896do_string_sub(
5897 char_u *str,
5898 char_u *pat,
5899 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005900 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005901 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
5903 int sublen;
5904 regmatch_T regmatch;
5905 int i;
5906 int do_all;
5907 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005908 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 garray_T ga;
5910 char_u *ret;
5911 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005912 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005914 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005916 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
5918 ga_init2(&ga, 1, 200);
5919
5920 do_all = (flags[0] == 'g');
5921
5922 regmatch.rm_ic = p_ic;
5923 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5924 if (regmatch.regprog != NULL)
5925 {
5926 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005927 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5929 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005930 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005931 if (regmatch.startp[0] == regmatch.endp[0])
5932 {
5933 if (zero_width == regmatch.startp[0])
5934 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005935 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005936 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005937 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5938 (size_t)i);
5939 ga.ga_len += i;
5940 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005941 continue;
5942 }
5943 zero_width = regmatch.startp[0];
5944 }
5945
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 /*
5947 * Get some space for a temporary buffer to do the substitution
5948 * into. It will contain:
5949 * - The text up to where the match is.
5950 * - The substituted text.
5951 * - The text after the match.
5952 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005953 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005954 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5956 {
5957 ga_clear(&ga);
5958 break;
5959 }
5960
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005961 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 i = (int)(regmatch.startp[0] - tail);
5963 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005964 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005965 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 + ga.ga_len + i, TRUE, TRUE, FALSE);
5967 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005968 tail = regmatch.endp[0];
5969 if (*tail == NUL)
5970 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 if (!do_all)
5972 break;
5973 }
5974
5975 if (ga.ga_data != NULL)
5976 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5977
Bram Moolenaar473de612013-06-08 18:19:48 +02005978 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 }
5980
5981 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5982 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005983 if (p_cpo == empty_option)
5984 p_cpo = save_cpo;
5985 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005986 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005987 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988
5989 return ret;
5990}