blob: 95dd5e2e6ce99f880f1842b2d2eabcba3c8380b2 [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.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010060 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010061 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020062 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010064{
65 varnumber_T result;
66
Bram Moolenaar99880f92021-01-20 21:23:14 +010067 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010068 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 if (failed != NULL)
73 *failed = TRUE;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 if (n1 == 0)
76 result = VARNUM_MIN; // similar to NaN
77 else if (n1 < 0)
78 result = -VARNUM_MAX;
79 else
80 result = VARNUM_MAX;
81 }
82 else
83 result = n1 / n2;
84
85 return result;
86}
87
88/*
89 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010090 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010091 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020092 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010094{
Bram Moolenaar99880f92021-01-20 21:23:14 +010095 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 if (failed != NULL)
99 *failed = TRUE;
100 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100101 return (n2 == 0) ? 0 : (n1 % n2);
102}
103
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100104#if defined(EBCDIC) || defined(PROTO)
105/*
106 * Compare struct fst by function name.
107 */
108 static int
109compare_func_name(const void *s1, const void *s2)
110{
111 struct fst *p1 = (struct fst *)s1;
112 struct fst *p2 = (struct fst *)s2;
113
114 return STRCMP(p1->f_name, p2->f_name);
115}
116
117/*
118 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100119 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100120 * On machines using EBCDIC we have to sort it.
121 */
122 static void
123sortFunctions(void)
124{
125 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
126
127 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
128}
129#endif
130
Bram Moolenaar33570922005-01-25 22:26:29 +0000131/*
132 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000133 */
134 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100135eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000136{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200137 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200138 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000139
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200140#ifdef EBCDIC
141 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100142 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200143 */
144 sortFunctions();
145#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000146}
147
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000148#if defined(EXITFREE) || defined(PROTO)
149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100150eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200152 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100153 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200154 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000155
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200156 // autoloaded script names
157 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000158
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200159 // unreferenced lists and dicts
160 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200161
162 // functions not garbage collected
163 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000164}
165#endif
166
Bram Moolenaare6b53242020-07-01 17:28:33 +0200167 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200168fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
169{
170 CLEAR_FIELD(*evalarg);
171 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
172 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
173 {
174 evalarg->eval_getline = eap->getline;
175 evalarg->eval_cookie = eap->cookie;
176 }
177}
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179/*
180 * Top level evaluation function, returning a boolean.
181 * Sets "error" to TRUE if there was an error.
182 * Return TRUE or FALSE.
183 */
184 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100185eval_to_bool(
186 char_u *arg,
187 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200188 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100189 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
Bram Moolenaar33570922005-01-25 22:26:29 +0000191 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200192 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200193 evalarg_T evalarg;
194
Bram Moolenaar37c83712020-06-30 21:18:36 +0200195 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
197 if (skip)
198 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200199 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 else
202 {
203 *error = FALSE;
204 if (!skip)
205 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200206 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200207 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200208 else
209 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000210 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 }
212 }
213 if (skip)
214 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200217 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
219
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100220/*
221 * Call eval1() and give an error message if not done at a lower level.
222 */
223 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200224eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100225{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100226 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100227 int ret;
228 int did_emsg_before = did_emsg;
229 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200230 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100231
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
233
234 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100235 if (ret == FAIL)
236 {
237 // Report the invalid expression unless the expression evaluation has
238 // been cancelled due to an aborting error, an interrupt, or an
239 // exception, or we already gave a more specific error.
240 // Also check called_emsg for when using assert_fails().
241 if (!aborting() && did_emsg == did_emsg_before
242 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100243 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100244 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200245 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100246 return ret;
247}
248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200250 * Return whether a typval is a valid expression to pass to eval_expr_typval()
251 * or eval_expr_to_bool(). An empty string returns FALSE;
252 */
253 int
254eval_expr_valid_arg(typval_T *tv)
255{
256 return tv->v_type != VAR_UNKNOWN
257 && (tv->v_type != VAR_STRING
258 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
259}
260
261/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 * Evaluate an expression, which can be a function, partial or string.
263 * Pass arguments "argv[argc]".
264 * Return the result in "rettv" and OK or FAIL.
265 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200266 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100267eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
268{
269 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100270 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200271 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100272
273 if (expr->v_type == VAR_FUNC)
274 {
275 s = expr->vval.v_string;
276 if (s == NULL || *s == NUL)
277 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200278 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200279 funcexe.evaluate = TRUE;
280 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100281 return FAIL;
282 }
283 else if (expr->v_type == VAR_PARTIAL)
284 {
285 partial_T *partial = expr->vval.v_partial;
286
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200287 if (partial == NULL)
288 return FAIL;
289
Bram Moolenaar822ba242020-05-24 23:00:18 +0200290 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200291 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200293 if (call_def_function(partial->pt_func, argc, argv,
294 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303 funcexe.evaluate = TRUE;
304 funcexe.partial = partial;
305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
309 else
310 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100311 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100312 if (s == NULL)
313 return FAIL;
314 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200315 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100316 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200317 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100318 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200319 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100320 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100321 return FAIL;
322 }
323 }
324 return OK;
325}
326
327/*
328 * Like eval_to_bool() but using a typval_T instead of a string.
329 * Works for string, funcref and partial.
330 */
331 int
332eval_expr_to_bool(typval_T *expr, int *error)
333{
334 typval_T rettv;
335 int res;
336
337 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
338 {
339 *error = TRUE;
340 return FALSE;
341 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200342 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100343 clear_tv(&rettv);
344 return res;
345}
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347/*
348 * Top level evaluation function, returning a string. If "skip" is TRUE,
349 * only parsing to "nextcmd" is done, without reporting errors. Return
350 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
351 */
352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100353eval_to_string_skip(
354 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200355 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100356 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357{
Bram Moolenaar33570922005-01-25 22:26:29 +0000358 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200360 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar37c83712020-06-30 21:18:36 +0200362 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (skip)
364 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200365 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 retval = NULL;
367 else
368 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100369 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000370 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 }
372 if (skip)
373 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200374 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
376 return retval;
377}
378
379/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000380 * Skip over an expression at "*pp".
381 * Return FAIL for an error, OK otherwise.
382 */
383 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200384skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000385{
Bram Moolenaar33570922005-01-25 22:26:29 +0000386 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000387
388 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200389 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000390}
391
392/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200393 * Skip over an expression at "*pp".
394 * If in Vim9 script and line breaks are encountered, the lines are
395 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200396 * "arg" is advanced to just after the expression.
397 * "start" is set to the start of the expression, "end" to just after the end.
398 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 * Return FAIL for an error, OK otherwise.
400 */
401 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200402skip_expr_concatenate(
403 char_u **arg,
404 char_u **start,
405 char_u **end,
406 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200407{
408 typval_T rettv;
409 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200410 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200411 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200413 int evaluate = evalarg == NULL
414 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200416 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200417 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200418 {
419 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200420 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200421 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422 ++gap->ga_len;
423 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200424 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200425
426 // Don't evaluate the expression.
427 if (evalarg != NULL)
428 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200429 *arg = skipwhite(*arg);
430 res = eval1(arg, &rettv, evalarg);
431 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200432 if (evalarg != NULL)
433 evalarg->eval_flags = save_flags;
434
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200435 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200437 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200438 if (evalarg->eval_ga.ga_len == 1)
439 {
440 // just one line, no need to concatenate
441 ga_clear(gap);
442 gap->ga_itemsize = 0;
443 }
444 else
445 {
446 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200447 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200448
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200449 // Line breaks encountered, concatenate all the lines.
450 *((char_u **)gap->ga_data) = *start;
451 p = ga_concat_strings(gap, "");
452
453 // free the lines only when using getsourceline()
454 if (evalarg->eval_cookie != NULL)
455 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200456 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200457 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200458 // Do not free the last line, "arg" points into it, free it
459 // later.
460 vim_free(evalarg->eval_tofree);
461 evalarg->eval_tofree =
462 ((char_u **)gap->ga_data)[gap->ga_len - 1];
463 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200464 ga_clear_strings(gap);
465 }
466 else
467 ga_clear(gap);
468 gap->ga_itemsize = 0;
469 if (p == NULL)
470 return FAIL;
471 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200472 vim_free(evalarg->eval_tofree_lambda);
473 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200474 // Compute "end" relative to the end.
475 *end = *start + STRLEN(*start) - endoff;
476 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200477 }
478
479 return res;
480}
481
482/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100483 * Convert "tv" to a string.
484 * When "convert" is TRUE convert a List into a sequence of lines and convert
485 * a Float to a String.
486 * Returns an allocated string (NULL when out of memory).
487 */
488 char_u *
489typval2string(typval_T *tv, int convert)
490{
491 garray_T ga;
492 char_u *retval;
493#ifdef FEAT_FLOAT
494 char_u numbuf[NUMBUFLEN];
495#endif
496
497 if (convert && tv->v_type == VAR_LIST)
498 {
499 ga_init2(&ga, (int)sizeof(char), 80);
500 if (tv->vval.v_list != NULL)
501 {
502 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
503 if (tv->vval.v_list->lv_len > 0)
504 ga_append(&ga, NL);
505 }
506 ga_append(&ga, NUL);
507 retval = (char_u *)ga.ga_data;
508 }
509#ifdef FEAT_FLOAT
510 else if (convert && tv->v_type == VAR_FLOAT)
511 {
512 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
513 retval = vim_strsave(numbuf);
514 }
515#endif
516 else
517 retval = vim_strsave(tv_get_string(tv));
518 return retval;
519}
520
521/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200522 * Top level evaluation function, returning a string. Does not handle line
523 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000524 * When "convert" is TRUE convert a List into a sequence of lines and convert
525 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 * Return pointer to allocated memory, or NULL for failure.
527 */
528 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100529eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100530 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100531 int convert,
532 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
Bram Moolenaar33570922005-01-25 22:26:29 +0000534 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100536 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100538 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
539 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 retval = NULL;
541 else
542 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100543 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000544 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100546 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
548 return retval;
549}
550
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100551 char_u *
552eval_to_string(
553 char_u *arg,
554 int convert)
555{
556 return eval_to_string_eap(arg, convert, NULL);
557}
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000560 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200561 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200562 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 */
564 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100565eval_to_string_safe(
566 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100567 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
569 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200570 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200571 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200573 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200574 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000575 if (use_sandbox)
576 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200577 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200578 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000579 if (use_sandbox)
580 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200581 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200582 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200583 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 return retval;
585}
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587/*
588 * Top level evaluation function, returning a number.
589 * Evaluates "expr" silently.
590 * Returns -1 for an error.
591 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200592 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100593eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
Bram Moolenaar33570922005-01-25 22:26:29 +0000595 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200596 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000597 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
599 ++emsg_off;
600
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200601 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 retval = -1;
603 else
604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100605 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000606 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608 --emsg_off;
609
610 return retval;
611}
612
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000613/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000614 * Top level evaluation function.
615 * Returns an allocated typval_T with the result.
616 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000617 */
618 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200619eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620{
621 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200622 evalarg_T evalarg;
623
624 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000625
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200626 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200627 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100628 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000629
Bram Moolenaar37c83712020-06-30 21:18:36 +0200630 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000631 return tv;
632}
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100635 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200636 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
637 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000638 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641call_vim_function(
642 char_u *func,
643 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200644 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200645 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000647 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200648 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100650 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200651 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200652 funcexe.firstline = curwin->w_cursor.lnum;
653 funcexe.lastline = curwin->w_cursor.lnum;
654 funcexe.evaluate = TRUE;
655 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000656 if (ret == FAIL)
657 clear_tv(rettv);
658
659 return ret;
660}
661
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100662/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100663 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100664 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200665 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
666 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100667 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200668 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100669call_func_retnr(
670 char_u *func,
671 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200672 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100673{
674 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200675 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100676
Bram Moolenaarded27a12018-08-01 19:06:03 +0200677 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100678 return -1;
679
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100680 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100681 clear_tv(&rettv);
682 return retval;
683}
684
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000685/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100686 * Call Vim script function like call_func_retnr() and drop the result.
687 * Returns FAIL when calling the function fails.
688 */
689 int
690call_func_noret(
691 char_u *func,
692 int argc,
693 typval_T *argv)
694{
695 typval_T rettv;
696
697 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
698 return FAIL;
699 clear_tv(&rettv);
700 return OK;
701}
702
703/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100704 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100705 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000706 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707 */
708 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100709call_func_retstr(
710 char_u *func,
711 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200712 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000713{
714 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000715 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000716
Bram Moolenaarded27a12018-08-01 19:06:03 +0200717 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718 return NULL;
719
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100720 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000721 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 return retval;
723}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000724
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000725/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100726 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100727 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000728 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000729 */
730 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100731call_func_retlist(
732 char_u *func,
733 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200734 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000735{
736 typval_T rettv;
737
Bram Moolenaarded27a12018-08-01 19:06:03 +0200738 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000739 return NULL;
740
741 if (rettv.v_type != VAR_LIST)
742 {
743 clear_tv(&rettv);
744 return NULL;
745 }
746
747 return rettv.vval.v_list;
748}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750#ifdef FEAT_FOLDING
751/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100752 * Evaluate "arg", which is 'foldexpr'.
753 * Note: caller must set "curwin" to match "arg".
754 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
755 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
757 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100758eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
Bram Moolenaar33570922005-01-25 22:26:29 +0000760 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200761 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000763 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
764 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000767 if (use_sandbox)
768 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200769 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200771 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 retval = 0;
773 else
774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100775 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000776 if (tv.v_type == VAR_NUMBER)
777 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000778 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 retval = 0;
780 else
781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100782 // If the result is a string, check if there is a non-digit before
783 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000784 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (!VIM_ISDIGIT(*s) && *s != '-')
786 *cp = *s++;
787 retval = atol((char *)s);
788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000789 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000792 if (use_sandbox)
793 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200794 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200795 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200797 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798}
799#endif
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000802 * Get an lval: variable, Dict item or List item that can be assigned a value
803 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
804 * "name.key", "name.key[expr]" etc.
805 * Indexing only works if "name" is an existing List or Dictionary.
806 * "name" points to the start of the name.
807 * If "rettv" is not NULL it points to the value to be assigned.
808 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
809 * wrong; must end in space or cmd separator.
810 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100811 * flags:
812 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100813 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100814 * GLV_NO_AUTOLOAD: do not use script autoloading
815 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000817 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000818 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000819 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200820 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100821get_lval(
822 char_u *name,
823 typval_T *rettv,
824 lval_T *lp,
825 int unlet,
826 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100827 int flags, // GLV_ values
828 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000830 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000831 char_u *expr_start, *expr_end;
832 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000833 dictitem_T *v;
834 typval_T var1;
835 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000837 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000838 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000839 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100840 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100841 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100842 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100844 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200845 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000846
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100847 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100849 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200851 lp->ll_name_end = find_name_end(name, NULL, NULL,
852 FNE_INCL_BR | fne_flags);
853 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000854 }
855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100856 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000857 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 if (expr_start != NULL)
860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100861 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100862 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000863 && *p != '[' && *p != '.')
864 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200865 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 return NULL;
867 }
868
869 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
870 if (lp->ll_exp_name == NULL)
871 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100872 // Report an invalid expression in braces, unless the
873 // expression evaluation has been cancelled due to an
874 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 if (!aborting() && !quiet)
876 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000877 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100878 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 return NULL;
880 }
881 }
882 lp->ll_name = lp->ll_exp_name;
883 }
884 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 lp->ll_name = name;
887
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200888 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200890 // "a: type" is declaring variable "a" with a type, not "a:".
891 if (p == name + 2 && p[-1] == ':')
892 {
893 --p;
894 lp->ll_name_end = p;
895 }
896 if (*p == ':')
897 {
898 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
899 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200901 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100902 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
903 if (lp->ll_type == NULL && !quiet)
904 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200905 lp->ll_name_end = tp;
906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907 }
908 }
909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100910 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
912 return p;
913
914 cc = *p;
915 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100916 // When we would write to the variable pass &ht and prevent autoload.
917 writing = !(flags & GLV_READ_ONLY);
918 v = find_var(lp->ll_name, writing ? &ht : NULL,
919 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000920 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200921 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000923 if (v == NULL)
924 return NULL;
925
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100926 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
927 {
928 if (!quiet)
929 semsg(_(e_variable_already_declared), lp->ll_name);
930 return NULL;
931 }
932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 /*
934 * Loop until no more [idx] or .key is following.
935 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000936 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100937 var1.v_type = VAR_UNKNOWN;
938 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000939 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200942 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100943 && !(lp->ll_tv->v_type == VAR_BLOB
944 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000946 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100947 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000949 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000950 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100953 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000955 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000956
Bram Moolenaar10c65862020-10-08 21:16:42 +0200957 if (in_vim9script() && lp->ll_valtype == NULL
958 && lp->ll_tv == &v->di_tv
959 && ht != NULL && ht == get_script_local_ht())
960 {
961 svar_T *sv = find_typval_in_script(lp->ll_tv);
962
963 // Vim9 script local variable: get the type
964 if (sv != NULL)
965 lp->ll_valtype = sv->sv_type;
966 }
967
Bram Moolenaar8c711452005-01-14 21:53:12 +0000968 len = -1;
969 if (*p == '.')
970 {
971 key = p + 1;
972 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
973 ;
974 if (len == 0)
975 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100977 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000979 }
980 p = key + len;
981 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000982 else
983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100984 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000985 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000986 if (*p == ':')
987 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000988 else
989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200991 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100993 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000994 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100995 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000996 clear_tv(&var1);
997 return NULL;
998 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200999 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 }
1001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001003 if (*p == ':')
1004 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001008 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001009 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001011 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001012 if (rettv != NULL
1013 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001014 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001015 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001016 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001019 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001020 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001022 }
1023 p = skipwhite(p + 1);
1024 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001026 else
1027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001029 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001030 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001031 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001032 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001035 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001036 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001037 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001038 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001039 clear_tv(&var2);
1040 return NULL;
1041 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001042 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001043 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001044 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001047
Bram Moolenaar8c711452005-01-14 21:53:12 +00001048 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001049 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001051 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001052 clear_tv(&var1);
1053 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001055 }
1056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001057 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001058 ++p;
1059 }
1060
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 {
1063 if (len == -1)
1064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001065 // "[key]": get key from "var1"
1066 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001067 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001070 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 }
1072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001073 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001074
1075 // a NULL dict is equivalent with an empty dict
1076 if (lp->ll_tv->vval.v_dict == NULL)
1077 {
1078 lp->ll_tv->vval.v_dict = dict_alloc();
1079 if (lp->ll_tv->vval.v_dict == NULL)
1080 {
1081 clear_tv(&var1);
1082 return NULL;
1083 }
1084 ++lp->ll_tv->vval.v_dict->dv_refcount;
1085 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001086 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001087
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001088 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001090 // When assigning to a scope dictionary check that a function and
1091 // variable name is valid (only variable name unless it is l: or
1092 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001093 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001094 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001095 int prevval;
1096 int wrong;
1097
1098 if (len != -1)
1099 {
1100 prevval = key[len];
1101 key[len] = NUL;
1102 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001103 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001104 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001105 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1106 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001107 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001108 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001109 if (len != -1)
1110 key[len] = prevval;
1111 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001112 {
1113 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001114 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001115 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001116 }
1117
Bram Moolenaar10c65862020-10-08 21:16:42 +02001118 if (lp->ll_valtype != NULL)
1119 // use the type of the member
1120 lp->ll_valtype = lp->ll_valtype->tt_member;
1121
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001123 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001124 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001125 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001126 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001128 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001129 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001130 return NULL;
1131 }
1132
Bram Moolenaar31b81602019-02-10 22:14:27 +01001133 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001137 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001138 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001140 }
1141 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001145 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001147 p = NULL;
1148 break;
1149 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001150 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001151 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001152 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1153 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001154 {
1155 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001156 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001157 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001158
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001159 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001161 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001162 else if (lp->ll_tv->v_type == VAR_BLOB)
1163 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001164 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1165
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001166 /*
1167 * Get the number and item for the only or first index of the List.
1168 */
1169 if (empty1)
1170 lp->ll_n1 = 0;
1171 else
1172 // is number or string
1173 lp->ll_n1 = (long)tv_get_number(&var1);
1174 clear_tv(&var1);
1175
1176 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001177 || lp->ll_n1 > bloblen
1178 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 {
1180 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001181 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001182 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001183 return NULL;
1184 }
1185 if (lp->ll_range && !lp->ll_empty2)
1186 {
1187 lp->ll_n2 = (long)tv_get_number(&var2);
1188 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001189 if (lp->ll_n2 < 0
1190 || lp->ll_n2 >= bloblen
1191 || lp->ll_n2 < lp->ll_n1)
1192 {
1193 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001194 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001195 return NULL;
1196 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001197 }
1198 lp->ll_blob = lp->ll_tv->vval.v_blob;
1199 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001200 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 else
1203 {
1204 /*
1205 * Get the number and item for the only or first index of the List.
1206 */
1207 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001209 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001210 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001211 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001212 clear_tv(&var1);
1213
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001214 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001216 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001217 if (lp->ll_li == NULL)
1218 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001219 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001220 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001223 }
1224
Bram Moolenaar10c65862020-10-08 21:16:42 +02001225 if (lp->ll_valtype != NULL)
1226 // use the type of the member
1227 lp->ll_valtype = lp->ll_valtype->tt_member;
1228
Bram Moolenaar8c711452005-01-14 21:53:12 +00001229 /*
1230 * May need to find the item or absolute index for the second
1231 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 * When no index given: "lp->ll_empty2" is TRUE.
1233 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001237 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001238 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001244 {
1245 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001246 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001248 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 }
1251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001252 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253 if (lp->ll_n1 < 0)
1254 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1255 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001256 {
1257 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001258 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001260 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001261 }
1262
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001265 }
1266
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001267 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 return p;
1270}
1271
1272/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001273 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001274 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001276clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277{
1278 vim_free(lp->ll_exp_name);
1279 vim_free(lp->ll_newkey);
1280}
1281
1282/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001283 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001284 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001285 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1286 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001289set_var_lval(
1290 lval_T *lp,
1291 char_u *endp,
1292 typval_T *rettv,
1293 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001294 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1295 char_u *op,
1296 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001297{
1298 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001299 listitem_T *ri;
1300 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001301
1302 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001304 cc = *endp;
1305 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001306 if (lp->ll_blob != NULL)
1307 {
1308 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001309
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001310 if (op != NULL && *op != '=')
1311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001312 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001313 return;
1314 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001315 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001316 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001317
1318 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1319 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001320 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001321
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001322 if (lp->ll_empty2)
1323 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1324
1325 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001327 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001328 return;
1329 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001330 if (lp->ll_empty2)
1331 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001332
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001333 ir = 0;
1334 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1335 blob_set(lp->ll_blob, il,
1336 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001337 }
1338 else
1339 {
1340 val = (int)tv_get_number_chk(rettv, &error);
1341 if (!error)
1342 {
1343 garray_T *gap = &lp->ll_blob->bv_ga;
1344
1345 // Allow for appending a byte. Setting a byte beyond
1346 // the end is an error otherwise.
1347 if (lp->ll_n1 < gap->ga_len
1348 || (lp->ll_n1 == gap->ga_len
1349 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1350 {
1351 blob_set(lp->ll_blob, lp->ll_n1, val);
1352 if (lp->ll_n1 == gap->ga_len)
1353 ++gap->ga_len;
1354 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001355 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001356 }
1357 }
1358 }
1359 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001360 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001361 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001362
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001363 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001364 {
1365 emsg(_(e_cannot_mod));
1366 *endp = cc;
1367 return;
1368 }
1369
Bram Moolenaarff697e62019-02-12 22:28:33 +01001370 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001371 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001372 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001373 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001374 {
1375 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001376 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1377 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001378 && tv_op(&tv, rettv, op) == OK)
1379 set_var(lp->ll_name, &tv, FALSE);
1380 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001383 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001384 {
1385 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001386 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001387 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001388 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1389 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001390 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001391 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001392 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001393 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001394 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001395 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001396 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001397 else if (lp->ll_range)
1398 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001399 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001400 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001401
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001402 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001403 {
1404 emsg(_("E996: Cannot lock a range"));
1405 return;
1406 }
1407
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001408 /*
1409 * Check whether any of the list items is locked
1410 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001411 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001412 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001413 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001414 return;
1415 ri = ri->li_next;
1416 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1417 break;
1418 ll_li = ll_li->li_next;
1419 ++ll_n1;
1420 }
1421
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 /*
1423 * Assign the List values to the list items.
1424 */
1425 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001426 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001427 if (op != NULL && *op != '=')
1428 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1429 else
1430 {
1431 clear_tv(&lp->ll_li->li_tv);
1432 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1433 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001434 ri = ri->li_next;
1435 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1436 break;
1437 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001438 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001439 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001440 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001441 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001442 ri = NULL;
1443 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001444 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001445 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 lp->ll_li = lp->ll_li->li_next;
1447 ++lp->ll_n1;
1448 }
1449 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001450 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001451 else if (lp->ll_empty2
1452 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001453 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001454 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001455 }
1456 else
1457 {
1458 /*
1459 * Assign to a List or Dictionary item.
1460 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001461 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001462 {
1463 emsg(_("E996: Cannot lock a list or dict"));
1464 return;
1465 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001466
1467 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001468 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001469 return;
1470
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471 if (lp->ll_newkey != NULL)
1472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001473 if (op != NULL && *op != '=')
1474 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001475 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001476 return;
1477 }
1478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001479 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001480 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001481 if (di == NULL)
1482 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001483 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1484 {
1485 vim_free(di);
1486 return;
1487 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001488 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001489 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490 else if (op != NULL && *op != '=')
1491 {
1492 tv_op(lp->ll_tv, rettv, op);
1493 return;
1494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001496 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001497
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001498 /*
1499 * Assign the value to the variable or list item.
1500 */
1501 if (copy)
1502 copy_tv(rettv, lp->ll_tv);
1503 else
1504 {
1505 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001506 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001507 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001508 }
1509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001510}
1511
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001512/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001513 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1514 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 * Returns OK or FAIL.
1516 */
1517 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001518tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001519{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001520 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521 char_u numbuf[NUMBUFLEN];
1522 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001523 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001525 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001526 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001527 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001528 {
1529 switch (tv1->v_type)
1530 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001531 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001532 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001534 case VAR_DICT:
1535 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001536 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001537 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001538 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001539 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001540 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541 break;
1542
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001543 case VAR_BLOB:
1544 if (*op != '+' || tv2->v_type != VAR_BLOB)
1545 break;
1546 // BLOB += BLOB
1547 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1548 {
1549 blob_T *b1 = tv1->vval.v_blob;
1550 blob_T *b2 = tv2->vval.v_blob;
1551 int i, len = blob_len(b2);
1552 for (i = 0; i < len; i++)
1553 ga_append(&b1->bv_ga, blob_get(b2, i));
1554 }
1555 return OK;
1556
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001557 case VAR_LIST:
1558 if (*op != '+' || tv2->v_type != VAR_LIST)
1559 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001560 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001561 if (tv2->vval.v_list != NULL)
1562 {
1563 if (tv1->vval.v_list == NULL)
1564 {
1565 tv1->vval.v_list = tv2->vval.v_list;
1566 ++tv1->vval.v_list->lv_refcount;
1567 }
1568 else
1569 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1570 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001571 return OK;
1572
1573 case VAR_NUMBER:
1574 case VAR_STRING:
1575 if (tv2->v_type == VAR_LIST)
1576 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001577 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001578 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001579 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001580 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001581#ifdef FEAT_FLOAT
1582 if (tv2->v_type == VAR_FLOAT)
1583 {
1584 float_T f = n;
1585
Bram Moolenaarff697e62019-02-12 22:28:33 +01001586 if (*op == '%')
1587 break;
1588 switch (*op)
1589 {
1590 case '+': f += tv2->vval.v_float; break;
1591 case '-': f -= tv2->vval.v_float; break;
1592 case '*': f *= tv2->vval.v_float; break;
1593 case '/': f /= tv2->vval.v_float; break;
1594 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001595 clear_tv(tv1);
1596 tv1->v_type = VAR_FLOAT;
1597 tv1->vval.v_float = f;
1598 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001599 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600#endif
1601 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001602 switch (*op)
1603 {
1604 case '+': n += tv_get_number(tv2); break;
1605 case '-': n -= tv_get_number(tv2); break;
1606 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001607 case '/': n = num_divide(n, tv_get_number(tv2),
1608 &failed); break;
1609 case '%': n = num_modulus(n, tv_get_number(tv2),
1610 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001611 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001612 clear_tv(tv1);
1613 tv1->v_type = VAR_NUMBER;
1614 tv1->vval.v_number = n;
1615 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001616 }
1617 else
1618 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001619 if (tv2->v_type == VAR_FLOAT)
1620 break;
1621
Bram Moolenaarff697e62019-02-12 22:28:33 +01001622 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001623 s = tv_get_string(tv1);
1624 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001625 clear_tv(tv1);
1626 tv1->v_type = VAR_STRING;
1627 tv1->vval.v_string = s;
1628 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001629 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001630
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001631 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001632#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001633 {
1634 float_T f;
1635
Bram Moolenaarff697e62019-02-12 22:28:33 +01001636 if (*op == '%' || *op == '.'
1637 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001638 && tv2->v_type != VAR_NUMBER
1639 && tv2->v_type != VAR_STRING))
1640 break;
1641 if (tv2->v_type == VAR_FLOAT)
1642 f = tv2->vval.v_float;
1643 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001644 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001645 switch (*op)
1646 {
1647 case '+': tv1->vval.v_float += f; break;
1648 case '-': tv1->vval.v_float -= f; break;
1649 case '*': tv1->vval.v_float *= f; break;
1650 case '/': tv1->vval.v_float /= f; break;
1651 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001652 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001653#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001654 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001655 }
1656 }
1657
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001658 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001659 return FAIL;
1660}
1661
1662/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663 * Evaluate the expression used in a ":for var in expr" command.
1664 * "arg" points to "var".
1665 * Set "*errp" to TRUE for an error, FALSE otherwise;
1666 * Return a pointer that holds the info. Null when there is an error.
1667 */
1668 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001669eval_for_line(
1670 char_u *arg,
1671 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001672 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001673 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674{
Bram Moolenaar33570922005-01-25 22:26:29 +00001675 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001676 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001677 typval_T tv;
1678 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001679 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001681 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001683 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 if (fi == NULL)
1685 return NULL;
1686
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001687 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 if (expr == NULL)
1689 return fi;
1690
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001691 expr = skipwhite_and_linebreak(expr, evalarg);
1692 if (expr[0] != 'i' || expr[1] != 'n'
1693 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 return fi;
1697 }
1698
1699 if (skip)
1700 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001701 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1702 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703 {
1704 *errp = FALSE;
1705 if (!skip)
1706 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001707 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001708 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001709 l = tv.vval.v_list;
1710 if (l == NULL)
1711 {
1712 // a null list is like an empty list: do nothing
1713 clear_tv(&tv);
1714 }
1715 else
1716 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001717 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001718 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001720 // No need to increment the refcount, it's already set for
1721 // the list being used in "tv".
1722 fi->fi_list = l;
1723 list_add_watch(l, &fi->fi_lw);
1724 fi->fi_lw.lw_item = l->lv_first;
1725 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001726 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001727 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001728 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001729 fi->fi_bi = 0;
1730 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001731 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001732 typval_T btv;
1733
1734 // Make a copy, so that the iteration still works when the
1735 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001737 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001738 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001739 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001740 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741 else
1742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001743 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001744 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745 }
1746 }
1747 }
1748 if (skip)
1749 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001750 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751
1752 return fi;
1753}
1754
1755/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001756 * Used when looping over a :for line, skip the "in expr" part.
1757 */
1758 void
1759skip_for_lines(void *fi_void, evalarg_T *evalarg)
1760{
1761 forinfo_T *fi = (forinfo_T *)fi_void;
1762 int i;
1763
1764 for (i = 0; i < fi->fi_break_count; ++i)
1765 eval_next_line(evalarg);
1766}
1767
1768/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 * Use the first item in a ":for" list. Advance to the next.
1770 * Assign the values to the variable (list). "arg" points to the first one.
1771 * Return TRUE when a valid item was found, FALSE when at end of list or
1772 * something wrong.
1773 */
1774 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001777 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 int result;
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001779 int flag = in_vim9script() ? ASSIGN_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001780 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001782 if (fi->fi_blob != NULL)
1783 {
1784 typval_T tv;
1785
1786 if (fi->fi_bi >= blob_len(fi->fi_blob))
1787 return FALSE;
1788 tv.v_type = VAR_NUMBER;
1789 tv.v_lock = VAR_FIXED;
1790 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1791 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001792 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001793 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001794 }
1795
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 item = fi->fi_lw.lw_item;
1797 if (item == NULL)
1798 result = FALSE;
1799 else
1800 {
1801 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001802 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001803 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 }
1805 return result;
1806}
1807
1808/*
1809 * Free the structure used to store info used by ":for".
1810 */
1811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001812free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813{
Bram Moolenaar33570922005-01-25 22:26:29 +00001814 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001816 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001817 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001819 list_unref(fi->fi_list);
1820 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001821 if (fi != NULL && fi->fi_blob != NULL)
1822 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 vim_free(fi);
1824}
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001827set_context_for_expression(
1828 expand_T *xp,
1829 char_u *arg,
1830 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001832 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001836 if (cmdidx == CMD_let || cmdidx == CMD_var
1837 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838 {
1839 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001840 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001842 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001843 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 {
1845 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001846 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001847 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 break;
1849 }
1850 return;
1851 }
1852 }
1853 else
1854 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1855 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 while ((xp->xp_pattern = vim_strpbrk(arg,
1857 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1858 {
1859 c = *xp->xp_pattern;
1860 if (c == '&')
1861 {
1862 c = xp->xp_pattern[1];
1863 if (c == '&')
1864 {
1865 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001866 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001871 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1872 xp->xp_pattern += 2;
1873
1874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 else if (c == '$')
1877 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001878 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 xp->xp_context = EXPAND_ENV_VARS;
1880 }
1881 else if (c == '=')
1882 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001883 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 xp->xp_context = EXPAND_EXPRESSION;
1885 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001886 else if (c == '#'
1887 && xp->xp_context == EXPAND_EXPRESSION)
1888 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001889 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001890 break;
1891 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001892 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 && xp->xp_context == EXPAND_FUNCTIONS
1894 && vim_strchr(xp->xp_pattern, '(') == NULL)
1895 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001896 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 break;
1898 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001899 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001901 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 {
1903 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1904 if (c == '\\' && xp->xp_pattern[1] != NUL)
1905 ++xp->xp_pattern;
1906 xp->xp_context = EXPAND_NOTHING;
1907 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001908 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001910 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1912 /* skip */ ;
1913 xp->xp_context = EXPAND_NOTHING;
1914 }
1915 else if (c == '|')
1916 {
1917 if (xp->xp_pattern[1] == '|')
1918 {
1919 ++xp->xp_pattern;
1920 xp->xp_context = EXPAND_EXPRESSION;
1921 }
1922 else
1923 xp->xp_context = EXPAND_COMMANDS;
1924 }
1925 else
1926 xp->xp_context = EXPAND_EXPRESSION;
1927 }
1928 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001929 // Doesn't look like something valid, expand as an expression
1930 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 arg = xp->xp_pattern;
1933 if (*arg != NUL)
1934 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1935 /* skip */ ;
1936 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001937
1938 // ":exe one two" completes "two"
1939 if ((cmdidx == CMD_execute
1940 || cmdidx == CMD_echo
1941 || cmdidx == CMD_echon
1942 || cmdidx == CMD_echomsg)
1943 && xp->xp_context == EXPAND_EXPRESSION)
1944 {
1945 for (;;)
1946 {
1947 char_u *n = skiptowhite(arg);
1948
1949 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1950 break;
1951 arg = skipwhite(n);
1952 }
1953 }
1954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 xp->xp_pattern = arg;
1956}
1957
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001959 * Return TRUE if "pat" matches "text".
1960 * Does not use 'cpo' and always uses 'magic'.
1961 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001962 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001963pattern_match(char_u *pat, char_u *text, int ic)
1964{
1965 int matches = FALSE;
1966 char_u *save_cpo;
1967 regmatch_T regmatch;
1968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001969 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001970 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001971 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001972 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1973 if (regmatch.regprog != NULL)
1974 {
1975 regmatch.rm_ic = ic;
1976 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1977 vim_regfree(regmatch.regprog);
1978 }
1979 p_cpo = save_cpo;
1980 return matches;
1981}
1982
1983/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001984 * Handle a name followed by "(". Both for just "name(arg)" and for
1985 * "expr->name(arg)".
1986 * Returns OK or FAIL.
1987 */
1988 static int
1989eval_func(
1990 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001991 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001992 char_u *name,
1993 int name_len,
1994 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001995 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001996 typval_T *basetv) // "expr" for "expr->name(arg)"
1997{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001998 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001999 char_u *s = name;
2000 int len = name_len;
2001 partial_T *partial;
2002 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002003 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002004
2005 if (!evaluate)
2006 check_vars(s, len);
2007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002008 // If "s" is the name of a variable of type VAR_FUNC
2009 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002010 s = deref_func_name(s, &len, &partial,
2011 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002012
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002013 // Need to make a copy, in case evaluating the arguments makes
2014 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002015 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002016 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002017 ret = FAIL;
2018 else
2019 {
2020 funcexe_T funcexe;
2021
2022 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002023 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002024 funcexe.firstline = curwin->w_cursor.lnum;
2025 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002026 funcexe.evaluate = evaluate;
2027 funcexe.partial = partial;
2028 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002029 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002030 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002031 }
2032 vim_free(s);
2033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002034 // If evaluate is FALSE rettv->v_type was not set in
2035 // get_func_tv, but it's needed in handle_subscript() to parse
2036 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002037 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2038 {
2039 rettv->vval.v_string = NULL;
2040 rettv->v_type = VAR_FUNC;
2041 }
2042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002043 // Stop the expression evaluation when immediately
2044 // aborting on error, or when an interrupt occurred or
2045 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002046 if (evaluate && aborting())
2047 {
2048 if (ret == OK)
2049 clear_tv(rettv);
2050 ret = FAIL;
2051 }
2052 return ret;
2053}
2054
2055/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002056 * Get the next line source line without advancing. But do skip over comment
2057 * lines.
2058 */
2059 static char_u *
2060getline_peek_skip_comments(evalarg_T *evalarg)
2061{
2062 for (;;)
2063 {
2064 char_u *next = getline_peek(evalarg->eval_getline,
2065 evalarg->eval_cookie);
2066 char_u *p;
2067
2068 if (next == NULL)
2069 break;
2070 p = skipwhite(next);
2071 if (*p != NUL && !vim9_comment_start(p))
2072 return next;
2073 (void)eval_next_line(evalarg);
2074 }
2075 return NULL;
2076}
2077
2078/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002079 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2080 * comment) and there is a next line, return the next line (skipping blanks)
2081 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002082 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2083 * "arg" must point somewhere inside a line, not at the start.
2084 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002085 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002086eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2087{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002088 char_u *p = skipwhite(arg);
2089
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002091 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002092 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002093 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002094 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002095 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002096 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002097
2098 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002099 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002100 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002101 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002102
Bram Moolenaar9d489562020-07-30 20:08:50 +02002103 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002104 {
2105 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002106 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002107 }
2108 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002109 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002110}
2111
2112/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002113 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002114 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002115 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002116eval_next_line(evalarg_T *evalarg)
2117{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002118 garray_T *gap = &evalarg->eval_ga;
2119 char_u *line;
2120
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002121 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002122 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2123 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002124 else
2125 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002126 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002127 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2128 {
2129 // Going to concatenate the lines after parsing.
2130 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2131 ++gap->ga_len;
2132 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002133 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002134 {
2135 vim_free(evalarg->eval_tofree);
2136 evalarg->eval_tofree = line;
2137 }
2138 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002139}
2140
Bram Moolenaare6b53242020-07-01 17:28:33 +02002141/*
2142 * Call eval_next_non_blank() and get the next line if needed.
2143 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002144 char_u *
2145skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2146{
2147 int getnext;
2148 char_u *p = skipwhite(arg);
2149
Bram Moolenaar962d7212020-07-04 14:15:00 +02002150 if (evalarg == NULL)
2151 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002152 eval_next_non_blank(p, evalarg, &getnext);
2153 if (getnext)
2154 return eval_next_line(evalarg);
2155 return p;
2156}
2157
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002158/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002159 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002160 */
2161 void
2162clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2163{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002164 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002165 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002166 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002167 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002168 if (eap != NULL)
2169 {
2170 // We may need to keep the original command line, e.g. for
2171 // ":let" it has the variable names. But we may also need the
2172 // new one, "nextcmd" points into it. Keep both.
2173 vim_free(eap->cmdline_tofree);
2174 eap->cmdline_tofree = *eap->cmdlinep;
2175 *eap->cmdlinep = evalarg->eval_tofree;
2176 }
2177 else
2178 vim_free(evalarg->eval_tofree);
2179 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002180 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002181
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002182 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2183 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002184 }
2185}
2186
2187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2191 */
2192
2193/*
2194 * Handle zero level expression.
2195 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002197 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002198 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 * Return OK or FAIL.
2200 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002202eval0(
2203 char_u *arg,
2204 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002205 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002206 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207{
2208 int ret;
2209 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002210 int did_emsg_before = did_emsg;
2211 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002212 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
2214 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002215 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002216 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002217
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002218 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
2220 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 /*
2223 * Report the invalid expression unless the expression evaluation has
2224 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002225 * exception, or we already gave a more specific error.
2226 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002228 if (!aborting()
2229 && did_emsg == did_emsg_before
2230 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002231 && (flags & EVAL_CONSTANT) == 0
2232 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002233 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002234
2235 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002236 // a next command to avoid more errors, unless "|" is following, which
2237 // could only be a command separator.
2238 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2239 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002240 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002242
2243 if (eap != NULL)
2244 eap->nextcmd = check_nextcmd(p);
2245
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 return ret;
2247}
2248
2249/*
2250 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002251 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002252 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 *
2254 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002255 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002257 * Note: "rettv.v_lock" is not set.
2258 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 * Return OK or FAIL.
2260 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002261 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002262eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002264 char_u *p;
2265 int getnext;
2266
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002267 CLEAR_POINTER(rettv);
2268
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 /*
2270 * Get the first variable.
2271 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002272 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 return FAIL;
2274
Bram Moolenaar793648f2020-06-26 21:28:25 +02002275 p = eval_next_non_blank(*arg, evalarg, &getnext);
2276 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002278 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002279 int result;
2280 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002281 evalarg_T *evalarg_used = evalarg;
2282 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002283 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002284 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002285 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002286
2287 if (evalarg == NULL)
2288 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002289 CLEAR_FIELD(local_evalarg);
2290 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002291 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002292 orig_flags = evalarg_used->eval_flags;
2293 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002295 if (getnext)
2296 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002297 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002298 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002299 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002300 {
2301 error_white_both(p, 1);
2302 clear_tv(rettv);
2303 return FAIL;
2304 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002305 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002306 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002309 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002311 int error = FALSE;
2312
Bram Moolenaar13106602020-10-04 16:06:05 +02002313 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002314 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002315 else if (vim9script)
2316 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002317 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002319 if (error || !op_falsy || !result)
2320 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002321 if (error)
2322 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324
2325 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002326 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002328 if (op_falsy)
2329 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002330 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002331 {
2332 error_white_both(p, 1);
2333 clear_tv(rettv);
2334 return FAIL;
2335 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002336 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002337 evalarg_used->eval_flags = (op_falsy ? !result : result)
2338 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2339 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002340 {
2341 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002343 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002344 if (!op_falsy || !result)
2345 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002347 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002349 /*
2350 * Check for the ":".
2351 */
2352 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2353 if (*p != ':')
2354 {
2355 emsg(_(e_missing_colon));
2356 if (evaluate && result)
2357 clear_tv(rettv);
2358 evalarg_used->eval_flags = orig_flags;
2359 return FAIL;
2360 }
2361 if (getnext)
2362 *arg = eval_next_line(evalarg_used);
2363 else
2364 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002365 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002366 {
2367 error_white_both(p, 1);
2368 clear_tv(rettv);
2369 evalarg_used->eval_flags = orig_flags;
2370 return FAIL;
2371 }
2372 *arg = p;
2373 }
2374
2375 /*
2376 * Get the third variable. Recursive!
2377 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002378 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002379 {
2380 error_white_both(p, 1);
2381 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002382 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002383 return FAIL;
2384 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002385 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2386 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002387 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002388 if (eval1(arg, &var2, evalarg_used) == FAIL)
2389 {
2390 if (evaluate && result)
2391 clear_tv(rettv);
2392 evalarg_used->eval_flags = orig_flags;
2393 return FAIL;
2394 }
2395 if (evaluate && !result)
2396 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002398
2399 if (evalarg == NULL)
2400 clear_evalarg(&local_evalarg, NULL);
2401 else
2402 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404
2405 return OK;
2406}
2407
2408/*
2409 * Handle first level expression:
2410 * expr2 || expr2 || expr2 logical OR
2411 *
2412 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002413 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 *
2415 * Return OK or FAIL.
2416 */
2417 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002418eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002420 char_u *p;
2421 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423 /*
2424 * Get the first variable.
2425 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 return FAIL;
2428
2429 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002430 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002432 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002433 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002435 evalarg_T *evalarg_used = evalarg;
2436 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002437 int evaluate;
2438 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002439 long result = FALSE;
2440 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002441 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002442 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002443
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002444 if (evalarg == NULL)
2445 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002446 CLEAR_FIELD(local_evalarg);
2447 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002448 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002449 orig_flags = evalarg_used->eval_flags;
2450 evaluate = orig_flags & EVAL_EVALUATE;
2451 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002452 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002453 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002454 result = tv_get_bool_chk(rettv, &error);
2455 else if (tv_get_number_chk(rettv, &error) != 0)
2456 result = TRUE;
2457 clear_tv(rettv);
2458 if (error)
2459 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461
2462 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002463 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002465 while (p[0] == '|' && p[1] == '|')
2466 {
2467 if (getnext)
2468 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002469 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002470 {
2471 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2472 {
2473 error_white_both(p, 2);
2474 clear_tv(rettv);
2475 return FAIL;
2476 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002477 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002478 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002479
2480 /*
2481 * Get the second variable.
2482 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002483 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2484 {
2485 error_white_both(p, 2);
2486 clear_tv(rettv);
2487 return FAIL;
2488 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002489 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2490 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002491 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002492 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002493 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002494
2495 /*
2496 * Compute the result.
2497 */
2498 if (evaluate && !result)
2499 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002500 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002501 result = tv_get_bool_chk(&var2, &error);
2502 else if (tv_get_number_chk(&var2, &error) != 0)
2503 result = TRUE;
2504 clear_tv(&var2);
2505 if (error)
2506 return FAIL;
2507 }
2508 if (evaluate)
2509 {
2510 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002511 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002512 rettv->v_type = VAR_BOOL;
2513 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002514 }
2515 else
2516 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002517 rettv->v_type = VAR_NUMBER;
2518 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002519 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002520 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002521
2522 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002524
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002525 if (evalarg == NULL)
2526 clear_evalarg(&local_evalarg, NULL);
2527 else
2528 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 }
2530
2531 return OK;
2532}
2533
2534/*
2535 * Handle second level expression:
2536 * expr3 && expr3 && expr3 logical AND
2537 *
2538 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002539 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 *
2541 * Return OK or FAIL.
2542 */
2543 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002544eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002546 char_u *p;
2547 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
2549 /*
2550 * Get the first variable.
2551 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002552 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 return FAIL;
2554
2555 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002556 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002558 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002559 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002561 evalarg_T *evalarg_used = evalarg;
2562 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002563 int orig_flags;
2564 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002565 long result = TRUE;
2566 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002567 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002568 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002569
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002570 if (evalarg == NULL)
2571 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002572 CLEAR_FIELD(local_evalarg);
2573 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002574 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002575 orig_flags = evalarg_used->eval_flags;
2576 evaluate = orig_flags & EVAL_EVALUATE;
2577 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002578 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002579 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002580 result = tv_get_bool_chk(rettv, &error);
2581 else if (tv_get_number_chk(rettv, &error) == 0)
2582 result = FALSE;
2583 clear_tv(rettv);
2584 if (error)
2585 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
2587
2588 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002589 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002591 while (p[0] == '&' && p[1] == '&')
2592 {
2593 if (getnext)
2594 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002595 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002596 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002597 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002598 {
2599 error_white_both(p, 2);
2600 clear_tv(rettv);
2601 return FAIL;
2602 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002603 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002604 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002605
2606 /*
2607 * Get the second variable.
2608 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002609 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2610 {
2611 error_white_both(p, 2);
2612 clear_tv(rettv);
2613 return FAIL;
2614 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002615 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2616 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002617 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002618 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002619 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002620 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002621
2622 /*
2623 * Compute the result.
2624 */
2625 if (evaluate && result)
2626 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002627 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002628 result = tv_get_bool_chk(&var2, &error);
2629 else if (tv_get_number_chk(&var2, &error) == 0)
2630 result = FALSE;
2631 clear_tv(&var2);
2632 if (error)
2633 return FAIL;
2634 }
2635 if (evaluate)
2636 {
2637 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002638 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002639 rettv->v_type = VAR_BOOL;
2640 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002641 }
2642 else
2643 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002644 rettv->v_type = VAR_NUMBER;
2645 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002646 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002647 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002648
2649 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002651
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002652 if (evalarg == NULL)
2653 clear_evalarg(&local_evalarg, NULL);
2654 else
2655 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
2657
2658 return OK;
2659}
2660
2661/*
2662 * Handle third level expression:
2663 * var1 == var2
2664 * var1 =~ var2
2665 * var1 != var2
2666 * var1 !~ var2
2667 * var1 > var2
2668 * var1 >= var2
2669 * var1 < var2
2670 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002671 * var1 is var2
2672 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 *
2674 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002675 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 *
2677 * Return OK or FAIL.
2678 */
2679 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002680eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002683 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002684 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002686 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
2688 /*
2689 * Get the first variable.
2690 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002691 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return FAIL;
2693
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002694 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002695 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002698 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002700 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002702 typval_T var2;
2703 int ic;
2704 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002705 int evaluate = evalarg == NULL
2706 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002707
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002708 if (getnext)
2709 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002710 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2711 {
2712 error_white_both(p, len);
2713 clear_tv(rettv);
2714 return FAIL;
2715 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002716
Bram Moolenaar696ba232020-07-29 21:20:41 +02002717 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2718 {
2719 semsg(_(e_invexpr2), p);
2720 clear_tv(rettv);
2721 return FAIL;
2722 }
2723
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002724 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (p[len] == '?')
2726 {
2727 ic = TRUE;
2728 ++len;
2729 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002730 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 else if (p[len] == '#')
2732 {
2733 ic = FALSE;
2734 ++len;
2735 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002736 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002738 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739
2740 /*
2741 * Get the second variable.
2742 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002743 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2744 {
2745 error_white_both(p, 1);
2746 clear_tv(rettv);
2747 return FAIL;
2748 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002749 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002750 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002752 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 return FAIL;
2754 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002755 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002756 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002757 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002758
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002759 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002760 {
2761 ret = FAIL;
2762 clear_tv(rettv);
2763 }
2764 else
2765 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002766 clear_tv(&var2);
2767 return ret;
2768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770
2771 return OK;
2772}
2773
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002774/*
2775 * Make a copy of blob "tv1" and append blob "tv2".
2776 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 void
2778eval_addblob(typval_T *tv1, typval_T *tv2)
2779{
2780 blob_T *b1 = tv1->vval.v_blob;
2781 blob_T *b2 = tv2->vval.v_blob;
2782 blob_T *b = blob_alloc();
2783 int i;
2784
2785 if (b != NULL)
2786 {
2787 for (i = 0; i < blob_len(b1); i++)
2788 ga_append(&b->bv_ga, blob_get(b1, i));
2789 for (i = 0; i < blob_len(b2); i++)
2790 ga_append(&b->bv_ga, blob_get(b2, i));
2791
2792 clear_tv(tv1);
2793 rettv_blob_set(tv1, b);
2794 }
2795}
2796
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002797/*
2798 * Make a copy of list "tv1" and append list "tv2".
2799 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 int
2801eval_addlist(typval_T *tv1, typval_T *tv2)
2802{
2803 typval_T var3;
2804
2805 // concatenate Lists
2806 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2807 {
2808 clear_tv(tv1);
2809 clear_tv(tv2);
2810 return FAIL;
2811 }
2812 clear_tv(tv1);
2813 *tv1 = var3;
2814 return OK;
2815}
2816
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817/*
2818 * Handle fourth level expression:
2819 * + number addition
2820 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002821 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002822 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 *
2824 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002825 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 *
2827 * Return OK or FAIL.
2828 */
2829 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002830eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 /*
2833 * Get the first variable.
2834 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002835 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 return FAIL;
2837
2838 /*
2839 * Repeat computing, until no '+', '-' or '.' is following.
2840 */
2841 for (;;)
2842 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002843 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002844 int getnext;
2845 char_u *p;
2846 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002847 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002848 int concat;
2849 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002850 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002851
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002852 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002853 // "+=", "-=" and "..=" are assignments
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002854 p = eval_next_non_blank(*arg, evalarg, &getnext);
2855 op = *p;
2856 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002857 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2858 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002860
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002861 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002862 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002863 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002864 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002865 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002866 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002867 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002868 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002869 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002870 clear_tv(rettv);
2871 return FAIL;
2872 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002873 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002874 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002875 if ((op != '+' || (rettv->v_type != VAR_LIST
2876 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002877#ifdef FEAT_FLOAT
2878 && (op == '.' || rettv->v_type != VAR_FLOAT)
2879#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002880 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002881 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002882 int error = FALSE;
2883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002884 // For "list + ...", an illegal use of the first operand as
2885 // a number cannot be determined before evaluating the 2nd
2886 // operand: if this is also a list, all is ok.
2887 // For "something . ...", "something - ..." or "non-list + ...",
2888 // we know that the first operand needs to be a string or number
2889 // without evaluating the 2nd operand. So check before to avoid
2890 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002891 if (op != '.')
2892 tv_get_number_chk(rettv, &error);
2893 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002894 {
2895 clear_tv(rettv);
2896 return FAIL;
2897 }
2898 }
2899
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 /*
2901 * Get the second variable.
2902 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002903 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002904 {
2905 error_white_both(p, oplen);
2906 clear_tv(rettv);
2907 return FAIL;
2908 }
2909 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002910 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 return FAIL;
2914 }
2915
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002916 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 {
2918 /*
2919 * Compute the result.
2920 */
2921 if (op == '.')
2922 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002923 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2924 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002925 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002926
Bram Moolenaar2e086612020-08-25 22:37:48 +02002927 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002928 || var2.v_type == VAR_CHANNEL
2929 || var2.v_type == VAR_JOB))
2930 emsg(_(e_inval_string));
2931#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002932 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002933 {
2934 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2935 var2.vval.v_float);
2936 s2 = buf2;
2937 }
2938#endif
2939 else
2940 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002941 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002942 {
2943 clear_tv(rettv);
2944 clear_tv(&var2);
2945 return FAIL;
2946 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002948 clear_tv(rettv);
2949 rettv->v_type = VAR_STRING;
2950 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002952 else if (op == '+' && rettv->v_type == VAR_BLOB
2953 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002955 else if (op == '+' && rettv->v_type == VAR_LIST
2956 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002957 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002959 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 else
2962 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002963 int error = FALSE;
2964 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002965#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002966 float_T f1 = 0, f2 = 0;
2967
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002968 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002969 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002970 f1 = rettv->vval.v_float;
2971 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002972 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002973 else
2974#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002976 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002977 if (error)
2978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002979 // This can only happen for "list + non-list". For
2980 // "non-list + ..." or "something - ...", we returned
2981 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002982 clear_tv(rettv);
2983 return FAIL;
2984 }
2985#ifdef FEAT_FLOAT
2986 if (var2.v_type == VAR_FLOAT)
2987 f1 = n1;
2988#endif
2989 }
2990#ifdef FEAT_FLOAT
2991 if (var2.v_type == VAR_FLOAT)
2992 {
2993 f2 = var2.vval.v_float;
2994 n2 = 0;
2995 }
2996 else
2997#endif
2998 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002999 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003000 if (error)
3001 {
3002 clear_tv(rettv);
3003 clear_tv(&var2);
3004 return FAIL;
3005 }
3006#ifdef FEAT_FLOAT
3007 if (rettv->v_type == VAR_FLOAT)
3008 f2 = n2;
3009#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003010 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003011 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003012
3013#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003014 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003015 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3016 {
3017 if (op == '+')
3018 f1 = f1 + f2;
3019 else
3020 f1 = f1 - f2;
3021 rettv->v_type = VAR_FLOAT;
3022 rettv->vval.v_float = f1;
3023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003025#endif
3026 {
3027 if (op == '+')
3028 n1 = n1 + n2;
3029 else
3030 n1 = n1 - n2;
3031 rettv->v_type = VAR_NUMBER;
3032 rettv->vval.v_number = n1;
3033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003035 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 }
3037 }
3038 return OK;
3039}
3040
3041/*
3042 * Handle fifth level expression:
3043 * * number multiplication
3044 * / number division
3045 * % number modulo
3046 *
3047 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003048 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 *
3050 * Return OK or FAIL.
3051 */
3052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003053eval6(
3054 char_u **arg,
3055 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003056 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003057 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003059#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003060 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063 /*
3064 * Get the first variable.
3065 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003066 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 return FAIL;
3068
3069 /*
3070 * Repeat computing, until no '*', '/' or '%' is following.
3071 */
3072 for (;;)
3073 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003074 int evaluate;
3075 int getnext;
3076 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003077 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003078 int op;
3079 varnumber_T n1, n2;
3080#ifdef FEAT_FLOAT
3081 float_T f1, f2;
3082#endif
3083 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003084
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003085 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003086 p = eval_next_non_blank(*arg, evalarg, &getnext);
3087 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003088 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003090
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003091 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003092 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003093 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003094 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003095 {
3096 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3097 {
3098 error_white_both(p, 1);
3099 clear_tv(rettv);
3100 return FAIL;
3101 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003102 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003105#ifdef FEAT_FLOAT
3106 f1 = 0;
3107 f2 = 0;
3108#endif
3109 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003110 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003112#ifdef FEAT_FLOAT
3113 if (rettv->v_type == VAR_FLOAT)
3114 {
3115 f1 = rettv->vval.v_float;
3116 use_float = TRUE;
3117 n1 = 0;
3118 }
3119 else
3120#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003121 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003122 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003123 if (error)
3124 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 }
3126 else
3127 n1 = 0;
3128
3129 /*
3130 * Get the second variable.
3131 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003132 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3133 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003134 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003135 clear_tv(rettv);
3136 return FAIL;
3137 }
3138 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003139 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 return FAIL;
3141
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003142 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003144#ifdef FEAT_FLOAT
3145 if (var2.v_type == VAR_FLOAT)
3146 {
3147 if (!use_float)
3148 {
3149 f1 = n1;
3150 use_float = TRUE;
3151 }
3152 f2 = var2.vval.v_float;
3153 n2 = 0;
3154 }
3155 else
3156#endif
3157 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003158 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159 clear_tv(&var2);
3160 if (error)
3161 return FAIL;
3162#ifdef FEAT_FLOAT
3163 if (use_float)
3164 f2 = n2;
3165#endif
3166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
3168 /*
3169 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003170 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003172#ifdef FEAT_FLOAT
3173 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003175 if (op == '*')
3176 f1 = f1 * f2;
3177 else if (op == '/')
3178 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003179# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003180 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003181 if (f2 == 0.0)
3182 {
3183 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003184 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003185 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003186 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003187 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003188 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003189 }
3190 else
3191 f1 = f1 / f2;
3192# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003193 // We rely on the floating point library to handle divide
3194 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003195 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003196# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003199 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003201 return FAIL;
3202 }
3203 rettv->v_type = VAR_FLOAT;
3204 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
3206 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003209 int failed = FALSE;
3210
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003211 if (op == '*')
3212 n1 = n1 * n2;
3213 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003214 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003216 n1 = num_modulus(n1, n2, &failed);
3217 if (failed)
3218 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003219
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003220 rettv->v_type = VAR_NUMBER;
3221 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 }
3224 }
3225
3226 return OK;
3227}
3228
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003229 int
3230eval_leader(char_u **arg, int vim9)
3231{
3232 char_u *s = *arg;
3233 char_u *p = *arg;
3234
3235 while (*p == '!' || *p == '-' || *p == '+')
3236 {
3237 char_u *n = skipwhite(p + 1);
3238
3239 // ++, --, -+ and +- are not accepted in Vim9 script
3240 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3241 {
3242 semsg(_(e_invexpr2), s);
3243 return FAIL;
3244 }
3245 p = n;
3246 }
3247 *arg = p;
3248 return OK;
3249}
3250
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251/*
3252 * Handle sixth level expression:
3253 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003254 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003255 * "string" string constant
3256 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 * &option-name option value
3258 * @r register contents
3259 * identifier variable value
3260 * function() function call
3261 * $VAR environment variable
3262 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003263 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003265 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003266 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 *
3268 * Also handle:
3269 * ! in front logical NOT
3270 * - in front unary minus
3271 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003272 * trailing [] subscript in String or List
3273 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003274 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 *
3276 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003277 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 *
3279 * Return OK or FAIL.
3280 */
3281 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003282eval7(
3283 char_u **arg,
3284 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003285 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003288 int evaluate = evalarg != NULL
3289 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 int len;
3291 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 char_u *start_leader, *end_leader;
3293 int ret = OK;
3294 char_u *alias;
3295
3296 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003297 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003298 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003300 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301
3302 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003303 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 */
3305 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003306 if (eval_leader(arg, in_vim9script()) == FAIL)
3307 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 end_leader = *arg;
3309
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003310 if (**arg == '.' && (!isdigit(*(*arg + 1))
3311#ifdef FEAT_FLOAT
3312 || current_sctx.sc_version < 2
3313#endif
3314 ))
3315 {
3316 semsg(_(e_invexpr2), *arg);
3317 ++*arg;
3318 return FAIL;
3319 }
3320
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 switch (**arg)
3322 {
3323 /*
3324 * Number constant.
3325 */
3326 case '0':
3327 case '1':
3328 case '2':
3329 case '3':
3330 case '4':
3331 case '5':
3332 case '6':
3333 case '7':
3334 case '8':
3335 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003336 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003337
3338 // Apply prefixed "-" and "+" now. Matters especially when
3339 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003340 if (ret == OK && evaluate && end_leader > start_leader
3341 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003342 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 break;
3344
3345 /*
3346 * String constant: "string".
3347 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003348 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 break;
3350
3351 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003352 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003354 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003355 break;
3356
3357 /*
3358 * List: [expr, expr]
3359 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003360 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 break;
3362
3363 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003364 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003365 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003366 case '#': if (in_vim9script())
3367 {
3368 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3369 }
3370 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003371 {
3372 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003373 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003374 }
3375 else
3376 ret = NOTDONE;
3377 break;
3378
3379 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003380 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003381 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003382 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003383 case '{': if (in_vim9script())
3384 ret = NOTDONE;
3385 else
3386 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003387 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003388 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003389 break;
3390
3391 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003392 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003394 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 break;
3396
3397 /*
3398 * Environment variable: $VAR.
3399 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003400 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 break;
3402
3403 /*
3404 * Register contents: @r.
3405 */
3406 case '@': ++*arg;
3407 if (evaluate)
3408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003409 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003410 rettv->vval.v_string = get_reg_contents(**arg,
3411 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 }
3413 if (**arg != NUL)
3414 ++*arg;
3415 break;
3416
3417 /*
3418 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003419 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003421 case '(': ret = NOTDONE;
3422 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003423 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003424 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003425 if (ret == OK && evaluate)
3426 {
3427 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3428
3429 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003430 if (compile_def_function(ufunc,
3431 TRUE, PROFILING(ufunc), NULL) == FAIL)
3432 {
3433 clear_tv(rettv);
3434 ret = FAIL;
3435 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003436 }
3437 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003438 if (ret == NOTDONE)
3439 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003440 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003441 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003442
Bram Moolenaar9215f012020-06-27 21:18:00 +02003443 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003444 if (**arg == ')')
3445 ++*arg;
3446 else if (ret == OK)
3447 {
3448 emsg(_(e_missing_close));
3449 clear_tv(rettv);
3450 ret = FAIL;
3451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
3453 break;
3454
Bram Moolenaar8c711452005-01-14 21:53:12 +00003455 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 break;
3457 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003458
3459 if (ret == NOTDONE)
3460 {
3461 /*
3462 * Must be a variable or function name.
3463 * Can also be a curly-braces kind of name: {expr}.
3464 */
3465 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003466 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003467 if (alias != NULL)
3468 s = alias;
3469
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003470 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003471 ret = FAIL;
3472 else
3473 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003474 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3475
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003476 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3477 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003478 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003479 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003480 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003481 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003482 else if (flags & EVAL_CONSTANT)
3483 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003484 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003485 {
3486 // get the value of "true", "false" or a variable
3487 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3488 {
3489 rettv->v_type = VAR_BOOL;
3490 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003491 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003492 }
3493 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003494 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003495 {
3496 rettv->v_type = VAR_BOOL;
3497 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003498 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003499 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003500 else if (len == 4 && in_vim9script()
3501 && STRNCMP(s, "null", 4) == 0)
3502 {
3503 rettv->v_type = VAR_SPECIAL;
3504 rettv->vval.v_number = VVAL_NULL;
3505 ret = OK;
3506 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003507 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003508 ret = eval_variable(s, len, rettv, NULL,
3509 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003510 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003511 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003512 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003513 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003514 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003515 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003516 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003517 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003518 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003519 }
3520
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003521 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3522 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003523 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003524 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
3526 /*
3527 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3528 */
3529 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003530 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003531 return ret;
3532}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003533
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003534/*
3535 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003536 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003537 * Adjusts "end_leaderp" until it is at "start_leader".
3538 */
3539 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003540eval7_leader(
3541 typval_T *rettv,
3542 int numeric_only,
3543 char_u *start_leader,
3544 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003545{
3546 char_u *end_leader = *end_leaderp;
3547 int ret = OK;
3548 int error = FALSE;
3549 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003550 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003551#ifdef FEAT_FLOAT
3552 float_T f = 0.0;
3553
3554 if (rettv->v_type == VAR_FLOAT)
3555 f = rettv->vval.v_float;
3556 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003557#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003558 {
3559 while (VIM_ISWHITE(end_leader[-1]))
3560 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003561 if (in_vim9script() && end_leader[-1] == '!')
3562 val = tv2bool(rettv);
3563 else
3564 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003565 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003566 if (error)
3567 {
3568 clear_tv(rettv);
3569 ret = FAIL;
3570 }
3571 else
3572 {
3573 while (end_leader > start_leader)
3574 {
3575 --end_leader;
3576 if (*end_leader == '!')
3577 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003578 if (numeric_only)
3579 {
3580 ++end_leader;
3581 break;
3582 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003583#ifdef FEAT_FLOAT
3584 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003585 {
3586 if (in_vim9script())
3587 {
3588 rettv->v_type = VAR_BOOL;
3589 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3590 }
3591 else
3592 f = !f;
3593 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003594 else
3595#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003596 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003597 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003598 type = VAR_BOOL;
3599 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003600 }
3601 else if (*end_leader == '-')
3602 {
3603#ifdef FEAT_FLOAT
3604 if (rettv->v_type == VAR_FLOAT)
3605 f = -f;
3606 else
3607#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003608 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003609 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003610 type = VAR_NUMBER;
3611 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003612 }
3613 }
3614#ifdef FEAT_FLOAT
3615 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003617 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003618 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003620 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003621#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003622 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003623 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003624 if (in_vim9script())
3625 rettv->v_type = type;
3626 else
3627 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003628 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003631 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 return ret;
3633}
3634
3635/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003636 * Call the function referred to in "rettv".
3637 */
3638 static int
3639call_func_rettv(
3640 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003641 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003642 typval_T *rettv,
3643 int evaluate,
3644 dict_T *selfdict,
3645 typval_T *basetv)
3646{
3647 partial_T *pt = NULL;
3648 funcexe_T funcexe;
3649 typval_T functv;
3650 char_u *s;
3651 int ret;
3652
3653 // need to copy the funcref so that we can clear rettv
3654 if (evaluate)
3655 {
3656 functv = *rettv;
3657 rettv->v_type = VAR_UNKNOWN;
3658
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003659 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003660 if (functv.v_type == VAR_PARTIAL)
3661 {
3662 pt = functv.vval.v_partial;
3663 s = partial_name(pt);
3664 }
3665 else
3666 s = functv.vval.v_string;
3667 }
3668 else
3669 s = (char_u *)"";
3670
Bram Moolenaara80faa82020-04-12 19:37:17 +02003671 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003672 funcexe.firstline = curwin->w_cursor.lnum;
3673 funcexe.lastline = curwin->w_cursor.lnum;
3674 funcexe.evaluate = evaluate;
3675 funcexe.partial = pt;
3676 funcexe.selfdict = selfdict;
3677 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003678 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003680 // Clear the funcref afterwards, so that deleting it while
3681 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003682 if (evaluate)
3683 clear_tv(&functv);
3684
3685 return ret;
3686}
3687
3688/*
3689 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003690 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003691 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3692 */
3693 static int
3694eval_lambda(
3695 char_u **arg,
3696 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003697 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003698 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003699{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003700 int evaluate = evalarg != NULL
3701 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003702 typval_T base = *rettv;
3703 int ret;
3704
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003705 rettv->v_type = VAR_UNKNOWN;
3706
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003707 if (**arg == '{')
3708 {
3709 // ->{lambda}()
3710 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3711 }
3712 else
3713 {
3714 // ->(lambda)()
3715 ++*arg;
3716 ret = eval1(arg, rettv, evalarg);
3717 *arg = skipwhite_and_linebreak(*arg, evalarg);
3718 if (**arg != ')')
3719 {
3720 emsg(_(e_missing_close));
3721 ret = FAIL;
3722 }
3723 ++*arg;
3724 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003725 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003726 return FAIL;
3727 else if (**arg != '(')
3728 {
3729 if (verbose)
3730 {
3731 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003732 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003733 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003735 }
3736 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003737 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003738 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003739 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003740 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003741
3742 // Clear the funcref afterwards, so that deleting it while
3743 // evaluating the arguments is possible (see test55).
3744 if (evaluate)
3745 clear_tv(&base);
3746
3747 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003748}
3749
3750/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003751 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003752 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003753 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3754 */
3755 static int
3756eval_method(
3757 char_u **arg,
3758 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003759 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003760 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003761{
3762 char_u *name;
3763 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003764 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003765 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003766 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003767 int evaluate = evalarg != NULL
3768 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003769
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003770 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003771
Bram Moolenaarac92e252019-08-03 21:58:38 +02003772 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003773 len = get_name_len(arg, &alias, evaluate, TRUE);
3774 if (alias != NULL)
3775 name = alias;
3776
3777 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003778 {
3779 if (verbose)
3780 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003781 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003782 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003783 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003784 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003785 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003786 if (**arg != '(')
3787 {
3788 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003790 ret = FAIL;
3791 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003792 else if (VIM_ISWHITE((*arg)[-1]))
3793 {
3794 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003795 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003796 ret = FAIL;
3797 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003798 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003799 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003800 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003801 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003802
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003803 // Clear the funcref afterwards, so that deleting it while
3804 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003805 if (evaluate)
3806 clear_tv(&base);
3807
Bram Moolenaarac92e252019-08-03 21:58:38 +02003808 return ret;
3809}
3810
3811/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003812 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3813 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003814 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3815 */
3816 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003817eval_index(
3818 char_u **arg,
3819 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003820 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003821 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003822{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003823 int evaluate = evalarg != NULL
3824 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003825 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003826 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003827 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003828 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003829 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003830 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003831
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003832 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3833 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003834
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003835 init_tv(&var1);
3836 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003837 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003838 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003839 /*
3840 * dict.name
3841 */
3842 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003843 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003844 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003845 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003846 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003847 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003848 }
3849 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003850 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003851 /*
3852 * something[idx]
3853 *
3854 * Get the (first) variable from inside the [].
3855 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003856 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003857 if (**arg == ':')
3858 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003859 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003860 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003861 else if (vim9 && **arg == ':')
3862 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003863 semsg(_(e_white_space_required_before_and_after_str_at_str),
3864 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003865 clear_tv(&var1);
3866 return FAIL;
3867 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003868 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003869 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003870#ifdef FEAT_FLOAT
3871 // allow for indexing with float
3872 if (vim9 && rettv->v_type == VAR_DICT
3873 && var1.v_type == VAR_FLOAT)
3874 {
3875 var1.vval.v_string = typval_tostring(&var1, TRUE);
3876 var1.v_type = VAR_STRING;
3877 }
3878#endif
3879 if (tv_get_string_chk(&var1) == NULL)
3880 {
3881 // not a number or string
3882 clear_tv(&var1);
3883 return FAIL;
3884 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003885 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003886
3887 /*
3888 * Get the second variable from inside the [:].
3889 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003890 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003891 if (**arg == ':')
3892 {
3893 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003894 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003895 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003896 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003897 semsg(_(e_white_space_required_before_and_after_str_at_str),
3898 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003899 if (!empty1)
3900 clear_tv(&var1);
3901 return FAIL;
3902 }
3903 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003904 if (**arg == ']')
3905 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003906 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003908 if (!empty1)
3909 clear_tv(&var1);
3910 return FAIL;
3911 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003912 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003913 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003914 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003915 if (!empty1)
3916 clear_tv(&var1);
3917 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003918 return FAIL;
3919 }
3920 }
3921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003922 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003923 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003924 if (**arg != ']')
3925 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003926 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003927 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003928 clear_tv(&var1);
3929 if (range)
3930 clear_tv(&var2);
3931 return FAIL;
3932 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003933 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003934 }
3935
3936 if (evaluate)
3937 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003938 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003939 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003940 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003941
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003942 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003943 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003944 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003945 clear_tv(&var2);
3946 return res;
3947 }
3948 return OK;
3949}
3950
3951/*
3952 * Check if "rettv" can have an [index] or [sli:ce]
3953 */
3954 int
3955check_can_index(typval_T *rettv, int evaluate, int verbose)
3956{
3957 switch (rettv->v_type)
3958 {
3959 case VAR_FUNC:
3960 case VAR_PARTIAL:
3961 if (verbose)
3962 emsg(_("E695: Cannot index a Funcref"));
3963 return FAIL;
3964 case VAR_FLOAT:
3965#ifdef FEAT_FLOAT
3966 if (verbose)
3967 emsg(_(e_float_as_string));
3968 return FAIL;
3969#endif
3970 case VAR_BOOL:
3971 case VAR_SPECIAL:
3972 case VAR_JOB:
3973 case VAR_CHANNEL:
3974 if (verbose)
3975 emsg(_(e_cannot_index_special_variable));
3976 return FAIL;
3977 case VAR_UNKNOWN:
3978 case VAR_ANY:
3979 case VAR_VOID:
3980 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003981 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003982 emsg(_(e_cannot_index_special_variable));
3983 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003984 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003985 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003986
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003987 case VAR_STRING:
3988 case VAR_LIST:
3989 case VAR_DICT:
3990 case VAR_BLOB:
3991 break;
3992 case VAR_NUMBER:
3993 if (in_vim9script())
3994 emsg(_(e_cannot_index_number));
3995 break;
3996 }
3997 return OK;
3998}
3999
4000/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004001 * slice() function
4002 */
4003 void
4004f_slice(typval_T *argvars, typval_T *rettv)
4005{
4006 if (check_can_index(argvars, TRUE, FALSE) == OK)
4007 {
4008 copy_tv(argvars, rettv);
4009 eval_index_inner(rettv, TRUE, argvars + 1,
4010 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4011 TRUE, NULL, 0, FALSE);
4012 }
4013}
4014
4015/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004016 * Apply index or range to "rettv".
4017 * "var1" is the first index, NULL for [:expr].
4018 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004019 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4020 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004021 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4022 */
4023 int
4024eval_index_inner(
4025 typval_T *rettv,
4026 int is_range,
4027 typval_T *var1,
4028 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004029 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004030 char_u *key,
4031 int keylen,
4032 int verbose)
4033{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004034 varnumber_T n1, n2 = 0;
4035 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004036
4037 n1 = 0;
4038 if (var1 != NULL && rettv->v_type != VAR_DICT)
4039 n1 = tv_get_number(var1);
4040
4041 if (is_range)
4042 {
4043 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004044 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004045 if (verbose)
4046 emsg(_(e_cannot_slice_dictionary));
4047 return FAIL;
4048 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004049 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004050 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004051 else
4052 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004053 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004054
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004055 switch (rettv->v_type)
4056 {
4057 case VAR_UNKNOWN:
4058 case VAR_ANY:
4059 case VAR_VOID:
4060 case VAR_FUNC:
4061 case VAR_PARTIAL:
4062 case VAR_FLOAT:
4063 case VAR_BOOL:
4064 case VAR_SPECIAL:
4065 case VAR_JOB:
4066 case VAR_CHANNEL:
4067 break; // not evaluating, skipping over subscript
4068
4069 case VAR_NUMBER:
4070 case VAR_STRING:
4071 {
4072 char_u *s = tv_get_string(rettv);
4073
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004074 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004075 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004076 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004077 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004078 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004079 else
4080 s = char_from_string(s, n1);
4081 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004082 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004083 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004084 // The resulting variable is a substring. If the indexes
4085 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004086 if (n1 < 0)
4087 {
4088 n1 = len + n1;
4089 if (n1 < 0)
4090 n1 = 0;
4091 }
4092 if (n2 < 0)
4093 n2 = len + n2;
4094 else if (n2 >= len)
4095 n2 = len;
4096 if (n1 >= len || n2 < 0 || n1 > n2)
4097 s = NULL;
4098 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004099 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004100 }
4101 else
4102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004103 // The resulting variable is a string of a single
4104 // character. If the index is too big or negative the
4105 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004106 if (n1 >= len || n1 < 0)
4107 s = NULL;
4108 else
4109 s = vim_strnsave(s + n1, 1);
4110 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 clear_tv(rettv);
4112 rettv->v_type = VAR_STRING;
4113 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004114 }
4115 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004116
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004117 case VAR_BLOB:
4118 len = blob_len(rettv->vval.v_blob);
4119 if (is_range)
4120 {
4121 // The resulting variable is a sub-blob. If the indexes
4122 // are out of range the result is empty.
4123 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004124 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004125 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004126 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004127 n1 = 0;
4128 }
4129 if (n2 < 0)
4130 n2 = len + n2;
4131 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004132 n2 = len - (exclusive ? 0 : 1);
4133 if (exclusive)
4134 --n2;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004135 if (n1 >= len || n2 < 0 || n1 > n2)
4136 {
4137 clear_tv(rettv);
4138 rettv->v_type = VAR_BLOB;
4139 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004140 }
4141 else
4142 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004143 blob_T *blob = blob_alloc();
4144 long i;
4145
4146 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004147 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004148 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004149 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004150 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004151 return FAIL;
4152 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004153 blob->bv_ga.ga_len = n2 - n1 + 1;
4154 for (i = n1; i <= n2; i++)
4155 blob_set(blob, i - n1,
4156 blob_get(rettv->vval.v_blob, i));
4157
4158 clear_tv(rettv);
4159 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004160 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004161 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004162 }
4163 else
4164 {
4165 // The resulting variable is a byte value.
4166 // If the index is too big or negative that is an error.
4167 if (n1 < 0)
4168 n1 = len + n1;
4169 if (n1 < len && n1 >= 0)
4170 {
4171 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004172
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004173 clear_tv(rettv);
4174 rettv->v_type = VAR_NUMBER;
4175 rettv->vval.v_number = v;
4176 }
4177 else
4178 semsg(_(e_blobidx), n1);
4179 }
4180 break;
4181
4182 case VAR_LIST:
4183 if (var1 == NULL)
4184 n1 = 0;
4185 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004186 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004187 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004188 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004189 return FAIL;
4190 break;
4191
4192 case VAR_DICT:
4193 {
4194 dictitem_T *item;
4195 typval_T tmp;
4196
4197 if (key == NULL)
4198 {
4199 key = tv_get_string_chk(var1);
4200 if (key == NULL)
4201 return FAIL;
4202 }
4203
4204 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4205
4206 if (item == NULL && verbose)
4207 semsg(_(e_dictkey), key);
4208 if (item == NULL)
4209 return FAIL;
4210
4211 copy_tv(&item->di_tv, &tmp);
4212 clear_tv(rettv);
4213 *rettv = tmp;
4214 }
4215 break;
4216 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004217 return OK;
4218}
4219
4220/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004221 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004222 */
4223 char_u *
4224partial_name(partial_T *pt)
4225{
4226 if (pt->pt_name != NULL)
4227 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004228 if (pt->pt_func != NULL)
4229 return pt->pt_func->uf_name;
4230 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004231}
4232
Bram Moolenaarddecc252016-04-06 22:59:37 +02004233 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004234partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004235{
4236 int i;
4237
4238 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004239 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004240 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004241 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004242 if (pt->pt_name != NULL)
4243 {
4244 func_unref(pt->pt_name);
4245 vim_free(pt->pt_name);
4246 }
4247 else
4248 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004249
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004250 // Decrease the reference count for the context of a closure. If down
4251 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004252 if (pt->pt_funcstack != NULL)
4253 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004254 --pt->pt_funcstack->fs_refcount;
4255 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004256 }
4257
Bram Moolenaarddecc252016-04-06 22:59:37 +02004258 vim_free(pt);
4259}
4260
4261/*
4262 * Unreference a closure: decrement the reference count and free it when it
4263 * becomes zero.
4264 */
4265 void
4266partial_unref(partial_T *pt)
4267{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004268 if (pt != NULL)
4269 {
4270 if (--pt->pt_refcount <= 0)
4271 partial_free(pt);
4272
4273 // If the reference count goes down to one, the funcstack may be the
4274 // only reference and can be freed if no other partials reference it.
4275 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4276 funcstack_check_refcount(pt->pt_funcstack);
4277 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004278}
4279
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004280/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004281 * Return the next (unique) copy ID.
4282 * Used for serializing nested structures.
4283 */
4284 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004285get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004286{
4287 current_copyID += COPYID_INC;
4288 return current_copyID;
4289}
4290
4291/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004292 * Garbage collection for lists and dictionaries.
4293 *
4294 * We use reference counts to be able to free most items right away when they
4295 * are no longer used. But for composite items it's possible that it becomes
4296 * unused while the reference count is > 0: When there is a recursive
4297 * reference. Example:
4298 * :let l = [1, 2, 3]
4299 * :let d = {9: l}
4300 * :let l[1] = d
4301 *
4302 * Since this is quite unusual we handle this with garbage collection: every
4303 * once in a while find out which lists and dicts are not referenced from any
4304 * variable.
4305 *
4306 * Here is a good reference text about garbage collection (refers to Python
4307 * but it applies to all reference-counting mechanisms):
4308 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004309 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004310
4311/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004312 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004313 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004314 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004315 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004316 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004317garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004318{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004319 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004320 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004321 buf_T *buf;
4322 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004323 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004324 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004325
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004326 if (!testing)
4327 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004328 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004329 want_garbage_collect = FALSE;
4330 may_garbage_collect = FALSE;
4331 garbage_collect_at_exit = FALSE;
4332 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004333
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004334 // The execution stack can grow big, limit the size.
4335 if (exestack.ga_maxlen - exestack.ga_len > 500)
4336 {
4337 size_t new_len;
4338 char_u *pp;
4339 int n;
4340
4341 // Keep 150% of the current size, with a minimum of the growth size.
4342 n = exestack.ga_len / 2;
4343 if (n < exestack.ga_growsize)
4344 n = exestack.ga_growsize;
4345
4346 // Don't make it bigger though.
4347 if (exestack.ga_len + n < exestack.ga_maxlen)
4348 {
4349 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4350 pp = vim_realloc(exestack.ga_data, new_len);
4351 if (pp == NULL)
4352 return FAIL;
4353 exestack.ga_maxlen = exestack.ga_len + n;
4354 exestack.ga_data = pp;
4355 }
4356 }
4357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004358 // We advance by two because we add one for items referenced through
4359 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004360 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004361
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004362 /*
4363 * 1. Go through all accessible variables and mark all lists and dicts
4364 * with copyID.
4365 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004367 // Don't free variables in the previous_funccal list unless they are only
4368 // referenced through previous_funccal. This must be first, because if
4369 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004370 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004371
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004372 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004373 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004375 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004376 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004377 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4378 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004379
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004380 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004381 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004382 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4383 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004384 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004385 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4386 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004387#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004388 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004389 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4390 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004391 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004392 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004393 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4394 NULL, NULL);
4395#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004396
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004397 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004398 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004399 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4400 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004401 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004402 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004404 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004405 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004407 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004408 abort = abort || set_ref_in_functions(copyID);
4409
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004410 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004412
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004413 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004414 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004415
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004416 // callbacks in buffers
4417 abort = abort || set_ref_in_buffers(copyID);
4418
Bram Moolenaar1dced572012-04-05 16:54:08 +02004419#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004420 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004421#endif
4422
Bram Moolenaardb913952012-06-29 12:54:53 +02004423#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004424 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004425#endif
4426
4427#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004428 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004429#endif
4430
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004431#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004432 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004433 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004434#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004435#ifdef FEAT_NETBEANS_INTG
4436 abort = abort || set_ref_in_nb_channel(copyID);
4437#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004438
Bram Moolenaare3188e22016-05-31 21:13:04 +02004439#ifdef FEAT_TIMERS
4440 abort = abort || set_ref_in_timer(copyID);
4441#endif
4442
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004443#ifdef FEAT_QUICKFIX
4444 abort = abort || set_ref_in_quickfix(copyID);
4445#endif
4446
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004447#ifdef FEAT_TERMINAL
4448 abort = abort || set_ref_in_term(copyID);
4449#endif
4450
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004451#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004452 abort = abort || set_ref_in_popups(copyID);
4453#endif
4454
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004455 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004456 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004457 /*
4458 * 2. Free lists and dictionaries that are not referenced.
4459 */
4460 did_free = free_unref_items(copyID);
4461
4462 /*
4463 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004464 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004465 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004466 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004467 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004468 else if (p_verbose > 0)
4469 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004470 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004471 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004472
4473 return did_free;
4474}
4475
4476/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004477 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004478 */
4479 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004480free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004481{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004482 int did_free = FALSE;
4483
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004484 // Let all "free" functions know that we are here. This means no
4485 // dictionaries, lists, channels or jobs are to be freed, because we will
4486 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004487 in_free_unref_items = TRUE;
4488
4489 /*
4490 * PASS 1: free the contents of the items. We don't free the items
4491 * themselves yet, so that it is possible to decrement refcount counters
4492 */
4493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004494 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004495 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004497 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004498 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004499
4500#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004501 // Go through the list of jobs and free items without the copyID. This
4502 // must happen before doing channels, because jobs refer to channels, but
4503 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004504 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004506 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004507 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4508#endif
4509
4510 /*
4511 * PASS 2: free the items themselves.
4512 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004513 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004514 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004515
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004516#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004517 // Go through the list of jobs and free items without the copyID. This
4518 // must happen before doing channels, because jobs refer to channels, but
4519 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004520 free_unused_jobs(copyID, COPYID_MASK);
4521
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004522 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004523 free_unused_channels(copyID, COPYID_MASK);
4524#endif
4525
4526 in_free_unref_items = FALSE;
4527
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004528 return did_free;
4529}
4530
4531/*
4532 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004533 * "list_stack" is used to add lists to be marked. Can be NULL.
4534 *
4535 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004536 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004538set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004539{
4540 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004541 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004542 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004543 hashtab_T *cur_ht;
4544 ht_stack_T *ht_stack = NULL;
4545 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004546
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004547 cur_ht = ht;
4548 for (;;)
4549 {
4550 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004551 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004552 // Mark each item in the hashtab. If the item contains a hashtab
4553 // it is added to ht_stack, if it contains a list it is added to
4554 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004555 todo = (int)cur_ht->ht_used;
4556 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4557 if (!HASHITEM_EMPTY(hi))
4558 {
4559 --todo;
4560 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4561 &ht_stack, list_stack);
4562 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004563 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004564
4565 if (ht_stack == NULL)
4566 break;
4567
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004568 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004569 cur_ht = ht_stack->ht;
4570 tempitem = ht_stack;
4571 ht_stack = ht_stack->prev;
4572 free(tempitem);
4573 }
4574
4575 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004576}
4577
4578/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004579 * Mark a dict and its items with "copyID".
4580 * Returns TRUE if setting references failed somehow.
4581 */
4582 int
4583set_ref_in_dict(dict_T *d, int copyID)
4584{
4585 if (d != NULL && d->dv_copyID != copyID)
4586 {
4587 d->dv_copyID = copyID;
4588 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4589 }
4590 return FALSE;
4591}
4592
4593/*
4594 * Mark a list and its items with "copyID".
4595 * Returns TRUE if setting references failed somehow.
4596 */
4597 int
4598set_ref_in_list(list_T *ll, int copyID)
4599{
4600 if (ll != NULL && ll->lv_copyID != copyID)
4601 {
4602 ll->lv_copyID = copyID;
4603 return set_ref_in_list_items(ll, copyID, NULL);
4604 }
4605 return FALSE;
4606}
4607
4608/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004609 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004610 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4611 *
4612 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004613 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004614 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004615set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004616{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004617 listitem_T *li;
4618 int abort = FALSE;
4619 list_T *cur_l;
4620 list_stack_T *list_stack = NULL;
4621 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004622
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004623 cur_l = l;
4624 for (;;)
4625 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004626 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004627 // Mark each item in the list. If the item contains a hashtab
4628 // it is added to ht_stack, if it contains a list it is added to
4629 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004630 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4631 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4632 ht_stack, &list_stack);
4633 if (list_stack == NULL)
4634 break;
4635
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004636 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004637 cur_l = list_stack->list;
4638 tempitem = list_stack;
4639 list_stack = list_stack->prev;
4640 free(tempitem);
4641 }
4642
4643 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004644}
4645
4646/*
4647 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004648 * "list_stack" is used to add lists to be marked. Can be NULL.
4649 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4650 *
4651 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004652 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004654set_ref_in_item(
4655 typval_T *tv,
4656 int copyID,
4657 ht_stack_T **ht_stack,
4658 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004659{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004660 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004661
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004662 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004663 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004664 dict_T *dd = tv->vval.v_dict;
4665
Bram Moolenaara03f2332016-02-06 18:09:59 +01004666 if (dd != NULL && dd->dv_copyID != copyID)
4667 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004668 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004669 dd->dv_copyID = copyID;
4670 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004671 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004672 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4673 }
4674 else
4675 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004676 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4677
Bram Moolenaara03f2332016-02-06 18:09:59 +01004678 if (newitem == NULL)
4679 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004680 else
4681 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004682 newitem->ht = &dd->dv_hashtab;
4683 newitem->prev = *ht_stack;
4684 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004685 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004686 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004687 }
4688 }
4689 else if (tv->v_type == VAR_LIST)
4690 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004691 list_T *ll = tv->vval.v_list;
4692
Bram Moolenaara03f2332016-02-06 18:09:59 +01004693 if (ll != NULL && ll->lv_copyID != copyID)
4694 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004695 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004696 ll->lv_copyID = copyID;
4697 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004698 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004699 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004700 }
4701 else
4702 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004703 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4704
Bram Moolenaara03f2332016-02-06 18:09:59 +01004705 if (newitem == NULL)
4706 abort = TRUE;
4707 else
4708 {
4709 newitem->list = ll;
4710 newitem->prev = *list_stack;
4711 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004712 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004713 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004714 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004715 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004716 else if (tv->v_type == VAR_FUNC)
4717 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004718 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004719 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004720 else if (tv->v_type == VAR_PARTIAL)
4721 {
4722 partial_T *pt = tv->vval.v_partial;
4723 int i;
4724
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004725 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004726 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004727 // Didn't see this partial yet.
4728 pt->pt_copyID = copyID;
4729
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004730 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004731
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004732 if (pt->pt_dict != NULL)
4733 {
4734 typval_T dtv;
4735
4736 dtv.v_type = VAR_DICT;
4737 dtv.vval.v_dict = pt->pt_dict;
4738 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4739 }
4740
4741 for (i = 0; i < pt->pt_argc; ++i)
4742 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4743 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004744 if (pt->pt_funcstack != NULL)
4745 {
4746 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4747
4748 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4749 abort = abort || set_ref_in_item(stack + i, copyID,
4750 ht_stack, list_stack);
4751 }
4752
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004753 }
4754 }
4755#ifdef FEAT_JOB_CHANNEL
4756 else if (tv->v_type == VAR_JOB)
4757 {
4758 job_T *job = tv->vval.v_job;
4759 typval_T dtv;
4760
4761 if (job != NULL && job->jv_copyID != copyID)
4762 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004763 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004764 if (job->jv_channel != NULL)
4765 {
4766 dtv.v_type = VAR_CHANNEL;
4767 dtv.vval.v_channel = job->jv_channel;
4768 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4769 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004770 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004771 {
4772 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004773 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004774 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4775 }
4776 }
4777 }
4778 else if (tv->v_type == VAR_CHANNEL)
4779 {
4780 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004781 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004782 typval_T dtv;
4783 jsonq_T *jq;
4784 cbq_T *cq;
4785
4786 if (ch != NULL && ch->ch_copyID != copyID)
4787 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004788 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004789 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004790 {
4791 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4792 jq = jq->jq_next)
4793 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4794 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4795 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004796 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004797 {
4798 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004799 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004800 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4801 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004802 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004803 {
4804 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004805 dtv.vval.v_partial =
4806 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004807 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4808 }
4809 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004810 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004811 {
4812 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004813 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004814 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4815 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004816 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004817 {
4818 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004819 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004820 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4821 }
4822 }
4823 }
4824#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004825 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004826}
4827
Bram Moolenaar8c711452005-01-14 21:53:12 +00004828/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 * Return a string with the string representation of a variable.
4830 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004831 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004832 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004833 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004834 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004835 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004836 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4837 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004838 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004840 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004841echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004842 typval_T *tv,
4843 char_u **tofree,
4844 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004845 int copyID,
4846 int echo_style,
4847 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004848 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004849{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004850 static int recurse = 0;
4851 char_u *r = NULL;
4852
Bram Moolenaar33570922005-01-25 22:26:29 +00004853 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004854 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004855 if (!did_echo_string_emsg)
4856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004857 // Only give this message once for a recursive call to avoid
4858 // flooding the user with errors. And stop iterating over lists
4859 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004860 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004861 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004863 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004864 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004865 }
4866 ++recurse;
4867
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004868 switch (tv->v_type)
4869 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004870 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004871 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004872 {
4873 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004874 r = tv->vval.v_string;
4875 if (r == NULL)
4876 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004877 }
4878 else
4879 {
4880 *tofree = string_quote(tv->vval.v_string, FALSE);
4881 r = *tofree;
4882 }
4883 break;
4884
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004885 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004886 if (echo_style)
4887 {
4888 *tofree = NULL;
4889 r = tv->vval.v_string;
4890 }
4891 else
4892 {
4893 *tofree = string_quote(tv->vval.v_string, TRUE);
4894 r = *tofree;
4895 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004896 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004897
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004898 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004899 {
4900 partial_T *pt = tv->vval.v_partial;
4901 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004902 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004903 garray_T ga;
4904 int i;
4905 char_u *tf;
4906
4907 ga_init2(&ga, 1, 100);
4908 ga_concat(&ga, (char_u *)"function(");
4909 if (fname != NULL)
4910 {
4911 ga_concat(&ga, fname);
4912 vim_free(fname);
4913 }
4914 if (pt != NULL && pt->pt_argc > 0)
4915 {
4916 ga_concat(&ga, (char_u *)", [");
4917 for (i = 0; i < pt->pt_argc; ++i)
4918 {
4919 if (i > 0)
4920 ga_concat(&ga, (char_u *)", ");
4921 ga_concat(&ga,
4922 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4923 vim_free(tf);
4924 }
4925 ga_concat(&ga, (char_u *)"]");
4926 }
4927 if (pt != NULL && pt->pt_dict != NULL)
4928 {
4929 typval_T dtv;
4930
4931 ga_concat(&ga, (char_u *)", ");
4932 dtv.v_type = VAR_DICT;
4933 dtv.vval.v_dict = pt->pt_dict;
4934 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4935 vim_free(tf);
4936 }
4937 ga_concat(&ga, (char_u *)")");
4938
4939 *tofree = ga.ga_data;
4940 r = *tofree;
4941 break;
4942 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004943
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004944 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004945 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004946 break;
4947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004948 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004949 if (tv->vval.v_list == NULL)
4950 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004951 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004952 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004953 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004954 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004955 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4956 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004957 {
4958 *tofree = NULL;
4959 r = (char_u *)"[...]";
4960 }
4961 else
4962 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004963 int old_copyID = tv->vval.v_list->lv_copyID;
4964
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004965 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004966 *tofree = list2string(tv, copyID, restore_copyID);
4967 if (restore_copyID)
4968 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004969 r = *tofree;
4970 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004971 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004972
Bram Moolenaar8c711452005-01-14 21:53:12 +00004973 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004974 if (tv->vval.v_dict == NULL)
4975 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004976 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004977 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004978 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004979 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004980 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4981 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004982 {
4983 *tofree = NULL;
4984 r = (char_u *)"{...}";
4985 }
4986 else
4987 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004988 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004989
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004990 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004991 *tofree = dict2string(tv, copyID, restore_copyID);
4992 if (restore_copyID)
4993 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004994 r = *tofree;
4995 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004996 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004997
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004999 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005000 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005002 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005003 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005004 break;
5005
Bram Moolenaar835dc632016-02-07 14:27:38 +01005006 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005007 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005008 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005009 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005010 if (composite_val)
5011 {
5012 *tofree = string_quote(r, FALSE);
5013 r = *tofree;
5014 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005015 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005016
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005017 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005018#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005019 *tofree = NULL;
5020 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5021 r = numbuf;
5022 break;
5023#endif
5024
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005025 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005026 case VAR_SPECIAL:
5027 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005028 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005029 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005030 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005031
Bram Moolenaar8502c702014-06-17 12:51:16 +02005032 if (--recurse == 0)
5033 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005034 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005035}
5036
5037/*
5038 * Return a string with the string representation of a variable.
5039 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5040 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005041 * Does not put quotes around strings, as ":echo" displays values.
5042 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5043 * May return NULL.
5044 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005045 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005046echo_string(
5047 typval_T *tv,
5048 char_u **tofree,
5049 char_u *numbuf,
5050 int copyID)
5051{
5052 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5053}
5054
5055/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005056 * Return string "str" in ' quotes, doubling ' characters.
5057 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005059 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005060 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005061string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062{
Bram Moolenaar33570922005-01-25 22:26:29 +00005063 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005064 char_u *p, *r, *s;
5065
Bram Moolenaar33570922005-01-25 22:26:29 +00005066 len = (function ? 13 : 3);
5067 if (str != NULL)
5068 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005069 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005070 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005071 if (*p == '\'')
5072 ++len;
5073 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005074 s = r = alloc(len);
5075 if (r != NULL)
5076 {
5077 if (function)
5078 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005080 r += 10;
5081 }
5082 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005084 if (str != NULL)
5085 for (p = str; *p != NUL; )
5086 {
5087 if (*p == '\'')
5088 *r++ = '\'';
5089 MB_COPY_CHAR(p, r);
5090 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005092 if (function)
5093 *r++ = ')';
5094 *r++ = NUL;
5095 }
5096 return s;
5097}
5098
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005099#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005100/*
5101 * Convert the string "text" to a floating point number.
5102 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5103 * this always uses a decimal point.
5104 * Returns the length of the text that was consumed.
5105 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005106 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005107string2float(
5108 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005109 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005110{
5111 char *s = (char *)text;
5112 float_T f;
5113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005114 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005115 if (STRNICMP(text, "inf", 3) == 0)
5116 {
5117 *value = INFINITY;
5118 return 3;
5119 }
5120 if (STRNICMP(text, "-inf", 3) == 0)
5121 {
5122 *value = -INFINITY;
5123 return 4;
5124 }
5125 if (STRNICMP(text, "nan", 3) == 0)
5126 {
5127 *value = NAN;
5128 return 3;
5129 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005130 f = strtod(s, &s);
5131 *value = f;
5132 return (int)((char_u *)s - text);
5133}
5134#endif
5135
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005136/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005137 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5138 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005139 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005140 */
5141 int
5142buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5143{
5144 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005145 char_u *t;
5146 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005147
5148 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5149 return -1;
5150
5151 if (lnum > buf->b_ml.ml_line_count)
5152 lnum = buf->b_ml.ml_line_count;
5153
5154 str = ml_get_buf(buf, lnum, FALSE);
5155 if (str == NULL)
5156 return -1;
5157
5158 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005159 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005160
Bram Moolenaar91458462021-01-13 20:08:38 +01005161 // count the number of characters
5162 t = str;
5163 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5164 t += mb_ptr2len(t);
5165
5166 // In insert mode, when the cursor is at the end of a non-empty line,
5167 // byteidx points to the NUL character immediately past the end of the
5168 // string. In this case, add one to the character count.
5169 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5170 count++;
5171
5172 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005173}
5174
5175/*
5176 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005177 * byte index. Works only for loaded buffers. Returns -1 on failure.
5178 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005179 */
5180 int
5181buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5182{
5183 char_u *str;
5184 char_u *t;
5185
5186 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5187 return -1;
5188
5189 if (lnum > buf->b_ml.ml_line_count)
5190 lnum = buf->b_ml.ml_line_count;
5191
5192 str = ml_get_buf(buf, lnum, FALSE);
5193 if (str == NULL)
5194 return -1;
5195
5196 // Convert the character offset to a byte offset
5197 t = str;
5198 while (*t != NUL && --charidx > 0)
5199 t += mb_ptr2len(t);
5200
Bram Moolenaar91458462021-01-13 20:08:38 +01005201 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005202}
5203
5204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005206 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005208 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005209var2fpos(
5210 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005211 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005212 int *fnum, // set to fnum for '0, 'A, etc.
5213 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005215 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005217 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005219 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005220 if (varp->v_type == VAR_LIST)
5221 {
5222 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005223 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005224 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005225 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005226
5227 l = varp->vval.v_list;
5228 if (l == NULL)
5229 return NULL;
5230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005231 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005232 pos.lnum = list_find_nr(l, 0L, &error);
5233 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005234 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005235 if (charcol)
5236 len = (long)mb_charlen(ml_get(pos.lnum));
5237 else
5238 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005239
Bram Moolenaarec65d772020-08-20 22:29:12 +02005240 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005241 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005242 li = list_find(l, 1L);
5243 if (li != NULL && li->li_tv.v_type == VAR_STRING
5244 && li->li_tv.vval.v_string != NULL
5245 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005246 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005247 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005248 }
5249 else
5250 {
5251 pos.col = list_find_nr(l, 1L, &error);
5252 if (error)
5253 return NULL;
5254 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005255
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005256 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005257 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005258 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005259 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005260
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005261 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005262 pos.coladd = list_find_nr(l, 2L, &error);
5263 if (error)
5264 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005265
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005266 return &pos;
5267 }
5268
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005269 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005270 if (name == NULL)
5271 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005272 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005273 {
5274 pos = curwin->w_cursor;
5275 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005276 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005277 return &pos;
5278 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005279 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005280 {
5281 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005282 pos = VIsual;
5283 else
5284 pos = curwin->w_cursor;
5285 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005286 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005287 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005288 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005289 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005291 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5293 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005294 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005295 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 return pp;
5297 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005298
Bram Moolenaara5525202006-03-02 22:52:09 +00005299 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005300
Bram Moolenaar477933c2007-07-17 14:32:23 +00005301 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005302 {
5303 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005304 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005305 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005306 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005307 // In silent Ex mode topline is zero, but that's not a valid line
5308 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005309 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005310 return &pos;
5311 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005312 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005313 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005314 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005315 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005316 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005317 return &pos;
5318 }
5319 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005320 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005322 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
5324 pos.lnum = curbuf->b_ml.ml_line_count;
5325 pos.col = 0;
5326 }
5327 else
5328 {
5329 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005330 if (charcol)
5331 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5332 else
5333 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335 return &pos;
5336 }
5337 return NULL;
5338}
5339
5340/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005341 * Convert list in "arg" into a position and optional file number.
5342 * When "fnump" is NULL there is no file number, only 3 items.
5343 * Note that the column is passed on as-is, the caller may want to decrement
5344 * it to use 1 for the first column.
5345 * Return FAIL when conversion is not possible, doesn't check the position for
5346 * validity.
5347 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005348 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005349list2fpos(
5350 typval_T *arg,
5351 pos_T *posp,
5352 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005353 colnr_T *curswantp,
5354 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005355{
5356 list_T *l = arg->vval.v_list;
5357 long i = 0;
5358 long n;
5359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005360 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5361 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005362 if (arg->v_type != VAR_LIST
5363 || l == NULL
5364 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005365 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005366 return FAIL;
5367
5368 if (fnump != NULL)
5369 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005370 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005371 if (n < 0)
5372 return FAIL;
5373 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005374 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005375 *fnump = n;
5376 }
5377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005379 if (n < 0)
5380 return FAIL;
5381 posp->lnum = n;
5382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005384 if (n < 0)
5385 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005386 // If character position is specified, then convert to byte position
5387 if (charcol)
5388 {
5389 buf_T *buf;
5390
5391 // Get the text for the specified line in a loaded buffer
5392 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5393 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5394 return FAIL;
5395
Bram Moolenaar91458462021-01-13 20:08:38 +01005396 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005397 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005398 posp->col = n;
5399
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005400 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005401 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005402 posp->coladd = 0;
5403 else
5404 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005405
Bram Moolenaar493c1782014-05-28 14:34:46 +02005406 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005407 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005408
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005409 return OK;
5410}
5411
5412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 * Get the length of an environment variable name.
5414 * Advance "arg" to the first character after the name.
5415 * Return 0 for error.
5416 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005417 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005418get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419{
5420 char_u *p;
5421 int len;
5422
5423 for (p = *arg; vim_isIDc(*p); ++p)
5424 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005425 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 return 0;
5427
5428 len = (int)(p - *arg);
5429 *arg = p;
5430 return len;
5431}
5432
5433/*
5434 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005435 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 * Return 0 if something is wrong.
5437 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005439get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
5441 char_u *p;
5442 int len;
5443
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005444 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005446 {
5447 if (*p == ':')
5448 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005449 // "s:" is start of "s:var", but "n:" is not and can be used in
5450 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005451 len = (int)(p - *arg);
5452 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5453 || len > 1)
5454 break;
5455 }
5456 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005457 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 return 0;
5459
5460 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005461 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 return len;
5464}
5465
5466/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005467 * Get the length of the name of a variable or function.
5468 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005470 * Return -1 if curly braces expansion failed.
5471 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 * If the name contains 'magic' {}'s, expand them and return the
5473 * expanded name in an allocated string via 'alias' - caller must free.
5474 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005476get_name_len(
5477 char_u **arg,
5478 char_u **alias,
5479 int evaluate,
5480 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481{
5482 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 char_u *p;
5484 char_u *expr_start;
5485 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005487 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
5489 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5490 && (*arg)[2] == (int)KE_SNR)
5491 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005492 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 *arg += 3;
5494 return get_id_len(arg) + 3;
5495 }
5496 len = eval_fname_script(*arg);
5497 if (len > 0)
5498 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005499 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 *arg += len;
5501 }
5502
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005504 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005506 p = find_name_end(*arg, &expr_start, &expr_end,
5507 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 if (expr_start != NULL)
5509 {
5510 char_u *temp_string;
5511
5512 if (!evaluate)
5513 {
5514 len += (int)(p - *arg);
5515 *arg = skipwhite(p);
5516 return len;
5517 }
5518
5519 /*
5520 * Include any <SID> etc in the expanded string:
5521 * Thus the -len here.
5522 */
5523 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5524 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005525 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 *alias = temp_string;
5527 *arg = skipwhite(p);
5528 return (int)STRLEN(temp_string);
5529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
5531 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005532 // Only give an error when there is something, otherwise it will be
5533 // reported at a higher level.
5534 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005535 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
5537 return len;
5538}
5539
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540/*
5541 * Find the end of a variable or function name, taking care of magic braces.
5542 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5543 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005544 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005545 * Return a pointer to just after the name. Equal to "arg" if there is no
5546 * valid name.
5547 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005548 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005549find_name_end(
5550 char_u *arg,
5551 char_u **expr_start,
5552 char_u **expr_end,
5553 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 int mb_nest = 0;
5556 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005558 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005559 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 *expr_start = NULL;
5564 *expr_end = NULL;
5565 }
5566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005567 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005568 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5569 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005570 return arg;
5571
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 for (p = arg; *p != NUL
5573 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005574 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005575 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005576 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005578 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005580 if (*p == '\'')
5581 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005582 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005583 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005584 ;
5585 if (*p == NUL)
5586 break;
5587 }
5588 else if (*p == '"')
5589 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005590 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005591 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005592 if (*p == '\\' && p[1] != NUL)
5593 ++p;
5594 if (*p == NUL)
5595 break;
5596 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005597 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005599 // "s:" is start of "s:var", but "n:" is not and can be used in
5600 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005601 len = (int)(p - arg);
5602 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005603 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005604 break;
5605 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005606
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005607 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005609 if (*p == '[')
5610 ++br_nest;
5611 else if (*p == ']')
5612 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005614
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005615 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005617 if (*p == '{')
5618 {
5619 mb_nest++;
5620 if (expr_start != NULL && *expr_start == NULL)
5621 *expr_start = p;
5622 }
5623 else if (*p == '}')
5624 {
5625 mb_nest--;
5626 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5627 *expr_end = p;
5628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
5631
5632 return p;
5633}
5634
5635/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005636 * Expands out the 'magic' {}'s in a variable/function name.
5637 * Note that this can call itself recursively, to deal with
5638 * constructs like foo{bar}{baz}{bam}
5639 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5640 * "in_start" ^
5641 * "expr_start" ^
5642 * "expr_end" ^
5643 * "in_end" ^
5644 *
5645 * Returns a new allocated string, which the caller must free.
5646 * Returns NULL for failure.
5647 */
5648 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005649make_expanded_name(
5650 char_u *in_start,
5651 char_u *expr_start,
5652 char_u *expr_end,
5653 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005654{
5655 char_u c1;
5656 char_u *retval = NULL;
5657 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005658
5659 if (expr_end == NULL || in_end == NULL)
5660 return NULL;
5661 *expr_start = NUL;
5662 *expr_end = NUL;
5663 c1 = *in_end;
5664 *in_end = NUL;
5665
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005666 temp_result = eval_to_string(expr_start + 1, FALSE);
5667 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005668 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005669 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5670 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005671 if (retval != NULL)
5672 {
5673 STRCPY(retval, in_start);
5674 STRCAT(retval, temp_result);
5675 STRCAT(retval, expr_end + 1);
5676 }
5677 }
5678 vim_free(temp_result);
5679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005680 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005681 *expr_start = '{';
5682 *expr_end = '}';
5683
5684 if (retval != NULL)
5685 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005686 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005687 if (expr_start != NULL)
5688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005689 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005690 temp_result = make_expanded_name(retval, expr_start,
5691 expr_end, temp_result);
5692 vim_free(retval);
5693 retval = temp_result;
5694 }
5695 }
5696
5697 return retval;
5698}
5699
5700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005702 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005705eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005707 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005708}
5709
5710/*
5711 * Return TRUE if character "c" can be used as the first character in a
5712 * variable or function name (excluding '{' and '}').
5713 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005714 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005715eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005716{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005717 return ASCII_ISALPHA(c) || c == '_';
5718}
5719
5720/*
5721 * Return TRUE if character "c" can be used as the first character of a
5722 * dictionary key.
5723 */
5724 int
5725eval_isdictc(int c)
5726{
5727 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728}
5729
5730/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005731 * Handle:
5732 * - expr[expr], expr[expr:expr] subscript
5733 * - ".name" lookup
5734 * - function call with Funcref variable: func(expr)
5735 * - method call: var->method()
5736 *
5737 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005738 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005739 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005740handle_subscript(
5741 char_u **arg,
5742 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005743 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005744 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005745{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005746 int evaluate = evalarg != NULL
5747 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005748 int ret = OK;
5749 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005750 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005751 int getnext;
5752 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005753
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005754 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005755 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005756 // When at the end of the line and ".name" or "->{" or "->X" follows in
5757 // the next line then consume the line break.
5758 p = eval_next_non_blank(*arg, evalarg, &getnext);
5759 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005760 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005761 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005762 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005763 {
5764 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005765 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005766 check_white = FALSE;
5767 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005768
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005769 if (rettv->v_type == VAR_ANY)
5770 {
5771 char_u *exp_name;
5772 int cc;
5773 int idx;
5774 ufunc_T *ufunc;
5775 type_T *type;
5776
5777 // Found script from "import * as {name}", script item name must
5778 // follow.
5779 if (**arg != '.')
5780 {
5781 if (verbose)
5782 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5783 ret = FAIL;
5784 break;
5785 }
5786 ++*arg;
5787 if (IS_WHITE_OR_NUL(**arg))
5788 {
5789 if (verbose)
5790 emsg(_(e_no_white_space_allowed_after_dot));
5791 ret = FAIL;
5792 break;
5793 }
5794
5795 // isolate the name
5796 exp_name = *arg;
5797 while (eval_isnamec(**arg))
5798 ++*arg;
5799 cc = **arg;
5800 **arg = NUL;
5801
5802 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5803 evalarg->eval_cctx, verbose);
5804 **arg = cc;
5805 *arg = skipwhite(*arg);
5806
5807 if (idx < 0 && ufunc == NULL)
5808 {
5809 ret = FAIL;
5810 break;
5811 }
5812 if (idx >= 0)
5813 {
5814 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5815 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5816
5817 copy_tv(sv->sv_tv, rettv);
5818 }
5819 else
5820 {
5821 rettv->v_type = VAR_FUNC;
5822 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5823 }
5824 }
5825
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005826 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5827 || rettv->v_type == VAR_PARTIAL))
5828 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005829 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005830 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5831 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005832
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005833 // Stop the expression evaluation when immediately aborting on
5834 // error, or when an interrupt occurred or an exception was thrown
5835 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005836 if (aborting())
5837 {
5838 if (ret == OK)
5839 clear_tv(rettv);
5840 ret = FAIL;
5841 }
5842 dict_unref(selfdict);
5843 selfdict = NULL;
5844 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005845 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005846 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005847 *arg = skipwhite(p + 2);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005848 if (ret == OK)
5849 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005850 if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005851 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005852 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005853 else
5854 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005855 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005856 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005857 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005858 // "." is ".name" lookup when we found a dict or when evaluating and
5859 // scriptversion is at least 2, where string concatenation is "..".
5860 else if (**arg == '['
5861 || (**arg == '.' && (rettv->v_type == VAR_DICT
5862 || (!evaluate
5863 && (*arg)[1] != '.'
5864 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005865 {
5866 dict_unref(selfdict);
5867 if (rettv->v_type == VAR_DICT)
5868 {
5869 selfdict = rettv->vval.v_dict;
5870 if (selfdict != NULL)
5871 ++selfdict->dv_refcount;
5872 }
5873 else
5874 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005875 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005876 {
5877 clear_tv(rettv);
5878 ret = FAIL;
5879 }
5880 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005881 else
5882 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005883 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005885 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5886 // Don't do this when "Func" is already a partial that was bound
5887 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005888 if (selfdict != NULL
5889 && (rettv->v_type == VAR_FUNC
5890 || (rettv->v_type == VAR_PARTIAL
5891 && (rettv->vval.v_partial->pt_auto
5892 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005893 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005894
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005895 dict_unref(selfdict);
5896 return ret;
5897}
5898
5899/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005900 * Make a copy of an item.
5901 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005902 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5903 * reference to an already copied list/dict can be used.
5904 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005905 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005906 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005907item_copy(
5908 typval_T *from,
5909 typval_T *to,
5910 int deep,
5911 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005912{
5913 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005914 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005915
Bram Moolenaar33570922005-01-25 22:26:29 +00005916 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005917 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005918 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005919 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005920 }
5921 ++recurse;
5922
5923 switch (from->v_type)
5924 {
5925 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005926 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005927 case VAR_STRING:
5928 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005929 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005930 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005931 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005932 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005933 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005934 copy_tv(from, to);
5935 break;
5936 case VAR_LIST:
5937 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005938 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005939 if (from->vval.v_list == NULL)
5940 to->vval.v_list = NULL;
5941 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5942 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005943 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005944 to->vval.v_list = from->vval.v_list->lv_copylist;
5945 ++to->vval.v_list->lv_refcount;
5946 }
5947 else
5948 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5949 if (to->vval.v_list == NULL)
5950 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005951 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005952 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005953 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005954 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005955 case VAR_DICT:
5956 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005957 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005958 if (from->vval.v_dict == NULL)
5959 to->vval.v_dict = NULL;
5960 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005962 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005963 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5964 ++to->vval.v_dict->dv_refcount;
5965 }
5966 else
5967 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5968 if (to->vval.v_dict == NULL)
5969 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005970 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005971 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005972 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005974 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005975 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005976 }
5977 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005978 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005979}
5980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005981 void
5982echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5983{
5984 char_u *tofree;
5985 char_u numbuf[NUMBUFLEN];
5986 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5987
5988 if (*atstart)
5989 {
5990 *atstart = FALSE;
5991 // Call msg_start() after eval1(), evaluating the expression
5992 // may cause a message to appear.
5993 if (with_space)
5994 {
5995 // Mark the saved text as finishing the line, so that what
5996 // follows is displayed on a new line when scrolling back
5997 // at the more prompt.
5998 msg_sb_eol();
5999 msg_start();
6000 }
6001 }
6002 else if (with_space)
6003 msg_puts_attr(" ", echo_attr);
6004
6005 if (p != NULL)
6006 for ( ; *p != NUL && !got_int; ++p)
6007 {
6008 if (*p == '\n' || *p == '\r' || *p == TAB)
6009 {
6010 if (*p != TAB && *needclr)
6011 {
6012 // remove any text still there from the command
6013 msg_clr_eos();
6014 *needclr = FALSE;
6015 }
6016 msg_putchar_attr(*p, echo_attr);
6017 }
6018 else
6019 {
6020 if (has_mbyte)
6021 {
6022 int i = (*mb_ptr2len)(p);
6023
6024 (void)msg_outtrans_len_attr(p, i, echo_attr);
6025 p += i - 1;
6026 }
6027 else
6028 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6029 }
6030 }
6031 vim_free(tofree);
6032}
6033
Bram Moolenaare9a41262005-01-15 22:18:47 +00006034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 * ":echo expr1 ..." print each argument separated with a space, add a
6036 * newline at the end.
6037 * ":echon expr1 ..." print each argument plain.
6038 */
6039 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006040ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 char_u *p;
6045 int needclr = TRUE;
6046 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006047 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006048 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006049 evalarg_T evalarg;
6050
Bram Moolenaare707c882020-07-01 13:07:37 +02006051 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052
6053 if (eap->skip)
6054 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006055 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006057 // If eval1() causes an error message the text from the command may
6058 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006059 need_clr_eos = needclr;
6060
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006062 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 {
6064 /*
6065 * Report the invalid expression unless the expression evaluation
6066 * has been cancelled due to an aborting error, an interrupt, or an
6067 * exception.
6068 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006069 if (!aborting() && did_emsg == did_emsg_before
6070 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006071 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006072 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 break;
6074 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006075 need_clr_eos = FALSE;
6076
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006078 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006079
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006080 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 arg = skipwhite(arg);
6082 }
6083 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006084 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085
6086 if (eap->skip)
6087 --emsg_skip;
6088 else
6089 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006090 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 if (needclr)
6092 msg_clr_eos();
6093 if (eap->cmdidx == CMD_echo)
6094 msg_end();
6095 }
6096}
6097
6098/*
6099 * ":echohl {name}".
6100 */
6101 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006102ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006104 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105}
6106
6107/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006108 * Returns the :echo attribute
6109 */
6110 int
6111get_echo_attr(void)
6112{
6113 return echo_attr;
6114}
6115
6116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 * ":execute expr1 ..." execute the result of an expression.
6118 * ":echomsg expr1 ..." Print a message
6119 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006120 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 * Each gets spaces around each argument and a newline at the end for
6122 * echo commands
6123 */
6124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006125ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126{
6127 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006128 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 int ret = OK;
6130 char_u *p;
6131 garray_T ga;
6132 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133
6134 ga_init2(&ga, 1, 80);
6135
6136 if (eap->skip)
6137 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006138 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006140 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006141 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
6144 if (!eap->skip)
6145 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006146 char_u buf[NUMBUFLEN];
6147
6148 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006149 {
6150 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6151 {
6152 emsg(_(e_inval_string));
6153 p = NULL;
6154 }
6155 else
6156 p = tv_get_string_buf(&rettv, buf);
6157 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006158 else
6159 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006160 if (p == NULL)
6161 {
6162 clear_tv(&rettv);
6163 ret = FAIL;
6164 break;
6165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 len = (int)STRLEN(p);
6167 if (ga_grow(&ga, len + 2) == FAIL)
6168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006169 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 ret = FAIL;
6171 break;
6172 }
6173 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 ga.ga_len += len;
6177 }
6178
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006179 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 arg = skipwhite(arg);
6181 }
6182
6183 if (ret != FAIL && ga.ga_data != NULL)
6184 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006185 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006187 // Mark the already saved text as finishing the line, so that what
6188 // follows is displayed on a new line when scrolling back at the
6189 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006190 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006191 }
6192
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006194 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006195 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006196 out_flush();
6197 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006198 else if (eap->cmdidx == CMD_echoconsole)
6199 {
6200 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6201 ui_write((char_u *)"\r\n", 2, TRUE);
6202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 else if (eap->cmdidx == CMD_echoerr)
6204 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006205 int save_did_emsg = did_emsg;
6206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006207 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006208 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 if (!force_abort)
6210 did_emsg = save_did_emsg;
6211 }
6212 else if (eap->cmdidx == CMD_execute)
6213 do_cmdline((char_u *)ga.ga_data,
6214 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6215 }
6216
6217 ga_clear(&ga);
6218
6219 if (eap->skip)
6220 --emsg_skip;
6221
6222 eap->nextcmd = check_nextcmd(arg);
6223}
6224
6225/*
6226 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6227 * "arg" points to the "&" or '+' when called, to "option" when returning.
6228 * Returns NULL when no option name found. Otherwise pointer to the char
6229 * after the option name.
6230 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006231 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006232find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233{
6234 char_u *p = *arg;
6235
6236 ++p;
6237 if (*p == 'g' && p[1] == ':')
6238 {
6239 *opt_flags = OPT_GLOBAL;
6240 p += 2;
6241 }
6242 else if (*p == 'l' && p[1] == ':')
6243 {
6244 *opt_flags = OPT_LOCAL;
6245 p += 2;
6246 }
6247 else
6248 *opt_flags = 0;
6249
6250 if (!ASCII_ISALPHA(*p))
6251 return NULL;
6252 *arg = p;
6253
6254 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006255 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 else
6257 while (ASCII_ISALPHA(*p))
6258 ++p;
6259 return p;
6260}
6261
6262/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006263 * Display script name where an item was last set.
6264 * Should only be invoked when 'verbose' is non-zero.
6265 */
6266 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006267last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006268{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006269 char_u *p;
6270
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006271 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006272 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006273 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006274 if (p != NULL)
6275 {
6276 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006277 msg_puts(_("\n\tLast set from "));
6278 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006279 if (script_ctx.sc_lnum > 0)
6280 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006281 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006282 msg_outnum((long)script_ctx.sc_lnum);
6283 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006284 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006285 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006286 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006287 }
6288}
6289
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006290#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291
6292/*
6293 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006294 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 * "flags" can be "g" to do a global substitute.
6296 * Returns an allocated string, NULL for error.
6297 */
6298 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006299do_string_sub(
6300 char_u *str,
6301 char_u *pat,
6302 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006303 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006304 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
6306 int sublen;
6307 regmatch_T regmatch;
6308 int i;
6309 int do_all;
6310 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006311 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 garray_T ga;
6313 char_u *ret;
6314 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006315 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006317 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006319 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320
6321 ga_init2(&ga, 1, 200);
6322
6323 do_all = (flags[0] == 'g');
6324
6325 regmatch.rm_ic = p_ic;
6326 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6327 if (regmatch.regprog != NULL)
6328 {
6329 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006330 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6332 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006333 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006334 if (regmatch.startp[0] == regmatch.endp[0])
6335 {
6336 if (zero_width == regmatch.startp[0])
6337 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006338 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006339 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006340 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6341 (size_t)i);
6342 ga.ga_len += i;
6343 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006344 continue;
6345 }
6346 zero_width = regmatch.startp[0];
6347 }
6348
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 /*
6350 * Get some space for a temporary buffer to do the substitution
6351 * into. It will contain:
6352 * - The text up to where the match is.
6353 * - The substituted text.
6354 * - The text after the match.
6355 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006356 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006357 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6359 {
6360 ga_clear(&ga);
6361 break;
6362 }
6363
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006364 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 i = (int)(regmatch.startp[0] - tail);
6366 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006367 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006368 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 + ga.ga_len + i, TRUE, TRUE, FALSE);
6370 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006371 tail = regmatch.endp[0];
6372 if (*tail == NUL)
6373 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (!do_all)
6375 break;
6376 }
6377
6378 if (ga.ga_data != NULL)
6379 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6380
Bram Moolenaar473de612013-06-08 18:19:48 +02006381 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 }
6383
6384 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6385 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006386 if (p_cpo == empty_option)
6387 p_cpo = save_cpo;
6388 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006389 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006390 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006391 // If it's still empty it was changed and restored, need to restore in
6392 // the complicated way.
6393 if (*p_cpo == NUL)
6394 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006395 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397
6398 return ret;
6399}