blob: 444c0faf388d1c27d52503f87ac99bfc424eb368 [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 Moolenaarf785aa12021-02-11 21:19:34 +01001302 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1303 char_u *op,
1304 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305{
1306 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001307 listitem_T *ri;
1308 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001309
1310 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001312 cc = *endp;
1313 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001314 if (lp->ll_blob != NULL)
1315 {
1316 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001317
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001318 if (op != NULL && *op != '=')
1319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001320 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001321 return;
1322 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001323 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001324 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001325
1326 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1327 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001328 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001329
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001330 if (lp->ll_empty2)
1331 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1332
1333 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001335 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001336 return;
1337 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001338 if (lp->ll_empty2)
1339 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001340
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001341 ir = 0;
1342 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1343 blob_set(lp->ll_blob, il,
1344 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001345 }
1346 else
1347 {
1348 val = (int)tv_get_number_chk(rettv, &error);
1349 if (!error)
1350 {
1351 garray_T *gap = &lp->ll_blob->bv_ga;
1352
1353 // Allow for appending a byte. Setting a byte beyond
1354 // the end is an error otherwise.
1355 if (lp->ll_n1 < gap->ga_len
1356 || (lp->ll_n1 == gap->ga_len
1357 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1358 {
1359 blob_set(lp->ll_blob, lp->ll_n1, val);
1360 if (lp->ll_n1 == gap->ga_len)
1361 ++gap->ga_len;
1362 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001363 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001364 }
1365 }
1366 }
1367 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001369 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001370
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001371 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001372 {
1373 emsg(_(e_cannot_mod));
1374 *endp = cc;
1375 return;
1376 }
1377
Bram Moolenaarff697e62019-02-12 22:28:33 +01001378 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001379 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001380 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001381 &tv, &di, TRUE, FALSE) == OK)
1382 {
1383 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001384 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1385 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001386 && tv_op(&tv, rettv, op) == OK)
1387 set_var(lp->ll_name, &tv, FALSE);
1388 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001389 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001391 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001392 {
1393 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001394 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001395 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001396 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1397 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001398 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001399 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001400 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001401 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001402 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001403 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001404 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001405 else if (lp->ll_range)
1406 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001407 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001408 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001409
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001410 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001411 {
1412 emsg(_("E996: Cannot lock a range"));
1413 return;
1414 }
1415
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001416 /*
1417 * Check whether any of the list items is locked
1418 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001419 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001420 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001421 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001422 return;
1423 ri = ri->li_next;
1424 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1425 break;
1426 ll_li = ll_li->li_next;
1427 ++ll_n1;
1428 }
1429
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 /*
1431 * Assign the List values to the list items.
1432 */
1433 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001434 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001435 if (op != NULL && *op != '=')
1436 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1437 else
1438 {
1439 clear_tv(&lp->ll_li->li_tv);
1440 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1441 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001442 ri = ri->li_next;
1443 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1444 break;
1445 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001446 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001447 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001448 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001449 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001450 ri = NULL;
1451 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001452 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001453 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001454 lp->ll_li = lp->ll_li->li_next;
1455 ++lp->ll_n1;
1456 }
1457 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001458 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001459 else if (lp->ll_empty2
1460 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001462 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001463 }
1464 else
1465 {
1466 /*
1467 * Assign to a List or Dictionary item.
1468 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001469 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001470 {
1471 emsg(_("E996: Cannot lock a list or dict"));
1472 return;
1473 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001474
1475 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001476 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001477 return;
1478
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001479 if (lp->ll_newkey != NULL)
1480 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001481 if (op != NULL && *op != '=')
1482 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001483 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 return;
1485 }
1486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001487 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001488 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001489 if (di == NULL)
1490 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001491 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1492 {
1493 vim_free(di);
1494 return;
1495 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001496 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001497 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001498 else if (op != NULL && *op != '=')
1499 {
1500 tv_op(lp->ll_tv, rettv, op);
1501 return;
1502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001503 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001504 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001505
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001506 /*
1507 * Assign the value to the variable or list item.
1508 */
1509 if (copy)
1510 copy_tv(rettv, lp->ll_tv);
1511 else
1512 {
1513 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001514 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001515 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 }
1517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001518}
1519
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001520/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001521 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1522 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001523 * Returns OK or FAIL.
1524 */
1525 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001526tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001527{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001528 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 char_u numbuf[NUMBUFLEN];
1530 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001531 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001533 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001534 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001535 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001536 {
1537 switch (tv1->v_type)
1538 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001539 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001540 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001542 case VAR_DICT:
1543 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001544 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001545 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001546 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001547 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001548 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001549 break;
1550
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001551 case VAR_BLOB:
1552 if (*op != '+' || tv2->v_type != VAR_BLOB)
1553 break;
1554 // BLOB += BLOB
1555 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1556 {
1557 blob_T *b1 = tv1->vval.v_blob;
1558 blob_T *b2 = tv2->vval.v_blob;
1559 int i, len = blob_len(b2);
1560 for (i = 0; i < len; i++)
1561 ga_append(&b1->bv_ga, blob_get(b2, i));
1562 }
1563 return OK;
1564
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001565 case VAR_LIST:
1566 if (*op != '+' || tv2->v_type != VAR_LIST)
1567 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001568 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001569 if (tv2->vval.v_list != NULL)
1570 {
1571 if (tv1->vval.v_list == NULL)
1572 {
1573 tv1->vval.v_list = tv2->vval.v_list;
1574 ++tv1->vval.v_list->lv_refcount;
1575 }
1576 else
1577 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1578 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001579 return OK;
1580
1581 case VAR_NUMBER:
1582 case VAR_STRING:
1583 if (tv2->v_type == VAR_LIST)
1584 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001585 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001587 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001588 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589#ifdef FEAT_FLOAT
1590 if (tv2->v_type == VAR_FLOAT)
1591 {
1592 float_T f = n;
1593
Bram Moolenaarff697e62019-02-12 22:28:33 +01001594 if (*op == '%')
1595 break;
1596 switch (*op)
1597 {
1598 case '+': f += tv2->vval.v_float; break;
1599 case '-': f -= tv2->vval.v_float; break;
1600 case '*': f *= tv2->vval.v_float; break;
1601 case '/': f /= tv2->vval.v_float; break;
1602 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001603 clear_tv(tv1);
1604 tv1->v_type = VAR_FLOAT;
1605 tv1->vval.v_float = f;
1606 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001608#endif
1609 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001610 switch (*op)
1611 {
1612 case '+': n += tv_get_number(tv2); break;
1613 case '-': n -= tv_get_number(tv2); break;
1614 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001615 case '/': n = num_divide(n, tv_get_number(tv2),
1616 &failed); break;
1617 case '%': n = num_modulus(n, tv_get_number(tv2),
1618 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001619 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001620 clear_tv(tv1);
1621 tv1->v_type = VAR_NUMBER;
1622 tv1->vval.v_number = n;
1623 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001624 }
1625 else
1626 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001627 if (tv2->v_type == VAR_FLOAT)
1628 break;
1629
Bram Moolenaarff697e62019-02-12 22:28:33 +01001630 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001631 s = tv_get_string(tv1);
1632 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001633 clear_tv(tv1);
1634 tv1->v_type = VAR_STRING;
1635 tv1->vval.v_string = s;
1636 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001637 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001639 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001640#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001641 {
1642 float_T f;
1643
Bram Moolenaarff697e62019-02-12 22:28:33 +01001644 if (*op == '%' || *op == '.'
1645 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001646 && tv2->v_type != VAR_NUMBER
1647 && tv2->v_type != VAR_STRING))
1648 break;
1649 if (tv2->v_type == VAR_FLOAT)
1650 f = tv2->vval.v_float;
1651 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001652 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001653 switch (*op)
1654 {
1655 case '+': tv1->vval.v_float += f; break;
1656 case '-': tv1->vval.v_float -= f; break;
1657 case '*': tv1->vval.v_float *= f; break;
1658 case '/': tv1->vval.v_float /= f; break;
1659 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001660 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001661#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001662 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001663 }
1664 }
1665
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001666 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001667 return FAIL;
1668}
1669
1670/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 * Evaluate the expression used in a ":for var in expr" command.
1672 * "arg" points to "var".
1673 * Set "*errp" to TRUE for an error, FALSE otherwise;
1674 * Return a pointer that holds the info. Null when there is an error.
1675 */
1676 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001677eval_for_line(
1678 char_u *arg,
1679 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001680 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001681 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682{
Bram Moolenaar33570922005-01-25 22:26:29 +00001683 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001685 typval_T tv;
1686 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001687 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001689 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001691 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 if (fi == NULL)
1693 return NULL;
1694
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001695 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 if (expr == NULL)
1697 return fi;
1698
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001699 expr = skipwhite_and_linebreak(expr, evalarg);
1700 if (expr[0] != 'i' || expr[1] != 'n'
1701 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 return fi;
1705 }
1706
1707 if (skip)
1708 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001709 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1710 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 {
1712 *errp = FALSE;
1713 if (!skip)
1714 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001715 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001716 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001717 l = tv.vval.v_list;
1718 if (l == NULL)
1719 {
1720 // a null list is like an empty list: do nothing
1721 clear_tv(&tv);
1722 }
1723 else
1724 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001726 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001728 // No need to increment the refcount, it's already set for
1729 // the list being used in "tv".
1730 fi->fi_list = l;
1731 list_add_watch(l, &fi->fi_lw);
1732 fi->fi_lw.lw_item = l->lv_first;
1733 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001734 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001735 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001736 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001737 fi->fi_bi = 0;
1738 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001739 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001740 typval_T btv;
1741
1742 // Make a copy, so that the iteration still works when the
1743 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001745 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001746 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001747 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001748 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 else
1750 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001751 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001752 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753 }
1754 }
1755 }
1756 if (skip)
1757 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001758 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759
1760 return fi;
1761}
1762
1763/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001764 * Used when looping over a :for line, skip the "in expr" part.
1765 */
1766 void
1767skip_for_lines(void *fi_void, evalarg_T *evalarg)
1768{
1769 forinfo_T *fi = (forinfo_T *)fi_void;
1770 int i;
1771
1772 for (i = 0; i < fi->fi_break_count; ++i)
1773 eval_next_line(evalarg);
1774}
1775
1776/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 * Use the first item in a ":for" list. Advance to the next.
1778 * Assign the values to the variable (list). "arg" points to the first one.
1779 * Return TRUE when a valid item was found, FALSE when at end of list or
1780 * something wrong.
1781 */
1782 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001783next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001785 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786 int result;
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001787 int flag = in_vim9script() ? ASSIGN_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001788 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001790 if (fi->fi_blob != NULL)
1791 {
1792 typval_T tv;
1793
1794 if (fi->fi_bi >= blob_len(fi->fi_blob))
1795 return FALSE;
1796 tv.v_type = VAR_NUMBER;
1797 tv.v_lock = VAR_FIXED;
1798 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1799 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001800 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001801 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001802 }
1803
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 item = fi->fi_lw.lw_item;
1805 if (item == NULL)
1806 result = FALSE;
1807 else
1808 {
1809 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001810 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001811 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812 }
1813 return result;
1814}
1815
1816/*
1817 * Free the structure used to store info used by ":for".
1818 */
1819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001820free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821{
Bram Moolenaar33570922005-01-25 22:26:29 +00001822 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001824 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001825 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001826 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001827 list_unref(fi->fi_list);
1828 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001829 if (fi != NULL && fi->fi_blob != NULL)
1830 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831 vim_free(fi);
1832}
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001835set_context_for_expression(
1836 expand_T *xp,
1837 char_u *arg,
1838 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001840 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001844 if (cmdidx == CMD_let || cmdidx == CMD_var
1845 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 {
1847 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001848 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001850 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001851 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 {
1853 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001854 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001855 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 break;
1857 }
1858 return;
1859 }
1860 }
1861 else
1862 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1863 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 while ((xp->xp_pattern = vim_strpbrk(arg,
1865 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1866 {
1867 c = *xp->xp_pattern;
1868 if (c == '&')
1869 {
1870 c = xp->xp_pattern[1];
1871 if (c == '&')
1872 {
1873 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001874 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001877 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001879 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1880 xp->xp_pattern += 2;
1881
1882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 }
1884 else if (c == '$')
1885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001886 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 xp->xp_context = EXPAND_ENV_VARS;
1888 }
1889 else if (c == '=')
1890 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001891 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 xp->xp_context = EXPAND_EXPRESSION;
1893 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001894 else if (c == '#'
1895 && xp->xp_context == EXPAND_EXPRESSION)
1896 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001897 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001898 break;
1899 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001900 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 && xp->xp_context == EXPAND_FUNCTIONS
1902 && vim_strchr(xp->xp_pattern, '(') == NULL)
1903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001904 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 break;
1906 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001907 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001909 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 {
1911 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1912 if (c == '\\' && xp->xp_pattern[1] != NUL)
1913 ++xp->xp_pattern;
1914 xp->xp_context = EXPAND_NOTHING;
1915 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001916 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001918 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1920 /* skip */ ;
1921 xp->xp_context = EXPAND_NOTHING;
1922 }
1923 else if (c == '|')
1924 {
1925 if (xp->xp_pattern[1] == '|')
1926 {
1927 ++xp->xp_pattern;
1928 xp->xp_context = EXPAND_EXPRESSION;
1929 }
1930 else
1931 xp->xp_context = EXPAND_COMMANDS;
1932 }
1933 else
1934 xp->xp_context = EXPAND_EXPRESSION;
1935 }
1936 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001937 // Doesn't look like something valid, expand as an expression
1938 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 arg = xp->xp_pattern;
1941 if (*arg != NUL)
1942 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1943 /* skip */ ;
1944 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001945
1946 // ":exe one two" completes "two"
1947 if ((cmdidx == CMD_execute
1948 || cmdidx == CMD_echo
1949 || cmdidx == CMD_echon
1950 || cmdidx == CMD_echomsg)
1951 && xp->xp_context == EXPAND_EXPRESSION)
1952 {
1953 for (;;)
1954 {
1955 char_u *n = skiptowhite(arg);
1956
1957 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1958 break;
1959 arg = skipwhite(n);
1960 }
1961 }
1962
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 xp->xp_pattern = arg;
1964}
1965
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001967 * Return TRUE if "pat" matches "text".
1968 * Does not use 'cpo' and always uses 'magic'.
1969 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001970 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001971pattern_match(char_u *pat, char_u *text, int ic)
1972{
1973 int matches = FALSE;
1974 char_u *save_cpo;
1975 regmatch_T regmatch;
1976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001977 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001978 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001979 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001980 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1981 if (regmatch.regprog != NULL)
1982 {
1983 regmatch.rm_ic = ic;
1984 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1985 vim_regfree(regmatch.regprog);
1986 }
1987 p_cpo = save_cpo;
1988 return matches;
1989}
1990
1991/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001992 * Handle a name followed by "(". Both for just "name(arg)" and for
1993 * "expr->name(arg)".
1994 * Returns OK or FAIL.
1995 */
1996 static int
1997eval_func(
1998 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001999 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002000 char_u *name,
2001 int name_len,
2002 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002003 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002004 typval_T *basetv) // "expr" for "expr->name(arg)"
2005{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002006 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002007 char_u *s = name;
2008 int len = name_len;
2009 partial_T *partial;
2010 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002011 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002012
2013 if (!evaluate)
2014 check_vars(s, len);
2015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002016 // If "s" is the name of a variable of type VAR_FUNC
2017 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002018 s = deref_func_name(s, &len, &partial,
2019 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002020
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002021 // Need to make a copy, in case evaluating the arguments makes
2022 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002023 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002024 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002025 ret = FAIL;
2026 else
2027 {
2028 funcexe_T funcexe;
2029
2030 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002031 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002032 funcexe.firstline = curwin->w_cursor.lnum;
2033 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002034 funcexe.evaluate = evaluate;
2035 funcexe.partial = partial;
2036 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002037 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002038 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002039 }
2040 vim_free(s);
2041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002042 // If evaluate is FALSE rettv->v_type was not set in
2043 // get_func_tv, but it's needed in handle_subscript() to parse
2044 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002045 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2046 {
2047 rettv->vval.v_string = NULL;
2048 rettv->v_type = VAR_FUNC;
2049 }
2050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002051 // Stop the expression evaluation when immediately
2052 // aborting on error, or when an interrupt occurred or
2053 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002054 if (evaluate && aborting())
2055 {
2056 if (ret == OK)
2057 clear_tv(rettv);
2058 ret = FAIL;
2059 }
2060 return ret;
2061}
2062
2063/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002064 * Get the next line source line without advancing. But do skip over comment
2065 * lines.
2066 */
2067 static char_u *
2068getline_peek_skip_comments(evalarg_T *evalarg)
2069{
2070 for (;;)
2071 {
2072 char_u *next = getline_peek(evalarg->eval_getline,
2073 evalarg->eval_cookie);
2074 char_u *p;
2075
2076 if (next == NULL)
2077 break;
2078 p = skipwhite(next);
2079 if (*p != NUL && !vim9_comment_start(p))
2080 return next;
2081 (void)eval_next_line(evalarg);
2082 }
2083 return NULL;
2084}
2085
2086/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002087 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2088 * comment) and there is a next line, return the next line (skipping blanks)
2089 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2091 * "arg" must point somewhere inside a line, not at the start.
2092 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002093 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002094eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2095{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002096 char_u *p = skipwhite(arg);
2097
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002098 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002099 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002100 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002101 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002102 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002103 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002104 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002105
2106 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002107 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002108 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002109 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002110
Bram Moolenaar9d489562020-07-30 20:08:50 +02002111 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002112 {
2113 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002114 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002115 }
2116 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002117 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002118}
2119
2120/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002121 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002122 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002123 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002124eval_next_line(evalarg_T *evalarg)
2125{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002126 garray_T *gap = &evalarg->eval_ga;
2127 char_u *line;
2128
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002129 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002130 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2131 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002132 else
2133 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002134 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002135 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2136 {
2137 // Going to concatenate the lines after parsing.
2138 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2139 ++gap->ga_len;
2140 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002141 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002142 {
2143 vim_free(evalarg->eval_tofree);
2144 evalarg->eval_tofree = line;
2145 }
2146 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002147}
2148
Bram Moolenaare6b53242020-07-01 17:28:33 +02002149/*
2150 * Call eval_next_non_blank() and get the next line if needed.
2151 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002152 char_u *
2153skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2154{
2155 int getnext;
2156 char_u *p = skipwhite(arg);
2157
Bram Moolenaar962d7212020-07-04 14:15:00 +02002158 if (evalarg == NULL)
2159 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002160 eval_next_non_blank(p, evalarg, &getnext);
2161 if (getnext)
2162 return eval_next_line(evalarg);
2163 return p;
2164}
2165
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002166/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002167 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002168 */
2169 void
2170clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2171{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002172 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002173 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002174 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002175 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002176 if (eap != NULL)
2177 {
2178 // We may need to keep the original command line, e.g. for
2179 // ":let" it has the variable names. But we may also need the
2180 // new one, "nextcmd" points into it. Keep both.
2181 vim_free(eap->cmdline_tofree);
2182 eap->cmdline_tofree = *eap->cmdlinep;
2183 *eap->cmdlinep = evalarg->eval_tofree;
2184 }
2185 else
2186 vim_free(evalarg->eval_tofree);
2187 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002188 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002189
2190 vim_free(evalarg->eval_tofree_lambda);
2191 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002192 }
2193}
2194
2195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2199 */
2200
2201/*
2202 * Handle zero level expression.
2203 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002205 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002206 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 * Return OK or FAIL.
2208 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002209 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210eval0(
2211 char_u *arg,
2212 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002213 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002214 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216 int ret;
2217 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002218 int did_emsg_before = did_emsg;
2219 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002220 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
2222 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002223 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002224 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002225
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002226 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
2228 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 /*
2231 * Report the invalid expression unless the expression evaluation has
2232 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002233 * exception, or we already gave a more specific error.
2234 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002236 if (!aborting()
2237 && did_emsg == did_emsg_before
2238 && called_emsg == called_emsg_before
2239 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002240 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002241
2242 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002243 // a next command to avoid more errors, unless "|" is following, which
2244 // could only be a command separator.
2245 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2246 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002249
2250 if (eap != NULL)
2251 eap->nextcmd = check_nextcmd(p);
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 return ret;
2254}
2255
2256/*
2257 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002258 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002259 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 *
2261 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002262 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002264 * Note: "rettv.v_lock" is not set.
2265 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 * Return OK or FAIL.
2267 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002268 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002269eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002271 char_u *p;
2272 int getnext;
2273
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002274 CLEAR_POINTER(rettv);
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 /*
2277 * Get the first variable.
2278 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002279 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 return FAIL;
2281
Bram Moolenaar793648f2020-06-26 21:28:25 +02002282 p = eval_next_non_blank(*arg, evalarg, &getnext);
2283 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002285 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002286 int result;
2287 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002288 evalarg_T *evalarg_used = evalarg;
2289 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002290 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002291 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002292 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002293
2294 if (evalarg == NULL)
2295 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002296 CLEAR_FIELD(local_evalarg);
2297 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002298 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002299 orig_flags = evalarg_used->eval_flags;
2300 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002301
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002302 if (getnext)
2303 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002304 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002305 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002306 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002307 {
2308 error_white_both(p, 1);
2309 clear_tv(rettv);
2310 return FAIL;
2311 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002312 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002313 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002316 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002318 int error = FALSE;
2319
Bram Moolenaar13106602020-10-04 16:06:05 +02002320 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002321 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002322 else if (vim9script)
2323 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002324 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002326 if (error || !op_falsy || !result)
2327 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002328 if (error)
2329 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331
2332 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002333 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002335 if (op_falsy)
2336 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002337 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002338 {
2339 error_white_both(p, 1);
2340 clear_tv(rettv);
2341 return FAIL;
2342 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002343 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002344 evalarg_used->eval_flags = (op_falsy ? !result : result)
2345 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2346 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002347 {
2348 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002350 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002351 if (!op_falsy || !result)
2352 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002354 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002356 /*
2357 * Check for the ":".
2358 */
2359 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2360 if (*p != ':')
2361 {
2362 emsg(_(e_missing_colon));
2363 if (evaluate && result)
2364 clear_tv(rettv);
2365 evalarg_used->eval_flags = orig_flags;
2366 return FAIL;
2367 }
2368 if (getnext)
2369 *arg = eval_next_line(evalarg_used);
2370 else
2371 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002372 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002373 {
2374 error_white_both(p, 1);
2375 clear_tv(rettv);
2376 evalarg_used->eval_flags = orig_flags;
2377 return FAIL;
2378 }
2379 *arg = p;
2380 }
2381
2382 /*
2383 * Get the third variable. Recursive!
2384 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002385 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002386 {
2387 error_white_both(p, 1);
2388 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002389 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002390 return FAIL;
2391 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002392 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2393 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002394 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002395 if (eval1(arg, &var2, evalarg_used) == FAIL)
2396 {
2397 if (evaluate && result)
2398 clear_tv(rettv);
2399 evalarg_used->eval_flags = orig_flags;
2400 return FAIL;
2401 }
2402 if (evaluate && !result)
2403 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002405
2406 if (evalarg == NULL)
2407 clear_evalarg(&local_evalarg, NULL);
2408 else
2409 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
2411
2412 return OK;
2413}
2414
2415/*
2416 * Handle first level expression:
2417 * expr2 || expr2 || expr2 logical OR
2418 *
2419 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002420 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 *
2422 * Return OK or FAIL.
2423 */
2424 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002425eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002427 char_u *p;
2428 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430 /*
2431 * Get the first variable.
2432 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002433 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 return FAIL;
2435
2436 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002437 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002439 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002440 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002442 evalarg_T *evalarg_used = evalarg;
2443 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002444 int evaluate;
2445 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002446 long result = FALSE;
2447 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002448 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002449 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002450
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002451 if (evalarg == NULL)
2452 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002453 CLEAR_FIELD(local_evalarg);
2454 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002455 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002456 orig_flags = evalarg_used->eval_flags;
2457 evaluate = orig_flags & EVAL_EVALUATE;
2458 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002459 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002460 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002461 result = tv_get_bool_chk(rettv, &error);
2462 else if (tv_get_number_chk(rettv, &error) != 0)
2463 result = TRUE;
2464 clear_tv(rettv);
2465 if (error)
2466 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 }
2468
2469 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002470 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002472 while (p[0] == '|' && p[1] == '|')
2473 {
2474 if (getnext)
2475 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002476 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002477 {
2478 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2479 {
2480 error_white_both(p, 2);
2481 clear_tv(rettv);
2482 return FAIL;
2483 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002484 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002485 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002486
2487 /*
2488 * Get the second variable.
2489 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002490 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2491 {
2492 error_white_both(p, 2);
2493 clear_tv(rettv);
2494 return FAIL;
2495 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002496 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2497 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002498 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002499 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002500 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002501
2502 /*
2503 * Compute the result.
2504 */
2505 if (evaluate && !result)
2506 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002507 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002508 result = tv_get_bool_chk(&var2, &error);
2509 else if (tv_get_number_chk(&var2, &error) != 0)
2510 result = TRUE;
2511 clear_tv(&var2);
2512 if (error)
2513 return FAIL;
2514 }
2515 if (evaluate)
2516 {
2517 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002518 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002519 rettv->v_type = VAR_BOOL;
2520 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002521 }
2522 else
2523 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002524 rettv->v_type = VAR_NUMBER;
2525 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002526 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002527 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002528
2529 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002531
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002532 if (evalarg == NULL)
2533 clear_evalarg(&local_evalarg, NULL);
2534 else
2535 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
2537
2538 return OK;
2539}
2540
2541/*
2542 * Handle second level expression:
2543 * expr3 && expr3 && expr3 logical AND
2544 *
2545 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002546 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 *
2548 * Return OK or FAIL.
2549 */
2550 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002551eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002553 char_u *p;
2554 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
2556 /*
2557 * Get the first variable.
2558 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002559 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 return FAIL;
2561
2562 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002563 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002565 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002566 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002568 evalarg_T *evalarg_used = evalarg;
2569 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002570 int orig_flags;
2571 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002572 long result = TRUE;
2573 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002574 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002575 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002576
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002577 if (evalarg == NULL)
2578 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002579 CLEAR_FIELD(local_evalarg);
2580 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002581 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 orig_flags = evalarg_used->eval_flags;
2583 evaluate = orig_flags & EVAL_EVALUATE;
2584 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002585 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002586 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002587 result = tv_get_bool_chk(rettv, &error);
2588 else if (tv_get_number_chk(rettv, &error) == 0)
2589 result = FALSE;
2590 clear_tv(rettv);
2591 if (error)
2592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594
2595 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598 while (p[0] == '&' && p[1] == '&')
2599 {
2600 if (getnext)
2601 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002602 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002603 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002604 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002605 {
2606 error_white_both(p, 2);
2607 clear_tv(rettv);
2608 return FAIL;
2609 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002610 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002611 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002612
2613 /*
2614 * Get the second variable.
2615 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002616 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2617 {
2618 error_white_both(p, 2);
2619 clear_tv(rettv);
2620 return FAIL;
2621 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2623 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002624 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002625 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002626 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002627 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002628
2629 /*
2630 * Compute the result.
2631 */
2632 if (evaluate && result)
2633 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002634 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002635 result = tv_get_bool_chk(&var2, &error);
2636 else if (tv_get_number_chk(&var2, &error) == 0)
2637 result = FALSE;
2638 clear_tv(&var2);
2639 if (error)
2640 return FAIL;
2641 }
2642 if (evaluate)
2643 {
2644 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002645 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002646 rettv->v_type = VAR_BOOL;
2647 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002648 }
2649 else
2650 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002651 rettv->v_type = VAR_NUMBER;
2652 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002653 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002654 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655
2656 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002658
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002659 if (evalarg == NULL)
2660 clear_evalarg(&local_evalarg, NULL);
2661 else
2662 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
2664
2665 return OK;
2666}
2667
2668/*
2669 * Handle third level expression:
2670 * var1 == var2
2671 * var1 =~ var2
2672 * var1 != var2
2673 * var1 !~ var2
2674 * var1 > var2
2675 * var1 >= var2
2676 * var1 < var2
2677 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002678 * var1 is var2
2679 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 *
2681 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002682 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 *
2684 * Return OK or FAIL.
2685 */
2686 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002687eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002690 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002691 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002693 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
2695 /*
2696 * Get the first variable.
2697 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002698 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 return FAIL;
2700
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002701 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002702 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
2704 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002705 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002707 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002709 typval_T var2;
2710 int ic;
2711 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002712 int evaluate = evalarg == NULL
2713 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002714
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002715 if (getnext)
2716 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002717 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2718 {
2719 error_white_both(p, len);
2720 clear_tv(rettv);
2721 return FAIL;
2722 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002723
Bram Moolenaar696ba232020-07-29 21:20:41 +02002724 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2725 {
2726 semsg(_(e_invexpr2), p);
2727 clear_tv(rettv);
2728 return FAIL;
2729 }
2730
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002731 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 if (p[len] == '?')
2733 {
2734 ic = TRUE;
2735 ++len;
2736 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002737 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 else if (p[len] == '#')
2739 {
2740 ic = FALSE;
2741 ++len;
2742 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002743 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002745 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 /*
2748 * Get the second variable.
2749 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002750 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2751 {
2752 error_white_both(p, 1);
2753 clear_tv(rettv);
2754 return FAIL;
2755 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002756 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002757 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002759 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 return FAIL;
2761 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002762 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002763 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002764 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002765
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002766 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002767 {
2768 ret = FAIL;
2769 clear_tv(rettv);
2770 }
2771 else
2772 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002773 clear_tv(&var2);
2774 return ret;
2775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777
2778 return OK;
2779}
2780
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002781/*
2782 * Make a copy of blob "tv1" and append blob "tv2".
2783 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 void
2785eval_addblob(typval_T *tv1, typval_T *tv2)
2786{
2787 blob_T *b1 = tv1->vval.v_blob;
2788 blob_T *b2 = tv2->vval.v_blob;
2789 blob_T *b = blob_alloc();
2790 int i;
2791
2792 if (b != NULL)
2793 {
2794 for (i = 0; i < blob_len(b1); i++)
2795 ga_append(&b->bv_ga, blob_get(b1, i));
2796 for (i = 0; i < blob_len(b2); i++)
2797 ga_append(&b->bv_ga, blob_get(b2, i));
2798
2799 clear_tv(tv1);
2800 rettv_blob_set(tv1, b);
2801 }
2802}
2803
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002804/*
2805 * Make a copy of list "tv1" and append list "tv2".
2806 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 int
2808eval_addlist(typval_T *tv1, typval_T *tv2)
2809{
2810 typval_T var3;
2811
2812 // concatenate Lists
2813 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2814 {
2815 clear_tv(tv1);
2816 clear_tv(tv2);
2817 return FAIL;
2818 }
2819 clear_tv(tv1);
2820 *tv1 = var3;
2821 return OK;
2822}
2823
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824/*
2825 * Handle fourth level expression:
2826 * + number addition
2827 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002828 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002829 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 *
2831 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002832 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 *
2834 * Return OK or FAIL.
2835 */
2836 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002837eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 /*
2840 * Get the first variable.
2841 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002842 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 return FAIL;
2844
2845 /*
2846 * Repeat computing, until no '+', '-' or '.' is following.
2847 */
2848 for (;;)
2849 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002850 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002851 int getnext;
2852 char_u *p;
2853 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002854 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002855 int concat;
2856 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002857 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002858
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002859 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002860 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002861 p = eval_next_non_blank(*arg, evalarg, &getnext);
2862 op = *p;
2863 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002864 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002866
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002867 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002868 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002869 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002870 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002871 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002872 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002873 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002874 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002875 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002876 clear_tv(rettv);
2877 return FAIL;
2878 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002879 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002880 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002881 if ((op != '+' || (rettv->v_type != VAR_LIST
2882 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002883#ifdef FEAT_FLOAT
2884 && (op == '.' || rettv->v_type != VAR_FLOAT)
2885#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002886 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002887 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002888 int error = FALSE;
2889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002890 // For "list + ...", an illegal use of the first operand as
2891 // a number cannot be determined before evaluating the 2nd
2892 // operand: if this is also a list, all is ok.
2893 // For "something . ...", "something - ..." or "non-list + ...",
2894 // we know that the first operand needs to be a string or number
2895 // without evaluating the 2nd operand. So check before to avoid
2896 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002897 if (op != '.')
2898 tv_get_number_chk(rettv, &error);
2899 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002900 {
2901 clear_tv(rettv);
2902 return FAIL;
2903 }
2904 }
2905
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 /*
2907 * Get the second variable.
2908 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002909 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002910 {
2911 error_white_both(p, oplen);
2912 clear_tv(rettv);
2913 return FAIL;
2914 }
2915 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002916 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002918 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 return FAIL;
2920 }
2921
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002922 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 {
2924 /*
2925 * Compute the result.
2926 */
2927 if (op == '.')
2928 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002929 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2930 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002931 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002932
Bram Moolenaar2e086612020-08-25 22:37:48 +02002933 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002934 || var2.v_type == VAR_CHANNEL
2935 || var2.v_type == VAR_JOB))
2936 emsg(_(e_inval_string));
2937#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002938 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002939 {
2940 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2941 var2.vval.v_float);
2942 s2 = buf2;
2943 }
2944#endif
2945 else
2946 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002947 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002948 {
2949 clear_tv(rettv);
2950 clear_tv(&var2);
2951 return FAIL;
2952 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002954 clear_tv(rettv);
2955 rettv->v_type = VAR_STRING;
2956 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002958 else if (op == '+' && rettv->v_type == VAR_BLOB
2959 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002961 else if (op == '+' && rettv->v_type == VAR_LIST
2962 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002963 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002965 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 else
2968 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002969 int error = FALSE;
2970 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002971#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002972 float_T f1 = 0, f2 = 0;
2973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002974 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002976 f1 = rettv->vval.v_float;
2977 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002978 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002979 else
2980#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002981 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002982 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002983 if (error)
2984 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002985 // This can only happen for "list + non-list". For
2986 // "non-list + ..." or "something - ...", we returned
2987 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002988 clear_tv(rettv);
2989 return FAIL;
2990 }
2991#ifdef FEAT_FLOAT
2992 if (var2.v_type == VAR_FLOAT)
2993 f1 = n1;
2994#endif
2995 }
2996#ifdef FEAT_FLOAT
2997 if (var2.v_type == VAR_FLOAT)
2998 {
2999 f2 = var2.vval.v_float;
3000 n2 = 0;
3001 }
3002 else
3003#endif
3004 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003005 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003006 if (error)
3007 {
3008 clear_tv(rettv);
3009 clear_tv(&var2);
3010 return FAIL;
3011 }
3012#ifdef FEAT_FLOAT
3013 if (rettv->v_type == VAR_FLOAT)
3014 f2 = n2;
3015#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003016 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003018
3019#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003020 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003021 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3022 {
3023 if (op == '+')
3024 f1 = f1 + f2;
3025 else
3026 f1 = f1 - f2;
3027 rettv->v_type = VAR_FLOAT;
3028 rettv->vval.v_float = f1;
3029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003031#endif
3032 {
3033 if (op == '+')
3034 n1 = n1 + n2;
3035 else
3036 n1 = n1 - n2;
3037 rettv->v_type = VAR_NUMBER;
3038 rettv->vval.v_number = n1;
3039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043 }
3044 return OK;
3045}
3046
3047/*
3048 * Handle fifth level expression:
3049 * * number multiplication
3050 * / number division
3051 * % number modulo
3052 *
3053 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003054 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 *
3056 * Return OK or FAIL.
3057 */
3058 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003059eval6(
3060 char_u **arg,
3061 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003062 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003063 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003066 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069 /*
3070 * Get the first variable.
3071 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003072 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 return FAIL;
3074
3075 /*
3076 * Repeat computing, until no '*', '/' or '%' is following.
3077 */
3078 for (;;)
3079 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003080 int evaluate;
3081 int getnext;
3082 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003083 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003084 int op;
3085 varnumber_T n1, n2;
3086#ifdef FEAT_FLOAT
3087 float_T f1, f2;
3088#endif
3089 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003090
Bram Moolenaar9d489562020-07-30 20:08:50 +02003091 p = eval_next_non_blank(*arg, evalarg, &getnext);
3092 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003093 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003095
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003096 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003097 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003098 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003099 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003100 {
3101 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3102 {
3103 error_white_both(p, 1);
3104 clear_tv(rettv);
3105 return FAIL;
3106 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003107 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003110#ifdef FEAT_FLOAT
3111 f1 = 0;
3112 f2 = 0;
3113#endif
3114 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003115 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117#ifdef FEAT_FLOAT
3118 if (rettv->v_type == VAR_FLOAT)
3119 {
3120 f1 = rettv->vval.v_float;
3121 use_float = TRUE;
3122 n1 = 0;
3123 }
3124 else
3125#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003126 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003127 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003128 if (error)
3129 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 }
3131 else
3132 n1 = 0;
3133
3134 /*
3135 * Get the second variable.
3136 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003137 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3138 {
3139 error_white_both(p, 1);
3140 clear_tv(rettv);
3141 return FAIL;
3142 }
3143 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003144 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 return FAIL;
3146
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003147 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149#ifdef FEAT_FLOAT
3150 if (var2.v_type == VAR_FLOAT)
3151 {
3152 if (!use_float)
3153 {
3154 f1 = n1;
3155 use_float = TRUE;
3156 }
3157 f2 = var2.vval.v_float;
3158 n2 = 0;
3159 }
3160 else
3161#endif
3162 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003163 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164 clear_tv(&var2);
3165 if (error)
3166 return FAIL;
3167#ifdef FEAT_FLOAT
3168 if (use_float)
3169 f2 = n2;
3170#endif
3171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
3173 /*
3174 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003175 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177#ifdef FEAT_FLOAT
3178 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180 if (op == '*')
3181 f1 = f1 * f2;
3182 else if (op == '/')
3183 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003184# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003185 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003186 if (f2 == 0.0)
3187 {
3188 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003189 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003190 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003191 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003192 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003193 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003194 }
3195 else
3196 f1 = f1 / f2;
3197# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003198 // We rely on the floating point library to handle divide
3199 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003200 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003201# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003204 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003206 return FAIL;
3207 }
3208 rettv->v_type = VAR_FLOAT;
3209 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
3211 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003214 int failed = FALSE;
3215
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003216 if (op == '*')
3217 n1 = n1 * n2;
3218 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003219 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003221 n1 = num_modulus(n1, n2, &failed);
3222 if (failed)
3223 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003224
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003225 rettv->v_type = VAR_NUMBER;
3226 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229 }
3230
3231 return OK;
3232}
3233
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003234 int
3235eval_leader(char_u **arg, int vim9)
3236{
3237 char_u *s = *arg;
3238 char_u *p = *arg;
3239
3240 while (*p == '!' || *p == '-' || *p == '+')
3241 {
3242 char_u *n = skipwhite(p + 1);
3243
3244 // ++, --, -+ and +- are not accepted in Vim9 script
3245 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3246 {
3247 semsg(_(e_invexpr2), s);
3248 return FAIL;
3249 }
3250 p = n;
3251 }
3252 *arg = p;
3253 return OK;
3254}
3255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256/*
3257 * Handle sixth level expression:
3258 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003259 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003260 * "string" string constant
3261 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 * &option-name option value
3263 * @r register contents
3264 * identifier variable value
3265 * function() function call
3266 * $VAR environment variable
3267 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003268 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003270 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003271 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 *
3273 * Also handle:
3274 * ! in front logical NOT
3275 * - in front unary minus
3276 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003277 * trailing [] subscript in String or List
3278 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003279 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 *
3281 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003282 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 *
3284 * Return OK or FAIL.
3285 */
3286 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003287eval7(
3288 char_u **arg,
3289 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003290 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003293 int evaluate = evalarg != NULL
3294 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 int len;
3296 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 char_u *start_leader, *end_leader;
3298 int ret = OK;
3299 char_u *alias;
3300
3301 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003302 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003303 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003305 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
3307 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003308 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 */
3310 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003311 if (eval_leader(arg, in_vim9script()) == FAIL)
3312 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 end_leader = *arg;
3314
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003315 if (**arg == '.' && (!isdigit(*(*arg + 1))
3316#ifdef FEAT_FLOAT
3317 || current_sctx.sc_version < 2
3318#endif
3319 ))
3320 {
3321 semsg(_(e_invexpr2), *arg);
3322 ++*arg;
3323 return FAIL;
3324 }
3325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 switch (**arg)
3327 {
3328 /*
3329 * Number constant.
3330 */
3331 case '0':
3332 case '1':
3333 case '2':
3334 case '3':
3335 case '4':
3336 case '5':
3337 case '6':
3338 case '7':
3339 case '8':
3340 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003341 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003342
3343 // Apply prefixed "-" and "+" now. Matters especially when
3344 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003345 if (ret == OK && evaluate && end_leader > start_leader
3346 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003347 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 break;
3349
3350 /*
3351 * String constant: "string".
3352 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003353 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 break;
3355
3356 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003357 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003359 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003360 break;
3361
3362 /*
3363 * List: [expr, expr]
3364 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003365 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 break;
3367
3368 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003369 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003370 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003371 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003372 {
3373 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003374 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003375 }
3376 else
3377 ret = NOTDONE;
3378 break;
3379
3380 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003381 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003382 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003383 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003384 case '{': if (in_vim9script())
3385 ret = NOTDONE;
3386 else
3387 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003388 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003389 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003390 break;
3391
3392 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003393 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003395 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 break;
3397
3398 /*
3399 * Environment variable: $VAR.
3400 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003401 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 break;
3403
3404 /*
3405 * Register contents: @r.
3406 */
3407 case '@': ++*arg;
3408 if (evaluate)
3409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003410 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003411 rettv->vval.v_string = get_reg_contents(**arg,
3412 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
3414 if (**arg != NUL)
3415 ++*arg;
3416 break;
3417
3418 /*
3419 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003420 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003422 case '(': ret = NOTDONE;
3423 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003424 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003425 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003426 if (ret == OK && evaluate)
3427 {
3428 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3429
3430 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003431 if (compile_def_function(ufunc,
3432 TRUE, PROFILING(ufunc), NULL) == FAIL)
3433 {
3434 clear_tv(rettv);
3435 ret = FAIL;
3436 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003437 }
3438 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003439 if (ret == NOTDONE)
3440 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003441 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003442 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003443
Bram Moolenaar9215f012020-06-27 21:18:00 +02003444 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003445 if (**arg == ')')
3446 ++*arg;
3447 else if (ret == OK)
3448 {
3449 emsg(_(e_missing_close));
3450 clear_tv(rettv);
3451 ret = FAIL;
3452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
3454 break;
3455
Bram Moolenaar8c711452005-01-14 21:53:12 +00003456 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 break;
3458 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003459
3460 if (ret == NOTDONE)
3461 {
3462 /*
3463 * Must be a variable or function name.
3464 * Can also be a curly-braces kind of name: {expr}.
3465 */
3466 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003467 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003468 if (alias != NULL)
3469 s = alias;
3470
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003471 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003472 ret = FAIL;
3473 else
3474 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003475 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3476
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003477 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3478 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003479 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003480 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003481 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003482 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003483 else if (flags & EVAL_CONSTANT)
3484 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003485 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003486 {
3487 // get the value of "true", "false" or a variable
3488 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3489 {
3490 rettv->v_type = VAR_BOOL;
3491 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003492 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003493 }
3494 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003495 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003496 {
3497 rettv->v_type = VAR_BOOL;
3498 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003499 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003500 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003501 else if (len == 4 && in_vim9script()
3502 && STRNCMP(s, "null", 4) == 0)
3503 {
3504 rettv->v_type = VAR_SPECIAL;
3505 rettv->vval.v_number = VVAL_NULL;
3506 ret = OK;
3507 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003508 else
3509 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3510 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003511 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003512 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003513 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003514 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003515 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003516 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003517 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003518 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003519 }
3520
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003521 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3522 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003523 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003524 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
3526 /*
3527 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3528 */
3529 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003530 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003531 return ret;
3532}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003533
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003534/*
3535 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003536 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003537 * Adjusts "end_leaderp" until it is at "start_leader".
3538 */
3539 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003540eval7_leader(
3541 typval_T *rettv,
3542 int numeric_only,
3543 char_u *start_leader,
3544 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003545{
3546 char_u *end_leader = *end_leaderp;
3547 int ret = OK;
3548 int error = FALSE;
3549 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003550 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003551#ifdef FEAT_FLOAT
3552 float_T f = 0.0;
3553
3554 if (rettv->v_type == VAR_FLOAT)
3555 f = rettv->vval.v_float;
3556 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003557#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003558 {
3559 while (VIM_ISWHITE(end_leader[-1]))
3560 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003561 if (in_vim9script() && end_leader[-1] == '!')
3562 val = tv2bool(rettv);
3563 else
3564 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003565 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003566 if (error)
3567 {
3568 clear_tv(rettv);
3569 ret = FAIL;
3570 }
3571 else
3572 {
3573 while (end_leader > start_leader)
3574 {
3575 --end_leader;
3576 if (*end_leader == '!')
3577 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003578 if (numeric_only)
3579 {
3580 ++end_leader;
3581 break;
3582 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003583#ifdef FEAT_FLOAT
3584 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003585 {
3586 if (in_vim9script())
3587 {
3588 rettv->v_type = VAR_BOOL;
3589 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3590 }
3591 else
3592 f = !f;
3593 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003594 else
3595#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003596 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003597 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003598 type = VAR_BOOL;
3599 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003600 }
3601 else if (*end_leader == '-')
3602 {
3603#ifdef FEAT_FLOAT
3604 if (rettv->v_type == VAR_FLOAT)
3605 f = -f;
3606 else
3607#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003608 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003609 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003610 type = VAR_NUMBER;
3611 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003612 }
3613 }
3614#ifdef FEAT_FLOAT
3615 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003617 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003618 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003620 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003621#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003622 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003623 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003624 if (in_vim9script())
3625 rettv->v_type = type;
3626 else
3627 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003628 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003631 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 return ret;
3633}
3634
3635/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003636 * Call the function referred to in "rettv".
3637 */
3638 static int
3639call_func_rettv(
3640 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003641 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003642 typval_T *rettv,
3643 int evaluate,
3644 dict_T *selfdict,
3645 typval_T *basetv)
3646{
3647 partial_T *pt = NULL;
3648 funcexe_T funcexe;
3649 typval_T functv;
3650 char_u *s;
3651 int ret;
3652
3653 // need to copy the funcref so that we can clear rettv
3654 if (evaluate)
3655 {
3656 functv = *rettv;
3657 rettv->v_type = VAR_UNKNOWN;
3658
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003659 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003660 if (functv.v_type == VAR_PARTIAL)
3661 {
3662 pt = functv.vval.v_partial;
3663 s = partial_name(pt);
3664 }
3665 else
3666 s = functv.vval.v_string;
3667 }
3668 else
3669 s = (char_u *)"";
3670
Bram Moolenaara80faa82020-04-12 19:37:17 +02003671 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003672 funcexe.firstline = curwin->w_cursor.lnum;
3673 funcexe.lastline = curwin->w_cursor.lnum;
3674 funcexe.evaluate = evaluate;
3675 funcexe.partial = pt;
3676 funcexe.selfdict = selfdict;
3677 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003678 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003680 // Clear the funcref afterwards, so that deleting it while
3681 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003682 if (evaluate)
3683 clear_tv(&functv);
3684
3685 return ret;
3686}
3687
3688/*
3689 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003690 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003691 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3692 */
3693 static int
3694eval_lambda(
3695 char_u **arg,
3696 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003697 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003698 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003699{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003700 int evaluate = evalarg != NULL
3701 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003702 typval_T base = *rettv;
3703 int ret;
3704
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003705 rettv->v_type = VAR_UNKNOWN;
3706
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003707 if (**arg == '{')
3708 {
3709 // ->{lambda}()
3710 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3711 }
3712 else
3713 {
3714 // ->(lambda)()
3715 ++*arg;
3716 ret = eval1(arg, rettv, evalarg);
3717 *arg = skipwhite_and_linebreak(*arg, evalarg);
3718 if (**arg != ')')
3719 {
3720 emsg(_(e_missing_close));
3721 ret = FAIL;
3722 }
3723 ++*arg;
3724 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003725 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003726 return FAIL;
3727 else if (**arg != '(')
3728 {
3729 if (verbose)
3730 {
3731 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003732 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003733 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003735 }
3736 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003737 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003738 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003739 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003740 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003741
3742 // Clear the funcref afterwards, so that deleting it while
3743 // evaluating the arguments is possible (see test55).
3744 if (evaluate)
3745 clear_tv(&base);
3746
3747 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003748}
3749
3750/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003751 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003752 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003753 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3754 */
3755 static int
3756eval_method(
3757 char_u **arg,
3758 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003759 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003760 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003761{
3762 char_u *name;
3763 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003764 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003765 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003766 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003767 int evaluate = evalarg != NULL
3768 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003769
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003770 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003771
Bram Moolenaarac92e252019-08-03 21:58:38 +02003772 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003773 len = get_name_len(arg, &alias, evaluate, TRUE);
3774 if (alias != NULL)
3775 name = alias;
3776
3777 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003778 {
3779 if (verbose)
3780 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003781 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003782 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003783 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003784 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003785 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003786 if (**arg != '(')
3787 {
3788 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003790 ret = FAIL;
3791 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003792 else if (VIM_ISWHITE((*arg)[-1]))
3793 {
3794 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003795 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003796 ret = FAIL;
3797 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003798 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003799 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003800 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003801 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003802
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003803 // Clear the funcref afterwards, so that deleting it while
3804 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003805 if (evaluate)
3806 clear_tv(&base);
3807
Bram Moolenaarac92e252019-08-03 21:58:38 +02003808 return ret;
3809}
3810
3811/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003812 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3813 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003814 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3815 */
3816 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003817eval_index(
3818 char_u **arg,
3819 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003820 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003821 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003822{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003823 int evaluate = evalarg != NULL
3824 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003825 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003826 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003827 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003828 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003829 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003830 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003831
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003832 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3833 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003834
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003835 init_tv(&var1);
3836 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003837 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003838 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003839 /*
3840 * dict.name
3841 */
3842 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003843 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003844 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003845 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003846 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003847 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003848 }
3849 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003850 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003851 /*
3852 * something[idx]
3853 *
3854 * Get the (first) variable from inside the [].
3855 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003856 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003857 if (**arg == ':')
3858 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003859 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003860 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003861 else if (vim9 && **arg == ':')
3862 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003863 semsg(_(e_white_space_required_before_and_after_str_at_str),
3864 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003865 clear_tv(&var1);
3866 return FAIL;
3867 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003868 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003869 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003870#ifdef FEAT_FLOAT
3871 // allow for indexing with float
3872 if (vim9 && rettv->v_type == VAR_DICT
3873 && var1.v_type == VAR_FLOAT)
3874 {
3875 var1.vval.v_string = typval_tostring(&var1, TRUE);
3876 var1.v_type = VAR_STRING;
3877 }
3878#endif
3879 if (tv_get_string_chk(&var1) == NULL)
3880 {
3881 // not a number or string
3882 clear_tv(&var1);
3883 return FAIL;
3884 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003885 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003886
3887 /*
3888 * Get the second variable from inside the [:].
3889 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003890 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003891 if (**arg == ':')
3892 {
3893 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003894 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003895 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003896 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003897 semsg(_(e_white_space_required_before_and_after_str_at_str),
3898 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003899 if (!empty1)
3900 clear_tv(&var1);
3901 return FAIL;
3902 }
3903 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003904 if (**arg == ']')
3905 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003906 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003908 if (!empty1)
3909 clear_tv(&var1);
3910 return FAIL;
3911 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003912 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003913 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003914 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003915 if (!empty1)
3916 clear_tv(&var1);
3917 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003918 return FAIL;
3919 }
3920 }
3921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003922 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003923 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003924 if (**arg != ']')
3925 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003926 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003927 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003928 clear_tv(&var1);
3929 if (range)
3930 clear_tv(&var2);
3931 return FAIL;
3932 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003933 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003934 }
3935
3936 if (evaluate)
3937 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003938 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003939 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003940 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003941
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003942 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003943 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003944 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003945 clear_tv(&var2);
3946 return res;
3947 }
3948 return OK;
3949}
3950
3951/*
3952 * Check if "rettv" can have an [index] or [sli:ce]
3953 */
3954 int
3955check_can_index(typval_T *rettv, int evaluate, int verbose)
3956{
3957 switch (rettv->v_type)
3958 {
3959 case VAR_FUNC:
3960 case VAR_PARTIAL:
3961 if (verbose)
3962 emsg(_("E695: Cannot index a Funcref"));
3963 return FAIL;
3964 case VAR_FLOAT:
3965#ifdef FEAT_FLOAT
3966 if (verbose)
3967 emsg(_(e_float_as_string));
3968 return FAIL;
3969#endif
3970 case VAR_BOOL:
3971 case VAR_SPECIAL:
3972 case VAR_JOB:
3973 case VAR_CHANNEL:
3974 if (verbose)
3975 emsg(_(e_cannot_index_special_variable));
3976 return FAIL;
3977 case VAR_UNKNOWN:
3978 case VAR_ANY:
3979 case VAR_VOID:
3980 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003981 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003982 emsg(_(e_cannot_index_special_variable));
3983 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003984 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003985 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003986
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003987 case VAR_STRING:
3988 case VAR_LIST:
3989 case VAR_DICT:
3990 case VAR_BLOB:
3991 break;
3992 case VAR_NUMBER:
3993 if (in_vim9script())
3994 emsg(_(e_cannot_index_number));
3995 break;
3996 }
3997 return OK;
3998}
3999
4000/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004001 * slice() function
4002 */
4003 void
4004f_slice(typval_T *argvars, typval_T *rettv)
4005{
4006 if (check_can_index(argvars, TRUE, FALSE) == OK)
4007 {
4008 copy_tv(argvars, rettv);
4009 eval_index_inner(rettv, TRUE, argvars + 1,
4010 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4011 TRUE, NULL, 0, FALSE);
4012 }
4013}
4014
4015/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004016 * Apply index or range to "rettv".
4017 * "var1" is the first index, NULL for [:expr].
4018 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004019 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4020 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004021 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4022 */
4023 int
4024eval_index_inner(
4025 typval_T *rettv,
4026 int is_range,
4027 typval_T *var1,
4028 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004029 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004030 char_u *key,
4031 int keylen,
4032 int verbose)
4033{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004034 varnumber_T n1, n2 = 0;
4035 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004036
4037 n1 = 0;
4038 if (var1 != NULL && rettv->v_type != VAR_DICT)
4039 n1 = tv_get_number(var1);
4040
4041 if (is_range)
4042 {
4043 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004044 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004045 if (verbose)
4046 emsg(_(e_cannot_slice_dictionary));
4047 return FAIL;
4048 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004049 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004050 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004051 else
4052 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004053 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004054
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004055 switch (rettv->v_type)
4056 {
4057 case VAR_UNKNOWN:
4058 case VAR_ANY:
4059 case VAR_VOID:
4060 case VAR_FUNC:
4061 case VAR_PARTIAL:
4062 case VAR_FLOAT:
4063 case VAR_BOOL:
4064 case VAR_SPECIAL:
4065 case VAR_JOB:
4066 case VAR_CHANNEL:
4067 break; // not evaluating, skipping over subscript
4068
4069 case VAR_NUMBER:
4070 case VAR_STRING:
4071 {
4072 char_u *s = tv_get_string(rettv);
4073
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004074 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004075 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004076 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004077 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004078 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004079 else
4080 s = char_from_string(s, n1);
4081 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004082 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004083 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004084 // The resulting variable is a substring. If the indexes
4085 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004086 if (n1 < 0)
4087 {
4088 n1 = len + n1;
4089 if (n1 < 0)
4090 n1 = 0;
4091 }
4092 if (n2 < 0)
4093 n2 = len + n2;
4094 else if (n2 >= len)
4095 n2 = len;
4096 if (n1 >= len || n2 < 0 || n1 > n2)
4097 s = NULL;
4098 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004099 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004100 }
4101 else
4102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004103 // The resulting variable is a string of a single
4104 // character. If the index is too big or negative the
4105 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004106 if (n1 >= len || n1 < 0)
4107 s = NULL;
4108 else
4109 s = vim_strnsave(s + n1, 1);
4110 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 clear_tv(rettv);
4112 rettv->v_type = VAR_STRING;
4113 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004114 }
4115 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004116
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004117 case VAR_BLOB:
4118 len = blob_len(rettv->vval.v_blob);
4119 if (is_range)
4120 {
4121 // The resulting variable is a sub-blob. If the indexes
4122 // are out of range the result is empty.
4123 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004124 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004125 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004126 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004127 n1 = 0;
4128 }
4129 if (n2 < 0)
4130 n2 = len + n2;
4131 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004132 n2 = len - (exclusive ? 0 : 1);
4133 if (exclusive)
4134 --n2;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004135 if (n1 >= len || n2 < 0 || n1 > n2)
4136 {
4137 clear_tv(rettv);
4138 rettv->v_type = VAR_BLOB;
4139 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004140 }
4141 else
4142 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004143 blob_T *blob = blob_alloc();
4144 long i;
4145
4146 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004147 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004148 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004149 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004150 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004151 return FAIL;
4152 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004153 blob->bv_ga.ga_len = n2 - n1 + 1;
4154 for (i = n1; i <= n2; i++)
4155 blob_set(blob, i - n1,
4156 blob_get(rettv->vval.v_blob, i));
4157
4158 clear_tv(rettv);
4159 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004160 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004161 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004162 }
4163 else
4164 {
4165 // The resulting variable is a byte value.
4166 // If the index is too big or negative that is an error.
4167 if (n1 < 0)
4168 n1 = len + n1;
4169 if (n1 < len && n1 >= 0)
4170 {
4171 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004172
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004173 clear_tv(rettv);
4174 rettv->v_type = VAR_NUMBER;
4175 rettv->vval.v_number = v;
4176 }
4177 else
4178 semsg(_(e_blobidx), n1);
4179 }
4180 break;
4181
4182 case VAR_LIST:
4183 if (var1 == NULL)
4184 n1 = 0;
4185 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004186 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004187 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004188 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004189 return FAIL;
4190 break;
4191
4192 case VAR_DICT:
4193 {
4194 dictitem_T *item;
4195 typval_T tmp;
4196
4197 if (key == NULL)
4198 {
4199 key = tv_get_string_chk(var1);
4200 if (key == NULL)
4201 return FAIL;
4202 }
4203
4204 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4205
4206 if (item == NULL && verbose)
4207 semsg(_(e_dictkey), key);
4208 if (item == NULL)
4209 return FAIL;
4210
4211 copy_tv(&item->di_tv, &tmp);
4212 clear_tv(rettv);
4213 *rettv = tmp;
4214 }
4215 break;
4216 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004217 return OK;
4218}
4219
4220/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004221 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004222 */
4223 char_u *
4224partial_name(partial_T *pt)
4225{
4226 if (pt->pt_name != NULL)
4227 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004228 if (pt->pt_func != NULL)
4229 return pt->pt_func->uf_name;
4230 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004231}
4232
Bram Moolenaarddecc252016-04-06 22:59:37 +02004233 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004234partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004235{
4236 int i;
4237
4238 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004239 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004240 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004241 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004242 if (pt->pt_name != NULL)
4243 {
4244 func_unref(pt->pt_name);
4245 vim_free(pt->pt_name);
4246 }
4247 else
4248 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004249
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004250 // Decrease the reference count for the context of a closure. If down
4251 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004252 if (pt->pt_funcstack != NULL)
4253 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004254 --pt->pt_funcstack->fs_refcount;
4255 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004256 }
4257
Bram Moolenaarddecc252016-04-06 22:59:37 +02004258 vim_free(pt);
4259}
4260
4261/*
4262 * Unreference a closure: decrement the reference count and free it when it
4263 * becomes zero.
4264 */
4265 void
4266partial_unref(partial_T *pt)
4267{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004268 if (pt != NULL)
4269 {
4270 if (--pt->pt_refcount <= 0)
4271 partial_free(pt);
4272
4273 // If the reference count goes down to one, the funcstack may be the
4274 // only reference and can be freed if no other partials reference it.
4275 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4276 funcstack_check_refcount(pt->pt_funcstack);
4277 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004278}
4279
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004280/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004281 * Return the next (unique) copy ID.
4282 * Used for serializing nested structures.
4283 */
4284 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004285get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004286{
4287 current_copyID += COPYID_INC;
4288 return current_copyID;
4289}
4290
4291/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004292 * Garbage collection for lists and dictionaries.
4293 *
4294 * We use reference counts to be able to free most items right away when they
4295 * are no longer used. But for composite items it's possible that it becomes
4296 * unused while the reference count is > 0: When there is a recursive
4297 * reference. Example:
4298 * :let l = [1, 2, 3]
4299 * :let d = {9: l}
4300 * :let l[1] = d
4301 *
4302 * Since this is quite unusual we handle this with garbage collection: every
4303 * once in a while find out which lists and dicts are not referenced from any
4304 * variable.
4305 *
4306 * Here is a good reference text about garbage collection (refers to Python
4307 * but it applies to all reference-counting mechanisms):
4308 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004309 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004310
4311/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004312 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004313 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004314 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004315 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004316 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004317garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004318{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004319 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004320 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004321 buf_T *buf;
4322 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004323 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004324 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004325
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004326 if (!testing)
4327 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004328 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004329 want_garbage_collect = FALSE;
4330 may_garbage_collect = FALSE;
4331 garbage_collect_at_exit = FALSE;
4332 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004333
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004334 // The execution stack can grow big, limit the size.
4335 if (exestack.ga_maxlen - exestack.ga_len > 500)
4336 {
4337 size_t new_len;
4338 char_u *pp;
4339 int n;
4340
4341 // Keep 150% of the current size, with a minimum of the growth size.
4342 n = exestack.ga_len / 2;
4343 if (n < exestack.ga_growsize)
4344 n = exestack.ga_growsize;
4345
4346 // Don't make it bigger though.
4347 if (exestack.ga_len + n < exestack.ga_maxlen)
4348 {
4349 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4350 pp = vim_realloc(exestack.ga_data, new_len);
4351 if (pp == NULL)
4352 return FAIL;
4353 exestack.ga_maxlen = exestack.ga_len + n;
4354 exestack.ga_data = pp;
4355 }
4356 }
4357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004358 // We advance by two because we add one for items referenced through
4359 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004360 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004361
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004362 /*
4363 * 1. Go through all accessible variables and mark all lists and dicts
4364 * with copyID.
4365 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004367 // Don't free variables in the previous_funccal list unless they are only
4368 // referenced through previous_funccal. This must be first, because if
4369 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004370 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004371
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004372 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004373 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004375 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004376 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004377 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4378 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004379
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004380 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004381 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004382 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4383 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004384 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004385 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4386 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004387#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004388 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004389 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4390 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004391 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004392 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004393 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4394 NULL, NULL);
4395#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004396
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004397 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004398 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004399 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4400 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004401 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004402 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004404 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004405 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004407 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004408 abort = abort || set_ref_in_functions(copyID);
4409
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004410 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004412
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004413 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004414 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004415
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004416 // callbacks in buffers
4417 abort = abort || set_ref_in_buffers(copyID);
4418
Bram Moolenaar1dced572012-04-05 16:54:08 +02004419#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004420 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004421#endif
4422
Bram Moolenaardb913952012-06-29 12:54:53 +02004423#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004424 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004425#endif
4426
4427#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004428 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004429#endif
4430
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004431#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004432 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004433 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004434#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004435#ifdef FEAT_NETBEANS_INTG
4436 abort = abort || set_ref_in_nb_channel(copyID);
4437#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004438
Bram Moolenaare3188e22016-05-31 21:13:04 +02004439#ifdef FEAT_TIMERS
4440 abort = abort || set_ref_in_timer(copyID);
4441#endif
4442
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004443#ifdef FEAT_QUICKFIX
4444 abort = abort || set_ref_in_quickfix(copyID);
4445#endif
4446
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004447#ifdef FEAT_TERMINAL
4448 abort = abort || set_ref_in_term(copyID);
4449#endif
4450
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004451#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004452 abort = abort || set_ref_in_popups(copyID);
4453#endif
4454
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004455 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004456 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004457 /*
4458 * 2. Free lists and dictionaries that are not referenced.
4459 */
4460 did_free = free_unref_items(copyID);
4461
4462 /*
4463 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004464 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004465 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004466 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004467 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004468 else if (p_verbose > 0)
4469 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004470 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004471 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004472
4473 return did_free;
4474}
4475
4476/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004477 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004478 */
4479 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004480free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004481{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004482 int did_free = FALSE;
4483
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004484 // Let all "free" functions know that we are here. This means no
4485 // dictionaries, lists, channels or jobs are to be freed, because we will
4486 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004487 in_free_unref_items = TRUE;
4488
4489 /*
4490 * PASS 1: free the contents of the items. We don't free the items
4491 * themselves yet, so that it is possible to decrement refcount counters
4492 */
4493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004494 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004495 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004497 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004498 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004499
4500#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004501 // Go through the list of jobs and free items without the copyID. This
4502 // must happen before doing channels, because jobs refer to channels, but
4503 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004504 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004506 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004507 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4508#endif
4509
4510 /*
4511 * PASS 2: free the items themselves.
4512 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004513 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004514 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004515
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004516#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004517 // Go through the list of jobs and free items without the copyID. This
4518 // must happen before doing channels, because jobs refer to channels, but
4519 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004520 free_unused_jobs(copyID, COPYID_MASK);
4521
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004522 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004523 free_unused_channels(copyID, COPYID_MASK);
4524#endif
4525
4526 in_free_unref_items = FALSE;
4527
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004528 return did_free;
4529}
4530
4531/*
4532 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004533 * "list_stack" is used to add lists to be marked. Can be NULL.
4534 *
4535 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004536 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004538set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004539{
4540 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004541 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004542 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004543 hashtab_T *cur_ht;
4544 ht_stack_T *ht_stack = NULL;
4545 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004546
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004547 cur_ht = ht;
4548 for (;;)
4549 {
4550 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004551 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004552 // Mark each item in the hashtab. If the item contains a hashtab
4553 // it is added to ht_stack, if it contains a list it is added to
4554 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004555 todo = (int)cur_ht->ht_used;
4556 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4557 if (!HASHITEM_EMPTY(hi))
4558 {
4559 --todo;
4560 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4561 &ht_stack, list_stack);
4562 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004563 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004564
4565 if (ht_stack == NULL)
4566 break;
4567
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004568 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004569 cur_ht = ht_stack->ht;
4570 tempitem = ht_stack;
4571 ht_stack = ht_stack->prev;
4572 free(tempitem);
4573 }
4574
4575 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004576}
4577
4578/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004579 * Mark a dict and its items with "copyID".
4580 * Returns TRUE if setting references failed somehow.
4581 */
4582 int
4583set_ref_in_dict(dict_T *d, int copyID)
4584{
4585 if (d != NULL && d->dv_copyID != copyID)
4586 {
4587 d->dv_copyID = copyID;
4588 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4589 }
4590 return FALSE;
4591}
4592
4593/*
4594 * Mark a list and its items with "copyID".
4595 * Returns TRUE if setting references failed somehow.
4596 */
4597 int
4598set_ref_in_list(list_T *ll, int copyID)
4599{
4600 if (ll != NULL && ll->lv_copyID != copyID)
4601 {
4602 ll->lv_copyID = copyID;
4603 return set_ref_in_list_items(ll, copyID, NULL);
4604 }
4605 return FALSE;
4606}
4607
4608/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004609 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004610 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4611 *
4612 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004613 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004614 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004615set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004616{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004617 listitem_T *li;
4618 int abort = FALSE;
4619 list_T *cur_l;
4620 list_stack_T *list_stack = NULL;
4621 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004622
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004623 cur_l = l;
4624 for (;;)
4625 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004626 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004627 // Mark each item in the list. If the item contains a hashtab
4628 // it is added to ht_stack, if it contains a list it is added to
4629 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004630 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4631 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4632 ht_stack, &list_stack);
4633 if (list_stack == NULL)
4634 break;
4635
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004636 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004637 cur_l = list_stack->list;
4638 tempitem = list_stack;
4639 list_stack = list_stack->prev;
4640 free(tempitem);
4641 }
4642
4643 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004644}
4645
4646/*
4647 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004648 * "list_stack" is used to add lists to be marked. Can be NULL.
4649 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4650 *
4651 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004652 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004654set_ref_in_item(
4655 typval_T *tv,
4656 int copyID,
4657 ht_stack_T **ht_stack,
4658 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004659{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004660 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004661
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004662 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004663 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004664 dict_T *dd = tv->vval.v_dict;
4665
Bram Moolenaara03f2332016-02-06 18:09:59 +01004666 if (dd != NULL && dd->dv_copyID != copyID)
4667 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004668 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004669 dd->dv_copyID = copyID;
4670 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004671 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004672 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4673 }
4674 else
4675 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004676 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4677
Bram Moolenaara03f2332016-02-06 18:09:59 +01004678 if (newitem == NULL)
4679 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004680 else
4681 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004682 newitem->ht = &dd->dv_hashtab;
4683 newitem->prev = *ht_stack;
4684 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004685 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004686 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004687 }
4688 }
4689 else if (tv->v_type == VAR_LIST)
4690 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004691 list_T *ll = tv->vval.v_list;
4692
Bram Moolenaara03f2332016-02-06 18:09:59 +01004693 if (ll != NULL && ll->lv_copyID != copyID)
4694 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004695 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004696 ll->lv_copyID = copyID;
4697 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004698 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004699 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004700 }
4701 else
4702 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004703 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4704
Bram Moolenaara03f2332016-02-06 18:09:59 +01004705 if (newitem == NULL)
4706 abort = TRUE;
4707 else
4708 {
4709 newitem->list = ll;
4710 newitem->prev = *list_stack;
4711 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004712 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004713 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004714 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004715 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004716 else if (tv->v_type == VAR_FUNC)
4717 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004718 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004719 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004720 else if (tv->v_type == VAR_PARTIAL)
4721 {
4722 partial_T *pt = tv->vval.v_partial;
4723 int i;
4724
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004725 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004726 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004727 // Didn't see this partial yet.
4728 pt->pt_copyID = copyID;
4729
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004730 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004731
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004732 if (pt->pt_dict != NULL)
4733 {
4734 typval_T dtv;
4735
4736 dtv.v_type = VAR_DICT;
4737 dtv.vval.v_dict = pt->pt_dict;
4738 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4739 }
4740
4741 for (i = 0; i < pt->pt_argc; ++i)
4742 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4743 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004744 if (pt->pt_funcstack != NULL)
4745 {
4746 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4747
4748 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4749 abort = abort || set_ref_in_item(stack + i, copyID,
4750 ht_stack, list_stack);
4751 }
4752
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004753 }
4754 }
4755#ifdef FEAT_JOB_CHANNEL
4756 else if (tv->v_type == VAR_JOB)
4757 {
4758 job_T *job = tv->vval.v_job;
4759 typval_T dtv;
4760
4761 if (job != NULL && job->jv_copyID != copyID)
4762 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004763 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004764 if (job->jv_channel != NULL)
4765 {
4766 dtv.v_type = VAR_CHANNEL;
4767 dtv.vval.v_channel = job->jv_channel;
4768 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4769 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004770 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004771 {
4772 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004773 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004774 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4775 }
4776 }
4777 }
4778 else if (tv->v_type == VAR_CHANNEL)
4779 {
4780 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004781 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004782 typval_T dtv;
4783 jsonq_T *jq;
4784 cbq_T *cq;
4785
4786 if (ch != NULL && ch->ch_copyID != copyID)
4787 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004788 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004789 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004790 {
4791 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4792 jq = jq->jq_next)
4793 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4794 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4795 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004796 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004797 {
4798 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004799 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004800 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4801 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004802 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004803 {
4804 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004805 dtv.vval.v_partial =
4806 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004807 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4808 }
4809 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004810 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004811 {
4812 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004813 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004814 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4815 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004816 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004817 {
4818 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004819 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004820 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4821 }
4822 }
4823 }
4824#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004825 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004826}
4827
Bram Moolenaar8c711452005-01-14 21:53:12 +00004828/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 * Return a string with the string representation of a variable.
4830 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004831 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004832 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004833 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004834 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004835 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004836 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4837 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004838 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004840 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004841echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004842 typval_T *tv,
4843 char_u **tofree,
4844 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004845 int copyID,
4846 int echo_style,
4847 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004848 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004849{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004850 static int recurse = 0;
4851 char_u *r = NULL;
4852
Bram Moolenaar33570922005-01-25 22:26:29 +00004853 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004854 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004855 if (!did_echo_string_emsg)
4856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004857 // Only give this message once for a recursive call to avoid
4858 // flooding the user with errors. And stop iterating over lists
4859 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004860 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004861 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004863 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004864 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004865 }
4866 ++recurse;
4867
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004868 switch (tv->v_type)
4869 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004870 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004871 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004872 {
4873 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004874 r = tv->vval.v_string;
4875 if (r == NULL)
4876 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004877 }
4878 else
4879 {
4880 *tofree = string_quote(tv->vval.v_string, FALSE);
4881 r = *tofree;
4882 }
4883 break;
4884
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004885 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004886 if (echo_style)
4887 {
4888 *tofree = NULL;
4889 r = tv->vval.v_string;
4890 }
4891 else
4892 {
4893 *tofree = string_quote(tv->vval.v_string, TRUE);
4894 r = *tofree;
4895 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004896 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004897
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004898 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004899 {
4900 partial_T *pt = tv->vval.v_partial;
4901 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004902 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004903 garray_T ga;
4904 int i;
4905 char_u *tf;
4906
4907 ga_init2(&ga, 1, 100);
4908 ga_concat(&ga, (char_u *)"function(");
4909 if (fname != NULL)
4910 {
4911 ga_concat(&ga, fname);
4912 vim_free(fname);
4913 }
4914 if (pt != NULL && pt->pt_argc > 0)
4915 {
4916 ga_concat(&ga, (char_u *)", [");
4917 for (i = 0; i < pt->pt_argc; ++i)
4918 {
4919 if (i > 0)
4920 ga_concat(&ga, (char_u *)", ");
4921 ga_concat(&ga,
4922 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4923 vim_free(tf);
4924 }
4925 ga_concat(&ga, (char_u *)"]");
4926 }
4927 if (pt != NULL && pt->pt_dict != NULL)
4928 {
4929 typval_T dtv;
4930
4931 ga_concat(&ga, (char_u *)", ");
4932 dtv.v_type = VAR_DICT;
4933 dtv.vval.v_dict = pt->pt_dict;
4934 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4935 vim_free(tf);
4936 }
4937 ga_concat(&ga, (char_u *)")");
4938
4939 *tofree = ga.ga_data;
4940 r = *tofree;
4941 break;
4942 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004943
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004944 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004945 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004946 break;
4947
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004948 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004949 if (tv->vval.v_list == NULL)
4950 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004951 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004952 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004953 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004954 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004955 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4956 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004957 {
4958 *tofree = NULL;
4959 r = (char_u *)"[...]";
4960 }
4961 else
4962 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004963 int old_copyID = tv->vval.v_list->lv_copyID;
4964
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004965 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004966 *tofree = list2string(tv, copyID, restore_copyID);
4967 if (restore_copyID)
4968 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004969 r = *tofree;
4970 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004971 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004972
Bram Moolenaar8c711452005-01-14 21:53:12 +00004973 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004974 if (tv->vval.v_dict == NULL)
4975 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004976 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004977 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004978 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004979 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004980 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4981 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004982 {
4983 *tofree = NULL;
4984 r = (char_u *)"{...}";
4985 }
4986 else
4987 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004988 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004989
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004990 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004991 *tofree = dict2string(tv, copyID, restore_copyID);
4992 if (restore_copyID)
4993 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004994 r = *tofree;
4995 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004996 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004997
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004999 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005000 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005002 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005003 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005004 break;
5005
Bram Moolenaar835dc632016-02-07 14:27:38 +01005006 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005007 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005008 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005009 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005010 if (composite_val)
5011 {
5012 *tofree = string_quote(r, FALSE);
5013 r = *tofree;
5014 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005015 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005016
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005017 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005018#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005019 *tofree = NULL;
5020 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5021 r = numbuf;
5022 break;
5023#endif
5024
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005025 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005026 case VAR_SPECIAL:
5027 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005028 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005029 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005030 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005031
Bram Moolenaar8502c702014-06-17 12:51:16 +02005032 if (--recurse == 0)
5033 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005034 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005035}
5036
5037/*
5038 * Return a string with the string representation of a variable.
5039 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5040 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005041 * Does not put quotes around strings, as ":echo" displays values.
5042 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5043 * May return NULL.
5044 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005045 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005046echo_string(
5047 typval_T *tv,
5048 char_u **tofree,
5049 char_u *numbuf,
5050 int copyID)
5051{
5052 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5053}
5054
5055/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005056 * Return string "str" in ' quotes, doubling ' characters.
5057 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005059 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005060 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005061string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062{
Bram Moolenaar33570922005-01-25 22:26:29 +00005063 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005064 char_u *p, *r, *s;
5065
Bram Moolenaar33570922005-01-25 22:26:29 +00005066 len = (function ? 13 : 3);
5067 if (str != NULL)
5068 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005069 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005070 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005071 if (*p == '\'')
5072 ++len;
5073 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005074 s = r = alloc(len);
5075 if (r != NULL)
5076 {
5077 if (function)
5078 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005080 r += 10;
5081 }
5082 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005084 if (str != NULL)
5085 for (p = str; *p != NUL; )
5086 {
5087 if (*p == '\'')
5088 *r++ = '\'';
5089 MB_COPY_CHAR(p, r);
5090 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005092 if (function)
5093 *r++ = ')';
5094 *r++ = NUL;
5095 }
5096 return s;
5097}
5098
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005099#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005100/*
5101 * Convert the string "text" to a floating point number.
5102 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5103 * this always uses a decimal point.
5104 * Returns the length of the text that was consumed.
5105 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005106 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005107string2float(
5108 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005109 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005110{
5111 char *s = (char *)text;
5112 float_T f;
5113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005114 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005115 if (STRNICMP(text, "inf", 3) == 0)
5116 {
5117 *value = INFINITY;
5118 return 3;
5119 }
5120 if (STRNICMP(text, "-inf", 3) == 0)
5121 {
5122 *value = -INFINITY;
5123 return 4;
5124 }
5125 if (STRNICMP(text, "nan", 3) == 0)
5126 {
5127 *value = NAN;
5128 return 3;
5129 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005130 f = strtod(s, &s);
5131 *value = f;
5132 return (int)((char_u *)s - text);
5133}
5134#endif
5135
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005136/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005137 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5138 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005139 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005140 */
5141 int
5142buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5143{
5144 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005145 char_u *t;
5146 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005147
5148 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5149 return -1;
5150
5151 if (lnum > buf->b_ml.ml_line_count)
5152 lnum = buf->b_ml.ml_line_count;
5153
5154 str = ml_get_buf(buf, lnum, FALSE);
5155 if (str == NULL)
5156 return -1;
5157
5158 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005159 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005160
Bram Moolenaar91458462021-01-13 20:08:38 +01005161 // count the number of characters
5162 t = str;
5163 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5164 t += mb_ptr2len(t);
5165
5166 // In insert mode, when the cursor is at the end of a non-empty line,
5167 // byteidx points to the NUL character immediately past the end of the
5168 // string. In this case, add one to the character count.
5169 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5170 count++;
5171
5172 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005173}
5174
5175/*
5176 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005177 * byte index. Works only for loaded buffers. Returns -1 on failure.
5178 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005179 */
5180 int
5181buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5182{
5183 char_u *str;
5184 char_u *t;
5185
5186 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5187 return -1;
5188
5189 if (lnum > buf->b_ml.ml_line_count)
5190 lnum = buf->b_ml.ml_line_count;
5191
5192 str = ml_get_buf(buf, lnum, FALSE);
5193 if (str == NULL)
5194 return -1;
5195
5196 // Convert the character offset to a byte offset
5197 t = str;
5198 while (*t != NUL && --charidx > 0)
5199 t += mb_ptr2len(t);
5200
Bram Moolenaar91458462021-01-13 20:08:38 +01005201 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005202}
5203
5204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005206 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005208 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005209var2fpos(
5210 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005211 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005212 int *fnum, // set to fnum for '0, 'A, etc.
5213 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005215 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005217 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005219 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005220 if (varp->v_type == VAR_LIST)
5221 {
5222 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005223 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005224 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005225 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005226
5227 l = varp->vval.v_list;
5228 if (l == NULL)
5229 return NULL;
5230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005231 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005232 pos.lnum = list_find_nr(l, 0L, &error);
5233 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005234 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005235 if (charcol)
5236 len = (long)mb_charlen(ml_get(pos.lnum));
5237 else
5238 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005239
Bram Moolenaarec65d772020-08-20 22:29:12 +02005240 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005241 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005242 li = list_find(l, 1L);
5243 if (li != NULL && li->li_tv.v_type == VAR_STRING
5244 && li->li_tv.vval.v_string != NULL
5245 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005246 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005247 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005248 }
5249 else
5250 {
5251 pos.col = list_find_nr(l, 1L, &error);
5252 if (error)
5253 return NULL;
5254 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005255
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005256 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005257 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005258 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005259 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005260
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005261 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005262 pos.coladd = list_find_nr(l, 2L, &error);
5263 if (error)
5264 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005265
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005266 return &pos;
5267 }
5268
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005269 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005270 if (name == NULL)
5271 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005272 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005273 {
5274 pos = curwin->w_cursor;
5275 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005276 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005277 return &pos;
5278 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005279 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005280 {
5281 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005282 pos = VIsual;
5283 else
5284 pos = curwin->w_cursor;
5285 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005286 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005287 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005288 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005289 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005291 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5293 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005294 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005295 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 return pp;
5297 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005298
Bram Moolenaara5525202006-03-02 22:52:09 +00005299 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005300
Bram Moolenaar477933c2007-07-17 14:32:23 +00005301 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005302 {
5303 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005304 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005305 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005306 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005307 // In silent Ex mode topline is zero, but that's not a valid line
5308 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005309 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005310 return &pos;
5311 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005312 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005313 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005314 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005315 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005316 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005317 return &pos;
5318 }
5319 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005320 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005322 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
5324 pos.lnum = curbuf->b_ml.ml_line_count;
5325 pos.col = 0;
5326 }
5327 else
5328 {
5329 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005330 if (charcol)
5331 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5332 else
5333 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335 return &pos;
5336 }
5337 return NULL;
5338}
5339
5340/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005341 * Convert list in "arg" into a position and optional file number.
5342 * When "fnump" is NULL there is no file number, only 3 items.
5343 * Note that the column is passed on as-is, the caller may want to decrement
5344 * it to use 1 for the first column.
5345 * Return FAIL when conversion is not possible, doesn't check the position for
5346 * validity.
5347 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005348 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005349list2fpos(
5350 typval_T *arg,
5351 pos_T *posp,
5352 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005353 colnr_T *curswantp,
5354 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005355{
5356 list_T *l = arg->vval.v_list;
5357 long i = 0;
5358 long n;
5359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005360 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5361 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005362 if (arg->v_type != VAR_LIST
5363 || l == NULL
5364 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005365 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005366 return FAIL;
5367
5368 if (fnump != NULL)
5369 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005370 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005371 if (n < 0)
5372 return FAIL;
5373 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005374 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005375 *fnump = n;
5376 }
5377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005379 if (n < 0)
5380 return FAIL;
5381 posp->lnum = n;
5382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005384 if (n < 0)
5385 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005386 // If character position is specified, then convert to byte position
5387 if (charcol)
5388 {
5389 buf_T *buf;
5390
5391 // Get the text for the specified line in a loaded buffer
5392 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5393 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5394 return FAIL;
5395
Bram Moolenaar91458462021-01-13 20:08:38 +01005396 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005397 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005398 posp->col = n;
5399
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005400 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005401 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005402 posp->coladd = 0;
5403 else
5404 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005405
Bram Moolenaar493c1782014-05-28 14:34:46 +02005406 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005407 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005408
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005409 return OK;
5410}
5411
5412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 * Get the length of an environment variable name.
5414 * Advance "arg" to the first character after the name.
5415 * Return 0 for error.
5416 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005417 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005418get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419{
5420 char_u *p;
5421 int len;
5422
5423 for (p = *arg; vim_isIDc(*p); ++p)
5424 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005425 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 return 0;
5427
5428 len = (int)(p - *arg);
5429 *arg = p;
5430 return len;
5431}
5432
5433/*
5434 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005435 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 * Return 0 if something is wrong.
5437 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005439get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
5441 char_u *p;
5442 int len;
5443
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005444 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005446 {
5447 if (*p == ':')
5448 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005449 // "s:" is start of "s:var", but "n:" is not and can be used in
5450 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005451 len = (int)(p - *arg);
5452 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5453 || len > 1)
5454 break;
5455 }
5456 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005457 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 return 0;
5459
5460 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005461 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 return len;
5464}
5465
5466/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005467 * Get the length of the name of a variable or function.
5468 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005470 * Return -1 if curly braces expansion failed.
5471 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 * If the name contains 'magic' {}'s, expand them and return the
5473 * expanded name in an allocated string via 'alias' - caller must free.
5474 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005476get_name_len(
5477 char_u **arg,
5478 char_u **alias,
5479 int evaluate,
5480 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481{
5482 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 char_u *p;
5484 char_u *expr_start;
5485 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005487 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
5489 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5490 && (*arg)[2] == (int)KE_SNR)
5491 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005492 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 *arg += 3;
5494 return get_id_len(arg) + 3;
5495 }
5496 len = eval_fname_script(*arg);
5497 if (len > 0)
5498 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005499 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 *arg += len;
5501 }
5502
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005504 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005506 p = find_name_end(*arg, &expr_start, &expr_end,
5507 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 if (expr_start != NULL)
5509 {
5510 char_u *temp_string;
5511
5512 if (!evaluate)
5513 {
5514 len += (int)(p - *arg);
5515 *arg = skipwhite(p);
5516 return len;
5517 }
5518
5519 /*
5520 * Include any <SID> etc in the expanded string:
5521 * Thus the -len here.
5522 */
5523 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5524 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005525 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 *alias = temp_string;
5527 *arg = skipwhite(p);
5528 return (int)STRLEN(temp_string);
5529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
5531 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005532 // Only give an error when there is something, otherwise it will be
5533 // reported at a higher level.
5534 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005535 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
5537 return len;
5538}
5539
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540/*
5541 * Find the end of a variable or function name, taking care of magic braces.
5542 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5543 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005544 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005545 * Return a pointer to just after the name. Equal to "arg" if there is no
5546 * valid name.
5547 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005548 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005549find_name_end(
5550 char_u *arg,
5551 char_u **expr_start,
5552 char_u **expr_end,
5553 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 int mb_nest = 0;
5556 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005558 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005559 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 *expr_start = NULL;
5564 *expr_end = NULL;
5565 }
5566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005567 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005568 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5569 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005570 return arg;
5571
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 for (p = arg; *p != NUL
5573 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005574 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005575 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005576 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005578 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005580 if (*p == '\'')
5581 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005582 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005583 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005584 ;
5585 if (*p == NUL)
5586 break;
5587 }
5588 else if (*p == '"')
5589 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005590 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005591 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005592 if (*p == '\\' && p[1] != NUL)
5593 ++p;
5594 if (*p == NUL)
5595 break;
5596 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005597 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005599 // "s:" is start of "s:var", but "n:" is not and can be used in
5600 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005601 len = (int)(p - arg);
5602 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005603 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005604 break;
5605 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005606
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005607 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005609 if (*p == '[')
5610 ++br_nest;
5611 else if (*p == ']')
5612 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005614
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005615 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005617 if (*p == '{')
5618 {
5619 mb_nest++;
5620 if (expr_start != NULL && *expr_start == NULL)
5621 *expr_start = p;
5622 }
5623 else if (*p == '}')
5624 {
5625 mb_nest--;
5626 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5627 *expr_end = p;
5628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
5631
5632 return p;
5633}
5634
5635/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005636 * Expands out the 'magic' {}'s in a variable/function name.
5637 * Note that this can call itself recursively, to deal with
5638 * constructs like foo{bar}{baz}{bam}
5639 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5640 * "in_start" ^
5641 * "expr_start" ^
5642 * "expr_end" ^
5643 * "in_end" ^
5644 *
5645 * Returns a new allocated string, which the caller must free.
5646 * Returns NULL for failure.
5647 */
5648 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005649make_expanded_name(
5650 char_u *in_start,
5651 char_u *expr_start,
5652 char_u *expr_end,
5653 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005654{
5655 char_u c1;
5656 char_u *retval = NULL;
5657 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005658
5659 if (expr_end == NULL || in_end == NULL)
5660 return NULL;
5661 *expr_start = NUL;
5662 *expr_end = NUL;
5663 c1 = *in_end;
5664 *in_end = NUL;
5665
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005666 temp_result = eval_to_string(expr_start + 1, FALSE);
5667 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005668 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005669 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5670 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005671 if (retval != NULL)
5672 {
5673 STRCPY(retval, in_start);
5674 STRCAT(retval, temp_result);
5675 STRCAT(retval, expr_end + 1);
5676 }
5677 }
5678 vim_free(temp_result);
5679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005680 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005681 *expr_start = '{';
5682 *expr_end = '}';
5683
5684 if (retval != NULL)
5685 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005686 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005687 if (expr_start != NULL)
5688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005689 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005690 temp_result = make_expanded_name(retval, expr_start,
5691 expr_end, temp_result);
5692 vim_free(retval);
5693 retval = temp_result;
5694 }
5695 }
5696
5697 return retval;
5698}
5699
5700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005702 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005705eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005707 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005708}
5709
5710/*
5711 * Return TRUE if character "c" can be used as the first character in a
5712 * variable or function name (excluding '{' and '}').
5713 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005714 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005715eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005716{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005717 return ASCII_ISALPHA(c) || c == '_';
5718}
5719
5720/*
5721 * Return TRUE if character "c" can be used as the first character of a
5722 * dictionary key.
5723 */
5724 int
5725eval_isdictc(int c)
5726{
5727 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728}
5729
5730/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005731 * Handle:
5732 * - expr[expr], expr[expr:expr] subscript
5733 * - ".name" lookup
5734 * - function call with Funcref variable: func(expr)
5735 * - method call: var->method()
5736 *
5737 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005738 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005739 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005740handle_subscript(
5741 char_u **arg,
5742 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005743 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005744 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005745{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005746 int evaluate = evalarg != NULL
5747 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005748 int ret = OK;
5749 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005750 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005751 int getnext;
5752 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005753
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005754 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005755 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005756 // When at the end of the line and ".name" or "->{" or "->X" follows in
5757 // the next line then consume the line break.
5758 p = eval_next_non_blank(*arg, evalarg, &getnext);
5759 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005760 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005761 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005762 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005763 {
5764 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005765 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005766 check_white = FALSE;
5767 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005768
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005769 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5770 || rettv->v_type == VAR_PARTIAL))
5771 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005772 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005773 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5774 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005775
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005776 // Stop the expression evaluation when immediately aborting on
5777 // error, or when an interrupt occurred or an exception was thrown
5778 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005779 if (aborting())
5780 {
5781 if (ret == OK)
5782 clear_tv(rettv);
5783 ret = FAIL;
5784 }
5785 dict_unref(selfdict);
5786 selfdict = NULL;
5787 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005788 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005789 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005790 *arg = skipwhite(p + 2);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005791 if (ret == OK)
5792 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005793 if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005794 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005795 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005796 else
5797 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005798 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005799 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005800 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005801 // "." is ".name" lookup when we found a dict or when evaluating and
5802 // scriptversion is at least 2, where string concatenation is "..".
5803 else if (**arg == '['
5804 || (**arg == '.' && (rettv->v_type == VAR_DICT
5805 || (!evaluate
5806 && (*arg)[1] != '.'
5807 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005808 {
5809 dict_unref(selfdict);
5810 if (rettv->v_type == VAR_DICT)
5811 {
5812 selfdict = rettv->vval.v_dict;
5813 if (selfdict != NULL)
5814 ++selfdict->dv_refcount;
5815 }
5816 else
5817 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005818 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005819 {
5820 clear_tv(rettv);
5821 ret = FAIL;
5822 }
5823 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005824 else
5825 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005826 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005828 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5829 // Don't do this when "Func" is already a partial that was bound
5830 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005831 if (selfdict != NULL
5832 && (rettv->v_type == VAR_FUNC
5833 || (rettv->v_type == VAR_PARTIAL
5834 && (rettv->vval.v_partial->pt_auto
5835 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005836 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005837
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005838 dict_unref(selfdict);
5839 return ret;
5840}
5841
5842/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005843 * Make a copy of an item.
5844 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005845 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5846 * reference to an already copied list/dict can be used.
5847 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005848 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005849 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005850item_copy(
5851 typval_T *from,
5852 typval_T *to,
5853 int deep,
5854 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005855{
5856 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005857 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005858
Bram Moolenaar33570922005-01-25 22:26:29 +00005859 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005860 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005861 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005862 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005863 }
5864 ++recurse;
5865
5866 switch (from->v_type)
5867 {
5868 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005869 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005870 case VAR_STRING:
5871 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005872 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005873 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005874 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005875 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005876 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005877 copy_tv(from, to);
5878 break;
5879 case VAR_LIST:
5880 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005881 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005882 if (from->vval.v_list == NULL)
5883 to->vval.v_list = NULL;
5884 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005886 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005887 to->vval.v_list = from->vval.v_list->lv_copylist;
5888 ++to->vval.v_list->lv_refcount;
5889 }
5890 else
5891 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5892 if (to->vval.v_list == NULL)
5893 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005894 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005895 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005896 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005897 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005898 case VAR_DICT:
5899 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005900 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005901 if (from->vval.v_dict == NULL)
5902 to->vval.v_dict = NULL;
5903 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005905 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005906 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5907 ++to->vval.v_dict->dv_refcount;
5908 }
5909 else
5910 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5911 if (to->vval.v_dict == NULL)
5912 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005913 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005914 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005915 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005917 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005918 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005919 }
5920 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005921 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005922}
5923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005924 void
5925echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5926{
5927 char_u *tofree;
5928 char_u numbuf[NUMBUFLEN];
5929 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5930
5931 if (*atstart)
5932 {
5933 *atstart = FALSE;
5934 // Call msg_start() after eval1(), evaluating the expression
5935 // may cause a message to appear.
5936 if (with_space)
5937 {
5938 // Mark the saved text as finishing the line, so that what
5939 // follows is displayed on a new line when scrolling back
5940 // at the more prompt.
5941 msg_sb_eol();
5942 msg_start();
5943 }
5944 }
5945 else if (with_space)
5946 msg_puts_attr(" ", echo_attr);
5947
5948 if (p != NULL)
5949 for ( ; *p != NUL && !got_int; ++p)
5950 {
5951 if (*p == '\n' || *p == '\r' || *p == TAB)
5952 {
5953 if (*p != TAB && *needclr)
5954 {
5955 // remove any text still there from the command
5956 msg_clr_eos();
5957 *needclr = FALSE;
5958 }
5959 msg_putchar_attr(*p, echo_attr);
5960 }
5961 else
5962 {
5963 if (has_mbyte)
5964 {
5965 int i = (*mb_ptr2len)(p);
5966
5967 (void)msg_outtrans_len_attr(p, i, echo_attr);
5968 p += i - 1;
5969 }
5970 else
5971 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5972 }
5973 }
5974 vim_free(tofree);
5975}
5976
Bram Moolenaare9a41262005-01-15 22:18:47 +00005977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 * ":echo expr1 ..." print each argument separated with a space, add a
5979 * newline at the end.
5980 * ":echon expr1 ..." print each argument plain.
5981 */
5982 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005983ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 char_u *p;
5988 int needclr = TRUE;
5989 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005990 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005991 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005992 evalarg_T evalarg;
5993
Bram Moolenaare707c882020-07-01 13:07:37 +02005994 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
5996 if (eap->skip)
5997 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005998 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006000 // If eval1() causes an error message the text from the command may
6001 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006002 need_clr_eos = needclr;
6003
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006005 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 {
6007 /*
6008 * Report the invalid expression unless the expression evaluation
6009 * has been cancelled due to an aborting error, an interrupt, or an
6010 * exception.
6011 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006012 if (!aborting() && did_emsg == did_emsg_before
6013 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006014 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006015 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 break;
6017 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006018 need_clr_eos = FALSE;
6019
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006022
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006023 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 arg = skipwhite(arg);
6025 }
6026 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006027 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028
6029 if (eap->skip)
6030 --emsg_skip;
6031 else
6032 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006033 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 if (needclr)
6035 msg_clr_eos();
6036 if (eap->cmdidx == CMD_echo)
6037 msg_end();
6038 }
6039}
6040
6041/*
6042 * ":echohl {name}".
6043 */
6044 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006045ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006047 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048}
6049
6050/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006051 * Returns the :echo attribute
6052 */
6053 int
6054get_echo_attr(void)
6055{
6056 return echo_attr;
6057}
6058
6059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 * ":execute expr1 ..." execute the result of an expression.
6061 * ":echomsg expr1 ..." Print a message
6062 * ":echoerr expr1 ..." Print an error
6063 * Each gets spaces around each argument and a newline at the end for
6064 * echo commands
6065 */
6066 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006067ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
6069 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006070 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 int ret = OK;
6072 char_u *p;
6073 garray_T ga;
6074 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
6076 ga_init2(&ga, 1, 80);
6077
6078 if (eap->skip)
6079 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006080 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006082 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006083 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085
6086 if (!eap->skip)
6087 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006088 char_u buf[NUMBUFLEN];
6089
6090 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006091 {
6092 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6093 {
6094 emsg(_(e_inval_string));
6095 p = NULL;
6096 }
6097 else
6098 p = tv_get_string_buf(&rettv, buf);
6099 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006100 else
6101 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006102 if (p == NULL)
6103 {
6104 clear_tv(&rettv);
6105 ret = FAIL;
6106 break;
6107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 len = (int)STRLEN(p);
6109 if (ga_grow(&ga, len + 2) == FAIL)
6110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006111 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 ret = FAIL;
6113 break;
6114 }
6115 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 ga.ga_len += len;
6119 }
6120
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006121 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 arg = skipwhite(arg);
6123 }
6124
6125 if (ret != FAIL && ga.ga_data != NULL)
6126 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006127 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006129 // Mark the already saved text as finishing the line, so that what
6130 // follows is displayed on a new line when scrolling back at the
6131 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006132 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006133 }
6134
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006136 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006137 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006138 out_flush();
6139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 else if (eap->cmdidx == CMD_echoerr)
6141 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006142 int save_did_emsg = did_emsg;
6143
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006144 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006145 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (!force_abort)
6147 did_emsg = save_did_emsg;
6148 }
6149 else if (eap->cmdidx == CMD_execute)
6150 do_cmdline((char_u *)ga.ga_data,
6151 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6152 }
6153
6154 ga_clear(&ga);
6155
6156 if (eap->skip)
6157 --emsg_skip;
6158
6159 eap->nextcmd = check_nextcmd(arg);
6160}
6161
6162/*
6163 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6164 * "arg" points to the "&" or '+' when called, to "option" when returning.
6165 * Returns NULL when no option name found. Otherwise pointer to the char
6166 * after the option name.
6167 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006168 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006169find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
6171 char_u *p = *arg;
6172
6173 ++p;
6174 if (*p == 'g' && p[1] == ':')
6175 {
6176 *opt_flags = OPT_GLOBAL;
6177 p += 2;
6178 }
6179 else if (*p == 'l' && p[1] == ':')
6180 {
6181 *opt_flags = OPT_LOCAL;
6182 p += 2;
6183 }
6184 else
6185 *opt_flags = 0;
6186
6187 if (!ASCII_ISALPHA(*p))
6188 return NULL;
6189 *arg = p;
6190
6191 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006192 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 else
6194 while (ASCII_ISALPHA(*p))
6195 ++p;
6196 return p;
6197}
6198
6199/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006200 * Display script name where an item was last set.
6201 * Should only be invoked when 'verbose' is non-zero.
6202 */
6203 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006204last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006205{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006206 char_u *p;
6207
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006208 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006209 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006210 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006211 if (p != NULL)
6212 {
6213 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006214 msg_puts(_("\n\tLast set from "));
6215 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006216 if (script_ctx.sc_lnum > 0)
6217 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006218 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006219 msg_outnum((long)script_ctx.sc_lnum);
6220 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006221 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006222 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006223 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006224 }
6225}
6226
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006227#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228
6229/*
6230 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006231 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 * "flags" can be "g" to do a global substitute.
6233 * Returns an allocated string, NULL for error.
6234 */
6235 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006236do_string_sub(
6237 char_u *str,
6238 char_u *pat,
6239 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006240 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006241 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243 int sublen;
6244 regmatch_T regmatch;
6245 int i;
6246 int do_all;
6247 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006248 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 garray_T ga;
6250 char_u *ret;
6251 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006252 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006254 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006256 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258 ga_init2(&ga, 1, 200);
6259
6260 do_all = (flags[0] == 'g');
6261
6262 regmatch.rm_ic = p_ic;
6263 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6264 if (regmatch.regprog != NULL)
6265 {
6266 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006267 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6269 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006270 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006271 if (regmatch.startp[0] == regmatch.endp[0])
6272 {
6273 if (zero_width == regmatch.startp[0])
6274 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006275 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006276 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006277 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6278 (size_t)i);
6279 ga.ga_len += i;
6280 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006281 continue;
6282 }
6283 zero_width = regmatch.startp[0];
6284 }
6285
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 /*
6287 * Get some space for a temporary buffer to do the substitution
6288 * into. It will contain:
6289 * - The text up to where the match is.
6290 * - The substituted text.
6291 * - The text after the match.
6292 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006293 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006294 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6296 {
6297 ga_clear(&ga);
6298 break;
6299 }
6300
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006301 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 i = (int)(regmatch.startp[0] - tail);
6303 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006304 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006305 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 + ga.ga_len + i, TRUE, TRUE, FALSE);
6307 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006308 tail = regmatch.endp[0];
6309 if (*tail == NUL)
6310 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (!do_all)
6312 break;
6313 }
6314
6315 if (ga.ga_data != NULL)
6316 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6317
Bram Moolenaar473de612013-06-08 18:19:48 +02006318 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 }
6320
6321 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6322 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006323 if (p_cpo == empty_option)
6324 p_cpo = save_cpo;
6325 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006326 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006327 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006328 // If it's still empty it was changed and restored, need to restore in
6329 // the complicated way.
6330 if (*p_cpo == NUL)
6331 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006332 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334
6335 return ret;
6336}