blob: 8e964de700b95a9612ba391170e1481aa5a3854b [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
2182 vim_free(evalarg->eval_tofree_lambda);
2183 evalarg->eval_tofree_lambda = NULL;
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
2231 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002232 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002233
2234 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002235 // a next command to avoid more errors, unless "|" is following, which
2236 // could only be a command separator.
2237 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2238 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002239 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002241
2242 if (eap != NULL)
2243 eap->nextcmd = check_nextcmd(p);
2244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 return ret;
2246}
2247
2248/*
2249 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002250 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002251 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 *
2253 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002254 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002256 * Note: "rettv.v_lock" is not set.
2257 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 * Return OK or FAIL.
2259 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002260 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002261eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002263 char_u *p;
2264 int getnext;
2265
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002266 CLEAR_POINTER(rettv);
2267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 /*
2269 * Get the first variable.
2270 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002271 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 return FAIL;
2273
Bram Moolenaar793648f2020-06-26 21:28:25 +02002274 p = eval_next_non_blank(*arg, evalarg, &getnext);
2275 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002277 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002278 int result;
2279 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002280 evalarg_T *evalarg_used = evalarg;
2281 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002282 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002283 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002284 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002285
2286 if (evalarg == NULL)
2287 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002288 CLEAR_FIELD(local_evalarg);
2289 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002290 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002291 orig_flags = evalarg_used->eval_flags;
2292 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002293
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002294 if (getnext)
2295 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002296 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002297 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002298 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002299 {
2300 error_white_both(p, 1);
2301 clear_tv(rettv);
2302 return FAIL;
2303 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002304 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002305 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002308 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002310 int error = FALSE;
2311
Bram Moolenaar13106602020-10-04 16:06:05 +02002312 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002313 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002314 else if (vim9script)
2315 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002316 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002318 if (error || !op_falsy || !result)
2319 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002320 if (error)
2321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 }
2323
2324 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002325 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002327 if (op_falsy)
2328 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002329 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002330 {
2331 error_white_both(p, 1);
2332 clear_tv(rettv);
2333 return FAIL;
2334 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002335 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002336 evalarg_used->eval_flags = (op_falsy ? !result : result)
2337 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2338 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002339 {
2340 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002342 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002343 if (!op_falsy || !result)
2344 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002346 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002348 /*
2349 * Check for the ":".
2350 */
2351 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2352 if (*p != ':')
2353 {
2354 emsg(_(e_missing_colon));
2355 if (evaluate && result)
2356 clear_tv(rettv);
2357 evalarg_used->eval_flags = orig_flags;
2358 return FAIL;
2359 }
2360 if (getnext)
2361 *arg = eval_next_line(evalarg_used);
2362 else
2363 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002364 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002365 {
2366 error_white_both(p, 1);
2367 clear_tv(rettv);
2368 evalarg_used->eval_flags = orig_flags;
2369 return FAIL;
2370 }
2371 *arg = p;
2372 }
2373
2374 /*
2375 * Get the third variable. Recursive!
2376 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002377 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002378 {
2379 error_white_both(p, 1);
2380 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002381 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002382 return FAIL;
2383 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002384 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2385 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002386 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002387 if (eval1(arg, &var2, evalarg_used) == FAIL)
2388 {
2389 if (evaluate && result)
2390 clear_tv(rettv);
2391 evalarg_used->eval_flags = orig_flags;
2392 return FAIL;
2393 }
2394 if (evaluate && !result)
2395 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002397
2398 if (evalarg == NULL)
2399 clear_evalarg(&local_evalarg, NULL);
2400 else
2401 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403
2404 return OK;
2405}
2406
2407/*
2408 * Handle first level expression:
2409 * expr2 || expr2 || expr2 logical OR
2410 *
2411 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002412 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 *
2414 * Return OK or FAIL.
2415 */
2416 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002417eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002419 char_u *p;
2420 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422 /*
2423 * Get the first variable.
2424 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002425 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 return FAIL;
2427
2428 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002429 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002431 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002432 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002434 evalarg_T *evalarg_used = evalarg;
2435 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002436 int evaluate;
2437 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 long result = FALSE;
2439 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002440 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002441 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002442
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002443 if (evalarg == NULL)
2444 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002445 CLEAR_FIELD(local_evalarg);
2446 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002447 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002448 orig_flags = evalarg_used->eval_flags;
2449 evaluate = orig_flags & EVAL_EVALUATE;
2450 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002451 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002452 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002453 result = tv_get_bool_chk(rettv, &error);
2454 else if (tv_get_number_chk(rettv, &error) != 0)
2455 result = TRUE;
2456 clear_tv(rettv);
2457 if (error)
2458 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460
2461 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002462 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002464 while (p[0] == '|' && p[1] == '|')
2465 {
2466 if (getnext)
2467 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002468 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002469 {
2470 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2471 {
2472 error_white_both(p, 2);
2473 clear_tv(rettv);
2474 return FAIL;
2475 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002476 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002477 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002478
2479 /*
2480 * Get the second variable.
2481 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002482 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2483 {
2484 error_white_both(p, 2);
2485 clear_tv(rettv);
2486 return FAIL;
2487 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002488 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2489 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002490 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002491 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002493
2494 /*
2495 * Compute the result.
2496 */
2497 if (evaluate && !result)
2498 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002499 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002500 result = tv_get_bool_chk(&var2, &error);
2501 else if (tv_get_number_chk(&var2, &error) != 0)
2502 result = TRUE;
2503 clear_tv(&var2);
2504 if (error)
2505 return FAIL;
2506 }
2507 if (evaluate)
2508 {
2509 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002510 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002511 rettv->v_type = VAR_BOOL;
2512 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002513 }
2514 else
2515 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002516 rettv->v_type = VAR_NUMBER;
2517 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002518 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002519 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002520
2521 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002523
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002524 if (evalarg == NULL)
2525 clear_evalarg(&local_evalarg, NULL);
2526 else
2527 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
2529
2530 return OK;
2531}
2532
2533/*
2534 * Handle second level expression:
2535 * expr3 && expr3 && expr3 logical AND
2536 *
2537 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002538 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 *
2540 * Return OK or FAIL.
2541 */
2542 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002543eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002545 char_u *p;
2546 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
2548 /*
2549 * Get the first variable.
2550 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002551 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 return FAIL;
2553
2554 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002555 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002557 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002558 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002560 evalarg_T *evalarg_used = evalarg;
2561 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002562 int orig_flags;
2563 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002564 long result = TRUE;
2565 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002566 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002567 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002568
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002569 if (evalarg == NULL)
2570 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002571 CLEAR_FIELD(local_evalarg);
2572 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002573 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002574 orig_flags = evalarg_used->eval_flags;
2575 evaluate = orig_flags & EVAL_EVALUATE;
2576 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002577 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002578 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002579 result = tv_get_bool_chk(rettv, &error);
2580 else if (tv_get_number_chk(rettv, &error) == 0)
2581 result = FALSE;
2582 clear_tv(rettv);
2583 if (error)
2584 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586
2587 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002588 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002590 while (p[0] == '&' && p[1] == '&')
2591 {
2592 if (getnext)
2593 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002594 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002595 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002596 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002597 {
2598 error_white_both(p, 2);
2599 clear_tv(rettv);
2600 return FAIL;
2601 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002602 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002603 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002604
2605 /*
2606 * Get the second variable.
2607 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002608 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2609 {
2610 error_white_both(p, 2);
2611 clear_tv(rettv);
2612 return FAIL;
2613 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2615 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002616 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002617 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002618 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002619 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620
2621 /*
2622 * Compute the result.
2623 */
2624 if (evaluate && result)
2625 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002626 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002627 result = tv_get_bool_chk(&var2, &error);
2628 else if (tv_get_number_chk(&var2, &error) == 0)
2629 result = FALSE;
2630 clear_tv(&var2);
2631 if (error)
2632 return FAIL;
2633 }
2634 if (evaluate)
2635 {
2636 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002637 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002638 rettv->v_type = VAR_BOOL;
2639 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002640 }
2641 else
2642 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002643 rettv->v_type = VAR_NUMBER;
2644 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002645 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002646 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002647
2648 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002650
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002651 if (evalarg == NULL)
2652 clear_evalarg(&local_evalarg, NULL);
2653 else
2654 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
2656
2657 return OK;
2658}
2659
2660/*
2661 * Handle third level expression:
2662 * var1 == var2
2663 * var1 =~ var2
2664 * var1 != var2
2665 * var1 !~ var2
2666 * var1 > var2
2667 * var1 >= var2
2668 * var1 < var2
2669 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002670 * var1 is var2
2671 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 *
2673 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002674 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 *
2676 * Return OK or FAIL.
2677 */
2678 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002679eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002682 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002683 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002685 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687 /*
2688 * Get the first variable.
2689 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002690 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 return FAIL;
2692
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002693 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002694 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002697 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002699 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002701 typval_T var2;
2702 int ic;
2703 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002704 int evaluate = evalarg == NULL
2705 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002706
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002707 if (getnext)
2708 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002709 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2710 {
2711 error_white_both(p, len);
2712 clear_tv(rettv);
2713 return FAIL;
2714 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002715
Bram Moolenaar696ba232020-07-29 21:20:41 +02002716 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2717 {
2718 semsg(_(e_invexpr2), p);
2719 clear_tv(rettv);
2720 return FAIL;
2721 }
2722
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002723 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 if (p[len] == '?')
2725 {
2726 ic = TRUE;
2727 ++len;
2728 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002729 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 else if (p[len] == '#')
2731 {
2732 ic = FALSE;
2733 ++len;
2734 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002735 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002737 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 /*
2740 * Get the second variable.
2741 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002742 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2743 {
2744 error_white_both(p, 1);
2745 clear_tv(rettv);
2746 return FAIL;
2747 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002748 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002749 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002751 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 return FAIL;
2753 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002754 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002755 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002756 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002757
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002758 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002759 {
2760 ret = FAIL;
2761 clear_tv(rettv);
2762 }
2763 else
2764 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002765 clear_tv(&var2);
2766 return ret;
2767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769
2770 return OK;
2771}
2772
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002773/*
2774 * Make a copy of blob "tv1" and append blob "tv2".
2775 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 void
2777eval_addblob(typval_T *tv1, typval_T *tv2)
2778{
2779 blob_T *b1 = tv1->vval.v_blob;
2780 blob_T *b2 = tv2->vval.v_blob;
2781 blob_T *b = blob_alloc();
2782 int i;
2783
2784 if (b != NULL)
2785 {
2786 for (i = 0; i < blob_len(b1); i++)
2787 ga_append(&b->bv_ga, blob_get(b1, i));
2788 for (i = 0; i < blob_len(b2); i++)
2789 ga_append(&b->bv_ga, blob_get(b2, i));
2790
2791 clear_tv(tv1);
2792 rettv_blob_set(tv1, b);
2793 }
2794}
2795
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002796/*
2797 * Make a copy of list "tv1" and append list "tv2".
2798 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 int
2800eval_addlist(typval_T *tv1, typval_T *tv2)
2801{
2802 typval_T var3;
2803
2804 // concatenate Lists
2805 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2806 {
2807 clear_tv(tv1);
2808 clear_tv(tv2);
2809 return FAIL;
2810 }
2811 clear_tv(tv1);
2812 *tv1 = var3;
2813 return OK;
2814}
2815
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816/*
2817 * Handle fourth level expression:
2818 * + number addition
2819 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002820 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002821 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 *
2823 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002824 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 *
2826 * Return OK or FAIL.
2827 */
2828 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002829eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 /*
2832 * Get the first variable.
2833 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002834 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 return FAIL;
2836
2837 /*
2838 * Repeat computing, until no '+', '-' or '.' is following.
2839 */
2840 for (;;)
2841 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002842 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002843 int getnext;
2844 char_u *p;
2845 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002846 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002847 int concat;
2848 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002849 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002850
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002851 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002852 // "+=", "-=" and "..=" are assignments
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002853 p = eval_next_non_blank(*arg, evalarg, &getnext);
2854 op = *p;
2855 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002856 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2857 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002859
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002860 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002861 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002862 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002863 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002864 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002865 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002866 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002867 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002868 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002869 clear_tv(rettv);
2870 return FAIL;
2871 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002872 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002873 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002874 if ((op != '+' || (rettv->v_type != VAR_LIST
2875 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002876#ifdef FEAT_FLOAT
2877 && (op == '.' || rettv->v_type != VAR_FLOAT)
2878#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002879 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002880 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002881 int error = FALSE;
2882
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002883 // For "list + ...", an illegal use of the first operand as
2884 // a number cannot be determined before evaluating the 2nd
2885 // operand: if this is also a list, all is ok.
2886 // For "something . ...", "something - ..." or "non-list + ...",
2887 // we know that the first operand needs to be a string or number
2888 // without evaluating the 2nd operand. So check before to avoid
2889 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002890 if (op != '.')
2891 tv_get_number_chk(rettv, &error);
2892 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002893 {
2894 clear_tv(rettv);
2895 return FAIL;
2896 }
2897 }
2898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 /*
2900 * Get the second variable.
2901 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002902 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002903 {
2904 error_white_both(p, oplen);
2905 clear_tv(rettv);
2906 return FAIL;
2907 }
2908 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002909 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002911 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 return FAIL;
2913 }
2914
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002915 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 {
2917 /*
2918 * Compute the result.
2919 */
2920 if (op == '.')
2921 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002922 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2923 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002924 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002925
Bram Moolenaar2e086612020-08-25 22:37:48 +02002926 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002927 || var2.v_type == VAR_CHANNEL
2928 || var2.v_type == VAR_JOB))
2929 emsg(_(e_inval_string));
2930#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002931 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002932 {
2933 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2934 var2.vval.v_float);
2935 s2 = buf2;
2936 }
2937#endif
2938 else
2939 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002940 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002941 {
2942 clear_tv(rettv);
2943 clear_tv(&var2);
2944 return FAIL;
2945 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002947 clear_tv(rettv);
2948 rettv->v_type = VAR_STRING;
2949 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002951 else if (op == '+' && rettv->v_type == VAR_BLOB
2952 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002954 else if (op == '+' && rettv->v_type == VAR_LIST
2955 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002956 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002958 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 else
2961 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002962 int error = FALSE;
2963 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002964#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002965 float_T f1 = 0, f2 = 0;
2966
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002967 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002968 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002969 f1 = rettv->vval.v_float;
2970 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002971 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002972 else
2973#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002974 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002975 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002976 if (error)
2977 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002978 // This can only happen for "list + non-list". For
2979 // "non-list + ..." or "something - ...", we returned
2980 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002981 clear_tv(rettv);
2982 return FAIL;
2983 }
2984#ifdef FEAT_FLOAT
2985 if (var2.v_type == VAR_FLOAT)
2986 f1 = n1;
2987#endif
2988 }
2989#ifdef FEAT_FLOAT
2990 if (var2.v_type == VAR_FLOAT)
2991 {
2992 f2 = var2.vval.v_float;
2993 n2 = 0;
2994 }
2995 else
2996#endif
2997 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002998 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002999 if (error)
3000 {
3001 clear_tv(rettv);
3002 clear_tv(&var2);
3003 return FAIL;
3004 }
3005#ifdef FEAT_FLOAT
3006 if (rettv->v_type == VAR_FLOAT)
3007 f2 = n2;
3008#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003010 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003011
3012#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003013 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003014 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3015 {
3016 if (op == '+')
3017 f1 = f1 + f2;
3018 else
3019 f1 = f1 - f2;
3020 rettv->v_type = VAR_FLOAT;
3021 rettv->vval.v_float = f1;
3022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024#endif
3025 {
3026 if (op == '+')
3027 n1 = n1 + n2;
3028 else
3029 n1 = n1 - n2;
3030 rettv->v_type = VAR_NUMBER;
3031 rettv->vval.v_number = n1;
3032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003034 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 }
3036 }
3037 return OK;
3038}
3039
3040/*
3041 * Handle fifth level expression:
3042 * * number multiplication
3043 * / number division
3044 * % number modulo
3045 *
3046 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003047 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 *
3049 * Return OK or FAIL.
3050 */
3051 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003052eval6(
3053 char_u **arg,
3054 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003055 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003056 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003058#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003059 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
3062 /*
3063 * Get the first variable.
3064 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003065 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 return FAIL;
3067
3068 /*
3069 * Repeat computing, until no '*', '/' or '%' is following.
3070 */
3071 for (;;)
3072 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003073 int evaluate;
3074 int getnext;
3075 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003076 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003077 int op;
3078 varnumber_T n1, n2;
3079#ifdef FEAT_FLOAT
3080 float_T f1, f2;
3081#endif
3082 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003083
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003084 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003085 p = eval_next_non_blank(*arg, evalarg, &getnext);
3086 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003087 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003089
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003090 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003091 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003092 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003093 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003094 {
3095 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3096 {
3097 error_white_both(p, 1);
3098 clear_tv(rettv);
3099 return FAIL;
3100 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003101 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003104#ifdef FEAT_FLOAT
3105 f1 = 0;
3106 f2 = 0;
3107#endif
3108 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003109 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111#ifdef FEAT_FLOAT
3112 if (rettv->v_type == VAR_FLOAT)
3113 {
3114 f1 = rettv->vval.v_float;
3115 use_float = TRUE;
3116 n1 = 0;
3117 }
3118 else
3119#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003120 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003121 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003122 if (error)
3123 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 }
3125 else
3126 n1 = 0;
3127
3128 /*
3129 * Get the second variable.
3130 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003131 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3132 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003133 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003134 clear_tv(rettv);
3135 return FAIL;
3136 }
3137 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003138 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 return FAIL;
3140
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003141 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143#ifdef FEAT_FLOAT
3144 if (var2.v_type == VAR_FLOAT)
3145 {
3146 if (!use_float)
3147 {
3148 f1 = n1;
3149 use_float = TRUE;
3150 }
3151 f2 = var2.vval.v_float;
3152 n2 = 0;
3153 }
3154 else
3155#endif
3156 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003157 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003158 clear_tv(&var2);
3159 if (error)
3160 return FAIL;
3161#ifdef FEAT_FLOAT
3162 if (use_float)
3163 f2 = n2;
3164#endif
3165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166
3167 /*
3168 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171#ifdef FEAT_FLOAT
3172 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003174 if (op == '*')
3175 f1 = f1 * f2;
3176 else if (op == '/')
3177 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003178# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003179 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003180 if (f2 == 0.0)
3181 {
3182 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003183 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003184 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003185 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003186 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003187 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003188 }
3189 else
3190 f1 = f1 / f2;
3191# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003192 // We rely on the floating point library to handle divide
3193 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003194 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003195# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003198 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003200 return FAIL;
3201 }
3202 rettv->v_type = VAR_FLOAT;
3203 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003208 int failed = FALSE;
3209
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003210 if (op == '*')
3211 n1 = n1 * n2;
3212 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003213 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003215 n1 = num_modulus(n1, n2, &failed);
3216 if (failed)
3217 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003218
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003219 rettv->v_type = VAR_NUMBER;
3220 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 }
3223 }
3224
3225 return OK;
3226}
3227
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003228 int
3229eval_leader(char_u **arg, int vim9)
3230{
3231 char_u *s = *arg;
3232 char_u *p = *arg;
3233
3234 while (*p == '!' || *p == '-' || *p == '+')
3235 {
3236 char_u *n = skipwhite(p + 1);
3237
3238 // ++, --, -+ and +- are not accepted in Vim9 script
3239 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3240 {
3241 semsg(_(e_invexpr2), s);
3242 return FAIL;
3243 }
3244 p = n;
3245 }
3246 *arg = p;
3247 return OK;
3248}
3249
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250/*
3251 * Handle sixth level expression:
3252 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003253 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003254 * "string" string constant
3255 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 * &option-name option value
3257 * @r register contents
3258 * identifier variable value
3259 * function() function call
3260 * $VAR environment variable
3261 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003262 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003264 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003265 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 *
3267 * Also handle:
3268 * ! in front logical NOT
3269 * - in front unary minus
3270 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003271 * trailing [] subscript in String or List
3272 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003273 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 *
3275 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003276 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 *
3278 * Return OK or FAIL.
3279 */
3280 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003281eval7(
3282 char_u **arg,
3283 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003284 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003287 int evaluate = evalarg != NULL
3288 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 int len;
3290 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 char_u *start_leader, *end_leader;
3292 int ret = OK;
3293 char_u *alias;
3294
3295 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003296 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003297 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003299 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
3301 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003302 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 */
3304 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003305 if (eval_leader(arg, in_vim9script()) == FAIL)
3306 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 end_leader = *arg;
3308
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003309 if (**arg == '.' && (!isdigit(*(*arg + 1))
3310#ifdef FEAT_FLOAT
3311 || current_sctx.sc_version < 2
3312#endif
3313 ))
3314 {
3315 semsg(_(e_invexpr2), *arg);
3316 ++*arg;
3317 return FAIL;
3318 }
3319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 switch (**arg)
3321 {
3322 /*
3323 * Number constant.
3324 */
3325 case '0':
3326 case '1':
3327 case '2':
3328 case '3':
3329 case '4':
3330 case '5':
3331 case '6':
3332 case '7':
3333 case '8':
3334 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003335 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003336
3337 // Apply prefixed "-" and "+" now. Matters especially when
3338 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003339 if (ret == OK && evaluate && end_leader > start_leader
3340 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003341 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 break;
3343
3344 /*
3345 * String constant: "string".
3346 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003347 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 break;
3349
3350 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003351 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003353 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003354 break;
3355
3356 /*
3357 * List: [expr, expr]
3358 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003359 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 break;
3361
3362 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003363 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003364 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003365 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003366 {
3367 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003368 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003369 }
3370 else
3371 ret = NOTDONE;
3372 break;
3373
3374 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003375 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003376 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003377 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003378 case '{': if (in_vim9script())
3379 ret = NOTDONE;
3380 else
3381 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003382 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003383 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003384 break;
3385
3386 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003387 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003389 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 break;
3391
3392 /*
3393 * Environment variable: $VAR.
3394 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003395 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 break;
3397
3398 /*
3399 * Register contents: @r.
3400 */
3401 case '@': ++*arg;
3402 if (evaluate)
3403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003404 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003405 rettv->vval.v_string = get_reg_contents(**arg,
3406 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 }
3408 if (**arg != NUL)
3409 ++*arg;
3410 break;
3411
3412 /*
3413 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003414 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003416 case '(': ret = NOTDONE;
3417 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003418 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003419 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003420 if (ret == OK && evaluate)
3421 {
3422 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3423
3424 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003425 if (compile_def_function(ufunc,
3426 TRUE, PROFILING(ufunc), NULL) == FAIL)
3427 {
3428 clear_tv(rettv);
3429 ret = FAIL;
3430 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003431 }
3432 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003433 if (ret == NOTDONE)
3434 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003435 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003436 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003437
Bram Moolenaar9215f012020-06-27 21:18:00 +02003438 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003439 if (**arg == ')')
3440 ++*arg;
3441 else if (ret == OK)
3442 {
3443 emsg(_(e_missing_close));
3444 clear_tv(rettv);
3445 ret = FAIL;
3446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448 break;
3449
Bram Moolenaar8c711452005-01-14 21:53:12 +00003450 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 break;
3452 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003453
3454 if (ret == NOTDONE)
3455 {
3456 /*
3457 * Must be a variable or function name.
3458 * Can also be a curly-braces kind of name: {expr}.
3459 */
3460 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003461 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003462 if (alias != NULL)
3463 s = alias;
3464
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003465 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003466 ret = FAIL;
3467 else
3468 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003469 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3470
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003471 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3472 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003473 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003474 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003475 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003476 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003477 else if (flags & EVAL_CONSTANT)
3478 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003479 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003480 {
3481 // get the value of "true", "false" or a variable
3482 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3483 {
3484 rettv->v_type = VAR_BOOL;
3485 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003486 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003487 }
3488 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003489 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003490 {
3491 rettv->v_type = VAR_BOOL;
3492 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003493 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003494 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003495 else if (len == 4 && in_vim9script()
3496 && STRNCMP(s, "null", 4) == 0)
3497 {
3498 rettv->v_type = VAR_SPECIAL;
3499 rettv->vval.v_number = VVAL_NULL;
3500 ret = OK;
3501 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003502 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003503 ret = eval_variable(s, len, rettv, NULL,
3504 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003505 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003506 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003507 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003508 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003509 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003510 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003511 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003512 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003513 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003514 }
3515
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003516 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3517 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003518 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003519 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521 /*
3522 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3523 */
3524 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003525 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003526 return ret;
3527}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003528
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003529/*
3530 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003531 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003532 * Adjusts "end_leaderp" until it is at "start_leader".
3533 */
3534 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003535eval7_leader(
3536 typval_T *rettv,
3537 int numeric_only,
3538 char_u *start_leader,
3539 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003540{
3541 char_u *end_leader = *end_leaderp;
3542 int ret = OK;
3543 int error = FALSE;
3544 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003545 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003546#ifdef FEAT_FLOAT
3547 float_T f = 0.0;
3548
3549 if (rettv->v_type == VAR_FLOAT)
3550 f = rettv->vval.v_float;
3551 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003552#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003553 {
3554 while (VIM_ISWHITE(end_leader[-1]))
3555 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003556 if (in_vim9script() && end_leader[-1] == '!')
3557 val = tv2bool(rettv);
3558 else
3559 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003560 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003561 if (error)
3562 {
3563 clear_tv(rettv);
3564 ret = FAIL;
3565 }
3566 else
3567 {
3568 while (end_leader > start_leader)
3569 {
3570 --end_leader;
3571 if (*end_leader == '!')
3572 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003573 if (numeric_only)
3574 {
3575 ++end_leader;
3576 break;
3577 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003578#ifdef FEAT_FLOAT
3579 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003580 {
3581 if (in_vim9script())
3582 {
3583 rettv->v_type = VAR_BOOL;
3584 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3585 }
3586 else
3587 f = !f;
3588 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003589 else
3590#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003591 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003592 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003593 type = VAR_BOOL;
3594 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003595 }
3596 else if (*end_leader == '-')
3597 {
3598#ifdef FEAT_FLOAT
3599 if (rettv->v_type == VAR_FLOAT)
3600 f = -f;
3601 else
3602#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003603 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003604 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003605 type = VAR_NUMBER;
3606 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003607 }
3608 }
3609#ifdef FEAT_FLOAT
3610 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003612 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003613 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003615 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003616#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003617 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003618 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003619 if (in_vim9script())
3620 rettv->v_type = type;
3621 else
3622 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003623 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003626 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 return ret;
3628}
3629
3630/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003631 * Call the function referred to in "rettv".
3632 */
3633 static int
3634call_func_rettv(
3635 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003636 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003637 typval_T *rettv,
3638 int evaluate,
3639 dict_T *selfdict,
3640 typval_T *basetv)
3641{
3642 partial_T *pt = NULL;
3643 funcexe_T funcexe;
3644 typval_T functv;
3645 char_u *s;
3646 int ret;
3647
3648 // need to copy the funcref so that we can clear rettv
3649 if (evaluate)
3650 {
3651 functv = *rettv;
3652 rettv->v_type = VAR_UNKNOWN;
3653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003654 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003655 if (functv.v_type == VAR_PARTIAL)
3656 {
3657 pt = functv.vval.v_partial;
3658 s = partial_name(pt);
3659 }
3660 else
3661 s = functv.vval.v_string;
3662 }
3663 else
3664 s = (char_u *)"";
3665
Bram Moolenaara80faa82020-04-12 19:37:17 +02003666 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003667 funcexe.firstline = curwin->w_cursor.lnum;
3668 funcexe.lastline = curwin->w_cursor.lnum;
3669 funcexe.evaluate = evaluate;
3670 funcexe.partial = pt;
3671 funcexe.selfdict = selfdict;
3672 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003673 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003675 // Clear the funcref afterwards, so that deleting it while
3676 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003677 if (evaluate)
3678 clear_tv(&functv);
3679
3680 return ret;
3681}
3682
3683/*
3684 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003685 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003686 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3687 */
3688 static int
3689eval_lambda(
3690 char_u **arg,
3691 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003692 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003693 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003694{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003695 int evaluate = evalarg != NULL
3696 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003697 typval_T base = *rettv;
3698 int ret;
3699
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003700 rettv->v_type = VAR_UNKNOWN;
3701
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003702 if (**arg == '{')
3703 {
3704 // ->{lambda}()
3705 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3706 }
3707 else
3708 {
3709 // ->(lambda)()
3710 ++*arg;
3711 ret = eval1(arg, rettv, evalarg);
3712 *arg = skipwhite_and_linebreak(*arg, evalarg);
3713 if (**arg != ')')
3714 {
3715 emsg(_(e_missing_close));
3716 ret = FAIL;
3717 }
3718 ++*arg;
3719 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003720 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003721 return FAIL;
3722 else if (**arg != '(')
3723 {
3724 if (verbose)
3725 {
3726 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003727 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003728 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003730 }
3731 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003732 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003733 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003734 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003735 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003736
3737 // Clear the funcref afterwards, so that deleting it while
3738 // evaluating the arguments is possible (see test55).
3739 if (evaluate)
3740 clear_tv(&base);
3741
3742 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003743}
3744
3745/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003746 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003747 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003748 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3749 */
3750 static int
3751eval_method(
3752 char_u **arg,
3753 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003754 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003755 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003756{
3757 char_u *name;
3758 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003759 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003760 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003761 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003762 int evaluate = evalarg != NULL
3763 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003764
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003765 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003766
Bram Moolenaarac92e252019-08-03 21:58:38 +02003767 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003768 len = get_name_len(arg, &alias, evaluate, TRUE);
3769 if (alias != NULL)
3770 name = alias;
3771
3772 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003773 {
3774 if (verbose)
3775 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003776 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003777 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003778 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003779 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003780 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003781 if (**arg != '(')
3782 {
3783 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003785 ret = FAIL;
3786 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003787 else if (VIM_ISWHITE((*arg)[-1]))
3788 {
3789 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003790 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003791 ret = FAIL;
3792 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003793 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003794 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003795 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003796 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003797
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003798 // Clear the funcref afterwards, so that deleting it while
3799 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003800 if (evaluate)
3801 clear_tv(&base);
3802
Bram Moolenaarac92e252019-08-03 21:58:38 +02003803 return ret;
3804}
3805
3806/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003807 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3808 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003809 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3810 */
3811 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003812eval_index(
3813 char_u **arg,
3814 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003815 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003816 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003817{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003818 int evaluate = evalarg != NULL
3819 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003820 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003821 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003822 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003823 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003824 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003825 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003826
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003827 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3828 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003829
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003830 init_tv(&var1);
3831 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003832 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003834 /*
3835 * dict.name
3836 */
3837 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003838 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003839 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003840 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003841 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003842 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003843 }
3844 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003846 /*
3847 * something[idx]
3848 *
3849 * Get the (first) variable from inside the [].
3850 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003851 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003852 if (**arg == ':')
3853 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003854 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003855 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003856 else if (vim9 && **arg == ':')
3857 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003858 semsg(_(e_white_space_required_before_and_after_str_at_str),
3859 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003860 clear_tv(&var1);
3861 return FAIL;
3862 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003863 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003864 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003865#ifdef FEAT_FLOAT
3866 // allow for indexing with float
3867 if (vim9 && rettv->v_type == VAR_DICT
3868 && var1.v_type == VAR_FLOAT)
3869 {
3870 var1.vval.v_string = typval_tostring(&var1, TRUE);
3871 var1.v_type = VAR_STRING;
3872 }
3873#endif
3874 if (tv_get_string_chk(&var1) == NULL)
3875 {
3876 // not a number or string
3877 clear_tv(&var1);
3878 return FAIL;
3879 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003880 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003881
3882 /*
3883 * Get the second variable from inside the [:].
3884 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003885 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003886 if (**arg == ':')
3887 {
3888 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003889 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003890 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003891 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003892 semsg(_(e_white_space_required_before_and_after_str_at_str),
3893 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003894 if (!empty1)
3895 clear_tv(&var1);
3896 return FAIL;
3897 }
3898 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003899 if (**arg == ']')
3900 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003901 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003902 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003903 if (!empty1)
3904 clear_tv(&var1);
3905 return FAIL;
3906 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003907 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003908 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003909 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003910 if (!empty1)
3911 clear_tv(&var1);
3912 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003913 return FAIL;
3914 }
3915 }
3916
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003917 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003918 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003919 if (**arg != ']')
3920 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003921 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003922 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003923 clear_tv(&var1);
3924 if (range)
3925 clear_tv(&var2);
3926 return FAIL;
3927 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003928 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003929 }
3930
3931 if (evaluate)
3932 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003933 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003934 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003935 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003936
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003937 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003938 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003939 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003940 clear_tv(&var2);
3941 return res;
3942 }
3943 return OK;
3944}
3945
3946/*
3947 * Check if "rettv" can have an [index] or [sli:ce]
3948 */
3949 int
3950check_can_index(typval_T *rettv, int evaluate, int verbose)
3951{
3952 switch (rettv->v_type)
3953 {
3954 case VAR_FUNC:
3955 case VAR_PARTIAL:
3956 if (verbose)
3957 emsg(_("E695: Cannot index a Funcref"));
3958 return FAIL;
3959 case VAR_FLOAT:
3960#ifdef FEAT_FLOAT
3961 if (verbose)
3962 emsg(_(e_float_as_string));
3963 return FAIL;
3964#endif
3965 case VAR_BOOL:
3966 case VAR_SPECIAL:
3967 case VAR_JOB:
3968 case VAR_CHANNEL:
3969 if (verbose)
3970 emsg(_(e_cannot_index_special_variable));
3971 return FAIL;
3972 case VAR_UNKNOWN:
3973 case VAR_ANY:
3974 case VAR_VOID:
3975 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003976 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003977 emsg(_(e_cannot_index_special_variable));
3978 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003979 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003980 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003981
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003982 case VAR_STRING:
3983 case VAR_LIST:
3984 case VAR_DICT:
3985 case VAR_BLOB:
3986 break;
3987 case VAR_NUMBER:
3988 if (in_vim9script())
3989 emsg(_(e_cannot_index_number));
3990 break;
3991 }
3992 return OK;
3993}
3994
3995/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01003996 * slice() function
3997 */
3998 void
3999f_slice(typval_T *argvars, typval_T *rettv)
4000{
4001 if (check_can_index(argvars, TRUE, FALSE) == OK)
4002 {
4003 copy_tv(argvars, rettv);
4004 eval_index_inner(rettv, TRUE, argvars + 1,
4005 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4006 TRUE, NULL, 0, FALSE);
4007 }
4008}
4009
4010/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004011 * Apply index or range to "rettv".
4012 * "var1" is the first index, NULL for [:expr].
4013 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004014 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4015 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004016 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4017 */
4018 int
4019eval_index_inner(
4020 typval_T *rettv,
4021 int is_range,
4022 typval_T *var1,
4023 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004024 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004025 char_u *key,
4026 int keylen,
4027 int verbose)
4028{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004029 varnumber_T n1, n2 = 0;
4030 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004031
4032 n1 = 0;
4033 if (var1 != NULL && rettv->v_type != VAR_DICT)
4034 n1 = tv_get_number(var1);
4035
4036 if (is_range)
4037 {
4038 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004039 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004040 if (verbose)
4041 emsg(_(e_cannot_slice_dictionary));
4042 return FAIL;
4043 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004044 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004045 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004046 else
4047 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004048 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004049
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004050 switch (rettv->v_type)
4051 {
4052 case VAR_UNKNOWN:
4053 case VAR_ANY:
4054 case VAR_VOID:
4055 case VAR_FUNC:
4056 case VAR_PARTIAL:
4057 case VAR_FLOAT:
4058 case VAR_BOOL:
4059 case VAR_SPECIAL:
4060 case VAR_JOB:
4061 case VAR_CHANNEL:
4062 break; // not evaluating, skipping over subscript
4063
4064 case VAR_NUMBER:
4065 case VAR_STRING:
4066 {
4067 char_u *s = tv_get_string(rettv);
4068
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004069 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004070 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004071 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004072 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004073 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004074 else
4075 s = char_from_string(s, n1);
4076 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004077 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004078 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004079 // The resulting variable is a substring. If the indexes
4080 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004081 if (n1 < 0)
4082 {
4083 n1 = len + n1;
4084 if (n1 < 0)
4085 n1 = 0;
4086 }
4087 if (n2 < 0)
4088 n2 = len + n2;
4089 else if (n2 >= len)
4090 n2 = len;
4091 if (n1 >= len || n2 < 0 || n1 > n2)
4092 s = NULL;
4093 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004094 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004095 }
4096 else
4097 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004098 // The resulting variable is a string of a single
4099 // character. If the index is too big or negative the
4100 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004101 if (n1 >= len || n1 < 0)
4102 s = NULL;
4103 else
4104 s = vim_strnsave(s + n1, 1);
4105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 clear_tv(rettv);
4107 rettv->v_type = VAR_STRING;
4108 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004109 }
4110 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004111
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004112 case VAR_BLOB:
4113 len = blob_len(rettv->vval.v_blob);
4114 if (is_range)
4115 {
4116 // The resulting variable is a sub-blob. If the indexes
4117 // are out of range the result is empty.
4118 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004119 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004120 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004121 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004122 n1 = 0;
4123 }
4124 if (n2 < 0)
4125 n2 = len + n2;
4126 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004127 n2 = len - (exclusive ? 0 : 1);
4128 if (exclusive)
4129 --n2;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004130 if (n1 >= len || n2 < 0 || n1 > n2)
4131 {
4132 clear_tv(rettv);
4133 rettv->v_type = VAR_BLOB;
4134 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004135 }
4136 else
4137 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004138 blob_T *blob = blob_alloc();
4139 long i;
4140
4141 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004142 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004143 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004144 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004145 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004146 return FAIL;
4147 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004148 blob->bv_ga.ga_len = n2 - n1 + 1;
4149 for (i = n1; i <= n2; i++)
4150 blob_set(blob, i - n1,
4151 blob_get(rettv->vval.v_blob, i));
4152
4153 clear_tv(rettv);
4154 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004155 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004156 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004157 }
4158 else
4159 {
4160 // The resulting variable is a byte value.
4161 // If the index is too big or negative that is an error.
4162 if (n1 < 0)
4163 n1 = len + n1;
4164 if (n1 < len && n1 >= 0)
4165 {
4166 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004167
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004168 clear_tv(rettv);
4169 rettv->v_type = VAR_NUMBER;
4170 rettv->vval.v_number = v;
4171 }
4172 else
4173 semsg(_(e_blobidx), n1);
4174 }
4175 break;
4176
4177 case VAR_LIST:
4178 if (var1 == NULL)
4179 n1 = 0;
4180 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004181 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004182 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004183 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004184 return FAIL;
4185 break;
4186
4187 case VAR_DICT:
4188 {
4189 dictitem_T *item;
4190 typval_T tmp;
4191
4192 if (key == NULL)
4193 {
4194 key = tv_get_string_chk(var1);
4195 if (key == NULL)
4196 return FAIL;
4197 }
4198
4199 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4200
4201 if (item == NULL && verbose)
4202 semsg(_(e_dictkey), key);
4203 if (item == NULL)
4204 return FAIL;
4205
4206 copy_tv(&item->di_tv, &tmp);
4207 clear_tv(rettv);
4208 *rettv = tmp;
4209 }
4210 break;
4211 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004212 return OK;
4213}
4214
4215/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004216 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004217 */
4218 char_u *
4219partial_name(partial_T *pt)
4220{
4221 if (pt->pt_name != NULL)
4222 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004223 if (pt->pt_func != NULL)
4224 return pt->pt_func->uf_name;
4225 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004226}
4227
Bram Moolenaarddecc252016-04-06 22:59:37 +02004228 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004229partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004230{
4231 int i;
4232
4233 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004234 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004235 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004236 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004237 if (pt->pt_name != NULL)
4238 {
4239 func_unref(pt->pt_name);
4240 vim_free(pt->pt_name);
4241 }
4242 else
4243 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004244
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004245 // Decrease the reference count for the context of a closure. If down
4246 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004247 if (pt->pt_funcstack != NULL)
4248 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004249 --pt->pt_funcstack->fs_refcount;
4250 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004251 }
4252
Bram Moolenaarddecc252016-04-06 22:59:37 +02004253 vim_free(pt);
4254}
4255
4256/*
4257 * Unreference a closure: decrement the reference count and free it when it
4258 * becomes zero.
4259 */
4260 void
4261partial_unref(partial_T *pt)
4262{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004263 if (pt != NULL)
4264 {
4265 if (--pt->pt_refcount <= 0)
4266 partial_free(pt);
4267
4268 // If the reference count goes down to one, the funcstack may be the
4269 // only reference and can be freed if no other partials reference it.
4270 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4271 funcstack_check_refcount(pt->pt_funcstack);
4272 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004273}
4274
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004275/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004276 * Return the next (unique) copy ID.
4277 * Used for serializing nested structures.
4278 */
4279 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004280get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004281{
4282 current_copyID += COPYID_INC;
4283 return current_copyID;
4284}
4285
4286/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004287 * Garbage collection for lists and dictionaries.
4288 *
4289 * We use reference counts to be able to free most items right away when they
4290 * are no longer used. But for composite items it's possible that it becomes
4291 * unused while the reference count is > 0: When there is a recursive
4292 * reference. Example:
4293 * :let l = [1, 2, 3]
4294 * :let d = {9: l}
4295 * :let l[1] = d
4296 *
4297 * Since this is quite unusual we handle this with garbage collection: every
4298 * once in a while find out which lists and dicts are not referenced from any
4299 * variable.
4300 *
4301 * Here is a good reference text about garbage collection (refers to Python
4302 * but it applies to all reference-counting mechanisms):
4303 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004304 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004305
4306/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004307 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004308 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004309 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004310 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004311 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004312garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004313{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004314 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004315 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004316 buf_T *buf;
4317 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004318 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004319 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004320
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004321 if (!testing)
4322 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004323 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004324 want_garbage_collect = FALSE;
4325 may_garbage_collect = FALSE;
4326 garbage_collect_at_exit = FALSE;
4327 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004328
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004329 // The execution stack can grow big, limit the size.
4330 if (exestack.ga_maxlen - exestack.ga_len > 500)
4331 {
4332 size_t new_len;
4333 char_u *pp;
4334 int n;
4335
4336 // Keep 150% of the current size, with a minimum of the growth size.
4337 n = exestack.ga_len / 2;
4338 if (n < exestack.ga_growsize)
4339 n = exestack.ga_growsize;
4340
4341 // Don't make it bigger though.
4342 if (exestack.ga_len + n < exestack.ga_maxlen)
4343 {
4344 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4345 pp = vim_realloc(exestack.ga_data, new_len);
4346 if (pp == NULL)
4347 return FAIL;
4348 exestack.ga_maxlen = exestack.ga_len + n;
4349 exestack.ga_data = pp;
4350 }
4351 }
4352
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004353 // We advance by two because we add one for items referenced through
4354 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004355 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004356
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004357 /*
4358 * 1. Go through all accessible variables and mark all lists and dicts
4359 * with copyID.
4360 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004361
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004362 // Don't free variables in the previous_funccal list unless they are only
4363 // referenced through previous_funccal. This must be first, because if
4364 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004365 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004367 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004368 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004369
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004370 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004371 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004372 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4373 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004375 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004376 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004377 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4378 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004379 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004380 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4381 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004382#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004383 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004384 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4385 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004386 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004387 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004388 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4389 NULL, NULL);
4390#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004391
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004392 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004393 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004394 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4395 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004396 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004397 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004398
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004399 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004400 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004402 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004403 abort = abort || set_ref_in_functions(copyID);
4404
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004405 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004406 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004408 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004409 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004410
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004411 // callbacks in buffers
4412 abort = abort || set_ref_in_buffers(copyID);
4413
Bram Moolenaar1dced572012-04-05 16:54:08 +02004414#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004415 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004416#endif
4417
Bram Moolenaardb913952012-06-29 12:54:53 +02004418#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004419 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004420#endif
4421
4422#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004423 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004424#endif
4425
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004426#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004427 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004428 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004429#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004430#ifdef FEAT_NETBEANS_INTG
4431 abort = abort || set_ref_in_nb_channel(copyID);
4432#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004433
Bram Moolenaare3188e22016-05-31 21:13:04 +02004434#ifdef FEAT_TIMERS
4435 abort = abort || set_ref_in_timer(copyID);
4436#endif
4437
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004438#ifdef FEAT_QUICKFIX
4439 abort = abort || set_ref_in_quickfix(copyID);
4440#endif
4441
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004442#ifdef FEAT_TERMINAL
4443 abort = abort || set_ref_in_term(copyID);
4444#endif
4445
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004446#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004447 abort = abort || set_ref_in_popups(copyID);
4448#endif
4449
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004450 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004451 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004452 /*
4453 * 2. Free lists and dictionaries that are not referenced.
4454 */
4455 did_free = free_unref_items(copyID);
4456
4457 /*
4458 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004459 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004460 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004461 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004462 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004463 else if (p_verbose > 0)
4464 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004465 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004466 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004467
4468 return did_free;
4469}
4470
4471/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004472 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004473 */
4474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004475free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004476{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004477 int did_free = FALSE;
4478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 // Let all "free" functions know that we are here. This means no
4480 // dictionaries, lists, channels or jobs are to be freed, because we will
4481 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004482 in_free_unref_items = TRUE;
4483
4484 /*
4485 * PASS 1: free the contents of the items. We don't free the items
4486 * themselves yet, so that it is possible to decrement refcount counters
4487 */
4488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004489 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004490 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004492 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004493 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004494
4495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004496 // Go through the list of jobs and free items without the copyID. This
4497 // must happen before doing channels, because jobs refer to channels, but
4498 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004499 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4500
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004501 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004502 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4503#endif
4504
4505 /*
4506 * PASS 2: free the items themselves.
4507 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004508 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004509 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004510
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004511#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004512 // Go through the list of jobs and free items without the copyID. This
4513 // must happen before doing channels, because jobs refer to channels, but
4514 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004515 free_unused_jobs(copyID, COPYID_MASK);
4516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004517 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004518 free_unused_channels(copyID, COPYID_MASK);
4519#endif
4520
4521 in_free_unref_items = FALSE;
4522
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004523 return did_free;
4524}
4525
4526/*
4527 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004528 * "list_stack" is used to add lists to be marked. Can be NULL.
4529 *
4530 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004531 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004533set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004534{
4535 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004536 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004537 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004538 hashtab_T *cur_ht;
4539 ht_stack_T *ht_stack = NULL;
4540 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004541
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004542 cur_ht = ht;
4543 for (;;)
4544 {
4545 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004546 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004547 // Mark each item in the hashtab. If the item contains a hashtab
4548 // it is added to ht_stack, if it contains a list it is added to
4549 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004550 todo = (int)cur_ht->ht_used;
4551 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4552 if (!HASHITEM_EMPTY(hi))
4553 {
4554 --todo;
4555 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4556 &ht_stack, list_stack);
4557 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004558 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004559
4560 if (ht_stack == NULL)
4561 break;
4562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004563 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004564 cur_ht = ht_stack->ht;
4565 tempitem = ht_stack;
4566 ht_stack = ht_stack->prev;
4567 free(tempitem);
4568 }
4569
4570 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004571}
4572
4573/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004574 * Mark a dict and its items with "copyID".
4575 * Returns TRUE if setting references failed somehow.
4576 */
4577 int
4578set_ref_in_dict(dict_T *d, int copyID)
4579{
4580 if (d != NULL && d->dv_copyID != copyID)
4581 {
4582 d->dv_copyID = copyID;
4583 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4584 }
4585 return FALSE;
4586}
4587
4588/*
4589 * Mark a list and its items with "copyID".
4590 * Returns TRUE if setting references failed somehow.
4591 */
4592 int
4593set_ref_in_list(list_T *ll, int copyID)
4594{
4595 if (ll != NULL && ll->lv_copyID != copyID)
4596 {
4597 ll->lv_copyID = copyID;
4598 return set_ref_in_list_items(ll, copyID, NULL);
4599 }
4600 return FALSE;
4601}
4602
4603/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004604 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004605 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4606 *
4607 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004608 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004609 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004610set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004611{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004612 listitem_T *li;
4613 int abort = FALSE;
4614 list_T *cur_l;
4615 list_stack_T *list_stack = NULL;
4616 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004617
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004618 cur_l = l;
4619 for (;;)
4620 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004621 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004622 // Mark each item in the list. If the item contains a hashtab
4623 // it is added to ht_stack, if it contains a list it is added to
4624 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004625 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4626 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4627 ht_stack, &list_stack);
4628 if (list_stack == NULL)
4629 break;
4630
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004631 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004632 cur_l = list_stack->list;
4633 tempitem = list_stack;
4634 list_stack = list_stack->prev;
4635 free(tempitem);
4636 }
4637
4638 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004639}
4640
4641/*
4642 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004643 * "list_stack" is used to add lists to be marked. Can be NULL.
4644 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4645 *
4646 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004647 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004649set_ref_in_item(
4650 typval_T *tv,
4651 int copyID,
4652 ht_stack_T **ht_stack,
4653 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004654{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004655 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004656
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004657 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004658 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004659 dict_T *dd = tv->vval.v_dict;
4660
Bram Moolenaara03f2332016-02-06 18:09:59 +01004661 if (dd != NULL && dd->dv_copyID != copyID)
4662 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004663 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004664 dd->dv_copyID = copyID;
4665 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004666 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004667 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4668 }
4669 else
4670 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004671 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4672
Bram Moolenaara03f2332016-02-06 18:09:59 +01004673 if (newitem == NULL)
4674 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004675 else
4676 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004677 newitem->ht = &dd->dv_hashtab;
4678 newitem->prev = *ht_stack;
4679 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004680 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004681 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004682 }
4683 }
4684 else if (tv->v_type == VAR_LIST)
4685 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004686 list_T *ll = tv->vval.v_list;
4687
Bram Moolenaara03f2332016-02-06 18:09:59 +01004688 if (ll != NULL && ll->lv_copyID != copyID)
4689 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004690 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004691 ll->lv_copyID = copyID;
4692 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004693 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004694 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004695 }
4696 else
4697 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004698 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4699
Bram Moolenaara03f2332016-02-06 18:09:59 +01004700 if (newitem == NULL)
4701 abort = TRUE;
4702 else
4703 {
4704 newitem->list = ll;
4705 newitem->prev = *list_stack;
4706 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004707 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004708 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004709 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004710 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004711 else if (tv->v_type == VAR_FUNC)
4712 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004713 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004714 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004715 else if (tv->v_type == VAR_PARTIAL)
4716 {
4717 partial_T *pt = tv->vval.v_partial;
4718 int i;
4719
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004720 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004721 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004722 // Didn't see this partial yet.
4723 pt->pt_copyID = copyID;
4724
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004725 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004726
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004727 if (pt->pt_dict != NULL)
4728 {
4729 typval_T dtv;
4730
4731 dtv.v_type = VAR_DICT;
4732 dtv.vval.v_dict = pt->pt_dict;
4733 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4734 }
4735
4736 for (i = 0; i < pt->pt_argc; ++i)
4737 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4738 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004739 if (pt->pt_funcstack != NULL)
4740 {
4741 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4742
4743 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4744 abort = abort || set_ref_in_item(stack + i, copyID,
4745 ht_stack, list_stack);
4746 }
4747
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004748 }
4749 }
4750#ifdef FEAT_JOB_CHANNEL
4751 else if (tv->v_type == VAR_JOB)
4752 {
4753 job_T *job = tv->vval.v_job;
4754 typval_T dtv;
4755
4756 if (job != NULL && job->jv_copyID != copyID)
4757 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004758 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004759 if (job->jv_channel != NULL)
4760 {
4761 dtv.v_type = VAR_CHANNEL;
4762 dtv.vval.v_channel = job->jv_channel;
4763 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4764 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004765 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004766 {
4767 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004768 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004769 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4770 }
4771 }
4772 }
4773 else if (tv->v_type == VAR_CHANNEL)
4774 {
4775 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004776 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004777 typval_T dtv;
4778 jsonq_T *jq;
4779 cbq_T *cq;
4780
4781 if (ch != NULL && ch->ch_copyID != copyID)
4782 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004783 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004784 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004785 {
4786 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4787 jq = jq->jq_next)
4788 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4789 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4790 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004791 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004792 {
4793 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004794 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004795 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4796 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004797 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004798 {
4799 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004800 dtv.vval.v_partial =
4801 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004802 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4803 }
4804 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004805 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004806 {
4807 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004808 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004809 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4810 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004811 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004812 {
4813 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004814 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004815 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4816 }
4817 }
4818 }
4819#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004820 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004821}
4822
Bram Moolenaar8c711452005-01-14 21:53:12 +00004823/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824 * Return a string with the string representation of a variable.
4825 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004826 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004827 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004828 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004829 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004830 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004831 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4832 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004833 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004834 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004835 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004836echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004837 typval_T *tv,
4838 char_u **tofree,
4839 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004840 int copyID,
4841 int echo_style,
4842 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004843 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004844{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004845 static int recurse = 0;
4846 char_u *r = NULL;
4847
Bram Moolenaar33570922005-01-25 22:26:29 +00004848 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004849 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004850 if (!did_echo_string_emsg)
4851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004852 // Only give this message once for a recursive call to avoid
4853 // flooding the user with errors. And stop iterating over lists
4854 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004855 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004856 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004857 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004858 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004859 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004860 }
4861 ++recurse;
4862
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004863 switch (tv->v_type)
4864 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004865 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004866 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004867 {
4868 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004869 r = tv->vval.v_string;
4870 if (r == NULL)
4871 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004872 }
4873 else
4874 {
4875 *tofree = string_quote(tv->vval.v_string, FALSE);
4876 r = *tofree;
4877 }
4878 break;
4879
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004880 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004881 if (echo_style)
4882 {
4883 *tofree = NULL;
4884 r = tv->vval.v_string;
4885 }
4886 else
4887 {
4888 *tofree = string_quote(tv->vval.v_string, TRUE);
4889 r = *tofree;
4890 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004891 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004892
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004893 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004894 {
4895 partial_T *pt = tv->vval.v_partial;
4896 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004897 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004898 garray_T ga;
4899 int i;
4900 char_u *tf;
4901
4902 ga_init2(&ga, 1, 100);
4903 ga_concat(&ga, (char_u *)"function(");
4904 if (fname != NULL)
4905 {
4906 ga_concat(&ga, fname);
4907 vim_free(fname);
4908 }
4909 if (pt != NULL && pt->pt_argc > 0)
4910 {
4911 ga_concat(&ga, (char_u *)", [");
4912 for (i = 0; i < pt->pt_argc; ++i)
4913 {
4914 if (i > 0)
4915 ga_concat(&ga, (char_u *)", ");
4916 ga_concat(&ga,
4917 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4918 vim_free(tf);
4919 }
4920 ga_concat(&ga, (char_u *)"]");
4921 }
4922 if (pt != NULL && pt->pt_dict != NULL)
4923 {
4924 typval_T dtv;
4925
4926 ga_concat(&ga, (char_u *)", ");
4927 dtv.v_type = VAR_DICT;
4928 dtv.vval.v_dict = pt->pt_dict;
4929 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4930 vim_free(tf);
4931 }
4932 ga_concat(&ga, (char_u *)")");
4933
4934 *tofree = ga.ga_data;
4935 r = *tofree;
4936 break;
4937 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004938
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004939 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004940 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004941 break;
4942
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004943 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004944 if (tv->vval.v_list == NULL)
4945 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004946 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004947 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004948 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004949 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004950 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4951 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004952 {
4953 *tofree = NULL;
4954 r = (char_u *)"[...]";
4955 }
4956 else
4957 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004958 int old_copyID = tv->vval.v_list->lv_copyID;
4959
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004960 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004961 *tofree = list2string(tv, copyID, restore_copyID);
4962 if (restore_copyID)
4963 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004964 r = *tofree;
4965 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004966 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004967
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004969 if (tv->vval.v_dict == NULL)
4970 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004971 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004972 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004973 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004974 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004975 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4976 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004977 {
4978 *tofree = NULL;
4979 r = (char_u *)"{...}";
4980 }
4981 else
4982 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004983 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004984
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004985 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004986 *tofree = dict2string(tv, copyID, restore_copyID);
4987 if (restore_copyID)
4988 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004989 r = *tofree;
4990 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004991 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004992
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004993 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004994 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004995 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004997 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004998 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004999 break;
5000
Bram Moolenaar835dc632016-02-07 14:27:38 +01005001 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005002 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005003 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005004 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005005 if (composite_val)
5006 {
5007 *tofree = string_quote(r, FALSE);
5008 r = *tofree;
5009 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005010 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005011
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005013#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014 *tofree = NULL;
5015 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5016 r = numbuf;
5017 break;
5018#endif
5019
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005020 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005021 case VAR_SPECIAL:
5022 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005023 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005024 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005025 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005026
Bram Moolenaar8502c702014-06-17 12:51:16 +02005027 if (--recurse == 0)
5028 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005029 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005030}
5031
5032/*
5033 * Return a string with the string representation of a variable.
5034 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5035 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005036 * Does not put quotes around strings, as ":echo" displays values.
5037 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5038 * May return NULL.
5039 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005040 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005041echo_string(
5042 typval_T *tv,
5043 char_u **tofree,
5044 char_u *numbuf,
5045 int copyID)
5046{
5047 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5048}
5049
5050/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005051 * Return string "str" in ' quotes, doubling ' characters.
5052 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005054 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005055 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005056string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005057{
Bram Moolenaar33570922005-01-25 22:26:29 +00005058 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005059 char_u *p, *r, *s;
5060
Bram Moolenaar33570922005-01-25 22:26:29 +00005061 len = (function ? 13 : 3);
5062 if (str != NULL)
5063 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005064 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005065 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005066 if (*p == '\'')
5067 ++len;
5068 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005069 s = r = alloc(len);
5070 if (r != NULL)
5071 {
5072 if (function)
5073 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005075 r += 10;
5076 }
5077 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005078 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005079 if (str != NULL)
5080 for (p = str; *p != NUL; )
5081 {
5082 if (*p == '\'')
5083 *r++ = '\'';
5084 MB_COPY_CHAR(p, r);
5085 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005087 if (function)
5088 *r++ = ')';
5089 *r++ = NUL;
5090 }
5091 return s;
5092}
5093
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005094#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005095/*
5096 * Convert the string "text" to a floating point number.
5097 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5098 * this always uses a decimal point.
5099 * Returns the length of the text that was consumed.
5100 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005102string2float(
5103 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005104 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105{
5106 char *s = (char *)text;
5107 float_T f;
5108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005109 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005110 if (STRNICMP(text, "inf", 3) == 0)
5111 {
5112 *value = INFINITY;
5113 return 3;
5114 }
5115 if (STRNICMP(text, "-inf", 3) == 0)
5116 {
5117 *value = -INFINITY;
5118 return 4;
5119 }
5120 if (STRNICMP(text, "nan", 3) == 0)
5121 {
5122 *value = NAN;
5123 return 3;
5124 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005125 f = strtod(s, &s);
5126 *value = f;
5127 return (int)((char_u *)s - text);
5128}
5129#endif
5130
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005131/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005132 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5133 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005134 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005135 */
5136 int
5137buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5138{
5139 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005140 char_u *t;
5141 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005142
5143 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5144 return -1;
5145
5146 if (lnum > buf->b_ml.ml_line_count)
5147 lnum = buf->b_ml.ml_line_count;
5148
5149 str = ml_get_buf(buf, lnum, FALSE);
5150 if (str == NULL)
5151 return -1;
5152
5153 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005154 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005155
Bram Moolenaar91458462021-01-13 20:08:38 +01005156 // count the number of characters
5157 t = str;
5158 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5159 t += mb_ptr2len(t);
5160
5161 // In insert mode, when the cursor is at the end of a non-empty line,
5162 // byteidx points to the NUL character immediately past the end of the
5163 // string. In this case, add one to the character count.
5164 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5165 count++;
5166
5167 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005168}
5169
5170/*
5171 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005172 * byte index. Works only for loaded buffers. Returns -1 on failure.
5173 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005174 */
5175 int
5176buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5177{
5178 char_u *str;
5179 char_u *t;
5180
5181 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5182 return -1;
5183
5184 if (lnum > buf->b_ml.ml_line_count)
5185 lnum = buf->b_ml.ml_line_count;
5186
5187 str = ml_get_buf(buf, lnum, FALSE);
5188 if (str == NULL)
5189 return -1;
5190
5191 // Convert the character offset to a byte offset
5192 t = str;
5193 while (*t != NUL && --charidx > 0)
5194 t += mb_ptr2len(t);
5195
Bram Moolenaar91458462021-01-13 20:08:38 +01005196 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005197}
5198
5199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005201 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005203 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005204var2fpos(
5205 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005206 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005207 int *fnum, // set to fnum for '0, 'A, etc.
5208 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005210 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005212 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005214 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005215 if (varp->v_type == VAR_LIST)
5216 {
5217 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005218 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005219 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005220 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005221
5222 l = varp->vval.v_list;
5223 if (l == NULL)
5224 return NULL;
5225
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005226 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005227 pos.lnum = list_find_nr(l, 0L, &error);
5228 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005229 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005230 if (charcol)
5231 len = (long)mb_charlen(ml_get(pos.lnum));
5232 else
5233 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005234
Bram Moolenaarec65d772020-08-20 22:29:12 +02005235 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005237 li = list_find(l, 1L);
5238 if (li != NULL && li->li_tv.v_type == VAR_STRING
5239 && li->li_tv.vval.v_string != NULL
5240 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005241 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005242 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005243 }
5244 else
5245 {
5246 pos.col = list_find_nr(l, 1L, &error);
5247 if (error)
5248 return NULL;
5249 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005250
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005251 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005252 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005253 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005254 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005255
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005256 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005257 pos.coladd = list_find_nr(l, 2L, &error);
5258 if (error)
5259 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005260
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005261 return &pos;
5262 }
5263
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005264 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005265 if (name == NULL)
5266 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005267 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005268 {
5269 pos = curwin->w_cursor;
5270 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005271 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005272 return &pos;
5273 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005274 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005275 {
5276 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005277 pos = VIsual;
5278 else
5279 pos = curwin->w_cursor;
5280 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005281 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005282 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005283 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005284 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005286 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5288 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005289 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005290 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 return pp;
5292 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005293
Bram Moolenaara5525202006-03-02 22:52:09 +00005294 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005295
Bram Moolenaar477933c2007-07-17 14:32:23 +00005296 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005297 {
5298 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005299 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005300 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005301 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005302 // In silent Ex mode topline is zero, but that's not a valid line
5303 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005304 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005305 return &pos;
5306 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005307 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005308 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005309 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005310 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005311 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005312 return &pos;
5313 }
5314 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005315 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005317 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
5319 pos.lnum = curbuf->b_ml.ml_line_count;
5320 pos.col = 0;
5321 }
5322 else
5323 {
5324 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005325 if (charcol)
5326 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5327 else
5328 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330 return &pos;
5331 }
5332 return NULL;
5333}
5334
5335/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005336 * Convert list in "arg" into a position and optional file number.
5337 * When "fnump" is NULL there is no file number, only 3 items.
5338 * Note that the column is passed on as-is, the caller may want to decrement
5339 * it to use 1 for the first column.
5340 * Return FAIL when conversion is not possible, doesn't check the position for
5341 * validity.
5342 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005343 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005344list2fpos(
5345 typval_T *arg,
5346 pos_T *posp,
5347 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005348 colnr_T *curswantp,
5349 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005350{
5351 list_T *l = arg->vval.v_list;
5352 long i = 0;
5353 long n;
5354
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005355 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5356 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005357 if (arg->v_type != VAR_LIST
5358 || l == NULL
5359 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005360 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005361 return FAIL;
5362
5363 if (fnump != NULL)
5364 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005365 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005366 if (n < 0)
5367 return FAIL;
5368 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005369 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005370 *fnump = n;
5371 }
5372
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005373 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005374 if (n < 0)
5375 return FAIL;
5376 posp->lnum = n;
5377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005379 if (n < 0)
5380 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005381 // If character position is specified, then convert to byte position
5382 if (charcol)
5383 {
5384 buf_T *buf;
5385
5386 // Get the text for the specified line in a loaded buffer
5387 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5388 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5389 return FAIL;
5390
Bram Moolenaar91458462021-01-13 20:08:38 +01005391 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005392 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005393 posp->col = n;
5394
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005395 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005396 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005397 posp->coladd = 0;
5398 else
5399 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005400
Bram Moolenaar493c1782014-05-28 14:34:46 +02005401 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005402 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005403
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005404 return OK;
5405}
5406
5407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 * Get the length of an environment variable name.
5409 * Advance "arg" to the first character after the name.
5410 * Return 0 for error.
5411 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005413get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 char_u *p;
5416 int len;
5417
5418 for (p = *arg; vim_isIDc(*p); ++p)
5419 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005420 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 return 0;
5422
5423 len = (int)(p - *arg);
5424 *arg = p;
5425 return len;
5426}
5427
5428/*
5429 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005430 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 * Return 0 if something is wrong.
5432 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005434get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
5436 char_u *p;
5437 int len;
5438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005439 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005441 {
5442 if (*p == ':')
5443 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005444 // "s:" is start of "s:var", but "n:" is not and can be used in
5445 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005446 len = (int)(p - *arg);
5447 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5448 || len > 1)
5449 break;
5450 }
5451 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005452 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 return 0;
5454
5455 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005456 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457
5458 return len;
5459}
5460
5461/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005462 * Get the length of the name of a variable or function.
5463 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005465 * Return -1 if curly braces expansion failed.
5466 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 * If the name contains 'magic' {}'s, expand them and return the
5468 * expanded name in an allocated string via 'alias' - caller must free.
5469 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005470 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005471get_name_len(
5472 char_u **arg,
5473 char_u **alias,
5474 int evaluate,
5475 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
5477 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 char_u *p;
5479 char_u *expr_start;
5480 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005482 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
5484 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5485 && (*arg)[2] == (int)KE_SNR)
5486 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005487 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 *arg += 3;
5489 return get_id_len(arg) + 3;
5490 }
5491 len = eval_fname_script(*arg);
5492 if (len > 0)
5493 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005494 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 *arg += len;
5496 }
5497
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005499 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005501 p = find_name_end(*arg, &expr_start, &expr_end,
5502 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 if (expr_start != NULL)
5504 {
5505 char_u *temp_string;
5506
5507 if (!evaluate)
5508 {
5509 len += (int)(p - *arg);
5510 *arg = skipwhite(p);
5511 return len;
5512 }
5513
5514 /*
5515 * Include any <SID> etc in the expanded string:
5516 * Thus the -len here.
5517 */
5518 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5519 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005520 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 *alias = temp_string;
5522 *arg = skipwhite(p);
5523 return (int)STRLEN(temp_string);
5524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
5526 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005527 // Only give an error when there is something, otherwise it will be
5528 // reported at a higher level.
5529 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005530 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
5532 return len;
5533}
5534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535/*
5536 * Find the end of a variable or function name, taking care of magic braces.
5537 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5538 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005539 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540 * Return a pointer to just after the name. Equal to "arg" if there is no
5541 * valid name.
5542 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005543 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005544find_name_end(
5545 char_u *arg,
5546 char_u **expr_start,
5547 char_u **expr_end,
5548 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 int mb_nest = 0;
5551 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005553 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005554 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 *expr_start = NULL;
5559 *expr_end = NULL;
5560 }
5561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005562 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005563 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5564 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005565 return arg;
5566
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 for (p = arg; *p != NUL
5568 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005569 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005570 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005571 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005573 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005575 if (*p == '\'')
5576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005577 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005578 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005579 ;
5580 if (*p == NUL)
5581 break;
5582 }
5583 else if (*p == '"')
5584 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005585 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005586 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005587 if (*p == '\\' && p[1] != NUL)
5588 ++p;
5589 if (*p == NUL)
5590 break;
5591 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005592 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005594 // "s:" is start of "s:var", but "n:" is not and can be used in
5595 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005596 len = (int)(p - arg);
5597 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005598 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005599 break;
5600 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005601
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005602 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604 if (*p == '[')
5605 ++br_nest;
5606 else if (*p == ']')
5607 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005609
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005610 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 if (*p == '{')
5613 {
5614 mb_nest++;
5615 if (expr_start != NULL && *expr_start == NULL)
5616 *expr_start = p;
5617 }
5618 else if (*p == '}')
5619 {
5620 mb_nest--;
5621 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5622 *expr_end = p;
5623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
5626
5627 return p;
5628}
5629
5630/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005631 * Expands out the 'magic' {}'s in a variable/function name.
5632 * Note that this can call itself recursively, to deal with
5633 * constructs like foo{bar}{baz}{bam}
5634 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5635 * "in_start" ^
5636 * "expr_start" ^
5637 * "expr_end" ^
5638 * "in_end" ^
5639 *
5640 * Returns a new allocated string, which the caller must free.
5641 * Returns NULL for failure.
5642 */
5643 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005644make_expanded_name(
5645 char_u *in_start,
5646 char_u *expr_start,
5647 char_u *expr_end,
5648 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005649{
5650 char_u c1;
5651 char_u *retval = NULL;
5652 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005653
5654 if (expr_end == NULL || in_end == NULL)
5655 return NULL;
5656 *expr_start = NUL;
5657 *expr_end = NUL;
5658 c1 = *in_end;
5659 *in_end = NUL;
5660
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005661 temp_result = eval_to_string(expr_start + 1, FALSE);
5662 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005663 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005664 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5665 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005666 if (retval != NULL)
5667 {
5668 STRCPY(retval, in_start);
5669 STRCAT(retval, temp_result);
5670 STRCAT(retval, expr_end + 1);
5671 }
5672 }
5673 vim_free(temp_result);
5674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005675 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005676 *expr_start = '{';
5677 *expr_end = '}';
5678
5679 if (retval != NULL)
5680 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005681 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005682 if (expr_start != NULL)
5683 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005684 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005685 temp_result = make_expanded_name(retval, expr_start,
5686 expr_end, temp_result);
5687 vim_free(retval);
5688 retval = temp_result;
5689 }
5690 }
5691
5692 return retval;
5693}
5694
5695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005697 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005700eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005702 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005703}
5704
5705/*
5706 * Return TRUE if character "c" can be used as the first character in a
5707 * variable or function name (excluding '{' and '}').
5708 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005709 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005710eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005711{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005712 return ASCII_ISALPHA(c) || c == '_';
5713}
5714
5715/*
5716 * Return TRUE if character "c" can be used as the first character of a
5717 * dictionary key.
5718 */
5719 int
5720eval_isdictc(int c)
5721{
5722 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723}
5724
5725/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005726 * Handle:
5727 * - expr[expr], expr[expr:expr] subscript
5728 * - ".name" lookup
5729 * - function call with Funcref variable: func(expr)
5730 * - method call: var->method()
5731 *
5732 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005733 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005734 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005735handle_subscript(
5736 char_u **arg,
5737 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005738 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005739 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005740{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005741 int evaluate = evalarg != NULL
5742 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005743 int ret = OK;
5744 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005745 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005746 int getnext;
5747 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005748
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005749 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005750 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005751 // When at the end of the line and ".name" or "->{" or "->X" follows in
5752 // the next line then consume the line break.
5753 p = eval_next_non_blank(*arg, evalarg, &getnext);
5754 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005755 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005756 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005757 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005758 {
5759 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005760 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005761 check_white = FALSE;
5762 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005763
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005764 if (rettv->v_type == VAR_ANY)
5765 {
5766 char_u *exp_name;
5767 int cc;
5768 int idx;
5769 ufunc_T *ufunc;
5770 type_T *type;
5771
5772 // Found script from "import * as {name}", script item name must
5773 // follow.
5774 if (**arg != '.')
5775 {
5776 if (verbose)
5777 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5778 ret = FAIL;
5779 break;
5780 }
5781 ++*arg;
5782 if (IS_WHITE_OR_NUL(**arg))
5783 {
5784 if (verbose)
5785 emsg(_(e_no_white_space_allowed_after_dot));
5786 ret = FAIL;
5787 break;
5788 }
5789
5790 // isolate the name
5791 exp_name = *arg;
5792 while (eval_isnamec(**arg))
5793 ++*arg;
5794 cc = **arg;
5795 **arg = NUL;
5796
5797 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5798 evalarg->eval_cctx, verbose);
5799 **arg = cc;
5800 *arg = skipwhite(*arg);
5801
5802 if (idx < 0 && ufunc == NULL)
5803 {
5804 ret = FAIL;
5805 break;
5806 }
5807 if (idx >= 0)
5808 {
5809 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5810 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5811
5812 copy_tv(sv->sv_tv, rettv);
5813 }
5814 else
5815 {
5816 rettv->v_type = VAR_FUNC;
5817 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5818 }
5819 }
5820
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005821 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5822 || rettv->v_type == VAR_PARTIAL))
5823 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005824 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005825 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5826 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005827
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005828 // Stop the expression evaluation when immediately aborting on
5829 // error, or when an interrupt occurred or an exception was thrown
5830 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005831 if (aborting())
5832 {
5833 if (ret == OK)
5834 clear_tv(rettv);
5835 ret = FAIL;
5836 }
5837 dict_unref(selfdict);
5838 selfdict = NULL;
5839 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005840 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005841 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005842 *arg = skipwhite(p + 2);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005843 if (ret == OK)
5844 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005845 if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005846 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005847 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005848 else
5849 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005850 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005851 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005852 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005853 // "." is ".name" lookup when we found a dict or when evaluating and
5854 // scriptversion is at least 2, where string concatenation is "..".
5855 else if (**arg == '['
5856 || (**arg == '.' && (rettv->v_type == VAR_DICT
5857 || (!evaluate
5858 && (*arg)[1] != '.'
5859 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005860 {
5861 dict_unref(selfdict);
5862 if (rettv->v_type == VAR_DICT)
5863 {
5864 selfdict = rettv->vval.v_dict;
5865 if (selfdict != NULL)
5866 ++selfdict->dv_refcount;
5867 }
5868 else
5869 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005870 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005871 {
5872 clear_tv(rettv);
5873 ret = FAIL;
5874 }
5875 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005876 else
5877 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005878 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005879
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005880 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5881 // Don't do this when "Func" is already a partial that was bound
5882 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005883 if (selfdict != NULL
5884 && (rettv->v_type == VAR_FUNC
5885 || (rettv->v_type == VAR_PARTIAL
5886 && (rettv->vval.v_partial->pt_auto
5887 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005888 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005889
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005890 dict_unref(selfdict);
5891 return ret;
5892}
5893
5894/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005895 * Make a copy of an item.
5896 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005897 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5898 * reference to an already copied list/dict can be used.
5899 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005900 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005901 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005902item_copy(
5903 typval_T *from,
5904 typval_T *to,
5905 int deep,
5906 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005907{
5908 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005909 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005910
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005912 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005913 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005914 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005915 }
5916 ++recurse;
5917
5918 switch (from->v_type)
5919 {
5920 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005921 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005922 case VAR_STRING:
5923 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005924 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005925 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005926 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005927 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005928 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005929 copy_tv(from, to);
5930 break;
5931 case VAR_LIST:
5932 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005933 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005934 if (from->vval.v_list == NULL)
5935 to->vval.v_list = NULL;
5936 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5937 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005938 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005939 to->vval.v_list = from->vval.v_list->lv_copylist;
5940 ++to->vval.v_list->lv_refcount;
5941 }
5942 else
5943 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5944 if (to->vval.v_list == NULL)
5945 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005946 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005947 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005948 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005949 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005950 case VAR_DICT:
5951 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005952 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005953 if (from->vval.v_dict == NULL)
5954 to->vval.v_dict = NULL;
5955 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5956 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005957 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005958 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5959 ++to->vval.v_dict->dv_refcount;
5960 }
5961 else
5962 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5963 if (to->vval.v_dict == NULL)
5964 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005965 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005966 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005967 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005968 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005969 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005970 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005971 }
5972 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005973 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005974}
5975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005976 void
5977echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5978{
5979 char_u *tofree;
5980 char_u numbuf[NUMBUFLEN];
5981 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5982
5983 if (*atstart)
5984 {
5985 *atstart = FALSE;
5986 // Call msg_start() after eval1(), evaluating the expression
5987 // may cause a message to appear.
5988 if (with_space)
5989 {
5990 // Mark the saved text as finishing the line, so that what
5991 // follows is displayed on a new line when scrolling back
5992 // at the more prompt.
5993 msg_sb_eol();
5994 msg_start();
5995 }
5996 }
5997 else if (with_space)
5998 msg_puts_attr(" ", echo_attr);
5999
6000 if (p != NULL)
6001 for ( ; *p != NUL && !got_int; ++p)
6002 {
6003 if (*p == '\n' || *p == '\r' || *p == TAB)
6004 {
6005 if (*p != TAB && *needclr)
6006 {
6007 // remove any text still there from the command
6008 msg_clr_eos();
6009 *needclr = FALSE;
6010 }
6011 msg_putchar_attr(*p, echo_attr);
6012 }
6013 else
6014 {
6015 if (has_mbyte)
6016 {
6017 int i = (*mb_ptr2len)(p);
6018
6019 (void)msg_outtrans_len_attr(p, i, echo_attr);
6020 p += i - 1;
6021 }
6022 else
6023 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6024 }
6025 }
6026 vim_free(tofree);
6027}
6028
Bram Moolenaare9a41262005-01-15 22:18:47 +00006029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 * ":echo expr1 ..." print each argument separated with a space, add a
6031 * newline at the end.
6032 * ":echon expr1 ..." print each argument plain.
6033 */
6034 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006035ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036{
6037 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 char_u *p;
6040 int needclr = TRUE;
6041 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006042 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006043 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006044 evalarg_T evalarg;
6045
Bram Moolenaare707c882020-07-01 13:07:37 +02006046 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
6048 if (eap->skip)
6049 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006050 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006052 // If eval1() causes an error message the text from the command may
6053 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006054 need_clr_eos = needclr;
6055
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006057 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 {
6059 /*
6060 * Report the invalid expression unless the expression evaluation
6061 * has been cancelled due to an aborting error, an interrupt, or an
6062 * exception.
6063 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006064 if (!aborting() && did_emsg == did_emsg_before
6065 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006066 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006067 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 break;
6069 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006070 need_clr_eos = FALSE;
6071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006073 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006074
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006075 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 arg = skipwhite(arg);
6077 }
6078 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006079 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
6081 if (eap->skip)
6082 --emsg_skip;
6083 else
6084 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006085 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 if (needclr)
6087 msg_clr_eos();
6088 if (eap->cmdidx == CMD_echo)
6089 msg_end();
6090 }
6091}
6092
6093/*
6094 * ":echohl {name}".
6095 */
6096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006097ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006099 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100}
6101
6102/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006103 * Returns the :echo attribute
6104 */
6105 int
6106get_echo_attr(void)
6107{
6108 return echo_attr;
6109}
6110
6111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 * ":execute expr1 ..." execute the result of an expression.
6113 * ":echomsg expr1 ..." Print a message
6114 * ":echoerr expr1 ..." Print an error
6115 * Each gets spaces around each argument and a newline at the end for
6116 * echo commands
6117 */
6118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006119ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120{
6121 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 int ret = OK;
6124 char_u *p;
6125 garray_T ga;
6126 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128 ga_init2(&ga, 1, 80);
6129
6130 if (eap->skip)
6131 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006132 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006134 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006135 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
6138 if (!eap->skip)
6139 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006140 char_u buf[NUMBUFLEN];
6141
6142 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006143 {
6144 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6145 {
6146 emsg(_(e_inval_string));
6147 p = NULL;
6148 }
6149 else
6150 p = tv_get_string_buf(&rettv, buf);
6151 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006152 else
6153 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006154 if (p == NULL)
6155 {
6156 clear_tv(&rettv);
6157 ret = FAIL;
6158 break;
6159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 len = (int)STRLEN(p);
6161 if (ga_grow(&ga, len + 2) == FAIL)
6162 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006163 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 ret = FAIL;
6165 break;
6166 }
6167 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 ga.ga_len += len;
6171 }
6172
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006173 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 arg = skipwhite(arg);
6175 }
6176
6177 if (ret != FAIL && ga.ga_data != NULL)
6178 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006179 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6180 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006181 // Mark the already saved text as finishing the line, so that what
6182 // follows is displayed on a new line when scrolling back at the
6183 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006184 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006185 }
6186
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006188 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006189 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006190 out_flush();
6191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 else if (eap->cmdidx == CMD_echoerr)
6193 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006194 int save_did_emsg = did_emsg;
6195
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006196 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006197 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 if (!force_abort)
6199 did_emsg = save_did_emsg;
6200 }
6201 else if (eap->cmdidx == CMD_execute)
6202 do_cmdline((char_u *)ga.ga_data,
6203 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6204 }
6205
6206 ga_clear(&ga);
6207
6208 if (eap->skip)
6209 --emsg_skip;
6210
6211 eap->nextcmd = check_nextcmd(arg);
6212}
6213
6214/*
6215 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6216 * "arg" points to the "&" or '+' when called, to "option" when returning.
6217 * Returns NULL when no option name found. Otherwise pointer to the char
6218 * after the option name.
6219 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006220 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006221find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 char_u *p = *arg;
6224
6225 ++p;
6226 if (*p == 'g' && p[1] == ':')
6227 {
6228 *opt_flags = OPT_GLOBAL;
6229 p += 2;
6230 }
6231 else if (*p == 'l' && p[1] == ':')
6232 {
6233 *opt_flags = OPT_LOCAL;
6234 p += 2;
6235 }
6236 else
6237 *opt_flags = 0;
6238
6239 if (!ASCII_ISALPHA(*p))
6240 return NULL;
6241 *arg = p;
6242
6243 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006244 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 else
6246 while (ASCII_ISALPHA(*p))
6247 ++p;
6248 return p;
6249}
6250
6251/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006252 * Display script name where an item was last set.
6253 * Should only be invoked when 'verbose' is non-zero.
6254 */
6255 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006256last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006257{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006258 char_u *p;
6259
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006260 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006261 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006262 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006263 if (p != NULL)
6264 {
6265 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006266 msg_puts(_("\n\tLast set from "));
6267 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006268 if (script_ctx.sc_lnum > 0)
6269 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006270 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006271 msg_outnum((long)script_ctx.sc_lnum);
6272 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006273 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006274 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006275 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006276 }
6277}
6278
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006279#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
6281/*
6282 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006283 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 * "flags" can be "g" to do a global substitute.
6285 * Returns an allocated string, NULL for error.
6286 */
6287 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006288do_string_sub(
6289 char_u *str,
6290 char_u *pat,
6291 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006292 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006293 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
6295 int sublen;
6296 regmatch_T regmatch;
6297 int i;
6298 int do_all;
6299 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006300 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 garray_T ga;
6302 char_u *ret;
6303 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006304 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006306 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006308 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
6310 ga_init2(&ga, 1, 200);
6311
6312 do_all = (flags[0] == 'g');
6313
6314 regmatch.rm_ic = p_ic;
6315 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6316 if (regmatch.regprog != NULL)
6317 {
6318 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006319 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6321 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006322 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006323 if (regmatch.startp[0] == regmatch.endp[0])
6324 {
6325 if (zero_width == regmatch.startp[0])
6326 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006327 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006328 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006329 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6330 (size_t)i);
6331 ga.ga_len += i;
6332 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006333 continue;
6334 }
6335 zero_width = regmatch.startp[0];
6336 }
6337
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 /*
6339 * Get some space for a temporary buffer to do the substitution
6340 * into. It will contain:
6341 * - The text up to where the match is.
6342 * - The substituted text.
6343 * - The text after the match.
6344 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006345 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006346 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6348 {
6349 ga_clear(&ga);
6350 break;
6351 }
6352
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006353 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 i = (int)(regmatch.startp[0] - tail);
6355 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006356 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006357 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 + ga.ga_len + i, TRUE, TRUE, FALSE);
6359 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006360 tail = regmatch.endp[0];
6361 if (*tail == NUL)
6362 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 if (!do_all)
6364 break;
6365 }
6366
6367 if (ga.ga_data != NULL)
6368 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6369
Bram Moolenaar473de612013-06-08 18:19:48 +02006370 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 }
6372
6373 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6374 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006375 if (p_cpo == empty_option)
6376 p_cpo = save_cpo;
6377 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006378 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006379 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006380 // If it's still empty it was changed and restored, need to restore in
6381 // the complicated way.
6382 if (*p_cpo == NUL)
6383 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006384 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386
6387 return ret;
6388}