blob: ebd25077c3ede3b3701a608f7c7dba2351640b5c [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;
1216 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1217 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001218 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001219 if (lp->ll_n1 < 0)
1220 {
1221 lp->ll_n1 = 0;
1222 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1223 }
1224 }
1225 if (lp->ll_li == NULL)
1226 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001227 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001228 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001229 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 }
1232
Bram Moolenaar10c65862020-10-08 21:16:42 +02001233 if (lp->ll_valtype != NULL)
1234 // use the type of the member
1235 lp->ll_valtype = lp->ll_valtype->tt_member;
1236
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 /*
1238 * May need to find the item or absolute index for the second
1239 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 * When no index given: "lp->ll_empty2" is TRUE.
1241 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001245 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001246 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001249 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001251 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001252 {
1253 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001254 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001256 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001258 }
1259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001260 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 if (lp->ll_n1 < 0)
1262 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1263 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001264 {
1265 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001266 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001268 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001269 }
1270
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 }
1274
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001275 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277 return p;
1278}
1279
1280/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001281 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001282 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001284clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001285{
1286 vim_free(lp->ll_exp_name);
1287 vim_free(lp->ll_newkey);
1288}
1289
1290/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001291 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001292 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001293 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1294 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001296 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001297set_var_lval(
1298 lval_T *lp,
1299 char_u *endp,
1300 typval_T *rettv,
1301 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001302 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001303 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304{
1305 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001306 listitem_T *ri;
1307 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308
1309 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001310 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001311 cc = *endp;
1312 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001313 if (lp->ll_blob != NULL)
1314 {
1315 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001316
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001317 if (op != NULL && *op != '=')
1318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001319 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001320 return;
1321 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001322 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001323 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001324
1325 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1326 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001327 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001328
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001329 if (lp->ll_empty2)
1330 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1331
1332 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001334 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001335 return;
1336 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001337 if (lp->ll_empty2)
1338 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001339
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001340 ir = 0;
1341 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1342 blob_set(lp->ll_blob, il,
1343 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001344 }
1345 else
1346 {
1347 val = (int)tv_get_number_chk(rettv, &error);
1348 if (!error)
1349 {
1350 garray_T *gap = &lp->ll_blob->bv_ga;
1351
1352 // Allow for appending a byte. Setting a byte beyond
1353 // the end is an error otherwise.
1354 if (lp->ll_n1 < gap->ga_len
1355 || (lp->ll_n1 == gap->ga_len
1356 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1357 {
1358 blob_set(lp->ll_blob, lp->ll_n1, val);
1359 if (lp->ll_n1 == gap->ga_len)
1360 ++gap->ga_len;
1361 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001362 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001363 }
1364 }
1365 }
1366 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001368 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001369
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001370 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001371 {
1372 emsg(_(e_cannot_mod));
1373 *endp = cc;
1374 return;
1375 }
1376
Bram Moolenaarff697e62019-02-12 22:28:33 +01001377 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001378 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001379 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001380 &tv, &di, TRUE, FALSE) == OK)
1381 {
1382 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001383 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1384 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001385 && tv_op(&tv, rettv, op) == OK)
1386 set_var(lp->ll_name, &tv, FALSE);
1387 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001388 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001389 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001390 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001391 {
1392 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001393 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001394 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001396 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001397 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001398 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001399 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001400 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001401 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001402 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001403 else if (lp->ll_range)
1404 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001405 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001406 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001407
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001408 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001409 {
1410 emsg(_("E996: Cannot lock a range"));
1411 return;
1412 }
1413
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001414 /*
1415 * Check whether any of the list items is locked
1416 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001417 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001418 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001419 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001420 return;
1421 ri = ri->li_next;
1422 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1423 break;
1424 ll_li = ll_li->li_next;
1425 ++ll_n1;
1426 }
1427
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 /*
1429 * Assign the List values to the list items.
1430 */
1431 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001432 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 if (op != NULL && *op != '=')
1434 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1435 else
1436 {
1437 clear_tv(&lp->ll_li->li_tv);
1438 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1439 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001440 ri = ri->li_next;
1441 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1442 break;
1443 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001444 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001445 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001446 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001447 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 ri = NULL;
1449 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001450 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001451 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001452 lp->ll_li = lp->ll_li->li_next;
1453 ++lp->ll_n1;
1454 }
1455 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001456 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001457 else if (lp->ll_empty2
1458 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001460 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461 }
1462 else
1463 {
1464 /*
1465 * Assign to a List or Dictionary item.
1466 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001467 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001468 {
1469 emsg(_("E996: Cannot lock a list or dict"));
1470 return;
1471 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001472
1473 if (lp->ll_valtype != NULL
1474 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1475 return;
1476
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477 if (lp->ll_newkey != NULL)
1478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001479 if (op != NULL && *op != '=')
1480 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001481 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001482 return;
1483 }
1484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001485 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001486 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001487 if (di == NULL)
1488 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001489 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1490 {
1491 vim_free(di);
1492 return;
1493 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001495 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496 else if (op != NULL && *op != '=')
1497 {
1498 tv_op(lp->ll_tv, rettv, op);
1499 return;
1500 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001501 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001502 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001503
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001504 /*
1505 * Assign the value to the variable or list item.
1506 */
1507 if (copy)
1508 copy_tv(rettv, lp->ll_tv);
1509 else
1510 {
1511 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001512 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001514 }
1515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516}
1517
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001518/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001519 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1520 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521 * Returns OK or FAIL.
1522 */
1523 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001525{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001526 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001527 char_u numbuf[NUMBUFLEN];
1528 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001529 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001531 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001532 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001533 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001534 {
1535 switch (tv1->v_type)
1536 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001537 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001538 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001540 case VAR_DICT:
1541 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001542 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001543 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001544 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001545 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001546 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001547 break;
1548
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001549 case VAR_BLOB:
1550 if (*op != '+' || tv2->v_type != VAR_BLOB)
1551 break;
1552 // BLOB += BLOB
1553 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1554 {
1555 blob_T *b1 = tv1->vval.v_blob;
1556 blob_T *b2 = tv2->vval.v_blob;
1557 int i, len = blob_len(b2);
1558 for (i = 0; i < len; i++)
1559 ga_append(&b1->bv_ga, blob_get(b2, i));
1560 }
1561 return OK;
1562
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001563 case VAR_LIST:
1564 if (*op != '+' || tv2->v_type != VAR_LIST)
1565 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001566 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001567 if (tv2->vval.v_list != NULL)
1568 {
1569 if (tv1->vval.v_list == NULL)
1570 {
1571 tv1->vval.v_list = tv2->vval.v_list;
1572 ++tv1->vval.v_list->lv_refcount;
1573 }
1574 else
1575 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1576 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001577 return OK;
1578
1579 case VAR_NUMBER:
1580 case VAR_STRING:
1581 if (tv2->v_type == VAR_LIST)
1582 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001583 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001584 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001585 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001586 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001587#ifdef FEAT_FLOAT
1588 if (tv2->v_type == VAR_FLOAT)
1589 {
1590 float_T f = n;
1591
Bram Moolenaarff697e62019-02-12 22:28:33 +01001592 if (*op == '%')
1593 break;
1594 switch (*op)
1595 {
1596 case '+': f += tv2->vval.v_float; break;
1597 case '-': f -= tv2->vval.v_float; break;
1598 case '*': f *= tv2->vval.v_float; break;
1599 case '/': f /= tv2->vval.v_float; break;
1600 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001601 clear_tv(tv1);
1602 tv1->v_type = VAR_FLOAT;
1603 tv1->vval.v_float = f;
1604 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001605 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001606#endif
1607 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001608 switch (*op)
1609 {
1610 case '+': n += tv_get_number(tv2); break;
1611 case '-': n -= tv_get_number(tv2); break;
1612 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001613 case '/': n = num_divide(n, tv_get_number(tv2),
1614 &failed); break;
1615 case '%': n = num_modulus(n, tv_get_number(tv2),
1616 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001617 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618 clear_tv(tv1);
1619 tv1->v_type = VAR_NUMBER;
1620 tv1->vval.v_number = n;
1621 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001622 }
1623 else
1624 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001625 if (tv2->v_type == VAR_FLOAT)
1626 break;
1627
Bram Moolenaarff697e62019-02-12 22:28:33 +01001628 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001629 s = tv_get_string(tv1);
1630 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001631 clear_tv(tv1);
1632 tv1->v_type = VAR_STRING;
1633 tv1->vval.v_string = s;
1634 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001635 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001636
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001637 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001638#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001639 {
1640 float_T f;
1641
Bram Moolenaarff697e62019-02-12 22:28:33 +01001642 if (*op == '%' || *op == '.'
1643 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001644 && tv2->v_type != VAR_NUMBER
1645 && tv2->v_type != VAR_STRING))
1646 break;
1647 if (tv2->v_type == VAR_FLOAT)
1648 f = tv2->vval.v_float;
1649 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001650 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001651 switch (*op)
1652 {
1653 case '+': tv1->vval.v_float += f; break;
1654 case '-': tv1->vval.v_float -= f; break;
1655 case '*': tv1->vval.v_float *= f; break;
1656 case '/': tv1->vval.v_float /= f; break;
1657 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001658 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001659#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001660 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001661 }
1662 }
1663
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001664 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001665 return FAIL;
1666}
1667
1668/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 * Evaluate the expression used in a ":for var in expr" command.
1670 * "arg" points to "var".
1671 * Set "*errp" to TRUE for an error, FALSE otherwise;
1672 * Return a pointer that holds the info. Null when there is an error.
1673 */
1674 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001675eval_for_line(
1676 char_u *arg,
1677 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001678 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001679 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680{
Bram Moolenaar33570922005-01-25 22:26:29 +00001681 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001683 typval_T tv;
1684 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001685 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001687 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001689 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 if (fi == NULL)
1691 return NULL;
1692
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001693 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 if (expr == NULL)
1695 return fi;
1696
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001697 expr = skipwhite_and_linebreak(expr, evalarg);
1698 if (expr[0] != 'i' || expr[1] != 'n'
1699 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 return fi;
1703 }
1704
1705 if (skip)
1706 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001707 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1708 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 {
1710 *errp = FALSE;
1711 if (!skip)
1712 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001713 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001714 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001715 l = tv.vval.v_list;
1716 if (l == NULL)
1717 {
1718 // a null list is like an empty list: do nothing
1719 clear_tv(&tv);
1720 }
1721 else
1722 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001724 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001726 // No need to increment the refcount, it's already set for
1727 // the list being used in "tv".
1728 fi->fi_list = l;
1729 list_add_watch(l, &fi->fi_lw);
1730 fi->fi_lw.lw_item = l->lv_first;
1731 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001732 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001733 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001734 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001735 fi->fi_bi = 0;
1736 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001737 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001738 typval_T btv;
1739
1740 // Make a copy, so that the iteration still works when the
1741 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001743 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001744 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001745 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001746 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 else
1748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001749 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001750 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751 }
1752 }
1753 }
1754 if (skip)
1755 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001756 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757
1758 return fi;
1759}
1760
1761/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001762 * Used when looping over a :for line, skip the "in expr" part.
1763 */
1764 void
1765skip_for_lines(void *fi_void, evalarg_T *evalarg)
1766{
1767 forinfo_T *fi = (forinfo_T *)fi_void;
1768 int i;
1769
1770 for (i = 0; i < fi->fi_break_count; ++i)
1771 eval_next_line(evalarg);
1772}
1773
1774/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775 * Use the first item in a ":for" list. Advance to the next.
1776 * Assign the values to the variable (list). "arg" points to the first one.
1777 * Return TRUE when a valid item was found, FALSE when at end of list or
1778 * something wrong.
1779 */
1780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001781next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001783 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 int result;
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001785 int flag = in_vim9script() ? ASSIGN_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001786 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001788 if (fi->fi_blob != NULL)
1789 {
1790 typval_T tv;
1791
1792 if (fi->fi_bi >= blob_len(fi->fi_blob))
1793 return FALSE;
1794 tv.v_type = VAR_NUMBER;
1795 tv.v_lock = VAR_FIXED;
1796 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1797 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001798 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001799 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001800 }
1801
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 item = fi->fi_lw.lw_item;
1803 if (item == NULL)
1804 result = FALSE;
1805 else
1806 {
1807 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001808 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001809 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 }
1811 return result;
1812}
1813
1814/*
1815 * Free the structure used to store info used by ":for".
1816 */
1817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819{
Bram Moolenaar33570922005-01-25 22:26:29 +00001820 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001822 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001823 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001825 list_unref(fi->fi_list);
1826 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001827 if (fi != NULL && fi->fi_blob != NULL)
1828 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 vim_free(fi);
1830}
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001833set_context_for_expression(
1834 expand_T *xp,
1835 char_u *arg,
1836 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001838 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001842 if (cmdidx == CMD_let || cmdidx == CMD_var
1843 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 {
1845 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001846 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001848 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001849 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 {
1851 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001852 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001853 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001854 break;
1855 }
1856 return;
1857 }
1858 }
1859 else
1860 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1861 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 while ((xp->xp_pattern = vim_strpbrk(arg,
1863 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1864 {
1865 c = *xp->xp_pattern;
1866 if (c == '&')
1867 {
1868 c = xp->xp_pattern[1];
1869 if (c == '&')
1870 {
1871 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001872 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 }
1874 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001877 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1878 xp->xp_pattern += 2;
1879
1880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
1882 else if (c == '$')
1883 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001884 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 xp->xp_context = EXPAND_ENV_VARS;
1886 }
1887 else if (c == '=')
1888 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001889 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 xp->xp_context = EXPAND_EXPRESSION;
1891 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001892 else if (c == '#'
1893 && xp->xp_context == EXPAND_EXPRESSION)
1894 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001895 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001896 break;
1897 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001898 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 && xp->xp_context == EXPAND_FUNCTIONS
1900 && vim_strchr(xp->xp_pattern, '(') == NULL)
1901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001902 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 break;
1904 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001905 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001907 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
1909 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1910 if (c == '\\' && xp->xp_pattern[1] != NUL)
1911 ++xp->xp_pattern;
1912 xp->xp_context = EXPAND_NOTHING;
1913 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001914 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001916 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1918 /* skip */ ;
1919 xp->xp_context = EXPAND_NOTHING;
1920 }
1921 else if (c == '|')
1922 {
1923 if (xp->xp_pattern[1] == '|')
1924 {
1925 ++xp->xp_pattern;
1926 xp->xp_context = EXPAND_EXPRESSION;
1927 }
1928 else
1929 xp->xp_context = EXPAND_COMMANDS;
1930 }
1931 else
1932 xp->xp_context = EXPAND_EXPRESSION;
1933 }
1934 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001935 // Doesn't look like something valid, expand as an expression
1936 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 arg = xp->xp_pattern;
1939 if (*arg != NUL)
1940 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1941 /* skip */ ;
1942 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001943
1944 // ":exe one two" completes "two"
1945 if ((cmdidx == CMD_execute
1946 || cmdidx == CMD_echo
1947 || cmdidx == CMD_echon
1948 || cmdidx == CMD_echomsg)
1949 && xp->xp_context == EXPAND_EXPRESSION)
1950 {
1951 for (;;)
1952 {
1953 char_u *n = skiptowhite(arg);
1954
1955 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1956 break;
1957 arg = skipwhite(n);
1958 }
1959 }
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 xp->xp_pattern = arg;
1962}
1963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001965 * Return TRUE if "pat" matches "text".
1966 * Does not use 'cpo' and always uses 'magic'.
1967 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001968 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001969pattern_match(char_u *pat, char_u *text, int ic)
1970{
1971 int matches = FALSE;
1972 char_u *save_cpo;
1973 regmatch_T regmatch;
1974
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001975 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001976 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001977 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001978 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1979 if (regmatch.regprog != NULL)
1980 {
1981 regmatch.rm_ic = ic;
1982 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1983 vim_regfree(regmatch.regprog);
1984 }
1985 p_cpo = save_cpo;
1986 return matches;
1987}
1988
1989/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001990 * Handle a name followed by "(". Both for just "name(arg)" and for
1991 * "expr->name(arg)".
1992 * Returns OK or FAIL.
1993 */
1994 static int
1995eval_func(
1996 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001997 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001998 char_u *name,
1999 int name_len,
2000 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002001 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002002 typval_T *basetv) // "expr" for "expr->name(arg)"
2003{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002004 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002005 char_u *s = name;
2006 int len = name_len;
2007 partial_T *partial;
2008 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002009 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002010
2011 if (!evaluate)
2012 check_vars(s, len);
2013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002014 // If "s" is the name of a variable of type VAR_FUNC
2015 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002016 s = deref_func_name(s, &len, &partial,
2017 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002019 // Need to make a copy, in case evaluating the arguments makes
2020 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002021 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002022 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002023 ret = FAIL;
2024 else
2025 {
2026 funcexe_T funcexe;
2027
2028 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002029 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002030 funcexe.firstline = curwin->w_cursor.lnum;
2031 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002032 funcexe.evaluate = evaluate;
2033 funcexe.partial = partial;
2034 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002035 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002036 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002037 }
2038 vim_free(s);
2039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002040 // If evaluate is FALSE rettv->v_type was not set in
2041 // get_func_tv, but it's needed in handle_subscript() to parse
2042 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002043 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2044 {
2045 rettv->vval.v_string = NULL;
2046 rettv->v_type = VAR_FUNC;
2047 }
2048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002049 // Stop the expression evaluation when immediately
2050 // aborting on error, or when an interrupt occurred or
2051 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002052 if (evaluate && aborting())
2053 {
2054 if (ret == OK)
2055 clear_tv(rettv);
2056 ret = FAIL;
2057 }
2058 return ret;
2059}
2060
2061/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002062 * Get the next line source line without advancing. But do skip over comment
2063 * lines.
2064 */
2065 static char_u *
2066getline_peek_skip_comments(evalarg_T *evalarg)
2067{
2068 for (;;)
2069 {
2070 char_u *next = getline_peek(evalarg->eval_getline,
2071 evalarg->eval_cookie);
2072 char_u *p;
2073
2074 if (next == NULL)
2075 break;
2076 p = skipwhite(next);
2077 if (*p != NUL && !vim9_comment_start(p))
2078 return next;
2079 (void)eval_next_line(evalarg);
2080 }
2081 return NULL;
2082}
2083
2084/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002085 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2086 * comment) and there is a next line, return the next line (skipping blanks)
2087 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002088 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2089 * "arg" must point somewhere inside a line, not at the start.
2090 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002091 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002092eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2093{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002094 char_u *p = skipwhite(arg);
2095
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002096 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002097 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002098 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002099 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002100 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002101 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002102 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002103
2104 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002105 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002106 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002107 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002108
Bram Moolenaar9d489562020-07-30 20:08:50 +02002109 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002110 {
2111 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002112 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002113 }
2114 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002115 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116}
2117
2118/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002119 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002120 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002121 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002122eval_next_line(evalarg_T *evalarg)
2123{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002124 garray_T *gap = &evalarg->eval_ga;
2125 char_u *line;
2126
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002127 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002128 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2129 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002130 else
2131 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002132 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002133 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2134 {
2135 // Going to concatenate the lines after parsing.
2136 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2137 ++gap->ga_len;
2138 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002139 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002140 {
2141 vim_free(evalarg->eval_tofree);
2142 evalarg->eval_tofree = line;
2143 }
2144 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002145}
2146
Bram Moolenaare6b53242020-07-01 17:28:33 +02002147/*
2148 * Call eval_next_non_blank() and get the next line if needed.
2149 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002150 char_u *
2151skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2152{
2153 int getnext;
2154 char_u *p = skipwhite(arg);
2155
Bram Moolenaar962d7212020-07-04 14:15:00 +02002156 if (evalarg == NULL)
2157 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002158 eval_next_non_blank(p, evalarg, &getnext);
2159 if (getnext)
2160 return eval_next_line(evalarg);
2161 return p;
2162}
2163
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002164/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002165 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002166 */
2167 void
2168clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2169{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002170 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002171 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002172 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002173 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002174 if (eap != NULL)
2175 {
2176 // We may need to keep the original command line, e.g. for
2177 // ":let" it has the variable names. But we may also need the
2178 // new one, "nextcmd" points into it. Keep both.
2179 vim_free(eap->cmdline_tofree);
2180 eap->cmdline_tofree = *eap->cmdlinep;
2181 *eap->cmdlinep = evalarg->eval_tofree;
2182 }
2183 else
2184 vim_free(evalarg->eval_tofree);
2185 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002186 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002187
2188 vim_free(evalarg->eval_tofree_lambda);
2189 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002190 }
2191}
2192
2193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2197 */
2198
2199/*
2200 * Handle zero level expression.
2201 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002203 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002204 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 * Return OK or FAIL.
2206 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002207 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002208eval0(
2209 char_u *arg,
2210 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002211 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002212 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213{
2214 int ret;
2215 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002216 int did_emsg_before = did_emsg;
2217 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002218 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002221 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002222 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002223
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002224 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
2226 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 /*
2229 * Report the invalid expression unless the expression evaluation has
2230 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002231 * exception, or we already gave a more specific error.
2232 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002234 if (!aborting()
2235 && did_emsg == did_emsg_before
2236 && called_emsg == called_emsg_before
2237 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002238 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002239
2240 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002241 // a next command to avoid more errors, unless "|" is following, which
2242 // could only be a command separator.
2243 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2244 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002247
2248 if (eap != NULL)
2249 eap->nextcmd = check_nextcmd(p);
2250
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 return ret;
2252}
2253
2254/*
2255 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002256 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002257 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 *
2259 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002260 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002262 * Note: "rettv.v_lock" is not set.
2263 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 * Return OK or FAIL.
2265 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002266 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002267eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002269 char_u *p;
2270 int getnext;
2271
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002272 CLEAR_POINTER(rettv);
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 /*
2275 * Get the first variable.
2276 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002277 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 return FAIL;
2279
Bram Moolenaar793648f2020-06-26 21:28:25 +02002280 p = eval_next_non_blank(*arg, evalarg, &getnext);
2281 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002283 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002284 int result;
2285 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002286 evalarg_T *evalarg_used = evalarg;
2287 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002288 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002289 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002290 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002291
2292 if (evalarg == NULL)
2293 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002294 CLEAR_FIELD(local_evalarg);
2295 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002296 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002297 orig_flags = evalarg_used->eval_flags;
2298 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002299
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002300 if (getnext)
2301 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002302 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002303 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002304 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002305 {
2306 error_white_both(p, 1);
2307 clear_tv(rettv);
2308 return FAIL;
2309 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002310 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002311 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002314 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002316 int error = FALSE;
2317
Bram Moolenaar13106602020-10-04 16:06:05 +02002318 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002319 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002320 else if (vim9script)
2321 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002322 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002324 if (error || !op_falsy || !result)
2325 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002326 if (error)
2327 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329
2330 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002331 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002333 if (op_falsy)
2334 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002335 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002336 {
2337 error_white_both(p, 1);
2338 clear_tv(rettv);
2339 return FAIL;
2340 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002341 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002342 evalarg_used->eval_flags = (op_falsy ? !result : result)
2343 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2344 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002345 {
2346 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002348 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002349 if (!op_falsy || !result)
2350 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002352 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002354 /*
2355 * Check for the ":".
2356 */
2357 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2358 if (*p != ':')
2359 {
2360 emsg(_(e_missing_colon));
2361 if (evaluate && result)
2362 clear_tv(rettv);
2363 evalarg_used->eval_flags = orig_flags;
2364 return FAIL;
2365 }
2366 if (getnext)
2367 *arg = eval_next_line(evalarg_used);
2368 else
2369 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002370 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002371 {
2372 error_white_both(p, 1);
2373 clear_tv(rettv);
2374 evalarg_used->eval_flags = orig_flags;
2375 return FAIL;
2376 }
2377 *arg = p;
2378 }
2379
2380 /*
2381 * Get the third variable. Recursive!
2382 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002383 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002384 {
2385 error_white_both(p, 1);
2386 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002387 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002388 return FAIL;
2389 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002390 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2391 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002392 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002393 if (eval1(arg, &var2, evalarg_used) == FAIL)
2394 {
2395 if (evaluate && result)
2396 clear_tv(rettv);
2397 evalarg_used->eval_flags = orig_flags;
2398 return FAIL;
2399 }
2400 if (evaluate && !result)
2401 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002403
2404 if (evalarg == NULL)
2405 clear_evalarg(&local_evalarg, NULL);
2406 else
2407 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409
2410 return OK;
2411}
2412
2413/*
2414 * Handle first level expression:
2415 * expr2 || expr2 || expr2 logical OR
2416 *
2417 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002418 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 *
2420 * Return OK or FAIL.
2421 */
2422 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002423eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002425 char_u *p;
2426 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427
2428 /*
2429 * Get the first variable.
2430 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002431 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 return FAIL;
2433
2434 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002435 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002437 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002440 evalarg_T *evalarg_used = evalarg;
2441 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002442 int evaluate;
2443 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002444 long result = FALSE;
2445 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002446 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002447 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002448
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002449 if (evalarg == NULL)
2450 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002451 CLEAR_FIELD(local_evalarg);
2452 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002453 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002454 orig_flags = evalarg_used->eval_flags;
2455 evaluate = orig_flags & EVAL_EVALUATE;
2456 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002457 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002458 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002459 result = tv_get_bool_chk(rettv, &error);
2460 else if (tv_get_number_chk(rettv, &error) != 0)
2461 result = TRUE;
2462 clear_tv(rettv);
2463 if (error)
2464 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
2466
2467 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002468 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002470 while (p[0] == '|' && p[1] == '|')
2471 {
2472 if (getnext)
2473 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002474 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002475 {
2476 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2477 {
2478 error_white_both(p, 2);
2479 clear_tv(rettv);
2480 return FAIL;
2481 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002482 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002483 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002484
2485 /*
2486 * Get the second variable.
2487 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002488 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2489 {
2490 error_white_both(p, 2);
2491 clear_tv(rettv);
2492 return FAIL;
2493 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002494 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2495 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002496 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002499
2500 /*
2501 * Compute the result.
2502 */
2503 if (evaluate && !result)
2504 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002505 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002506 result = tv_get_bool_chk(&var2, &error);
2507 else if (tv_get_number_chk(&var2, &error) != 0)
2508 result = TRUE;
2509 clear_tv(&var2);
2510 if (error)
2511 return FAIL;
2512 }
2513 if (evaluate)
2514 {
2515 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002516 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002517 rettv->v_type = VAR_BOOL;
2518 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002519 }
2520 else
2521 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002522 rettv->v_type = VAR_NUMBER;
2523 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002524 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002525 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002526
2527 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002529
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002530 if (evalarg == NULL)
2531 clear_evalarg(&local_evalarg, NULL);
2532 else
2533 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
2535
2536 return OK;
2537}
2538
2539/*
2540 * Handle second level expression:
2541 * expr3 && expr3 && expr3 logical AND
2542 *
2543 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002544 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 *
2546 * Return OK or FAIL.
2547 */
2548 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002549eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002551 char_u *p;
2552 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554 /*
2555 * Get the first variable.
2556 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002557 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 return FAIL;
2559
2560 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002561 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002563 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002564 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002566 evalarg_T *evalarg_used = evalarg;
2567 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002568 int orig_flags;
2569 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002570 long result = TRUE;
2571 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002572 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002573 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002574
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002575 if (evalarg == NULL)
2576 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002577 CLEAR_FIELD(local_evalarg);
2578 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002580 orig_flags = evalarg_used->eval_flags;
2581 evaluate = orig_flags & EVAL_EVALUATE;
2582 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002583 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002584 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002585 result = tv_get_bool_chk(rettv, &error);
2586 else if (tv_get_number_chk(rettv, &error) == 0)
2587 result = FALSE;
2588 clear_tv(rettv);
2589 if (error)
2590 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592
2593 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596 while (p[0] == '&' && p[1] == '&')
2597 {
2598 if (getnext)
2599 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002600 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002601 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002602 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002603 {
2604 error_white_both(p, 2);
2605 clear_tv(rettv);
2606 return FAIL;
2607 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002608 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002609 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002610
2611 /*
2612 * Get the second variable.
2613 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002614 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2615 {
2616 error_white_both(p, 2);
2617 clear_tv(rettv);
2618 return FAIL;
2619 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2621 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002622 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002623 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002624 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002625 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002626
2627 /*
2628 * Compute the result.
2629 */
2630 if (evaluate && result)
2631 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002632 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002633 result = tv_get_bool_chk(&var2, &error);
2634 else if (tv_get_number_chk(&var2, &error) == 0)
2635 result = FALSE;
2636 clear_tv(&var2);
2637 if (error)
2638 return FAIL;
2639 }
2640 if (evaluate)
2641 {
2642 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002643 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002644 rettv->v_type = VAR_BOOL;
2645 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002646 }
2647 else
2648 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002649 rettv->v_type = VAR_NUMBER;
2650 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002651 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002652 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002653
2654 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002656
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002657 if (evalarg == NULL)
2658 clear_evalarg(&local_evalarg, NULL);
2659 else
2660 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 }
2662
2663 return OK;
2664}
2665
2666/*
2667 * Handle third level expression:
2668 * var1 == var2
2669 * var1 =~ var2
2670 * var1 != var2
2671 * var1 !~ var2
2672 * var1 > var2
2673 * var1 >= var2
2674 * var1 < var2
2675 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002676 * var1 is var2
2677 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 *
2679 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002680 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 *
2682 * Return OK or FAIL.
2683 */
2684 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002685eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002688 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002689 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002691 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 /*
2694 * Get the first variable.
2695 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002696 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 return FAIL;
2698
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002699 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002700 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
2702 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002703 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002705 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002707 typval_T var2;
2708 int ic;
2709 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002710 int evaluate = evalarg == NULL
2711 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002712
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002713 if (getnext)
2714 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002715 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2716 {
2717 error_white_both(p, len);
2718 clear_tv(rettv);
2719 return FAIL;
2720 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002721
Bram Moolenaar696ba232020-07-29 21:20:41 +02002722 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2723 {
2724 semsg(_(e_invexpr2), p);
2725 clear_tv(rettv);
2726 return FAIL;
2727 }
2728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002729 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 if (p[len] == '?')
2731 {
2732 ic = TRUE;
2733 ++len;
2734 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002735 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 else if (p[len] == '#')
2737 {
2738 ic = FALSE;
2739 ++len;
2740 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002741 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002743 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
2745 /*
2746 * Get the second variable.
2747 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002748 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2749 {
2750 error_white_both(p, 1);
2751 clear_tv(rettv);
2752 return FAIL;
2753 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002754 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002755 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002757 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 return FAIL;
2759 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002760 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002761 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002762 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002763
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002764 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002765 {
2766 ret = FAIL;
2767 clear_tv(rettv);
2768 }
2769 else
2770 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002771 clear_tv(&var2);
2772 return ret;
2773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775
2776 return OK;
2777}
2778
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002779/*
2780 * Make a copy of blob "tv1" and append blob "tv2".
2781 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 void
2783eval_addblob(typval_T *tv1, typval_T *tv2)
2784{
2785 blob_T *b1 = tv1->vval.v_blob;
2786 blob_T *b2 = tv2->vval.v_blob;
2787 blob_T *b = blob_alloc();
2788 int i;
2789
2790 if (b != NULL)
2791 {
2792 for (i = 0; i < blob_len(b1); i++)
2793 ga_append(&b->bv_ga, blob_get(b1, i));
2794 for (i = 0; i < blob_len(b2); i++)
2795 ga_append(&b->bv_ga, blob_get(b2, i));
2796
2797 clear_tv(tv1);
2798 rettv_blob_set(tv1, b);
2799 }
2800}
2801
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002802/*
2803 * Make a copy of list "tv1" and append list "tv2".
2804 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 int
2806eval_addlist(typval_T *tv1, typval_T *tv2)
2807{
2808 typval_T var3;
2809
2810 // concatenate Lists
2811 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2812 {
2813 clear_tv(tv1);
2814 clear_tv(tv2);
2815 return FAIL;
2816 }
2817 clear_tv(tv1);
2818 *tv1 = var3;
2819 return OK;
2820}
2821
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822/*
2823 * Handle fourth level expression:
2824 * + number addition
2825 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002826 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002827 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 *
2829 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002830 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 *
2832 * Return OK or FAIL.
2833 */
2834 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002835eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 /*
2838 * Get the first variable.
2839 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002840 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 return FAIL;
2842
2843 /*
2844 * Repeat computing, until no '+', '-' or '.' is following.
2845 */
2846 for (;;)
2847 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002848 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002849 int getnext;
2850 char_u *p;
2851 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002852 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002853 int concat;
2854 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002855 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002856
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002857 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002858 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002859 p = eval_next_non_blank(*arg, evalarg, &getnext);
2860 op = *p;
2861 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002862 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002864
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002865 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002866 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002867 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002868 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002869 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002870 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002871 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002872 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002873 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002874 clear_tv(rettv);
2875 return FAIL;
2876 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002877 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002878 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002879 if ((op != '+' || (rettv->v_type != VAR_LIST
2880 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002881#ifdef FEAT_FLOAT
2882 && (op == '.' || rettv->v_type != VAR_FLOAT)
2883#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002884 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002885 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002886 int error = FALSE;
2887
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002888 // For "list + ...", an illegal use of the first operand as
2889 // a number cannot be determined before evaluating the 2nd
2890 // operand: if this is also a list, all is ok.
2891 // For "something . ...", "something - ..." or "non-list + ...",
2892 // we know that the first operand needs to be a string or number
2893 // without evaluating the 2nd operand. So check before to avoid
2894 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002895 if (op != '.')
2896 tv_get_number_chk(rettv, &error);
2897 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002898 {
2899 clear_tv(rettv);
2900 return FAIL;
2901 }
2902 }
2903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 /*
2905 * Get the second variable.
2906 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002907 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002908 {
2909 error_white_both(p, oplen);
2910 clear_tv(rettv);
2911 return FAIL;
2912 }
2913 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002914 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002916 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 return FAIL;
2918 }
2919
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002920 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
2922 /*
2923 * Compute the result.
2924 */
2925 if (op == '.')
2926 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002927 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2928 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002929 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002930
Bram Moolenaar2e086612020-08-25 22:37:48 +02002931 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002932 || var2.v_type == VAR_CHANNEL
2933 || var2.v_type == VAR_JOB))
2934 emsg(_(e_inval_string));
2935#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002936 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002937 {
2938 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2939 var2.vval.v_float);
2940 s2 = buf2;
2941 }
2942#endif
2943 else
2944 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002945 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002946 {
2947 clear_tv(rettv);
2948 clear_tv(&var2);
2949 return FAIL;
2950 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002952 clear_tv(rettv);
2953 rettv->v_type = VAR_STRING;
2954 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002956 else if (op == '+' && rettv->v_type == VAR_BLOB
2957 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002959 else if (op == '+' && rettv->v_type == VAR_LIST
2960 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002961 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002963 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 else
2966 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002967 int error = FALSE;
2968 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002969#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002970 float_T f1 = 0, f2 = 0;
2971
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002972 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002973 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002974 f1 = rettv->vval.v_float;
2975 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002976 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002977 else
2978#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002979 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002980 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002981 if (error)
2982 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002983 // This can only happen for "list + non-list". For
2984 // "non-list + ..." or "something - ...", we returned
2985 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002986 clear_tv(rettv);
2987 return FAIL;
2988 }
2989#ifdef FEAT_FLOAT
2990 if (var2.v_type == VAR_FLOAT)
2991 f1 = n1;
2992#endif
2993 }
2994#ifdef FEAT_FLOAT
2995 if (var2.v_type == VAR_FLOAT)
2996 {
2997 f2 = var2.vval.v_float;
2998 n2 = 0;
2999 }
3000 else
3001#endif
3002 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003003 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003004 if (error)
3005 {
3006 clear_tv(rettv);
3007 clear_tv(&var2);
3008 return FAIL;
3009 }
3010#ifdef FEAT_FLOAT
3011 if (rettv->v_type == VAR_FLOAT)
3012 f2 = n2;
3013#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003014 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003015 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003016
3017#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003018 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3020 {
3021 if (op == '+')
3022 f1 = f1 + f2;
3023 else
3024 f1 = f1 - f2;
3025 rettv->v_type = VAR_FLOAT;
3026 rettv->vval.v_float = f1;
3027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003029#endif
3030 {
3031 if (op == '+')
3032 n1 = n1 + n2;
3033 else
3034 n1 = n1 - n2;
3035 rettv->v_type = VAR_NUMBER;
3036 rettv->vval.v_number = n1;
3037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003039 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 }
3041 }
3042 return OK;
3043}
3044
3045/*
3046 * Handle fifth level expression:
3047 * * number multiplication
3048 * / number division
3049 * % number modulo
3050 *
3051 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003052 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 *
3054 * Return OK or FAIL.
3055 */
3056 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003057eval6(
3058 char_u **arg,
3059 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003060 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003061 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003064 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
3067 /*
3068 * Get the first variable.
3069 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003070 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 return FAIL;
3072
3073 /*
3074 * Repeat computing, until no '*', '/' or '%' is following.
3075 */
3076 for (;;)
3077 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003078 int evaluate;
3079 int getnext;
3080 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003081 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003082 int op;
3083 varnumber_T n1, n2;
3084#ifdef FEAT_FLOAT
3085 float_T f1, f2;
3086#endif
3087 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003088
Bram Moolenaar9d489562020-07-30 20:08:50 +02003089 p = eval_next_non_blank(*arg, evalarg, &getnext);
3090 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003091 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003093
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003094 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003095 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003096 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003097 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003098 {
3099 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3100 {
3101 error_white_both(p, 1);
3102 clear_tv(rettv);
3103 return FAIL;
3104 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003105 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003108#ifdef FEAT_FLOAT
3109 f1 = 0;
3110 f2 = 0;
3111#endif
3112 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003113 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003115#ifdef FEAT_FLOAT
3116 if (rettv->v_type == VAR_FLOAT)
3117 {
3118 f1 = rettv->vval.v_float;
3119 use_float = TRUE;
3120 n1 = 0;
3121 }
3122 else
3123#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003124 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003125 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003126 if (error)
3127 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 }
3129 else
3130 n1 = 0;
3131
3132 /*
3133 * Get the second variable.
3134 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003135 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3136 {
3137 error_white_both(p, 1);
3138 clear_tv(rettv);
3139 return FAIL;
3140 }
3141 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003142 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 return FAIL;
3144
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003145 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003147#ifdef FEAT_FLOAT
3148 if (var2.v_type == VAR_FLOAT)
3149 {
3150 if (!use_float)
3151 {
3152 f1 = n1;
3153 use_float = TRUE;
3154 }
3155 f2 = var2.vval.v_float;
3156 n2 = 0;
3157 }
3158 else
3159#endif
3160 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003161 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162 clear_tv(&var2);
3163 if (error)
3164 return FAIL;
3165#ifdef FEAT_FLOAT
3166 if (use_float)
3167 f2 = n2;
3168#endif
3169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 /*
3172 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003173 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003175#ifdef FEAT_FLOAT
3176 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003178 if (op == '*')
3179 f1 = f1 * f2;
3180 else if (op == '/')
3181 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003182# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003183 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003184 if (f2 == 0.0)
3185 {
3186 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003187 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003188 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003189 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003190 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003191 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003192 }
3193 else
3194 f1 = f1 / f2;
3195# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003196 // We rely on the floating point library to handle divide
3197 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003198 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003199# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003202 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003204 return FAIL;
3205 }
3206 rettv->v_type = VAR_FLOAT;
3207 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 }
3209 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003212 int failed = FALSE;
3213
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003214 if (op == '*')
3215 n1 = n1 * n2;
3216 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003217 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003219 n1 = num_modulus(n1, n2, &failed);
3220 if (failed)
3221 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003222
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003223 rettv->v_type = VAR_NUMBER;
3224 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
3227 }
3228
3229 return OK;
3230}
3231
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003232 int
3233eval_leader(char_u **arg, int vim9)
3234{
3235 char_u *s = *arg;
3236 char_u *p = *arg;
3237
3238 while (*p == '!' || *p == '-' || *p == '+')
3239 {
3240 char_u *n = skipwhite(p + 1);
3241
3242 // ++, --, -+ and +- are not accepted in Vim9 script
3243 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3244 {
3245 semsg(_(e_invexpr2), s);
3246 return FAIL;
3247 }
3248 p = n;
3249 }
3250 *arg = p;
3251 return OK;
3252}
3253
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254/*
3255 * Handle sixth level expression:
3256 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003257 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003258 * "string" string constant
3259 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 * &option-name option value
3261 * @r register contents
3262 * identifier variable value
3263 * function() function call
3264 * $VAR environment variable
3265 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003266 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003268 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003269 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 *
3271 * Also handle:
3272 * ! in front logical NOT
3273 * - in front unary minus
3274 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003275 * trailing [] subscript in String or List
3276 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003277 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 *
3279 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003280 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 *
3282 * Return OK or FAIL.
3283 */
3284 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003285eval7(
3286 char_u **arg,
3287 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003288 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003291 int evaluate = evalarg != NULL
3292 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 int len;
3294 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 char_u *start_leader, *end_leader;
3296 int ret = OK;
3297 char_u *alias;
3298
3299 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003300 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003301 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003303 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
3305 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003306 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 */
3308 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003309 if (eval_leader(arg, in_vim9script()) == FAIL)
3310 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 end_leader = *arg;
3312
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003313 if (**arg == '.' && (!isdigit(*(*arg + 1))
3314#ifdef FEAT_FLOAT
3315 || current_sctx.sc_version < 2
3316#endif
3317 ))
3318 {
3319 semsg(_(e_invexpr2), *arg);
3320 ++*arg;
3321 return FAIL;
3322 }
3323
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 switch (**arg)
3325 {
3326 /*
3327 * Number constant.
3328 */
3329 case '0':
3330 case '1':
3331 case '2':
3332 case '3':
3333 case '4':
3334 case '5':
3335 case '6':
3336 case '7':
3337 case '8':
3338 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003339 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003340
3341 // Apply prefixed "-" and "+" now. Matters especially when
3342 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003343 if (ret == OK && evaluate && end_leader > start_leader
3344 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003345 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 break;
3347
3348 /*
3349 * String constant: "string".
3350 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003351 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 break;
3353
3354 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003355 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003357 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003358 break;
3359
3360 /*
3361 * List: [expr, expr]
3362 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003363 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 break;
3365
3366 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003367 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003368 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003369 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003370 {
3371 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003372 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003373 }
3374 else
3375 ret = NOTDONE;
3376 break;
3377
3378 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003379 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003380 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003381 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003382 case '{': if (in_vim9script())
3383 ret = NOTDONE;
3384 else
3385 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003386 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003387 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003388 break;
3389
3390 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003391 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003393 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 break;
3395
3396 /*
3397 * Environment variable: $VAR.
3398 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003399 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 break;
3401
3402 /*
3403 * Register contents: @r.
3404 */
3405 case '@': ++*arg;
3406 if (evaluate)
3407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003408 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003409 rettv->vval.v_string = get_reg_contents(**arg,
3410 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 }
3412 if (**arg != NUL)
3413 ++*arg;
3414 break;
3415
3416 /*
3417 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003418 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003420 case '(': ret = NOTDONE;
3421 if (in_vim9script())
3422 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
3423 if (ret == NOTDONE)
3424 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003425 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003426 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003427
Bram Moolenaar9215f012020-06-27 21:18:00 +02003428 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003429 if (**arg == ')')
3430 ++*arg;
3431 else if (ret == OK)
3432 {
3433 emsg(_(e_missing_close));
3434 clear_tv(rettv);
3435 ret = FAIL;
3436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438 break;
3439
Bram Moolenaar8c711452005-01-14 21:53:12 +00003440 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 break;
3442 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003443
3444 if (ret == NOTDONE)
3445 {
3446 /*
3447 * Must be a variable or function name.
3448 * Can also be a curly-braces kind of name: {expr}.
3449 */
3450 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003451 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003452 if (alias != NULL)
3453 s = alias;
3454
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003455 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003456 ret = FAIL;
3457 else
3458 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003459 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3460
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003461 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3462 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003463 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003464 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003465 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003466 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003467 else if (flags & EVAL_CONSTANT)
3468 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003469 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003470 {
3471 // get the value of "true", "false" or a variable
3472 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3473 {
3474 rettv->v_type = VAR_BOOL;
3475 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003476 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003477 }
3478 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003479 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003480 {
3481 rettv->v_type = VAR_BOOL;
3482 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003483 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003484 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003485 else if (len == 4 && in_vim9script()
3486 && STRNCMP(s, "null", 4) == 0)
3487 {
3488 rettv->v_type = VAR_SPECIAL;
3489 rettv->vval.v_number = VVAL_NULL;
3490 ret = OK;
3491 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003492 else
3493 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3494 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003495 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003496 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003497 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003498 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003499 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003500 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003501 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003502 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003503 }
3504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003505 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3506 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003507 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003508 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
3510 /*
3511 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3512 */
3513 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003514 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003515 return ret;
3516}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003517
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003518/*
3519 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003520 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003521 * Adjusts "end_leaderp" until it is at "start_leader".
3522 */
3523 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003524eval7_leader(
3525 typval_T *rettv,
3526 int numeric_only,
3527 char_u *start_leader,
3528 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003529{
3530 char_u *end_leader = *end_leaderp;
3531 int ret = OK;
3532 int error = FALSE;
3533 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003534 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003535#ifdef FEAT_FLOAT
3536 float_T f = 0.0;
3537
3538 if (rettv->v_type == VAR_FLOAT)
3539 f = rettv->vval.v_float;
3540 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003541#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003542 {
3543 while (VIM_ISWHITE(end_leader[-1]))
3544 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003545 if (in_vim9script() && end_leader[-1] == '!')
3546 val = tv2bool(rettv);
3547 else
3548 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003549 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003550 if (error)
3551 {
3552 clear_tv(rettv);
3553 ret = FAIL;
3554 }
3555 else
3556 {
3557 while (end_leader > start_leader)
3558 {
3559 --end_leader;
3560 if (*end_leader == '!')
3561 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003562 if (numeric_only)
3563 {
3564 ++end_leader;
3565 break;
3566 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003567#ifdef FEAT_FLOAT
3568 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003569 {
3570 if (in_vim9script())
3571 {
3572 rettv->v_type = VAR_BOOL;
3573 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3574 }
3575 else
3576 f = !f;
3577 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003578 else
3579#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003580 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003581 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003582 type = VAR_BOOL;
3583 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003584 }
3585 else if (*end_leader == '-')
3586 {
3587#ifdef FEAT_FLOAT
3588 if (rettv->v_type == VAR_FLOAT)
3589 f = -f;
3590 else
3591#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003592 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003593 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003594 type = VAR_NUMBER;
3595 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003596 }
3597 }
3598#ifdef FEAT_FLOAT
3599 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003601 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003602 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003604 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003605#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003606 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003607 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003608 if (in_vim9script())
3609 rettv->v_type = type;
3610 else
3611 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003612 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003615 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 return ret;
3617}
3618
3619/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003620 * Call the function referred to in "rettv".
3621 */
3622 static int
3623call_func_rettv(
3624 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003625 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003626 typval_T *rettv,
3627 int evaluate,
3628 dict_T *selfdict,
3629 typval_T *basetv)
3630{
3631 partial_T *pt = NULL;
3632 funcexe_T funcexe;
3633 typval_T functv;
3634 char_u *s;
3635 int ret;
3636
3637 // need to copy the funcref so that we can clear rettv
3638 if (evaluate)
3639 {
3640 functv = *rettv;
3641 rettv->v_type = VAR_UNKNOWN;
3642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003643 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003644 if (functv.v_type == VAR_PARTIAL)
3645 {
3646 pt = functv.vval.v_partial;
3647 s = partial_name(pt);
3648 }
3649 else
3650 s = functv.vval.v_string;
3651 }
3652 else
3653 s = (char_u *)"";
3654
Bram Moolenaara80faa82020-04-12 19:37:17 +02003655 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003656 funcexe.firstline = curwin->w_cursor.lnum;
3657 funcexe.lastline = curwin->w_cursor.lnum;
3658 funcexe.evaluate = evaluate;
3659 funcexe.partial = pt;
3660 funcexe.selfdict = selfdict;
3661 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003662 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003663
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003664 // Clear the funcref afterwards, so that deleting it while
3665 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003666 if (evaluate)
3667 clear_tv(&functv);
3668
3669 return ret;
3670}
3671
3672/*
3673 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003674 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003675 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3676 */
3677 static int
3678eval_lambda(
3679 char_u **arg,
3680 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003681 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003682 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003683{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003684 int evaluate = evalarg != NULL
3685 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003686 typval_T base = *rettv;
3687 int ret;
3688
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003689 rettv->v_type = VAR_UNKNOWN;
3690
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003691 if (**arg == '{')
3692 {
3693 // ->{lambda}()
3694 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3695 }
3696 else
3697 {
3698 // ->(lambda)()
3699 ++*arg;
3700 ret = eval1(arg, rettv, evalarg);
3701 *arg = skipwhite_and_linebreak(*arg, evalarg);
3702 if (**arg != ')')
3703 {
3704 emsg(_(e_missing_close));
3705 ret = FAIL;
3706 }
3707 ++*arg;
3708 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003709 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003710 return FAIL;
3711 else if (**arg != '(')
3712 {
3713 if (verbose)
3714 {
3715 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003716 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003717 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003719 }
3720 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003721 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003722 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003723 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003724 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003725
3726 // Clear the funcref afterwards, so that deleting it while
3727 // evaluating the arguments is possible (see test55).
3728 if (evaluate)
3729 clear_tv(&base);
3730
3731 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003732}
3733
3734/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003735 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003736 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003737 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3738 */
3739 static int
3740eval_method(
3741 char_u **arg,
3742 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003743 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003744 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003745{
3746 char_u *name;
3747 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003748 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003749 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003750 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003751 int evaluate = evalarg != NULL
3752 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003753
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003754 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003755
Bram Moolenaarac92e252019-08-03 21:58:38 +02003756 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003757 len = get_name_len(arg, &alias, evaluate, TRUE);
3758 if (alias != NULL)
3759 name = alias;
3760
3761 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003762 {
3763 if (verbose)
3764 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003765 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003766 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003767 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003768 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003769 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003770 if (**arg != '(')
3771 {
3772 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003774 ret = FAIL;
3775 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003776 else if (VIM_ISWHITE((*arg)[-1]))
3777 {
3778 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003779 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003780 ret = FAIL;
3781 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003782 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003783 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003784 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003785 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003786
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003787 // Clear the funcref afterwards, so that deleting it while
3788 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003789 if (evaluate)
3790 clear_tv(&base);
3791
Bram Moolenaarac92e252019-08-03 21:58:38 +02003792 return ret;
3793}
3794
3795/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003796 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3797 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003798 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3799 */
3800 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003801eval_index(
3802 char_u **arg,
3803 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003804 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003805 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003806{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003807 int evaluate = evalarg != NULL
3808 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003809 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003810 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003811 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003812 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003813 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003814 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003815
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003816 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3817 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003818
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003819 init_tv(&var1);
3820 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003821 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003822 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003823 /*
3824 * dict.name
3825 */
3826 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003827 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003828 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003829 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003830 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003831 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003832 }
3833 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003834 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003835 /*
3836 * something[idx]
3837 *
3838 * Get the (first) variable from inside the [].
3839 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003840 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003841 if (**arg == ':')
3842 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003843 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003844 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003845 else if (vim9 && **arg == ':')
3846 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003847 semsg(_(e_white_space_required_before_and_after_str_at_str),
3848 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003849 clear_tv(&var1);
3850 return FAIL;
3851 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003852 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003853 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003854 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003855 clear_tv(&var1);
3856 return FAIL;
3857 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003858
3859 /*
3860 * Get the second variable from inside the [:].
3861 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003862 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003863 if (**arg == ':')
3864 {
3865 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003866 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003867 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003868 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003869 semsg(_(e_white_space_required_before_and_after_str_at_str),
3870 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003871 if (!empty1)
3872 clear_tv(&var1);
3873 return FAIL;
3874 }
3875 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003876 if (**arg == ']')
3877 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003878 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003880 if (!empty1)
3881 clear_tv(&var1);
3882 return FAIL;
3883 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003884 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003886 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003887 if (!empty1)
3888 clear_tv(&var1);
3889 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003890 return FAIL;
3891 }
3892 }
3893
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003894 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003895 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003896 if (**arg != ']')
3897 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003898 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003899 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003900 clear_tv(&var1);
3901 if (range)
3902 clear_tv(&var2);
3903 return FAIL;
3904 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003905 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003906 }
3907
3908 if (evaluate)
3909 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003910 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003911 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003912 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003913
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003914 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003915 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003916 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003917 clear_tv(&var2);
3918 return res;
3919 }
3920 return OK;
3921}
3922
3923/*
3924 * Check if "rettv" can have an [index] or [sli:ce]
3925 */
3926 int
3927check_can_index(typval_T *rettv, int evaluate, int verbose)
3928{
3929 switch (rettv->v_type)
3930 {
3931 case VAR_FUNC:
3932 case VAR_PARTIAL:
3933 if (verbose)
3934 emsg(_("E695: Cannot index a Funcref"));
3935 return FAIL;
3936 case VAR_FLOAT:
3937#ifdef FEAT_FLOAT
3938 if (verbose)
3939 emsg(_(e_float_as_string));
3940 return FAIL;
3941#endif
3942 case VAR_BOOL:
3943 case VAR_SPECIAL:
3944 case VAR_JOB:
3945 case VAR_CHANNEL:
3946 if (verbose)
3947 emsg(_(e_cannot_index_special_variable));
3948 return FAIL;
3949 case VAR_UNKNOWN:
3950 case VAR_ANY:
3951 case VAR_VOID:
3952 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003953 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003954 emsg(_(e_cannot_index_special_variable));
3955 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003956 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003957 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003958
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003959 case VAR_STRING:
3960 case VAR_LIST:
3961 case VAR_DICT:
3962 case VAR_BLOB:
3963 break;
3964 case VAR_NUMBER:
3965 if (in_vim9script())
3966 emsg(_(e_cannot_index_number));
3967 break;
3968 }
3969 return OK;
3970}
3971
3972/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01003973 * slice() function
3974 */
3975 void
3976f_slice(typval_T *argvars, typval_T *rettv)
3977{
3978 if (check_can_index(argvars, TRUE, FALSE) == OK)
3979 {
3980 copy_tv(argvars, rettv);
3981 eval_index_inner(rettv, TRUE, argvars + 1,
3982 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
3983 TRUE, NULL, 0, FALSE);
3984 }
3985}
3986
3987/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003988 * Apply index or range to "rettv".
3989 * "var1" is the first index, NULL for [:expr].
3990 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01003991 * "exclusive" is TRUE for slice(): second index is exclusive, use character
3992 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003993 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3994 */
3995 int
3996eval_index_inner(
3997 typval_T *rettv,
3998 int is_range,
3999 typval_T *var1,
4000 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004001 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004002 char_u *key,
4003 int keylen,
4004 int verbose)
4005{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004006 varnumber_T n1, n2 = 0;
4007 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004008
4009 n1 = 0;
4010 if (var1 != NULL && rettv->v_type != VAR_DICT)
4011 n1 = tv_get_number(var1);
4012
4013 if (is_range)
4014 {
4015 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004016 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004017 if (verbose)
4018 emsg(_(e_cannot_slice_dictionary));
4019 return FAIL;
4020 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004021 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004022 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004023 else
4024 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004025 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004026
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004027 switch (rettv->v_type)
4028 {
4029 case VAR_UNKNOWN:
4030 case VAR_ANY:
4031 case VAR_VOID:
4032 case VAR_FUNC:
4033 case VAR_PARTIAL:
4034 case VAR_FLOAT:
4035 case VAR_BOOL:
4036 case VAR_SPECIAL:
4037 case VAR_JOB:
4038 case VAR_CHANNEL:
4039 break; // not evaluating, skipping over subscript
4040
4041 case VAR_NUMBER:
4042 case VAR_STRING:
4043 {
4044 char_u *s = tv_get_string(rettv);
4045
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004046 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004047 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004048 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004049 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004050 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004051 else
4052 s = char_from_string(s, n1);
4053 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004054 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004055 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004056 // The resulting variable is a substring. If the indexes
4057 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004058 if (n1 < 0)
4059 {
4060 n1 = len + n1;
4061 if (n1 < 0)
4062 n1 = 0;
4063 }
4064 if (n2 < 0)
4065 n2 = len + n2;
4066 else if (n2 >= len)
4067 n2 = len;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004068 if (exclusive)
4069 --n2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004070 if (n1 >= len || n2 < 0 || n1 > n2)
4071 s = NULL;
4072 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004073 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004074 }
4075 else
4076 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004077 // The resulting variable is a string of a single
4078 // character. If the index is too big or negative the
4079 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004080 if (n1 >= len || n1 < 0)
4081 s = NULL;
4082 else
4083 s = vim_strnsave(s + n1, 1);
4084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 clear_tv(rettv);
4086 rettv->v_type = VAR_STRING;
4087 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004088 }
4089 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004090
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004091 case VAR_BLOB:
4092 len = blob_len(rettv->vval.v_blob);
4093 if (is_range)
4094 {
4095 // The resulting variable is a sub-blob. If the indexes
4096 // are out of range the result is empty.
4097 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004098 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004099 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004100 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004101 n1 = 0;
4102 }
4103 if (n2 < 0)
4104 n2 = len + n2;
4105 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004106 n2 = len - (exclusive ? 0 : 1);
4107 if (exclusive)
4108 --n2;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004109 if (n1 >= len || n2 < 0 || n1 > n2)
4110 {
4111 clear_tv(rettv);
4112 rettv->v_type = VAR_BLOB;
4113 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004114 }
4115 else
4116 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004117 blob_T *blob = blob_alloc();
4118 long i;
4119
4120 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004121 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004122 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004123 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004124 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004125 return FAIL;
4126 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004127 blob->bv_ga.ga_len = n2 - n1 + 1;
4128 for (i = n1; i <= n2; i++)
4129 blob_set(blob, i - n1,
4130 blob_get(rettv->vval.v_blob, i));
4131
4132 clear_tv(rettv);
4133 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004134 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004135 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004136 }
4137 else
4138 {
4139 // The resulting variable is a byte value.
4140 // If the index is too big or negative that is an error.
4141 if (n1 < 0)
4142 n1 = len + n1;
4143 if (n1 < len && n1 >= 0)
4144 {
4145 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004146
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004147 clear_tv(rettv);
4148 rettv->v_type = VAR_NUMBER;
4149 rettv->vval.v_number = v;
4150 }
4151 else
4152 semsg(_(e_blobidx), n1);
4153 }
4154 break;
4155
4156 case VAR_LIST:
4157 if (var1 == NULL)
4158 n1 = 0;
4159 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004160 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004161 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004162 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004163 return FAIL;
4164 break;
4165
4166 case VAR_DICT:
4167 {
4168 dictitem_T *item;
4169 typval_T tmp;
4170
4171 if (key == NULL)
4172 {
4173 key = tv_get_string_chk(var1);
4174 if (key == NULL)
4175 return FAIL;
4176 }
4177
4178 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4179
4180 if (item == NULL && verbose)
4181 semsg(_(e_dictkey), key);
4182 if (item == NULL)
4183 return FAIL;
4184
4185 copy_tv(&item->di_tv, &tmp);
4186 clear_tv(rettv);
4187 *rettv = tmp;
4188 }
4189 break;
4190 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004191 return OK;
4192}
4193
4194/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004195 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004196 */
4197 char_u *
4198partial_name(partial_T *pt)
4199{
4200 if (pt->pt_name != NULL)
4201 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004202 if (pt->pt_func != NULL)
4203 return pt->pt_func->uf_name;
4204 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004205}
4206
Bram Moolenaarddecc252016-04-06 22:59:37 +02004207 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004208partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004209{
4210 int i;
4211
4212 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004213 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004214 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004215 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004216 if (pt->pt_name != NULL)
4217 {
4218 func_unref(pt->pt_name);
4219 vim_free(pt->pt_name);
4220 }
4221 else
4222 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004223
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004224 // Decrease the reference count for the context of a closure. If down
4225 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004226 if (pt->pt_funcstack != NULL)
4227 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004228 --pt->pt_funcstack->fs_refcount;
4229 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004230 }
4231
Bram Moolenaarddecc252016-04-06 22:59:37 +02004232 vim_free(pt);
4233}
4234
4235/*
4236 * Unreference a closure: decrement the reference count and free it when it
4237 * becomes zero.
4238 */
4239 void
4240partial_unref(partial_T *pt)
4241{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004242 if (pt != NULL)
4243 {
4244 if (--pt->pt_refcount <= 0)
4245 partial_free(pt);
4246
4247 // If the reference count goes down to one, the funcstack may be the
4248 // only reference and can be freed if no other partials reference it.
4249 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4250 funcstack_check_refcount(pt->pt_funcstack);
4251 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004252}
4253
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004254/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004255 * Return the next (unique) copy ID.
4256 * Used for serializing nested structures.
4257 */
4258 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004259get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004260{
4261 current_copyID += COPYID_INC;
4262 return current_copyID;
4263}
4264
4265/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004266 * Garbage collection for lists and dictionaries.
4267 *
4268 * We use reference counts to be able to free most items right away when they
4269 * are no longer used. But for composite items it's possible that it becomes
4270 * unused while the reference count is > 0: When there is a recursive
4271 * reference. Example:
4272 * :let l = [1, 2, 3]
4273 * :let d = {9: l}
4274 * :let l[1] = d
4275 *
4276 * Since this is quite unusual we handle this with garbage collection: every
4277 * once in a while find out which lists and dicts are not referenced from any
4278 * variable.
4279 *
4280 * Here is a good reference text about garbage collection (refers to Python
4281 * but it applies to all reference-counting mechanisms):
4282 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004283 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004284
4285/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004286 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004287 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004288 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004289 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004290 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004291garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004292{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004293 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004294 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004295 buf_T *buf;
4296 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004297 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004298 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004299
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004300 if (!testing)
4301 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004302 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004303 want_garbage_collect = FALSE;
4304 may_garbage_collect = FALSE;
4305 garbage_collect_at_exit = FALSE;
4306 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004307
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004308 // The execution stack can grow big, limit the size.
4309 if (exestack.ga_maxlen - exestack.ga_len > 500)
4310 {
4311 size_t new_len;
4312 char_u *pp;
4313 int n;
4314
4315 // Keep 150% of the current size, with a minimum of the growth size.
4316 n = exestack.ga_len / 2;
4317 if (n < exestack.ga_growsize)
4318 n = exestack.ga_growsize;
4319
4320 // Don't make it bigger though.
4321 if (exestack.ga_len + n < exestack.ga_maxlen)
4322 {
4323 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4324 pp = vim_realloc(exestack.ga_data, new_len);
4325 if (pp == NULL)
4326 return FAIL;
4327 exestack.ga_maxlen = exestack.ga_len + n;
4328 exestack.ga_data = pp;
4329 }
4330 }
4331
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004332 // We advance by two because we add one for items referenced through
4333 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004334 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004335
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004336 /*
4337 * 1. Go through all accessible variables and mark all lists and dicts
4338 * with copyID.
4339 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004341 // Don't free variables in the previous_funccal list unless they are only
4342 // referenced through previous_funccal. This must be first, because if
4343 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004344 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004345
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004346 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004347 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004348
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004349 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004350 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004351 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4352 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004353
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004354 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004355 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004356 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4357 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004358 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004359 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4360 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004361#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004362 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004363 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4364 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004365 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004366 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004367 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4368 NULL, NULL);
4369#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004370
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004371 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004372 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004373 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4374 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004375 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004376 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004378 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004379 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004380
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004381 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004382 abort = abort || set_ref_in_functions(copyID);
4383
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004384 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004385 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004386
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004387 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004388 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004389
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004390 // callbacks in buffers
4391 abort = abort || set_ref_in_buffers(copyID);
4392
Bram Moolenaar1dced572012-04-05 16:54:08 +02004393#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004394 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004395#endif
4396
Bram Moolenaardb913952012-06-29 12:54:53 +02004397#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004398 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004399#endif
4400
4401#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004402 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004403#endif
4404
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004405#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004406 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004407 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004408#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004409#ifdef FEAT_NETBEANS_INTG
4410 abort = abort || set_ref_in_nb_channel(copyID);
4411#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004412
Bram Moolenaare3188e22016-05-31 21:13:04 +02004413#ifdef FEAT_TIMERS
4414 abort = abort || set_ref_in_timer(copyID);
4415#endif
4416
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004417#ifdef FEAT_QUICKFIX
4418 abort = abort || set_ref_in_quickfix(copyID);
4419#endif
4420
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004421#ifdef FEAT_TERMINAL
4422 abort = abort || set_ref_in_term(copyID);
4423#endif
4424
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004425#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004426 abort = abort || set_ref_in_popups(copyID);
4427#endif
4428
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004429 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004430 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004431 /*
4432 * 2. Free lists and dictionaries that are not referenced.
4433 */
4434 did_free = free_unref_items(copyID);
4435
4436 /*
4437 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004438 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004439 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004440 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004441 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004442 else if (p_verbose > 0)
4443 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004444 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004445 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004446
4447 return did_free;
4448}
4449
4450/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004451 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004452 */
4453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004454free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004455{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004456 int did_free = FALSE;
4457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004458 // Let all "free" functions know that we are here. This means no
4459 // dictionaries, lists, channels or jobs are to be freed, because we will
4460 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004461 in_free_unref_items = TRUE;
4462
4463 /*
4464 * PASS 1: free the contents of the items. We don't free the items
4465 * themselves yet, so that it is possible to decrement refcount counters
4466 */
4467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004468 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004469 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004470
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004471 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004472 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004473
4474#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004475 // Go through the list of jobs and free items without the copyID. This
4476 // must happen before doing channels, because jobs refer to channels, but
4477 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004478 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004480 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004481 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4482#endif
4483
4484 /*
4485 * PASS 2: free the items themselves.
4486 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004487 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004488 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004489
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004490#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004491 // Go through the list of jobs and free items without the copyID. This
4492 // must happen before doing channels, because jobs refer to channels, but
4493 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004494 free_unused_jobs(copyID, COPYID_MASK);
4495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004496 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004497 free_unused_channels(copyID, COPYID_MASK);
4498#endif
4499
4500 in_free_unref_items = FALSE;
4501
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004502 return did_free;
4503}
4504
4505/*
4506 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004507 * "list_stack" is used to add lists to be marked. Can be NULL.
4508 *
4509 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004510 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004511 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004512set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004513{
4514 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004515 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004516 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004517 hashtab_T *cur_ht;
4518 ht_stack_T *ht_stack = NULL;
4519 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004520
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004521 cur_ht = ht;
4522 for (;;)
4523 {
4524 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004525 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004526 // Mark each item in the hashtab. If the item contains a hashtab
4527 // it is added to ht_stack, if it contains a list it is added to
4528 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004529 todo = (int)cur_ht->ht_used;
4530 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4531 if (!HASHITEM_EMPTY(hi))
4532 {
4533 --todo;
4534 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4535 &ht_stack, list_stack);
4536 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004537 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004538
4539 if (ht_stack == NULL)
4540 break;
4541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004542 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004543 cur_ht = ht_stack->ht;
4544 tempitem = ht_stack;
4545 ht_stack = ht_stack->prev;
4546 free(tempitem);
4547 }
4548
4549 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004550}
4551
4552/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004553 * Mark a dict and its items with "copyID".
4554 * Returns TRUE if setting references failed somehow.
4555 */
4556 int
4557set_ref_in_dict(dict_T *d, int copyID)
4558{
4559 if (d != NULL && d->dv_copyID != copyID)
4560 {
4561 d->dv_copyID = copyID;
4562 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4563 }
4564 return FALSE;
4565}
4566
4567/*
4568 * Mark a list and its items with "copyID".
4569 * Returns TRUE if setting references failed somehow.
4570 */
4571 int
4572set_ref_in_list(list_T *ll, int copyID)
4573{
4574 if (ll != NULL && ll->lv_copyID != copyID)
4575 {
4576 ll->lv_copyID = copyID;
4577 return set_ref_in_list_items(ll, copyID, NULL);
4578 }
4579 return FALSE;
4580}
4581
4582/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004583 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004584 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4585 *
4586 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004587 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004588 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004589set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004590{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004591 listitem_T *li;
4592 int abort = FALSE;
4593 list_T *cur_l;
4594 list_stack_T *list_stack = NULL;
4595 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004596
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004597 cur_l = l;
4598 for (;;)
4599 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004600 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004601 // Mark each item in the list. If the item contains a hashtab
4602 // it is added to ht_stack, if it contains a list it is added to
4603 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004604 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4605 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4606 ht_stack, &list_stack);
4607 if (list_stack == NULL)
4608 break;
4609
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004610 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004611 cur_l = list_stack->list;
4612 tempitem = list_stack;
4613 list_stack = list_stack->prev;
4614 free(tempitem);
4615 }
4616
4617 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004618}
4619
4620/*
4621 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004622 * "list_stack" is used to add lists to be marked. Can be NULL.
4623 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4624 *
4625 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004626 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004627 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004628set_ref_in_item(
4629 typval_T *tv,
4630 int copyID,
4631 ht_stack_T **ht_stack,
4632 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004633{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004634 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004635
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004636 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004637 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004638 dict_T *dd = tv->vval.v_dict;
4639
Bram Moolenaara03f2332016-02-06 18:09:59 +01004640 if (dd != NULL && dd->dv_copyID != copyID)
4641 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004642 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004643 dd->dv_copyID = copyID;
4644 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004645 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004646 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4647 }
4648 else
4649 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004650 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4651
Bram Moolenaara03f2332016-02-06 18:09:59 +01004652 if (newitem == NULL)
4653 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004654 else
4655 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004656 newitem->ht = &dd->dv_hashtab;
4657 newitem->prev = *ht_stack;
4658 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004659 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004660 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004661 }
4662 }
4663 else if (tv->v_type == VAR_LIST)
4664 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004665 list_T *ll = tv->vval.v_list;
4666
Bram Moolenaara03f2332016-02-06 18:09:59 +01004667 if (ll != NULL && ll->lv_copyID != copyID)
4668 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004669 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004670 ll->lv_copyID = copyID;
4671 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004672 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004673 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004674 }
4675 else
4676 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004677 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4678
Bram Moolenaara03f2332016-02-06 18:09:59 +01004679 if (newitem == NULL)
4680 abort = TRUE;
4681 else
4682 {
4683 newitem->list = ll;
4684 newitem->prev = *list_stack;
4685 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004686 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004687 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004688 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004689 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004690 else if (tv->v_type == VAR_FUNC)
4691 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004692 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004693 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004694 else if (tv->v_type == VAR_PARTIAL)
4695 {
4696 partial_T *pt = tv->vval.v_partial;
4697 int i;
4698
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004699 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004700 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004701 // Didn't see this partial yet.
4702 pt->pt_copyID = copyID;
4703
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004704 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004705
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004706 if (pt->pt_dict != NULL)
4707 {
4708 typval_T dtv;
4709
4710 dtv.v_type = VAR_DICT;
4711 dtv.vval.v_dict = pt->pt_dict;
4712 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4713 }
4714
4715 for (i = 0; i < pt->pt_argc; ++i)
4716 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4717 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004718 if (pt->pt_funcstack != NULL)
4719 {
4720 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4721
4722 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4723 abort = abort || set_ref_in_item(stack + i, copyID,
4724 ht_stack, list_stack);
4725 }
4726
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004727 }
4728 }
4729#ifdef FEAT_JOB_CHANNEL
4730 else if (tv->v_type == VAR_JOB)
4731 {
4732 job_T *job = tv->vval.v_job;
4733 typval_T dtv;
4734
4735 if (job != NULL && job->jv_copyID != copyID)
4736 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004737 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004738 if (job->jv_channel != NULL)
4739 {
4740 dtv.v_type = VAR_CHANNEL;
4741 dtv.vval.v_channel = job->jv_channel;
4742 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4743 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004744 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004745 {
4746 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004747 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004748 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4749 }
4750 }
4751 }
4752 else if (tv->v_type == VAR_CHANNEL)
4753 {
4754 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004755 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004756 typval_T dtv;
4757 jsonq_T *jq;
4758 cbq_T *cq;
4759
4760 if (ch != NULL && ch->ch_copyID != copyID)
4761 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004762 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004763 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004764 {
4765 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4766 jq = jq->jq_next)
4767 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4768 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4769 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004770 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004771 {
4772 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004773 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004774 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4775 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004776 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004777 {
4778 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004779 dtv.vval.v_partial =
4780 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004781 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4782 }
4783 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004784 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004785 {
4786 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004787 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004788 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4789 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004790 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004791 {
4792 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004793 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004794 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4795 }
4796 }
4797 }
4798#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004799 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004800}
4801
Bram Moolenaar8c711452005-01-14 21:53:12 +00004802/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004803 * Return a string with the string representation of a variable.
4804 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004805 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004806 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004807 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004808 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004809 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004810 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4811 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004812 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004813 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004814 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004815echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004816 typval_T *tv,
4817 char_u **tofree,
4818 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004819 int copyID,
4820 int echo_style,
4821 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004822 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004824 static int recurse = 0;
4825 char_u *r = NULL;
4826
Bram Moolenaar33570922005-01-25 22:26:29 +00004827 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004828 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004829 if (!did_echo_string_emsg)
4830 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004831 // Only give this message once for a recursive call to avoid
4832 // flooding the user with errors. And stop iterating over lists
4833 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004834 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004835 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004837 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004838 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004839 }
4840 ++recurse;
4841
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004842 switch (tv->v_type)
4843 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004844 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004845 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004846 {
4847 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004848 r = tv->vval.v_string;
4849 if (r == NULL)
4850 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004851 }
4852 else
4853 {
4854 *tofree = string_quote(tv->vval.v_string, FALSE);
4855 r = *tofree;
4856 }
4857 break;
4858
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004859 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004860 if (echo_style)
4861 {
4862 *tofree = NULL;
4863 r = tv->vval.v_string;
4864 }
4865 else
4866 {
4867 *tofree = string_quote(tv->vval.v_string, TRUE);
4868 r = *tofree;
4869 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004870 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004871
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004872 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004873 {
4874 partial_T *pt = tv->vval.v_partial;
4875 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004876 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004877 garray_T ga;
4878 int i;
4879 char_u *tf;
4880
4881 ga_init2(&ga, 1, 100);
4882 ga_concat(&ga, (char_u *)"function(");
4883 if (fname != NULL)
4884 {
4885 ga_concat(&ga, fname);
4886 vim_free(fname);
4887 }
4888 if (pt != NULL && pt->pt_argc > 0)
4889 {
4890 ga_concat(&ga, (char_u *)", [");
4891 for (i = 0; i < pt->pt_argc; ++i)
4892 {
4893 if (i > 0)
4894 ga_concat(&ga, (char_u *)", ");
4895 ga_concat(&ga,
4896 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4897 vim_free(tf);
4898 }
4899 ga_concat(&ga, (char_u *)"]");
4900 }
4901 if (pt != NULL && pt->pt_dict != NULL)
4902 {
4903 typval_T dtv;
4904
4905 ga_concat(&ga, (char_u *)", ");
4906 dtv.v_type = VAR_DICT;
4907 dtv.vval.v_dict = pt->pt_dict;
4908 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4909 vim_free(tf);
4910 }
4911 ga_concat(&ga, (char_u *)")");
4912
4913 *tofree = ga.ga_data;
4914 r = *tofree;
4915 break;
4916 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004917
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004918 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004919 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004920 break;
4921
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004922 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004923 if (tv->vval.v_list == NULL)
4924 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004925 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004926 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004927 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004928 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004929 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4930 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004931 {
4932 *tofree = NULL;
4933 r = (char_u *)"[...]";
4934 }
4935 else
4936 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004937 int old_copyID = tv->vval.v_list->lv_copyID;
4938
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004939 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004940 *tofree = list2string(tv, copyID, restore_copyID);
4941 if (restore_copyID)
4942 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004943 r = *tofree;
4944 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004945 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004946
Bram Moolenaar8c711452005-01-14 21:53:12 +00004947 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004948 if (tv->vval.v_dict == NULL)
4949 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004950 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004951 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004952 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004953 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004954 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4955 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004956 {
4957 *tofree = NULL;
4958 r = (char_u *)"{...}";
4959 }
4960 else
4961 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004962 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004963
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004964 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004965 *tofree = dict2string(tv, copyID, restore_copyID);
4966 if (restore_copyID)
4967 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004968 r = *tofree;
4969 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004970 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004971
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004972 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004973 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004974 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004976 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004977 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004978 break;
4979
Bram Moolenaar835dc632016-02-07 14:27:38 +01004980 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004981 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004982 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004983 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004984 if (composite_val)
4985 {
4986 *tofree = string_quote(r, FALSE);
4987 r = *tofree;
4988 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004989 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004990
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004992#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 *tofree = NULL;
4994 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4995 r = numbuf;
4996 break;
4997#endif
4998
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004999 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005000 case VAR_SPECIAL:
5001 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005002 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005003 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005004 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005005
Bram Moolenaar8502c702014-06-17 12:51:16 +02005006 if (--recurse == 0)
5007 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005008 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005009}
5010
5011/*
5012 * Return a string with the string representation of a variable.
5013 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5014 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005015 * Does not put quotes around strings, as ":echo" displays values.
5016 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5017 * May return NULL.
5018 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005019 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005020echo_string(
5021 typval_T *tv,
5022 char_u **tofree,
5023 char_u *numbuf,
5024 int copyID)
5025{
5026 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5027}
5028
5029/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005030 * Return string "str" in ' quotes, doubling ' characters.
5031 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005033 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005034 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005035string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005036{
Bram Moolenaar33570922005-01-25 22:26:29 +00005037 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005038 char_u *p, *r, *s;
5039
Bram Moolenaar33570922005-01-25 22:26:29 +00005040 len = (function ? 13 : 3);
5041 if (str != NULL)
5042 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005043 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005044 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005045 if (*p == '\'')
5046 ++len;
5047 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005048 s = r = alloc(len);
5049 if (r != NULL)
5050 {
5051 if (function)
5052 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005054 r += 10;
5055 }
5056 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005058 if (str != NULL)
5059 for (p = str; *p != NUL; )
5060 {
5061 if (*p == '\'')
5062 *r++ = '\'';
5063 MB_COPY_CHAR(p, r);
5064 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005066 if (function)
5067 *r++ = ')';
5068 *r++ = NUL;
5069 }
5070 return s;
5071}
5072
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005073#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005074/*
5075 * Convert the string "text" to a floating point number.
5076 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5077 * this always uses a decimal point.
5078 * Returns the length of the text that was consumed.
5079 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005080 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005081string2float(
5082 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005083 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005084{
5085 char *s = (char *)text;
5086 float_T f;
5087
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005088 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005089 if (STRNICMP(text, "inf", 3) == 0)
5090 {
5091 *value = INFINITY;
5092 return 3;
5093 }
5094 if (STRNICMP(text, "-inf", 3) == 0)
5095 {
5096 *value = -INFINITY;
5097 return 4;
5098 }
5099 if (STRNICMP(text, "nan", 3) == 0)
5100 {
5101 *value = NAN;
5102 return 3;
5103 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005104 f = strtod(s, &s);
5105 *value = f;
5106 return (int)((char_u *)s - text);
5107}
5108#endif
5109
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005111 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5112 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005113 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005114 */
5115 int
5116buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5117{
5118 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005119 char_u *t;
5120 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005121
5122 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5123 return -1;
5124
5125 if (lnum > buf->b_ml.ml_line_count)
5126 lnum = buf->b_ml.ml_line_count;
5127
5128 str = ml_get_buf(buf, lnum, FALSE);
5129 if (str == NULL)
5130 return -1;
5131
5132 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005133 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005134
Bram Moolenaar91458462021-01-13 20:08:38 +01005135 // count the number of characters
5136 t = str;
5137 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5138 t += mb_ptr2len(t);
5139
5140 // In insert mode, when the cursor is at the end of a non-empty line,
5141 // byteidx points to the NUL character immediately past the end of the
5142 // string. In this case, add one to the character count.
5143 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5144 count++;
5145
5146 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005147}
5148
5149/*
5150 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005151 * byte index. Works only for loaded buffers. Returns -1 on failure.
5152 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005153 */
5154 int
5155buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5156{
5157 char_u *str;
5158 char_u *t;
5159
5160 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5161 return -1;
5162
5163 if (lnum > buf->b_ml.ml_line_count)
5164 lnum = buf->b_ml.ml_line_count;
5165
5166 str = ml_get_buf(buf, lnum, FALSE);
5167 if (str == NULL)
5168 return -1;
5169
5170 // Convert the character offset to a byte offset
5171 t = str;
5172 while (*t != NUL && --charidx > 0)
5173 t += mb_ptr2len(t);
5174
Bram Moolenaar91458462021-01-13 20:08:38 +01005175 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005176}
5177
5178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005180 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005182 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005183var2fpos(
5184 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005185 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005186 int *fnum, // set to fnum for '0, 'A, etc.
5187 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005189 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005191 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005193 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005194 if (varp->v_type == VAR_LIST)
5195 {
5196 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005197 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005198 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005199 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005200
5201 l = varp->vval.v_list;
5202 if (l == NULL)
5203 return NULL;
5204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005205 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005206 pos.lnum = list_find_nr(l, 0L, &error);
5207 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005208 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005209 if (charcol)
5210 len = (long)mb_charlen(ml_get(pos.lnum));
5211 else
5212 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005213
Bram Moolenaarec65d772020-08-20 22:29:12 +02005214 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005215 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005216 li = list_find(l, 1L);
5217 if (li != NULL && li->li_tv.v_type == VAR_STRING
5218 && li->li_tv.vval.v_string != NULL
5219 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005220 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005221 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005222 }
5223 else
5224 {
5225 pos.col = list_find_nr(l, 1L, &error);
5226 if (error)
5227 return NULL;
5228 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005229
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005230 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005231 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005233 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005234
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005235 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005236 pos.coladd = list_find_nr(l, 2L, &error);
5237 if (error)
5238 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005239
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005240 return &pos;
5241 }
5242
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005243 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005244 if (name == NULL)
5245 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005246 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005247 {
5248 pos = curwin->w_cursor;
5249 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005250 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005251 return &pos;
5252 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005253 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005254 {
5255 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005256 pos = VIsual;
5257 else
5258 pos = curwin->w_cursor;
5259 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005260 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005261 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005262 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005263 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005265 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5267 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005268 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005269 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 return pp;
5271 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005272
Bram Moolenaara5525202006-03-02 22:52:09 +00005273 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005274
Bram Moolenaar477933c2007-07-17 14:32:23 +00005275 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005276 {
5277 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005278 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005279 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005280 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005281 // In silent Ex mode topline is zero, but that's not a valid line
5282 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005283 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005284 return &pos;
5285 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005286 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005287 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005288 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005289 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005290 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005291 return &pos;
5292 }
5293 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005294 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005296 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 {
5298 pos.lnum = curbuf->b_ml.ml_line_count;
5299 pos.col = 0;
5300 }
5301 else
5302 {
5303 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005304 if (charcol)
5305 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5306 else
5307 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309 return &pos;
5310 }
5311 return NULL;
5312}
5313
5314/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005315 * Convert list in "arg" into a position and optional file number.
5316 * When "fnump" is NULL there is no file number, only 3 items.
5317 * Note that the column is passed on as-is, the caller may want to decrement
5318 * it to use 1 for the first column.
5319 * Return FAIL when conversion is not possible, doesn't check the position for
5320 * validity.
5321 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005322 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005323list2fpos(
5324 typval_T *arg,
5325 pos_T *posp,
5326 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005327 colnr_T *curswantp,
5328 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005329{
5330 list_T *l = arg->vval.v_list;
5331 long i = 0;
5332 long n;
5333
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005334 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5335 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005336 if (arg->v_type != VAR_LIST
5337 || l == NULL
5338 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005339 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005340 return FAIL;
5341
5342 if (fnump != NULL)
5343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005344 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005345 if (n < 0)
5346 return FAIL;
5347 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005348 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005349 *fnump = n;
5350 }
5351
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005352 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005353 if (n < 0)
5354 return FAIL;
5355 posp->lnum = n;
5356
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005357 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005358 if (n < 0)
5359 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005360 // If character position is specified, then convert to byte position
5361 if (charcol)
5362 {
5363 buf_T *buf;
5364
5365 // Get the text for the specified line in a loaded buffer
5366 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5367 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5368 return FAIL;
5369
Bram Moolenaar91458462021-01-13 20:08:38 +01005370 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005371 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005372 posp->col = n;
5373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005374 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005375 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005376 posp->coladd = 0;
5377 else
5378 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005379
Bram Moolenaar493c1782014-05-28 14:34:46 +02005380 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005381 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005382
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005383 return OK;
5384}
5385
5386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 * Get the length of an environment variable name.
5388 * Advance "arg" to the first character after the name.
5389 * Return 0 for error.
5390 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005391 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005392get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
5394 char_u *p;
5395 int len;
5396
5397 for (p = *arg; vim_isIDc(*p); ++p)
5398 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005399 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 return 0;
5401
5402 len = (int)(p - *arg);
5403 *arg = p;
5404 return len;
5405}
5406
5407/*
5408 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005409 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 * Return 0 if something is wrong.
5411 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005413get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 char_u *p;
5416 int len;
5417
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005418 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005420 {
5421 if (*p == ':')
5422 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005423 // "s:" is start of "s:var", but "n:" is not and can be used in
5424 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005425 len = (int)(p - *arg);
5426 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5427 || len > 1)
5428 break;
5429 }
5430 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005431 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 return 0;
5433
5434 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005435 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436
5437 return len;
5438}
5439
5440/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005441 * Get the length of the name of a variable or function.
5442 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005444 * Return -1 if curly braces expansion failed.
5445 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 * If the name contains 'magic' {}'s, expand them and return the
5447 * expanded name in an allocated string via 'alias' - caller must free.
5448 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005450get_name_len(
5451 char_u **arg,
5452 char_u **alias,
5453 int evaluate,
5454 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 char_u *p;
5458 char_u *expr_start;
5459 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005461 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5464 && (*arg)[2] == (int)KE_SNR)
5465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005466 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 *arg += 3;
5468 return get_id_len(arg) + 3;
5469 }
5470 len = eval_fname_script(*arg);
5471 if (len > 0)
5472 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005473 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 *arg += len;
5475 }
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005478 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005480 p = find_name_end(*arg, &expr_start, &expr_end,
5481 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 if (expr_start != NULL)
5483 {
5484 char_u *temp_string;
5485
5486 if (!evaluate)
5487 {
5488 len += (int)(p - *arg);
5489 *arg = skipwhite(p);
5490 return len;
5491 }
5492
5493 /*
5494 * Include any <SID> etc in the expanded string:
5495 * Thus the -len here.
5496 */
5497 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5498 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005499 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 *alias = temp_string;
5501 *arg = skipwhite(p);
5502 return (int)STRLEN(temp_string);
5503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005506 // Only give an error when there is something, otherwise it will be
5507 // reported at a higher level.
5508 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005509 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510
5511 return len;
5512}
5513
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514/*
5515 * Find the end of a variable or function name, taking care of magic braces.
5516 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5517 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005518 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 * Return a pointer to just after the name. Equal to "arg" if there is no
5520 * valid name.
5521 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005522 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005523find_name_end(
5524 char_u *arg,
5525 char_u **expr_start,
5526 char_u **expr_end,
5527 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 int mb_nest = 0;
5530 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005532 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005533 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 *expr_start = NULL;
5538 *expr_end = NULL;
5539 }
5540
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005541 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005542 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5543 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005544 return arg;
5545
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 for (p = arg; *p != NUL
5547 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005548 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005549 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005550 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005552 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005554 if (*p == '\'')
5555 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005556 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005557 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005558 ;
5559 if (*p == NUL)
5560 break;
5561 }
5562 else if (*p == '"')
5563 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005564 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005565 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005566 if (*p == '\\' && p[1] != NUL)
5567 ++p;
5568 if (*p == NUL)
5569 break;
5570 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005571 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5572 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005573 // "s:" is start of "s:var", but "n:" is not and can be used in
5574 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005575 len = (int)(p - arg);
5576 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005577 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005578 break;
5579 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005580
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 if (*p == '[')
5584 ++br_nest;
5585 else if (*p == ']')
5586 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005588
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005589 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005591 if (*p == '{')
5592 {
5593 mb_nest++;
5594 if (expr_start != NULL && *expr_start == NULL)
5595 *expr_start = p;
5596 }
5597 else if (*p == '}')
5598 {
5599 mb_nest--;
5600 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5601 *expr_end = p;
5602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 }
5605
5606 return p;
5607}
5608
5609/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005610 * Expands out the 'magic' {}'s in a variable/function name.
5611 * Note that this can call itself recursively, to deal with
5612 * constructs like foo{bar}{baz}{bam}
5613 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5614 * "in_start" ^
5615 * "expr_start" ^
5616 * "expr_end" ^
5617 * "in_end" ^
5618 *
5619 * Returns a new allocated string, which the caller must free.
5620 * Returns NULL for failure.
5621 */
5622 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005623make_expanded_name(
5624 char_u *in_start,
5625 char_u *expr_start,
5626 char_u *expr_end,
5627 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005628{
5629 char_u c1;
5630 char_u *retval = NULL;
5631 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005632
5633 if (expr_end == NULL || in_end == NULL)
5634 return NULL;
5635 *expr_start = NUL;
5636 *expr_end = NUL;
5637 c1 = *in_end;
5638 *in_end = NUL;
5639
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005640 temp_result = eval_to_string(expr_start + 1, FALSE);
5641 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005642 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005643 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5644 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005645 if (retval != NULL)
5646 {
5647 STRCPY(retval, in_start);
5648 STRCAT(retval, temp_result);
5649 STRCAT(retval, expr_end + 1);
5650 }
5651 }
5652 vim_free(temp_result);
5653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005654 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005655 *expr_start = '{';
5656 *expr_end = '}';
5657
5658 if (retval != NULL)
5659 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005660 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005661 if (expr_start != NULL)
5662 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005663 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005664 temp_result = make_expanded_name(retval, expr_start,
5665 expr_end, temp_result);
5666 vim_free(retval);
5667 retval = temp_result;
5668 }
5669 }
5670
5671 return retval;
5672}
5673
5674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005676 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005678 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005679eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005681 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005682}
5683
5684/*
5685 * Return TRUE if character "c" can be used as the first character in a
5686 * variable or function name (excluding '{' and '}').
5687 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005688 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005689eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005690{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005691 return ASCII_ISALPHA(c) || c == '_';
5692}
5693
5694/*
5695 * Return TRUE if character "c" can be used as the first character of a
5696 * dictionary key.
5697 */
5698 int
5699eval_isdictc(int c)
5700{
5701 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702}
5703
5704/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005705 * Handle:
5706 * - expr[expr], expr[expr:expr] subscript
5707 * - ".name" lookup
5708 * - function call with Funcref variable: func(expr)
5709 * - method call: var->method()
5710 *
5711 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005712 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005714handle_subscript(
5715 char_u **arg,
5716 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005717 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005718 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005719{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005720 int evaluate = evalarg != NULL
5721 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005722 int ret = OK;
5723 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005724 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005725 int getnext;
5726 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005727
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005728 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005729 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005730 // When at the end of the line and ".name" or "->{" or "->X" follows in
5731 // the next line then consume the line break.
5732 p = eval_next_non_blank(*arg, evalarg, &getnext);
5733 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005734 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005735 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005736 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005737 {
5738 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005739 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005740 check_white = FALSE;
5741 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005742
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005743 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5744 || rettv->v_type == VAR_PARTIAL))
5745 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005746 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005747 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5748 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005749
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005750 // Stop the expression evaluation when immediately aborting on
5751 // error, or when an interrupt occurred or an exception was thrown
5752 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005753 if (aborting())
5754 {
5755 if (ret == OK)
5756 clear_tv(rettv);
5757 ret = FAIL;
5758 }
5759 dict_unref(selfdict);
5760 selfdict = NULL;
5761 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005762 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005763 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005764 *arg = skipwhite(p + 2);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005765 if (ret == OK)
5766 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005767 if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005768 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005769 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005770 else
5771 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005772 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005773 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005774 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005775 // "." is ".name" lookup when we found a dict or when evaluating and
5776 // scriptversion is at least 2, where string concatenation is "..".
5777 else if (**arg == '['
5778 || (**arg == '.' && (rettv->v_type == VAR_DICT
5779 || (!evaluate
5780 && (*arg)[1] != '.'
5781 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005782 {
5783 dict_unref(selfdict);
5784 if (rettv->v_type == VAR_DICT)
5785 {
5786 selfdict = rettv->vval.v_dict;
5787 if (selfdict != NULL)
5788 ++selfdict->dv_refcount;
5789 }
5790 else
5791 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005792 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005793 {
5794 clear_tv(rettv);
5795 ret = FAIL;
5796 }
5797 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005798 else
5799 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005800 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005801
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005802 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5803 // Don't do this when "Func" is already a partial that was bound
5804 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005805 if (selfdict != NULL
5806 && (rettv->v_type == VAR_FUNC
5807 || (rettv->v_type == VAR_PARTIAL
5808 && (rettv->vval.v_partial->pt_auto
5809 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005810 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005811
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005812 dict_unref(selfdict);
5813 return ret;
5814}
5815
5816/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005817 * Make a copy of an item.
5818 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005819 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5820 * reference to an already copied list/dict can be used.
5821 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005822 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005823 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005824item_copy(
5825 typval_T *from,
5826 typval_T *to,
5827 int deep,
5828 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005829{
5830 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005831 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005832
Bram Moolenaar33570922005-01-25 22:26:29 +00005833 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005835 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005836 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005837 }
5838 ++recurse;
5839
5840 switch (from->v_type)
5841 {
5842 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005843 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005844 case VAR_STRING:
5845 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005846 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005847 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005848 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005849 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005850 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005851 copy_tv(from, to);
5852 break;
5853 case VAR_LIST:
5854 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005855 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005856 if (from->vval.v_list == NULL)
5857 to->vval.v_list = NULL;
5858 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5859 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005860 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005861 to->vval.v_list = from->vval.v_list->lv_copylist;
5862 ++to->vval.v_list->lv_refcount;
5863 }
5864 else
5865 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5866 if (to->vval.v_list == NULL)
5867 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005868 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005869 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005871 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005872 case VAR_DICT:
5873 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005874 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005875 if (from->vval.v_dict == NULL)
5876 to->vval.v_dict = NULL;
5877 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5878 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005879 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005880 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5881 ++to->vval.v_dict->dv_refcount;
5882 }
5883 else
5884 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5885 if (to->vval.v_dict == NULL)
5886 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005887 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005888 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005889 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005890 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005891 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005892 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005893 }
5894 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005895 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005896}
5897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898 void
5899echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5900{
5901 char_u *tofree;
5902 char_u numbuf[NUMBUFLEN];
5903 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5904
5905 if (*atstart)
5906 {
5907 *atstart = FALSE;
5908 // Call msg_start() after eval1(), evaluating the expression
5909 // may cause a message to appear.
5910 if (with_space)
5911 {
5912 // Mark the saved text as finishing the line, so that what
5913 // follows is displayed on a new line when scrolling back
5914 // at the more prompt.
5915 msg_sb_eol();
5916 msg_start();
5917 }
5918 }
5919 else if (with_space)
5920 msg_puts_attr(" ", echo_attr);
5921
5922 if (p != NULL)
5923 for ( ; *p != NUL && !got_int; ++p)
5924 {
5925 if (*p == '\n' || *p == '\r' || *p == TAB)
5926 {
5927 if (*p != TAB && *needclr)
5928 {
5929 // remove any text still there from the command
5930 msg_clr_eos();
5931 *needclr = FALSE;
5932 }
5933 msg_putchar_attr(*p, echo_attr);
5934 }
5935 else
5936 {
5937 if (has_mbyte)
5938 {
5939 int i = (*mb_ptr2len)(p);
5940
5941 (void)msg_outtrans_len_attr(p, i, echo_attr);
5942 p += i - 1;
5943 }
5944 else
5945 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5946 }
5947 }
5948 vim_free(tofree);
5949}
5950
Bram Moolenaare9a41262005-01-15 22:18:47 +00005951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 * ":echo expr1 ..." print each argument separated with a space, add a
5953 * newline at the end.
5954 * ":echon expr1 ..." print each argument plain.
5955 */
5956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005957ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958{
5959 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005960 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 char_u *p;
5962 int needclr = TRUE;
5963 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005964 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005965 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005966 evalarg_T evalarg;
5967
Bram Moolenaare707c882020-07-01 13:07:37 +02005968 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969
5970 if (eap->skip)
5971 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005972 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005974 // If eval1() causes an error message the text from the command may
5975 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005976 need_clr_eos = needclr;
5977
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005979 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {
5981 /*
5982 * Report the invalid expression unless the expression evaluation
5983 * has been cancelled due to an aborting error, an interrupt, or an
5984 * exception.
5985 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005986 if (!aborting() && did_emsg == did_emsg_before
5987 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005988 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005989 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 break;
5991 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005992 need_clr_eos = FALSE;
5993
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005995 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005996
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005997 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 arg = skipwhite(arg);
5999 }
6000 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006001 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002
6003 if (eap->skip)
6004 --emsg_skip;
6005 else
6006 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006007 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 if (needclr)
6009 msg_clr_eos();
6010 if (eap->cmdidx == CMD_echo)
6011 msg_end();
6012 }
6013}
6014
6015/*
6016 * ":echohl {name}".
6017 */
6018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006019ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006021 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022}
6023
6024/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006025 * Returns the :echo attribute
6026 */
6027 int
6028get_echo_attr(void)
6029{
6030 return echo_attr;
6031}
6032
6033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 * ":execute expr1 ..." execute the result of an expression.
6035 * ":echomsg expr1 ..." Print a message
6036 * ":echoerr expr1 ..." Print an error
6037 * Each gets spaces around each argument and a newline at the end for
6038 * echo commands
6039 */
6040 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006041ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042{
6043 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006044 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 int ret = OK;
6046 char_u *p;
6047 garray_T ga;
6048 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049
6050 ga_init2(&ga, 1, 80);
6051
6052 if (eap->skip)
6053 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006054 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006056 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006057 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
6060 if (!eap->skip)
6061 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006062 char_u buf[NUMBUFLEN];
6063
6064 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006065 {
6066 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6067 {
6068 emsg(_(e_inval_string));
6069 p = NULL;
6070 }
6071 else
6072 p = tv_get_string_buf(&rettv, buf);
6073 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006074 else
6075 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006076 if (p == NULL)
6077 {
6078 clear_tv(&rettv);
6079 ret = FAIL;
6080 break;
6081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 len = (int)STRLEN(p);
6083 if (ga_grow(&ga, len + 2) == FAIL)
6084 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006085 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 ret = FAIL;
6087 break;
6088 }
6089 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 ga.ga_len += len;
6093 }
6094
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006095 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 arg = skipwhite(arg);
6097 }
6098
6099 if (ret != FAIL && ga.ga_data != NULL)
6100 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006101 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006103 // Mark the already saved text as finishing the line, so that what
6104 // follows is displayed on a new line when scrolling back at the
6105 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006106 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006107 }
6108
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006110 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006111 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006112 out_flush();
6113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 else if (eap->cmdidx == CMD_echoerr)
6115 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006116 int save_did_emsg = did_emsg;
6117
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006118 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006119 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 if (!force_abort)
6121 did_emsg = save_did_emsg;
6122 }
6123 else if (eap->cmdidx == CMD_execute)
6124 do_cmdline((char_u *)ga.ga_data,
6125 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6126 }
6127
6128 ga_clear(&ga);
6129
6130 if (eap->skip)
6131 --emsg_skip;
6132
6133 eap->nextcmd = check_nextcmd(arg);
6134}
6135
6136/*
6137 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6138 * "arg" points to the "&" or '+' when called, to "option" when returning.
6139 * Returns NULL when no option name found. Otherwise pointer to the char
6140 * after the option name.
6141 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006142 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006143find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 char_u *p = *arg;
6146
6147 ++p;
6148 if (*p == 'g' && p[1] == ':')
6149 {
6150 *opt_flags = OPT_GLOBAL;
6151 p += 2;
6152 }
6153 else if (*p == 'l' && p[1] == ':')
6154 {
6155 *opt_flags = OPT_LOCAL;
6156 p += 2;
6157 }
6158 else
6159 *opt_flags = 0;
6160
6161 if (!ASCII_ISALPHA(*p))
6162 return NULL;
6163 *arg = p;
6164
6165 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006166 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 else
6168 while (ASCII_ISALPHA(*p))
6169 ++p;
6170 return p;
6171}
6172
6173/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006174 * Display script name where an item was last set.
6175 * Should only be invoked when 'verbose' is non-zero.
6176 */
6177 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006178last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006179{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006180 char_u *p;
6181
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006182 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006183 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006184 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006185 if (p != NULL)
6186 {
6187 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006188 msg_puts(_("\n\tLast set from "));
6189 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006190 if (script_ctx.sc_lnum > 0)
6191 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006192 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006193 msg_outnum((long)script_ctx.sc_lnum);
6194 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006195 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006196 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006197 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006198 }
6199}
6200
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006201#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
6203/*
6204 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006205 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 * "flags" can be "g" to do a global substitute.
6207 * Returns an allocated string, NULL for error.
6208 */
6209 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006210do_string_sub(
6211 char_u *str,
6212 char_u *pat,
6213 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006214 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006215 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217 int sublen;
6218 regmatch_T regmatch;
6219 int i;
6220 int do_all;
6221 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006222 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 garray_T ga;
6224 char_u *ret;
6225 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006226 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006228 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006230 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
6232 ga_init2(&ga, 1, 200);
6233
6234 do_all = (flags[0] == 'g');
6235
6236 regmatch.rm_ic = p_ic;
6237 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6238 if (regmatch.regprog != NULL)
6239 {
6240 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006241 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6243 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006244 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006245 if (regmatch.startp[0] == regmatch.endp[0])
6246 {
6247 if (zero_width == regmatch.startp[0])
6248 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006249 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006250 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006251 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6252 (size_t)i);
6253 ga.ga_len += i;
6254 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006255 continue;
6256 }
6257 zero_width = regmatch.startp[0];
6258 }
6259
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 /*
6261 * Get some space for a temporary buffer to do the substitution
6262 * into. It will contain:
6263 * - The text up to where the match is.
6264 * - The substituted text.
6265 * - The text after the match.
6266 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006267 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006268 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6270 {
6271 ga_clear(&ga);
6272 break;
6273 }
6274
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006275 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 i = (int)(regmatch.startp[0] - tail);
6277 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006278 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006279 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 + ga.ga_len + i, TRUE, TRUE, FALSE);
6281 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006282 tail = regmatch.endp[0];
6283 if (*tail == NUL)
6284 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (!do_all)
6286 break;
6287 }
6288
6289 if (ga.ga_data != NULL)
6290 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6291
Bram Moolenaar473de612013-06-08 18:19:48 +02006292 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
6294
6295 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6296 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006297 if (p_cpo == empty_option)
6298 p_cpo = save_cpo;
6299 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006300 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006301 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006302 // If it's still empty it was changed and restored, need to restore in
6303 // the complicated way.
6304 if (*p_cpo == NUL)
6305 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006306 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308
6309 return ret;
6310}