blob: 3e316bdb1061a5d568b3a38759be54b6d5e853fd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000044} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045
Bram Moolenaar48e697e2016-01-23 22:17:30 +010046static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020047static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
48static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
52static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020053static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020056static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020057
Bram Moolenaare21c1582019-03-02 11:57:09 +010058/*
59 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010060 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010061 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020062 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010064{
65 varnumber_T result;
66
Bram Moolenaar99880f92021-01-20 21:23:14 +010067 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010068 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 if (failed != NULL)
73 *failed = TRUE;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 if (n1 == 0)
76 result = VARNUM_MIN; // similar to NaN
77 else if (n1 < 0)
78 result = -VARNUM_MAX;
79 else
80 result = VARNUM_MAX;
81 }
82 else
83 result = n1 / n2;
84
85 return result;
86}
87
88/*
89 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010090 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010091 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020092 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010094{
Bram Moolenaar99880f92021-01-20 21:23:14 +010095 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 if (failed != NULL)
99 *failed = TRUE;
100 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100101 return (n2 == 0) ? 0 : (n1 % n2);
102}
103
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100104#if defined(EBCDIC) || defined(PROTO)
105/*
106 * Compare struct fst by function name.
107 */
108 static int
109compare_func_name(const void *s1, const void *s2)
110{
111 struct fst *p1 = (struct fst *)s1;
112 struct fst *p2 = (struct fst *)s2;
113
114 return STRCMP(p1->f_name, p2->f_name);
115}
116
117/*
118 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100119 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100120 * On machines using EBCDIC we have to sort it.
121 */
122 static void
123sortFunctions(void)
124{
125 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
126
127 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
128}
129#endif
130
Bram Moolenaar33570922005-01-25 22:26:29 +0000131/*
132 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000133 */
134 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100135eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000136{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200137 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200138 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000139
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200140#ifdef EBCDIC
141 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100142 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200143 */
144 sortFunctions();
145#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000146}
147
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000148#if defined(EXITFREE) || defined(PROTO)
149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100150eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200152 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100153 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200154 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000155
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200156 // autoloaded script names
157 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000158
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200159 // unreferenced lists and dicts
160 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200161
162 // functions not garbage collected
163 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000164}
165#endif
166
Bram Moolenaare6b53242020-07-01 17:28:33 +0200167 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200168fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
169{
170 CLEAR_FIELD(*evalarg);
171 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
172 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
173 {
174 evalarg->eval_getline = eap->getline;
175 evalarg->eval_cookie = eap->cookie;
176 }
177}
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179/*
180 * Top level evaluation function, returning a boolean.
181 * Sets "error" to TRUE if there was an error.
182 * Return TRUE or FALSE.
183 */
184 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100185eval_to_bool(
186 char_u *arg,
187 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200188 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100189 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
Bram Moolenaar33570922005-01-25 22:26:29 +0000191 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200192 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200193 evalarg_T evalarg;
194
Bram Moolenaar37c83712020-06-30 21:18:36 +0200195 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
197 if (skip)
198 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200199 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 else
202 {
203 *error = FALSE;
204 if (!skip)
205 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200206 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200207 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200208 else
209 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000210 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 }
212 }
213 if (skip)
214 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200217 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
219
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100220/*
221 * Call eval1() and give an error message if not done at a lower level.
222 */
223 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200224eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100225{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100226 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100227 int ret;
228 int did_emsg_before = did_emsg;
229 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200230 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100231
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
233
234 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100235 if (ret == FAIL)
236 {
237 // Report the invalid expression unless the expression evaluation has
238 // been cancelled due to an aborting error, an interrupt, or an
239 // exception, or we already gave a more specific error.
240 // Also check called_emsg for when using assert_fails().
241 if (!aborting() && did_emsg == did_emsg_before
242 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100243 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100244 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200245 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100246 return ret;
247}
248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200250 * Return whether a typval is a valid expression to pass to eval_expr_typval()
251 * or eval_expr_to_bool(). An empty string returns FALSE;
252 */
253 int
254eval_expr_valid_arg(typval_T *tv)
255{
256 return tv->v_type != VAR_UNKNOWN
257 && (tv->v_type != VAR_STRING
258 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
259}
260
261/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 * Evaluate an expression, which can be a function, partial or string.
263 * Pass arguments "argv[argc]".
264 * Return the result in "rettv" and OK or FAIL.
265 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200266 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100267eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
268{
269 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100270 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200271 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100272
273 if (expr->v_type == VAR_FUNC)
274 {
275 s = expr->vval.v_string;
276 if (s == NULL || *s == NUL)
277 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200278 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200279 funcexe.evaluate = TRUE;
280 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100281 return FAIL;
282 }
283 else if (expr->v_type == VAR_PARTIAL)
284 {
285 partial_T *partial = expr->vval.v_partial;
286
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200287 if (partial == NULL)
288 return FAIL;
289
Bram Moolenaar822ba242020-05-24 23:00:18 +0200290 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200291 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200293 if (call_def_function(partial->pt_func, argc, argv,
294 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303 funcexe.evaluate = TRUE;
304 funcexe.partial = partial;
305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
309 else
310 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100311 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100312 if (s == NULL)
313 return FAIL;
314 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200315 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100316 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200317 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100318 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200319 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100320 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100321 return FAIL;
322 }
323 }
324 return OK;
325}
326
327/*
328 * Like eval_to_bool() but using a typval_T instead of a string.
329 * Works for string, funcref and partial.
330 */
331 int
332eval_expr_to_bool(typval_T *expr, int *error)
333{
334 typval_T rettv;
335 int res;
336
337 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
338 {
339 *error = TRUE;
340 return FALSE;
341 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200342 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100343 clear_tv(&rettv);
344 return res;
345}
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347/*
348 * Top level evaluation function, returning a string. If "skip" is TRUE,
349 * only parsing to "nextcmd" is done, without reporting errors. Return
350 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
351 */
352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100353eval_to_string_skip(
354 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200355 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100356 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357{
Bram Moolenaar33570922005-01-25 22:26:29 +0000358 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200360 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar37c83712020-06-30 21:18:36 +0200362 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (skip)
364 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200365 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 retval = NULL;
367 else
368 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100369 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000370 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 }
372 if (skip)
373 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200374 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
376 return retval;
377}
378
379/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000380 * Skip over an expression at "*pp".
381 * Return FAIL for an error, OK otherwise.
382 */
383 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200384skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000385{
Bram Moolenaar33570922005-01-25 22:26:29 +0000386 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000387
388 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200389 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000390}
391
392/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200393 * Skip over an expression at "*pp".
394 * If in Vim9 script and line breaks are encountered, the lines are
395 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200396 * "arg" is advanced to just after the expression.
397 * "start" is set to the start of the expression, "end" to just after the end.
398 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 * Return FAIL for an error, OK otherwise.
400 */
401 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200402skip_expr_concatenate(
403 char_u **arg,
404 char_u **start,
405 char_u **end,
406 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200407{
408 typval_T rettv;
409 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200410 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200411 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200413 int evaluate = evalarg == NULL
414 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200416 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200417 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200418 {
419 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200420 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200421 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422 ++gap->ga_len;
423 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200424 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200425
426 // Don't evaluate the expression.
427 if (evalarg != NULL)
428 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200429 *arg = skipwhite(*arg);
430 res = eval1(arg, &rettv, evalarg);
431 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200432 if (evalarg != NULL)
433 evalarg->eval_flags = save_flags;
434
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200435 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200437 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200438 if (evalarg->eval_ga.ga_len == 1)
439 {
440 // just one line, no need to concatenate
441 ga_clear(gap);
442 gap->ga_itemsize = 0;
443 }
444 else
445 {
446 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200447 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200448
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200449 // Line breaks encountered, concatenate all the lines.
450 *((char_u **)gap->ga_data) = *start;
451 p = ga_concat_strings(gap, "");
452
453 // free the lines only when using getsourceline()
454 if (evalarg->eval_cookie != NULL)
455 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200456 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200457 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200458 // Do not free the last line, "arg" points into it, free it
459 // later.
460 vim_free(evalarg->eval_tofree);
461 evalarg->eval_tofree =
462 ((char_u **)gap->ga_data)[gap->ga_len - 1];
463 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200464 ga_clear_strings(gap);
465 }
466 else
467 ga_clear(gap);
468 gap->ga_itemsize = 0;
469 if (p == NULL)
470 return FAIL;
471 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200472 vim_free(evalarg->eval_tofree_lambda);
473 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200474 // Compute "end" relative to the end.
475 *end = *start + STRLEN(*start) - endoff;
476 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200477 }
478
479 return res;
480}
481
482/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100483 * Convert "tv" to a string.
484 * When "convert" is TRUE convert a List into a sequence of lines and convert
485 * a Float to a String.
486 * Returns an allocated string (NULL when out of memory).
487 */
488 char_u *
489typval2string(typval_T *tv, int convert)
490{
491 garray_T ga;
492 char_u *retval;
493#ifdef FEAT_FLOAT
494 char_u numbuf[NUMBUFLEN];
495#endif
496
497 if (convert && tv->v_type == VAR_LIST)
498 {
499 ga_init2(&ga, (int)sizeof(char), 80);
500 if (tv->vval.v_list != NULL)
501 {
502 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
503 if (tv->vval.v_list->lv_len > 0)
504 ga_append(&ga, NL);
505 }
506 ga_append(&ga, NUL);
507 retval = (char_u *)ga.ga_data;
508 }
509#ifdef FEAT_FLOAT
510 else if (convert && tv->v_type == VAR_FLOAT)
511 {
512 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
513 retval = vim_strsave(numbuf);
514 }
515#endif
516 else
517 retval = vim_strsave(tv_get_string(tv));
518 return retval;
519}
520
521/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200522 * Top level evaluation function, returning a string. Does not handle line
523 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000524 * When "convert" is TRUE convert a List into a sequence of lines and convert
525 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 * Return pointer to allocated memory, or NULL for failure.
527 */
528 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100529eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100530 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100531 int convert,
532 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
Bram Moolenaar33570922005-01-25 22:26:29 +0000534 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100536 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100538 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
539 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 retval = NULL;
541 else
542 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100543 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000544 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100546 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
548 return retval;
549}
550
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100551 char_u *
552eval_to_string(
553 char_u *arg,
554 int convert)
555{
556 return eval_to_string_eap(arg, convert, NULL);
557}
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000560 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200561 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200562 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 */
564 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100565eval_to_string_safe(
566 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100567 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
569 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200570 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200571 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200573 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200574 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000575 if (use_sandbox)
576 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200577 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200578 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000579 if (use_sandbox)
580 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200581 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200582 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200583 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 return retval;
585}
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587/*
588 * Top level evaluation function, returning a number.
589 * Evaluates "expr" silently.
590 * Returns -1 for an error.
591 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200592 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100593eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
Bram Moolenaar33570922005-01-25 22:26:29 +0000595 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200596 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000597 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
599 ++emsg_off;
600
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200601 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 retval = -1;
603 else
604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100605 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000606 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608 --emsg_off;
609
610 return retval;
611}
612
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000613/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000614 * Top level evaluation function.
615 * Returns an allocated typval_T with the result.
616 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000617 */
618 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200619eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620{
621 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200622 evalarg_T evalarg;
623
624 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000625
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200626 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200627 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100628 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000629
Bram Moolenaar37c83712020-06-30 21:18:36 +0200630 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000631 return tv;
632}
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100635 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200636 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
637 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000638 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641call_vim_function(
642 char_u *func,
643 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200644 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200645 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000647 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200648 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100650 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200651 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200652 funcexe.firstline = curwin->w_cursor.lnum;
653 funcexe.lastline = curwin->w_cursor.lnum;
654 funcexe.evaluate = TRUE;
655 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000656 if (ret == FAIL)
657 clear_tv(rettv);
658
659 return ret;
660}
661
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100662/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100663 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100664 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200665 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
666 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100667 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200668 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100669call_func_retnr(
670 char_u *func,
671 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200672 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100673{
674 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200675 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100676
Bram Moolenaarded27a12018-08-01 19:06:03 +0200677 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100678 return -1;
679
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100680 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100681 clear_tv(&rettv);
682 return retval;
683}
684
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000685/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100686 * Call Vim script function like call_func_retnr() and drop the result.
687 * Returns FAIL when calling the function fails.
688 */
689 int
690call_func_noret(
691 char_u *func,
692 int argc,
693 typval_T *argv)
694{
695 typval_T rettv;
696
697 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
698 return FAIL;
699 clear_tv(&rettv);
700 return OK;
701}
702
703/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100704 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100705 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000706 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707 */
708 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100709call_func_retstr(
710 char_u *func,
711 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200712 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000713{
714 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000715 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000716
Bram Moolenaarded27a12018-08-01 19:06:03 +0200717 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718 return NULL;
719
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100720 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000721 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 return retval;
723}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000724
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000725/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100726 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100727 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000728 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000729 */
730 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100731call_func_retlist(
732 char_u *func,
733 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200734 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000735{
736 typval_T rettv;
737
Bram Moolenaarded27a12018-08-01 19:06:03 +0200738 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000739 return NULL;
740
741 if (rettv.v_type != VAR_LIST)
742 {
743 clear_tv(&rettv);
744 return NULL;
745 }
746
747 return rettv.vval.v_list;
748}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750#ifdef FEAT_FOLDING
751/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100752 * Evaluate "arg", which is 'foldexpr'.
753 * Note: caller must set "curwin" to match "arg".
754 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
755 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
757 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100758eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
Bram Moolenaar33570922005-01-25 22:26:29 +0000760 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200761 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000763 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
764 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000767 if (use_sandbox)
768 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200769 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200771 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 retval = 0;
773 else
774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100775 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000776 if (tv.v_type == VAR_NUMBER)
777 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000778 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 retval = 0;
780 else
781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100782 // If the result is a string, check if there is a non-digit before
783 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000784 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (!VIM_ISDIGIT(*s) && *s != '-')
786 *cp = *s++;
787 retval = atol((char *)s);
788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000789 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000792 if (use_sandbox)
793 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200794 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200795 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200797 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798}
799#endif
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000802 * Get an lval: variable, Dict item or List item that can be assigned a value
803 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
804 * "name.key", "name.key[expr]" etc.
805 * Indexing only works if "name" is an existing List or Dictionary.
806 * "name" points to the start of the name.
807 * If "rettv" is not NULL it points to the value to be assigned.
808 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
809 * wrong; must end in space or cmd separator.
810 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100811 * flags:
812 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100813 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100814 * GLV_NO_AUTOLOAD: do not use script autoloading
815 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000817 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000818 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000819 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200820 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100821get_lval(
822 char_u *name,
823 typval_T *rettv,
824 lval_T *lp,
825 int unlet,
826 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100827 int flags, // GLV_ values
828 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000830 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000831 char_u *expr_start, *expr_end;
832 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000833 dictitem_T *v;
834 typval_T var1;
835 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000837 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000838 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000839 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100840 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100841 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100842 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100844 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200845 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000846
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100847 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100849 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200851 lp->ll_name_end = find_name_end(name, NULL, NULL,
852 FNE_INCL_BR | fne_flags);
853 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000854 }
855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100856 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000857 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 if (expr_start != NULL)
860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100861 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100862 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000863 && *p != '[' && *p != '.')
864 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200865 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 return NULL;
867 }
868
869 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
870 if (lp->ll_exp_name == NULL)
871 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100872 // Report an invalid expression in braces, unless the
873 // expression evaluation has been cancelled due to an
874 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 if (!aborting() && !quiet)
876 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000877 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100878 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 return NULL;
880 }
881 }
882 lp->ll_name = lp->ll_exp_name;
883 }
884 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 lp->ll_name = name;
887
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200888 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200890 // "a: type" is declaring variable "a" with a type, not "a:".
891 if (p == name + 2 && p[-1] == ':')
892 {
893 --p;
894 lp->ll_name_end = p;
895 }
896 if (*p == ':')
897 {
898 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
899 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200901 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100902 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
903 if (lp->ll_type == NULL && !quiet)
904 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200905 lp->ll_name_end = tp;
906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907 }
908 }
909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100910 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
912 return p;
913
914 cc = *p;
915 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100916 // When we would write to the variable pass &ht and prevent autoload.
917 writing = !(flags & GLV_READ_ONLY);
918 v = find_var(lp->ll_name, writing ? &ht : NULL,
919 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000920 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200921 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000923 if (v == NULL)
924 return NULL;
925
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100926 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
927 {
928 if (!quiet)
929 semsg(_(e_variable_already_declared), lp->ll_name);
930 return NULL;
931 }
932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 /*
934 * Loop until no more [idx] or .key is following.
935 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000936 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100937 var1.v_type = VAR_UNKNOWN;
938 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000939 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200942 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100943 && !(lp->ll_tv->v_type == VAR_BLOB
944 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000946 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100947 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000949 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000950 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100953 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000955 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000956
Bram Moolenaar10c65862020-10-08 21:16:42 +0200957 if (in_vim9script() && lp->ll_valtype == NULL
958 && lp->ll_tv == &v->di_tv
959 && ht != NULL && ht == get_script_local_ht())
960 {
961 svar_T *sv = find_typval_in_script(lp->ll_tv);
962
963 // Vim9 script local variable: get the type
964 if (sv != NULL)
965 lp->ll_valtype = sv->sv_type;
966 }
967
Bram Moolenaar8c711452005-01-14 21:53:12 +0000968 len = -1;
969 if (*p == '.')
970 {
971 key = p + 1;
972 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
973 ;
974 if (len == 0)
975 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100977 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000979 }
980 p = key + len;
981 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000982 else
983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100984 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000985 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000986 if (*p == ':')
987 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000988 else
989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200991 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100993 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000994 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100995 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000996 clear_tv(&var1);
997 return NULL;
998 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200999 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 }
1001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001003 if (*p == ':')
1004 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001008 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001009 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001011 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001012 if (rettv != NULL
1013 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001014 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001015 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001016 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001019 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001020 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001022 }
1023 p = skipwhite(p + 1);
1024 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001026 else
1027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001029 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001030 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001031 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001032 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001035 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001036 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001037 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001038 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001039 clear_tv(&var2);
1040 return NULL;
1041 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001042 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001043 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001044 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001047
Bram Moolenaar8c711452005-01-14 21:53:12 +00001048 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001049 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001051 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001052 clear_tv(&var1);
1053 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001055 }
1056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001057 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001058 ++p;
1059 }
1060
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 {
1063 if (len == -1)
1064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001065 // "[key]": get key from "var1"
1066 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001067 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001070 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 }
1072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001073 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001074
1075 // a NULL dict is equivalent with an empty dict
1076 if (lp->ll_tv->vval.v_dict == NULL)
1077 {
1078 lp->ll_tv->vval.v_dict = dict_alloc();
1079 if (lp->ll_tv->vval.v_dict == NULL)
1080 {
1081 clear_tv(&var1);
1082 return NULL;
1083 }
1084 ++lp->ll_tv->vval.v_dict->dv_refcount;
1085 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001086 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001087
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001088 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001090 // When assigning to a scope dictionary check that a function and
1091 // variable name is valid (only variable name unless it is l: or
1092 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001093 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001094 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001095 int prevval;
1096 int wrong;
1097
1098 if (len != -1)
1099 {
1100 prevval = key[len];
1101 key[len] = NUL;
1102 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001103 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001104 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001105 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1106 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001107 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001108 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001109 if (len != -1)
1110 key[len] = prevval;
1111 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001112 {
1113 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001114 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001115 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001116 }
1117
Bram Moolenaar10c65862020-10-08 21:16:42 +02001118 if (lp->ll_valtype != NULL)
1119 // use the type of the member
1120 lp->ll_valtype = lp->ll_valtype->tt_member;
1121
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001123 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001124 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001125 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001126 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001128 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001129 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001130 return NULL;
1131 }
1132
Bram Moolenaar31b81602019-02-10 22:14:27 +01001133 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001137 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001138 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001140 }
1141 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001145 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001147 p = NULL;
1148 break;
1149 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001150 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001151 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001152 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1153 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001154 {
1155 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001156 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001157 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001158
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001159 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001161 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001162 else if (lp->ll_tv->v_type == VAR_BLOB)
1163 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001164 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1165
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001166 /*
1167 * Get the number and item for the only or first index of the List.
1168 */
1169 if (empty1)
1170 lp->ll_n1 = 0;
1171 else
1172 // is number or string
1173 lp->ll_n1 = (long)tv_get_number(&var1);
1174 clear_tv(&var1);
1175
1176 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001177 || lp->ll_n1 > bloblen
1178 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 {
1180 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001181 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001182 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001183 return NULL;
1184 }
1185 if (lp->ll_range && !lp->ll_empty2)
1186 {
1187 lp->ll_n2 = (long)tv_get_number(&var2);
1188 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001189 if (lp->ll_n2 < 0
1190 || lp->ll_n2 >= bloblen
1191 || lp->ll_n2 < lp->ll_n1)
1192 {
1193 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001194 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001195 return NULL;
1196 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001197 }
1198 lp->ll_blob = lp->ll_tv->vval.v_blob;
1199 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001200 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 else
1203 {
1204 /*
1205 * Get the number and item for the only or first index of the List.
1206 */
1207 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001209 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001210 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001211 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001212 clear_tv(&var1);
1213
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001214 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 lp->ll_list = lp->ll_tv->vval.v_list;
1216 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1217 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001218 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001219 if (lp->ll_n1 < 0)
1220 {
1221 lp->ll_n1 = 0;
1222 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1223 }
1224 }
1225 if (lp->ll_li == NULL)
1226 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001227 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001228 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001229 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 }
1232
Bram Moolenaar10c65862020-10-08 21:16:42 +02001233 if (lp->ll_valtype != NULL)
1234 // use the type of the member
1235 lp->ll_valtype = lp->ll_valtype->tt_member;
1236
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 /*
1238 * May need to find the item or absolute index for the second
1239 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 * When no index given: "lp->ll_empty2" is TRUE.
1241 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001245 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001246 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001249 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001251 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001252 {
1253 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001254 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001256 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001258 }
1259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001260 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 if (lp->ll_n1 < 0)
1262 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1263 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001264 {
1265 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001266 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001268 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001269 }
1270
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 }
1274
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001275 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277 return p;
1278}
1279
1280/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001281 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001282 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001284clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001285{
1286 vim_free(lp->ll_exp_name);
1287 vim_free(lp->ll_newkey);
1288}
1289
1290/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001291 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001292 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001293 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1294 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001296 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001297set_var_lval(
1298 lval_T *lp,
1299 char_u *endp,
1300 typval_T *rettv,
1301 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001302 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001303 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304{
1305 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001306 listitem_T *ri;
1307 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308
1309 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001310 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001311 cc = *endp;
1312 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001313 if (lp->ll_blob != NULL)
1314 {
1315 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001316
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001317 if (op != NULL && *op != '=')
1318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001319 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001320 return;
1321 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001322 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001323 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001324
1325 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1326 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001327 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001328
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001329 if (lp->ll_empty2)
1330 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1331
1332 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001334 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001335 return;
1336 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001337 if (lp->ll_empty2)
1338 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001339
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001340 ir = 0;
1341 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1342 blob_set(lp->ll_blob, il,
1343 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001344 }
1345 else
1346 {
1347 val = (int)tv_get_number_chk(rettv, &error);
1348 if (!error)
1349 {
1350 garray_T *gap = &lp->ll_blob->bv_ga;
1351
1352 // Allow for appending a byte. Setting a byte beyond
1353 // the end is an error otherwise.
1354 if (lp->ll_n1 < gap->ga_len
1355 || (lp->ll_n1 == gap->ga_len
1356 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1357 {
1358 blob_set(lp->ll_blob, lp->ll_n1, val);
1359 if (lp->ll_n1 == gap->ga_len)
1360 ++gap->ga_len;
1361 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001362 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001363 }
1364 }
1365 }
1366 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001367 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001368 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001369
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001370 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001371 {
1372 emsg(_(e_cannot_mod));
1373 *endp = cc;
1374 return;
1375 }
1376
Bram Moolenaarff697e62019-02-12 22:28:33 +01001377 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001378 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001379 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001380 &tv, &di, TRUE, FALSE) == OK)
1381 {
1382 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001383 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1384 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001385 && tv_op(&tv, rettv, op) == OK)
1386 set_var(lp->ll_name, &tv, FALSE);
1387 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001388 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001389 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001390 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001391 {
1392 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001393 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001394 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001396 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001397 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001398 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001399 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001400 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001401 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001402 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001403 else if (lp->ll_range)
1404 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001405 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001406 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001407
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001408 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001409 {
1410 emsg(_("E996: Cannot lock a range"));
1411 return;
1412 }
1413
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001414 /*
1415 * Check whether any of the list items is locked
1416 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001417 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001418 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001419 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001420 return;
1421 ri = ri->li_next;
1422 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1423 break;
1424 ll_li = ll_li->li_next;
1425 ++ll_n1;
1426 }
1427
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 /*
1429 * Assign the List values to the list items.
1430 */
1431 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001432 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 if (op != NULL && *op != '=')
1434 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1435 else
1436 {
1437 clear_tv(&lp->ll_li->li_tv);
1438 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1439 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001440 ri = ri->li_next;
1441 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1442 break;
1443 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001444 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001445 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001446 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001447 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 ri = NULL;
1449 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001450 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001451 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001452 lp->ll_li = lp->ll_li->li_next;
1453 ++lp->ll_n1;
1454 }
1455 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001456 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001457 else if (lp->ll_empty2
1458 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001460 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461 }
1462 else
1463 {
1464 /*
1465 * Assign to a List or Dictionary item.
1466 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001467 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001468 {
1469 emsg(_("E996: Cannot lock a list or dict"));
1470 return;
1471 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001472
1473 if (lp->ll_valtype != NULL
1474 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1475 return;
1476
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477 if (lp->ll_newkey != NULL)
1478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001479 if (op != NULL && *op != '=')
1480 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001481 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001482 return;
1483 }
1484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001485 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001486 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001487 if (di == NULL)
1488 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001489 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1490 {
1491 vim_free(di);
1492 return;
1493 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001495 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496 else if (op != NULL && *op != '=')
1497 {
1498 tv_op(lp->ll_tv, rettv, op);
1499 return;
1500 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001501 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001502 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001503
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001504 /*
1505 * Assign the value to the variable or list item.
1506 */
1507 if (copy)
1508 copy_tv(rettv, lp->ll_tv);
1509 else
1510 {
1511 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001512 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001513 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001514 }
1515 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516}
1517
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001518/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001519 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1520 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521 * Returns OK or FAIL.
1522 */
1523 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001525{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001526 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001527 char_u numbuf[NUMBUFLEN];
1528 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001529 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001531 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001532 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001533 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001534 {
1535 switch (tv1->v_type)
1536 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001537 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001538 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001540 case VAR_DICT:
1541 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001542 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001543 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001544 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001545 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001546 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001547 break;
1548
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001549 case VAR_BLOB:
1550 if (*op != '+' || tv2->v_type != VAR_BLOB)
1551 break;
1552 // BLOB += BLOB
1553 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1554 {
1555 blob_T *b1 = tv1->vval.v_blob;
1556 blob_T *b2 = tv2->vval.v_blob;
1557 int i, len = blob_len(b2);
1558 for (i = 0; i < len; i++)
1559 ga_append(&b1->bv_ga, blob_get(b2, i));
1560 }
1561 return OK;
1562
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001563 case VAR_LIST:
1564 if (*op != '+' || tv2->v_type != VAR_LIST)
1565 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001566 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001567 if (tv2->vval.v_list != NULL)
1568 {
1569 if (tv1->vval.v_list == NULL)
1570 {
1571 tv1->vval.v_list = tv2->vval.v_list;
1572 ++tv1->vval.v_list->lv_refcount;
1573 }
1574 else
1575 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1576 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001577 return OK;
1578
1579 case VAR_NUMBER:
1580 case VAR_STRING:
1581 if (tv2->v_type == VAR_LIST)
1582 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001583 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001584 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001585 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001586 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001587#ifdef FEAT_FLOAT
1588 if (tv2->v_type == VAR_FLOAT)
1589 {
1590 float_T f = n;
1591
Bram Moolenaarff697e62019-02-12 22:28:33 +01001592 if (*op == '%')
1593 break;
1594 switch (*op)
1595 {
1596 case '+': f += tv2->vval.v_float; break;
1597 case '-': f -= tv2->vval.v_float; break;
1598 case '*': f *= tv2->vval.v_float; break;
1599 case '/': f /= tv2->vval.v_float; break;
1600 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001601 clear_tv(tv1);
1602 tv1->v_type = VAR_FLOAT;
1603 tv1->vval.v_float = f;
1604 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001605 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001606#endif
1607 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001608 switch (*op)
1609 {
1610 case '+': n += tv_get_number(tv2); break;
1611 case '-': n -= tv_get_number(tv2); break;
1612 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001613 case '/': n = num_divide(n, tv_get_number(tv2),
1614 &failed); break;
1615 case '%': n = num_modulus(n, tv_get_number(tv2),
1616 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001617 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618 clear_tv(tv1);
1619 tv1->v_type = VAR_NUMBER;
1620 tv1->vval.v_number = n;
1621 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001622 }
1623 else
1624 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001625 if (tv2->v_type == VAR_FLOAT)
1626 break;
1627
Bram Moolenaarff697e62019-02-12 22:28:33 +01001628 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001629 s = tv_get_string(tv1);
1630 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001631 clear_tv(tv1);
1632 tv1->v_type = VAR_STRING;
1633 tv1->vval.v_string = s;
1634 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001635 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001636
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001637 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001638#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001639 {
1640 float_T f;
1641
Bram Moolenaarff697e62019-02-12 22:28:33 +01001642 if (*op == '%' || *op == '.'
1643 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001644 && tv2->v_type != VAR_NUMBER
1645 && tv2->v_type != VAR_STRING))
1646 break;
1647 if (tv2->v_type == VAR_FLOAT)
1648 f = tv2->vval.v_float;
1649 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001650 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001651 switch (*op)
1652 {
1653 case '+': tv1->vval.v_float += f; break;
1654 case '-': tv1->vval.v_float -= f; break;
1655 case '*': tv1->vval.v_float *= f; break;
1656 case '/': tv1->vval.v_float /= f; break;
1657 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001658 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001659#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001660 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001661 }
1662 }
1663
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001664 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001665 return FAIL;
1666}
1667
1668/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 * Evaluate the expression used in a ":for var in expr" command.
1670 * "arg" points to "var".
1671 * Set "*errp" to TRUE for an error, FALSE otherwise;
1672 * Return a pointer that holds the info. Null when there is an error.
1673 */
1674 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001675eval_for_line(
1676 char_u *arg,
1677 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001678 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001679 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680{
Bram Moolenaar33570922005-01-25 22:26:29 +00001681 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001683 typval_T tv;
1684 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001685 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001687 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001689 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 if (fi == NULL)
1691 return NULL;
1692
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001693 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 if (expr == NULL)
1695 return fi;
1696
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001697 expr = skipwhite_and_linebreak(expr, evalarg);
1698 if (expr[0] != 'i' || expr[1] != 'n'
1699 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 return fi;
1703 }
1704
1705 if (skip)
1706 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001707 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1708 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 {
1710 *errp = FALSE;
1711 if (!skip)
1712 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001713 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001714 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001715 l = tv.vval.v_list;
1716 if (l == NULL)
1717 {
1718 // a null list is like an empty list: do nothing
1719 clear_tv(&tv);
1720 }
1721 else
1722 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001724 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001726 // No need to increment the refcount, it's already set for
1727 // the list being used in "tv".
1728 fi->fi_list = l;
1729 list_add_watch(l, &fi->fi_lw);
1730 fi->fi_lw.lw_item = l->lv_first;
1731 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001732 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001733 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001734 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001735 fi->fi_bi = 0;
1736 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001737 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001738 typval_T btv;
1739
1740 // Make a copy, so that the iteration still works when the
1741 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001743 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001744 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001745 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001746 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 else
1748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001749 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001750 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751 }
1752 }
1753 }
1754 if (skip)
1755 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001756 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757
1758 return fi;
1759}
1760
1761/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001762 * Used when looping over a :for line, skip the "in expr" part.
1763 */
1764 void
1765skip_for_lines(void *fi_void, evalarg_T *evalarg)
1766{
1767 forinfo_T *fi = (forinfo_T *)fi_void;
1768 int i;
1769
1770 for (i = 0; i < fi->fi_break_count; ++i)
1771 eval_next_line(evalarg);
1772}
1773
1774/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775 * Use the first item in a ":for" list. Advance to the next.
1776 * Assign the values to the variable (list). "arg" points to the first one.
1777 * Return TRUE when a valid item was found, FALSE when at end of list or
1778 * something wrong.
1779 */
1780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001781next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001783 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 int result;
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001785 int flag = in_vim9script() ? ASSIGN_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001786 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001788 if (fi->fi_blob != NULL)
1789 {
1790 typval_T tv;
1791
1792 if (fi->fi_bi >= blob_len(fi->fi_blob))
1793 return FALSE;
1794 tv.v_type = VAR_NUMBER;
1795 tv.v_lock = VAR_FIXED;
1796 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1797 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001798 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001799 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001800 }
1801
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 item = fi->fi_lw.lw_item;
1803 if (item == NULL)
1804 result = FALSE;
1805 else
1806 {
1807 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001808 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001809 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 }
1811 return result;
1812}
1813
1814/*
1815 * Free the structure used to store info used by ":for".
1816 */
1817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819{
Bram Moolenaar33570922005-01-25 22:26:29 +00001820 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001822 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001823 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001825 list_unref(fi->fi_list);
1826 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001827 if (fi != NULL && fi->fi_blob != NULL)
1828 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 vim_free(fi);
1830}
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001833set_context_for_expression(
1834 expand_T *xp,
1835 char_u *arg,
1836 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001838 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001842 if (cmdidx == CMD_let || cmdidx == CMD_var
1843 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 {
1845 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001846 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001848 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001849 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 {
1851 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001852 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001853 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001854 break;
1855 }
1856 return;
1857 }
1858 }
1859 else
1860 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1861 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 while ((xp->xp_pattern = vim_strpbrk(arg,
1863 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1864 {
1865 c = *xp->xp_pattern;
1866 if (c == '&')
1867 {
1868 c = xp->xp_pattern[1];
1869 if (c == '&')
1870 {
1871 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001872 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 }
1874 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001877 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1878 xp->xp_pattern += 2;
1879
1880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
1882 else if (c == '$')
1883 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001884 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 xp->xp_context = EXPAND_ENV_VARS;
1886 }
1887 else if (c == '=')
1888 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001889 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 xp->xp_context = EXPAND_EXPRESSION;
1891 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001892 else if (c == '#'
1893 && xp->xp_context == EXPAND_EXPRESSION)
1894 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001895 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001896 break;
1897 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001898 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 && xp->xp_context == EXPAND_FUNCTIONS
1900 && vim_strchr(xp->xp_pattern, '(') == NULL)
1901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001902 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 break;
1904 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001905 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001907 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
1909 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1910 if (c == '\\' && xp->xp_pattern[1] != NUL)
1911 ++xp->xp_pattern;
1912 xp->xp_context = EXPAND_NOTHING;
1913 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001914 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001916 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1918 /* skip */ ;
1919 xp->xp_context = EXPAND_NOTHING;
1920 }
1921 else if (c == '|')
1922 {
1923 if (xp->xp_pattern[1] == '|')
1924 {
1925 ++xp->xp_pattern;
1926 xp->xp_context = EXPAND_EXPRESSION;
1927 }
1928 else
1929 xp->xp_context = EXPAND_COMMANDS;
1930 }
1931 else
1932 xp->xp_context = EXPAND_EXPRESSION;
1933 }
1934 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001935 // Doesn't look like something valid, expand as an expression
1936 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 arg = xp->xp_pattern;
1939 if (*arg != NUL)
1940 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1941 /* skip */ ;
1942 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001943
1944 // ":exe one two" completes "two"
1945 if ((cmdidx == CMD_execute
1946 || cmdidx == CMD_echo
1947 || cmdidx == CMD_echon
1948 || cmdidx == CMD_echomsg)
1949 && xp->xp_context == EXPAND_EXPRESSION)
1950 {
1951 for (;;)
1952 {
1953 char_u *n = skiptowhite(arg);
1954
1955 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1956 break;
1957 arg = skipwhite(n);
1958 }
1959 }
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 xp->xp_pattern = arg;
1962}
1963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001965 * Return TRUE if "pat" matches "text".
1966 * Does not use 'cpo' and always uses 'magic'.
1967 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001968 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001969pattern_match(char_u *pat, char_u *text, int ic)
1970{
1971 int matches = FALSE;
1972 char_u *save_cpo;
1973 regmatch_T regmatch;
1974
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001975 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001976 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001977 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001978 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1979 if (regmatch.regprog != NULL)
1980 {
1981 regmatch.rm_ic = ic;
1982 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1983 vim_regfree(regmatch.regprog);
1984 }
1985 p_cpo = save_cpo;
1986 return matches;
1987}
1988
1989/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001990 * Handle a name followed by "(". Both for just "name(arg)" and for
1991 * "expr->name(arg)".
1992 * Returns OK or FAIL.
1993 */
1994 static int
1995eval_func(
1996 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001997 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001998 char_u *name,
1999 int name_len,
2000 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002001 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002002 typval_T *basetv) // "expr" for "expr->name(arg)"
2003{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002004 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002005 char_u *s = name;
2006 int len = name_len;
2007 partial_T *partial;
2008 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002009 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002010
2011 if (!evaluate)
2012 check_vars(s, len);
2013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002014 // If "s" is the name of a variable of type VAR_FUNC
2015 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002016 s = deref_func_name(s, &len, &partial,
2017 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002019 // Need to make a copy, in case evaluating the arguments makes
2020 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002021 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002022 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002023 ret = FAIL;
2024 else
2025 {
2026 funcexe_T funcexe;
2027
2028 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002029 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002030 funcexe.firstline = curwin->w_cursor.lnum;
2031 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002032 funcexe.evaluate = evaluate;
2033 funcexe.partial = partial;
2034 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002035 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002036 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002037 }
2038 vim_free(s);
2039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002040 // If evaluate is FALSE rettv->v_type was not set in
2041 // get_func_tv, but it's needed in handle_subscript() to parse
2042 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002043 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2044 {
2045 rettv->vval.v_string = NULL;
2046 rettv->v_type = VAR_FUNC;
2047 }
2048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002049 // Stop the expression evaluation when immediately
2050 // aborting on error, or when an interrupt occurred or
2051 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002052 if (evaluate && aborting())
2053 {
2054 if (ret == OK)
2055 clear_tv(rettv);
2056 ret = FAIL;
2057 }
2058 return ret;
2059}
2060
2061/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002062 * Get the next line source line without advancing. But do skip over comment
2063 * lines.
2064 */
2065 static char_u *
2066getline_peek_skip_comments(evalarg_T *evalarg)
2067{
2068 for (;;)
2069 {
2070 char_u *next = getline_peek(evalarg->eval_getline,
2071 evalarg->eval_cookie);
2072 char_u *p;
2073
2074 if (next == NULL)
2075 break;
2076 p = skipwhite(next);
2077 if (*p != NUL && !vim9_comment_start(p))
2078 return next;
2079 (void)eval_next_line(evalarg);
2080 }
2081 return NULL;
2082}
2083
2084/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002085 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2086 * comment) and there is a next line, return the next line (skipping blanks)
2087 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002088 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2089 * "arg" must point somewhere inside a line, not at the start.
2090 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002091 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002092eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2093{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002094 char_u *p = skipwhite(arg);
2095
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002096 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002097 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002098 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002099 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002100 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002101 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002102 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002103
2104 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002105 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002106 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002107 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002108
Bram Moolenaar9d489562020-07-30 20:08:50 +02002109 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002110 {
2111 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002112 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002113 }
2114 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002115 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116}
2117
2118/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002119 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002120 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002121 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002122eval_next_line(evalarg_T *evalarg)
2123{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002124 garray_T *gap = &evalarg->eval_ga;
2125 char_u *line;
2126
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002127 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002128 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2129 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002130 else
2131 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002132 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002133 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2134 {
2135 // Going to concatenate the lines after parsing.
2136 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2137 ++gap->ga_len;
2138 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002139 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002140 {
2141 vim_free(evalarg->eval_tofree);
2142 evalarg->eval_tofree = line;
2143 }
2144 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002145}
2146
Bram Moolenaare6b53242020-07-01 17:28:33 +02002147/*
2148 * Call eval_next_non_blank() and get the next line if needed.
2149 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002150 char_u *
2151skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2152{
2153 int getnext;
2154 char_u *p = skipwhite(arg);
2155
Bram Moolenaar962d7212020-07-04 14:15:00 +02002156 if (evalarg == NULL)
2157 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002158 eval_next_non_blank(p, evalarg, &getnext);
2159 if (getnext)
2160 return eval_next_line(evalarg);
2161 return p;
2162}
2163
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002164/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002165 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002166 */
2167 void
2168clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2169{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002170 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002171 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002172 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002173 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002174 if (eap != NULL)
2175 {
2176 // We may need to keep the original command line, e.g. for
2177 // ":let" it has the variable names. But we may also need the
2178 // new one, "nextcmd" points into it. Keep both.
2179 vim_free(eap->cmdline_tofree);
2180 eap->cmdline_tofree = *eap->cmdlinep;
2181 *eap->cmdlinep = evalarg->eval_tofree;
2182 }
2183 else
2184 vim_free(evalarg->eval_tofree);
2185 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002186 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002187
2188 vim_free(evalarg->eval_tofree_lambda);
2189 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002190 }
2191}
2192
2193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2197 */
2198
2199/*
2200 * Handle zero level expression.
2201 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002203 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002204 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 * Return OK or FAIL.
2206 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002207 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002208eval0(
2209 char_u *arg,
2210 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002211 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002212 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213{
2214 int ret;
2215 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002216 int did_emsg_before = did_emsg;
2217 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002218 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002221 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002222 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002223
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002224 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
2226 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 /*
2229 * Report the invalid expression unless the expression evaluation has
2230 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002231 * exception, or we already gave a more specific error.
2232 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002234 if (!aborting()
2235 && did_emsg == did_emsg_before
2236 && called_emsg == called_emsg_before
2237 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002238 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002239
2240 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002241 // a next command to avoid more errors, unless "|" is following, which
2242 // could only be a command separator.
2243 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2244 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002247
2248 if (eap != NULL)
2249 eap->nextcmd = check_nextcmd(p);
2250
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 return ret;
2252}
2253
2254/*
2255 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002256 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002257 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 *
2259 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002260 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002262 * Note: "rettv.v_lock" is not set.
2263 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 * Return OK or FAIL.
2265 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002266 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002267eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002269 char_u *p;
2270 int getnext;
2271
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002272 CLEAR_POINTER(rettv);
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 /*
2275 * Get the first variable.
2276 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002277 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 return FAIL;
2279
Bram Moolenaar793648f2020-06-26 21:28:25 +02002280 p = eval_next_non_blank(*arg, evalarg, &getnext);
2281 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002283 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002284 int result;
2285 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002286 evalarg_T *evalarg_used = evalarg;
2287 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002288 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002289 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002290 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002291
2292 if (evalarg == NULL)
2293 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002294 CLEAR_FIELD(local_evalarg);
2295 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002296 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002297 orig_flags = evalarg_used->eval_flags;
2298 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002299
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002300 if (getnext)
2301 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002302 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002303 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002304 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002305 {
2306 error_white_both(p, 1);
2307 clear_tv(rettv);
2308 return FAIL;
2309 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002310 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002311 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002314 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002316 int error = FALSE;
2317
Bram Moolenaar13106602020-10-04 16:06:05 +02002318 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002319 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002320 else if (vim9script)
2321 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002322 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002324 if (error || !op_falsy || !result)
2325 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002326 if (error)
2327 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329
2330 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002331 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002333 if (op_falsy)
2334 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002335 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002336 {
2337 error_white_both(p, 1);
2338 clear_tv(rettv);
2339 return FAIL;
2340 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002341 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002342 evalarg_used->eval_flags = (op_falsy ? !result : result)
2343 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2344 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002345 {
2346 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002348 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002349 if (!op_falsy || !result)
2350 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002352 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002354 /*
2355 * Check for the ":".
2356 */
2357 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2358 if (*p != ':')
2359 {
2360 emsg(_(e_missing_colon));
2361 if (evaluate && result)
2362 clear_tv(rettv);
2363 evalarg_used->eval_flags = orig_flags;
2364 return FAIL;
2365 }
2366 if (getnext)
2367 *arg = eval_next_line(evalarg_used);
2368 else
2369 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002370 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002371 {
2372 error_white_both(p, 1);
2373 clear_tv(rettv);
2374 evalarg_used->eval_flags = orig_flags;
2375 return FAIL;
2376 }
2377 *arg = p;
2378 }
2379
2380 /*
2381 * Get the third variable. Recursive!
2382 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002383 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002384 {
2385 error_white_both(p, 1);
2386 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002387 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002388 return FAIL;
2389 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002390 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2391 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002392 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002393 if (eval1(arg, &var2, evalarg_used) == FAIL)
2394 {
2395 if (evaluate && result)
2396 clear_tv(rettv);
2397 evalarg_used->eval_flags = orig_flags;
2398 return FAIL;
2399 }
2400 if (evaluate && !result)
2401 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002403
2404 if (evalarg == NULL)
2405 clear_evalarg(&local_evalarg, NULL);
2406 else
2407 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409
2410 return OK;
2411}
2412
2413/*
2414 * Handle first level expression:
2415 * expr2 || expr2 || expr2 logical OR
2416 *
2417 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002418 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 *
2420 * Return OK or FAIL.
2421 */
2422 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002423eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002425 char_u *p;
2426 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427
2428 /*
2429 * Get the first variable.
2430 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002431 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 return FAIL;
2433
2434 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002435 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002437 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002440 evalarg_T *evalarg_used = evalarg;
2441 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002442 int evaluate;
2443 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002444 long result = FALSE;
2445 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002446 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002447 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002448
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002449 if (evalarg == NULL)
2450 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002451 CLEAR_FIELD(local_evalarg);
2452 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002453 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002454 orig_flags = evalarg_used->eval_flags;
2455 evaluate = orig_flags & EVAL_EVALUATE;
2456 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002457 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002458 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002459 result = tv_get_bool_chk(rettv, &error);
2460 else if (tv_get_number_chk(rettv, &error) != 0)
2461 result = TRUE;
2462 clear_tv(rettv);
2463 if (error)
2464 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
2466
2467 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002468 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002470 while (p[0] == '|' && p[1] == '|')
2471 {
2472 if (getnext)
2473 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002474 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002475 {
2476 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2477 {
2478 error_white_both(p, 2);
2479 clear_tv(rettv);
2480 return FAIL;
2481 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002482 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002483 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002484
2485 /*
2486 * Get the second variable.
2487 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002488 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2489 {
2490 error_white_both(p, 2);
2491 clear_tv(rettv);
2492 return FAIL;
2493 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002494 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2495 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002496 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002499
2500 /*
2501 * Compute the result.
2502 */
2503 if (evaluate && !result)
2504 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002505 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002506 result = tv_get_bool_chk(&var2, &error);
2507 else if (tv_get_number_chk(&var2, &error) != 0)
2508 result = TRUE;
2509 clear_tv(&var2);
2510 if (error)
2511 return FAIL;
2512 }
2513 if (evaluate)
2514 {
2515 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002516 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002517 rettv->v_type = VAR_BOOL;
2518 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002519 }
2520 else
2521 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002522 rettv->v_type = VAR_NUMBER;
2523 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002524 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002525 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002526
2527 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002529
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002530 if (evalarg == NULL)
2531 clear_evalarg(&local_evalarg, NULL);
2532 else
2533 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
2535
2536 return OK;
2537}
2538
2539/*
2540 * Handle second level expression:
2541 * expr3 && expr3 && expr3 logical AND
2542 *
2543 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002544 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 *
2546 * Return OK or FAIL.
2547 */
2548 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002549eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002551 char_u *p;
2552 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554 /*
2555 * Get the first variable.
2556 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002557 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 return FAIL;
2559
2560 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002561 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002563 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002564 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002566 evalarg_T *evalarg_used = evalarg;
2567 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002568 int orig_flags;
2569 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002570 long result = TRUE;
2571 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002572 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002573 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002574
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002575 if (evalarg == NULL)
2576 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002577 CLEAR_FIELD(local_evalarg);
2578 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002580 orig_flags = evalarg_used->eval_flags;
2581 evaluate = orig_flags & EVAL_EVALUATE;
2582 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002583 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002584 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002585 result = tv_get_bool_chk(rettv, &error);
2586 else if (tv_get_number_chk(rettv, &error) == 0)
2587 result = FALSE;
2588 clear_tv(rettv);
2589 if (error)
2590 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592
2593 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596 while (p[0] == '&' && p[1] == '&')
2597 {
2598 if (getnext)
2599 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002600 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002601 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002602 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002603 {
2604 error_white_both(p, 2);
2605 clear_tv(rettv);
2606 return FAIL;
2607 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002608 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002609 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002610
2611 /*
2612 * Get the second variable.
2613 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002614 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2615 {
2616 error_white_both(p, 2);
2617 clear_tv(rettv);
2618 return FAIL;
2619 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2621 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002622 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002623 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002624 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002625 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002626
2627 /*
2628 * Compute the result.
2629 */
2630 if (evaluate && result)
2631 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002632 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002633 result = tv_get_bool_chk(&var2, &error);
2634 else if (tv_get_number_chk(&var2, &error) == 0)
2635 result = FALSE;
2636 clear_tv(&var2);
2637 if (error)
2638 return FAIL;
2639 }
2640 if (evaluate)
2641 {
2642 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002643 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002644 rettv->v_type = VAR_BOOL;
2645 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002646 }
2647 else
2648 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002649 rettv->v_type = VAR_NUMBER;
2650 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002651 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002652 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002653
2654 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002656
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002657 if (evalarg == NULL)
2658 clear_evalarg(&local_evalarg, NULL);
2659 else
2660 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 }
2662
2663 return OK;
2664}
2665
2666/*
2667 * Handle third level expression:
2668 * var1 == var2
2669 * var1 =~ var2
2670 * var1 != var2
2671 * var1 !~ var2
2672 * var1 > var2
2673 * var1 >= var2
2674 * var1 < var2
2675 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002676 * var1 is var2
2677 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 *
2679 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002680 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 *
2682 * Return OK or FAIL.
2683 */
2684 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002685eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002688 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002689 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002691 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 /*
2694 * Get the first variable.
2695 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002696 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 return FAIL;
2698
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002699 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002700 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
2702 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002703 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002705 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002707 typval_T var2;
2708 int ic;
2709 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002710 int evaluate = evalarg == NULL
2711 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002712
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002713 if (getnext)
2714 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002715 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2716 {
2717 error_white_both(p, len);
2718 clear_tv(rettv);
2719 return FAIL;
2720 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002721
Bram Moolenaar696ba232020-07-29 21:20:41 +02002722 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2723 {
2724 semsg(_(e_invexpr2), p);
2725 clear_tv(rettv);
2726 return FAIL;
2727 }
2728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002729 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 if (p[len] == '?')
2731 {
2732 ic = TRUE;
2733 ++len;
2734 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002735 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 else if (p[len] == '#')
2737 {
2738 ic = FALSE;
2739 ++len;
2740 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002741 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002743 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
2745 /*
2746 * Get the second variable.
2747 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002748 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2749 {
2750 error_white_both(p, 1);
2751 clear_tv(rettv);
2752 return FAIL;
2753 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002754 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002755 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002757 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 return FAIL;
2759 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002760 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002761 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002762 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002763
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002764 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002765 {
2766 ret = FAIL;
2767 clear_tv(rettv);
2768 }
2769 else
2770 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002771 clear_tv(&var2);
2772 return ret;
2773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775
2776 return OK;
2777}
2778
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002779/*
2780 * Make a copy of blob "tv1" and append blob "tv2".
2781 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 void
2783eval_addblob(typval_T *tv1, typval_T *tv2)
2784{
2785 blob_T *b1 = tv1->vval.v_blob;
2786 blob_T *b2 = tv2->vval.v_blob;
2787 blob_T *b = blob_alloc();
2788 int i;
2789
2790 if (b != NULL)
2791 {
2792 for (i = 0; i < blob_len(b1); i++)
2793 ga_append(&b->bv_ga, blob_get(b1, i));
2794 for (i = 0; i < blob_len(b2); i++)
2795 ga_append(&b->bv_ga, blob_get(b2, i));
2796
2797 clear_tv(tv1);
2798 rettv_blob_set(tv1, b);
2799 }
2800}
2801
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002802/*
2803 * Make a copy of list "tv1" and append list "tv2".
2804 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 int
2806eval_addlist(typval_T *tv1, typval_T *tv2)
2807{
2808 typval_T var3;
2809
2810 // concatenate Lists
2811 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2812 {
2813 clear_tv(tv1);
2814 clear_tv(tv2);
2815 return FAIL;
2816 }
2817 clear_tv(tv1);
2818 *tv1 = var3;
2819 return OK;
2820}
2821
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822/*
2823 * Handle fourth level expression:
2824 * + number addition
2825 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002826 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002827 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 *
2829 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002830 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 *
2832 * Return OK or FAIL.
2833 */
2834 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002835eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 /*
2838 * Get the first variable.
2839 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002840 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 return FAIL;
2842
2843 /*
2844 * Repeat computing, until no '+', '-' or '.' is following.
2845 */
2846 for (;;)
2847 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002848 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002849 int getnext;
2850 char_u *p;
2851 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002852 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002853 int concat;
2854 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002855 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002856
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002857 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002858 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002859 p = eval_next_non_blank(*arg, evalarg, &getnext);
2860 op = *p;
2861 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002862 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002864
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002865 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002866 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002867 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002868 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002869 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002870 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002871 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002872 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002873 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002874 clear_tv(rettv);
2875 return FAIL;
2876 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002877 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002878 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002879 if ((op != '+' || (rettv->v_type != VAR_LIST
2880 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002881#ifdef FEAT_FLOAT
2882 && (op == '.' || rettv->v_type != VAR_FLOAT)
2883#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002884 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002885 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002886 int error = FALSE;
2887
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002888 // For "list + ...", an illegal use of the first operand as
2889 // a number cannot be determined before evaluating the 2nd
2890 // operand: if this is also a list, all is ok.
2891 // For "something . ...", "something - ..." or "non-list + ...",
2892 // we know that the first operand needs to be a string or number
2893 // without evaluating the 2nd operand. So check before to avoid
2894 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002895 if (op != '.')
2896 tv_get_number_chk(rettv, &error);
2897 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002898 {
2899 clear_tv(rettv);
2900 return FAIL;
2901 }
2902 }
2903
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 /*
2905 * Get the second variable.
2906 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002907 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002908 {
2909 error_white_both(p, oplen);
2910 clear_tv(rettv);
2911 return FAIL;
2912 }
2913 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002914 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002916 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 return FAIL;
2918 }
2919
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002920 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
2922 /*
2923 * Compute the result.
2924 */
2925 if (op == '.')
2926 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002927 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2928 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002929 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002930
Bram Moolenaar2e086612020-08-25 22:37:48 +02002931 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002932 || var2.v_type == VAR_CHANNEL
2933 || var2.v_type == VAR_JOB))
2934 emsg(_(e_inval_string));
2935#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002936 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002937 {
2938 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2939 var2.vval.v_float);
2940 s2 = buf2;
2941 }
2942#endif
2943 else
2944 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002945 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002946 {
2947 clear_tv(rettv);
2948 clear_tv(&var2);
2949 return FAIL;
2950 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002951 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002952 clear_tv(rettv);
2953 rettv->v_type = VAR_STRING;
2954 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002956 else if (op == '+' && rettv->v_type == VAR_BLOB
2957 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002959 else if (op == '+' && rettv->v_type == VAR_LIST
2960 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002961 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002963 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 else
2966 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002967 int error = FALSE;
2968 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002969#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002970 float_T f1 = 0, f2 = 0;
2971
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002972 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002973 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002974 f1 = rettv->vval.v_float;
2975 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002976 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002977 else
2978#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002979 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002980 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002981 if (error)
2982 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002983 // This can only happen for "list + non-list". For
2984 // "non-list + ..." or "something - ...", we returned
2985 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002986 clear_tv(rettv);
2987 return FAIL;
2988 }
2989#ifdef FEAT_FLOAT
2990 if (var2.v_type == VAR_FLOAT)
2991 f1 = n1;
2992#endif
2993 }
2994#ifdef FEAT_FLOAT
2995 if (var2.v_type == VAR_FLOAT)
2996 {
2997 f2 = var2.vval.v_float;
2998 n2 = 0;
2999 }
3000 else
3001#endif
3002 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003003 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003004 if (error)
3005 {
3006 clear_tv(rettv);
3007 clear_tv(&var2);
3008 return FAIL;
3009 }
3010#ifdef FEAT_FLOAT
3011 if (rettv->v_type == VAR_FLOAT)
3012 f2 = n2;
3013#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003014 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003015 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003016
3017#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003018 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3020 {
3021 if (op == '+')
3022 f1 = f1 + f2;
3023 else
3024 f1 = f1 - f2;
3025 rettv->v_type = VAR_FLOAT;
3026 rettv->vval.v_float = f1;
3027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003029#endif
3030 {
3031 if (op == '+')
3032 n1 = n1 + n2;
3033 else
3034 n1 = n1 - n2;
3035 rettv->v_type = VAR_NUMBER;
3036 rettv->vval.v_number = n1;
3037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003039 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 }
3041 }
3042 return OK;
3043}
3044
3045/*
3046 * Handle fifth level expression:
3047 * * number multiplication
3048 * / number division
3049 * % number modulo
3050 *
3051 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003052 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 *
3054 * Return OK or FAIL.
3055 */
3056 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003057eval6(
3058 char_u **arg,
3059 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003060 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003061 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003064 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
3067 /*
3068 * Get the first variable.
3069 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003070 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 return FAIL;
3072
3073 /*
3074 * Repeat computing, until no '*', '/' or '%' is following.
3075 */
3076 for (;;)
3077 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003078 int evaluate;
3079 int getnext;
3080 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003081 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003082 int op;
3083 varnumber_T n1, n2;
3084#ifdef FEAT_FLOAT
3085 float_T f1, f2;
3086#endif
3087 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003088
Bram Moolenaar9d489562020-07-30 20:08:50 +02003089 p = eval_next_non_blank(*arg, evalarg, &getnext);
3090 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003091 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003093
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003094 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003095 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003096 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003097 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003098 {
3099 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3100 {
3101 error_white_both(p, 1);
3102 clear_tv(rettv);
3103 return FAIL;
3104 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003105 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003108#ifdef FEAT_FLOAT
3109 f1 = 0;
3110 f2 = 0;
3111#endif
3112 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003113 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003115#ifdef FEAT_FLOAT
3116 if (rettv->v_type == VAR_FLOAT)
3117 {
3118 f1 = rettv->vval.v_float;
3119 use_float = TRUE;
3120 n1 = 0;
3121 }
3122 else
3123#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003124 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003125 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003126 if (error)
3127 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 }
3129 else
3130 n1 = 0;
3131
3132 /*
3133 * Get the second variable.
3134 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003135 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3136 {
3137 error_white_both(p, 1);
3138 clear_tv(rettv);
3139 return FAIL;
3140 }
3141 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003142 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 return FAIL;
3144
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003145 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003147#ifdef FEAT_FLOAT
3148 if (var2.v_type == VAR_FLOAT)
3149 {
3150 if (!use_float)
3151 {
3152 f1 = n1;
3153 use_float = TRUE;
3154 }
3155 f2 = var2.vval.v_float;
3156 n2 = 0;
3157 }
3158 else
3159#endif
3160 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003161 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162 clear_tv(&var2);
3163 if (error)
3164 return FAIL;
3165#ifdef FEAT_FLOAT
3166 if (use_float)
3167 f2 = n2;
3168#endif
3169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 /*
3172 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003173 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003175#ifdef FEAT_FLOAT
3176 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003178 if (op == '*')
3179 f1 = f1 * f2;
3180 else if (op == '/')
3181 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003182# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003183 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003184 if (f2 == 0.0)
3185 {
3186 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003187 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003188 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003189 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003190 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003191 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003192 }
3193 else
3194 f1 = f1 / f2;
3195# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003196 // We rely on the floating point library to handle divide
3197 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003198 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003199# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003202 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003204 return FAIL;
3205 }
3206 rettv->v_type = VAR_FLOAT;
3207 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 }
3209 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003212 int failed = FALSE;
3213
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003214 if (op == '*')
3215 n1 = n1 * n2;
3216 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003217 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003219 n1 = num_modulus(n1, n2, &failed);
3220 if (failed)
3221 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003222
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003223 rettv->v_type = VAR_NUMBER;
3224 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
3227 }
3228
3229 return OK;
3230}
3231
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003232 int
3233eval_leader(char_u **arg, int vim9)
3234{
3235 char_u *s = *arg;
3236 char_u *p = *arg;
3237
3238 while (*p == '!' || *p == '-' || *p == '+')
3239 {
3240 char_u *n = skipwhite(p + 1);
3241
3242 // ++, --, -+ and +- are not accepted in Vim9 script
3243 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3244 {
3245 semsg(_(e_invexpr2), s);
3246 return FAIL;
3247 }
3248 p = n;
3249 }
3250 *arg = p;
3251 return OK;
3252}
3253
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254/*
3255 * Handle sixth level expression:
3256 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003257 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003258 * "string" string constant
3259 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 * &option-name option value
3261 * @r register contents
3262 * identifier variable value
3263 * function() function call
3264 * $VAR environment variable
3265 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003266 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003268 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003269 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 *
3271 * Also handle:
3272 * ! in front logical NOT
3273 * - in front unary minus
3274 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003275 * trailing [] subscript in String or List
3276 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003277 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 *
3279 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003280 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 *
3282 * Return OK or FAIL.
3283 */
3284 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003285eval7(
3286 char_u **arg,
3287 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003288 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003291 int evaluate = evalarg != NULL
3292 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 int len;
3294 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 char_u *start_leader, *end_leader;
3296 int ret = OK;
3297 char_u *alias;
3298
3299 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003300 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003301 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003303 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
3305 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003306 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 */
3308 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003309 if (eval_leader(arg, in_vim9script()) == FAIL)
3310 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 end_leader = *arg;
3312
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003313 if (**arg == '.' && (!isdigit(*(*arg + 1))
3314#ifdef FEAT_FLOAT
3315 || current_sctx.sc_version < 2
3316#endif
3317 ))
3318 {
3319 semsg(_(e_invexpr2), *arg);
3320 ++*arg;
3321 return FAIL;
3322 }
3323
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 switch (**arg)
3325 {
3326 /*
3327 * Number constant.
3328 */
3329 case '0':
3330 case '1':
3331 case '2':
3332 case '3':
3333 case '4':
3334 case '5':
3335 case '6':
3336 case '7':
3337 case '8':
3338 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003339 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003340
3341 // Apply prefixed "-" and "+" now. Matters especially when
3342 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003343 if (ret == OK && evaluate && end_leader > start_leader
3344 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003345 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 break;
3347
3348 /*
3349 * String constant: "string".
3350 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003351 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 break;
3353
3354 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003355 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003357 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003358 break;
3359
3360 /*
3361 * List: [expr, expr]
3362 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003363 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 break;
3365
3366 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003367 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003368 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003369 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003370 {
3371 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003372 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003373 }
3374 else
3375 ret = NOTDONE;
3376 break;
3377
3378 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003379 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003380 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003381 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003382 case '{': if (in_vim9script())
3383 ret = NOTDONE;
3384 else
3385 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003386 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003387 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003388 break;
3389
3390 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003391 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003393 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 break;
3395
3396 /*
3397 * Environment variable: $VAR.
3398 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003399 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 break;
3401
3402 /*
3403 * Register contents: @r.
3404 */
3405 case '@': ++*arg;
3406 if (evaluate)
3407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003408 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003409 rettv->vval.v_string = get_reg_contents(**arg,
3410 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 }
3412 if (**arg != NUL)
3413 ++*arg;
3414 break;
3415
3416 /*
3417 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003418 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003420 case '(': ret = NOTDONE;
3421 if (in_vim9script())
3422 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
3423 if (ret == NOTDONE)
3424 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003425 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003426 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003427
Bram Moolenaar9215f012020-06-27 21:18:00 +02003428 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003429 if (**arg == ')')
3430 ++*arg;
3431 else if (ret == OK)
3432 {
3433 emsg(_(e_missing_close));
3434 clear_tv(rettv);
3435 ret = FAIL;
3436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438 break;
3439
Bram Moolenaar8c711452005-01-14 21:53:12 +00003440 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 break;
3442 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003443
3444 if (ret == NOTDONE)
3445 {
3446 /*
3447 * Must be a variable or function name.
3448 * Can also be a curly-braces kind of name: {expr}.
3449 */
3450 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003451 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003452 if (alias != NULL)
3453 s = alias;
3454
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003455 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003456 ret = FAIL;
3457 else
3458 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003459 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3460
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003461 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3462 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003463 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003464 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003465 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003466 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003467 else if (flags & EVAL_CONSTANT)
3468 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003469 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003470 {
3471 // get the value of "true", "false" or a variable
3472 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3473 {
3474 rettv->v_type = VAR_BOOL;
3475 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003476 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003477 }
3478 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003479 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003480 {
3481 rettv->v_type = VAR_BOOL;
3482 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003483 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003484 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003485 else if (len == 4 && in_vim9script()
3486 && STRNCMP(s, "null", 4) == 0)
3487 {
3488 rettv->v_type = VAR_SPECIAL;
3489 rettv->vval.v_number = VVAL_NULL;
3490 ret = OK;
3491 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003492 else
3493 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3494 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003495 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003496 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003497 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003498 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003499 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003500 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003501 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003502 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003503 }
3504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003505 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3506 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003507 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003508 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
3510 /*
3511 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3512 */
3513 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003514 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003515 return ret;
3516}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003517
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003518/*
3519 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003520 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003521 * Adjusts "end_leaderp" until it is at "start_leader".
3522 */
3523 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003524eval7_leader(
3525 typval_T *rettv,
3526 int numeric_only,
3527 char_u *start_leader,
3528 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003529{
3530 char_u *end_leader = *end_leaderp;
3531 int ret = OK;
3532 int error = FALSE;
3533 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003534 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003535#ifdef FEAT_FLOAT
3536 float_T f = 0.0;
3537
3538 if (rettv->v_type == VAR_FLOAT)
3539 f = rettv->vval.v_float;
3540 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003541#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003542 {
3543 while (VIM_ISWHITE(end_leader[-1]))
3544 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003545 if (in_vim9script() && end_leader[-1] == '!')
3546 val = tv2bool(rettv);
3547 else
3548 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003549 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003550 if (error)
3551 {
3552 clear_tv(rettv);
3553 ret = FAIL;
3554 }
3555 else
3556 {
3557 while (end_leader > start_leader)
3558 {
3559 --end_leader;
3560 if (*end_leader == '!')
3561 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003562 if (numeric_only)
3563 {
3564 ++end_leader;
3565 break;
3566 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003567#ifdef FEAT_FLOAT
3568 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003569 {
3570 if (in_vim9script())
3571 {
3572 rettv->v_type = VAR_BOOL;
3573 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3574 }
3575 else
3576 f = !f;
3577 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003578 else
3579#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003580 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003581 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003582 type = VAR_BOOL;
3583 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003584 }
3585 else if (*end_leader == '-')
3586 {
3587#ifdef FEAT_FLOAT
3588 if (rettv->v_type == VAR_FLOAT)
3589 f = -f;
3590 else
3591#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003592 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003593 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003594 type = VAR_NUMBER;
3595 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003596 }
3597 }
3598#ifdef FEAT_FLOAT
3599 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003601 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003602 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003604 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003605#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003606 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003607 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003608 if (in_vim9script())
3609 rettv->v_type = type;
3610 else
3611 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003612 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003615 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 return ret;
3617}
3618
3619/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003620 * Call the function referred to in "rettv".
3621 */
3622 static int
3623call_func_rettv(
3624 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003625 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003626 typval_T *rettv,
3627 int evaluate,
3628 dict_T *selfdict,
3629 typval_T *basetv)
3630{
3631 partial_T *pt = NULL;
3632 funcexe_T funcexe;
3633 typval_T functv;
3634 char_u *s;
3635 int ret;
3636
3637 // need to copy the funcref so that we can clear rettv
3638 if (evaluate)
3639 {
3640 functv = *rettv;
3641 rettv->v_type = VAR_UNKNOWN;
3642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003643 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003644 if (functv.v_type == VAR_PARTIAL)
3645 {
3646 pt = functv.vval.v_partial;
3647 s = partial_name(pt);
3648 }
3649 else
3650 s = functv.vval.v_string;
3651 }
3652 else
3653 s = (char_u *)"";
3654
Bram Moolenaara80faa82020-04-12 19:37:17 +02003655 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003656 funcexe.firstline = curwin->w_cursor.lnum;
3657 funcexe.lastline = curwin->w_cursor.lnum;
3658 funcexe.evaluate = evaluate;
3659 funcexe.partial = pt;
3660 funcexe.selfdict = selfdict;
3661 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003662 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003663
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003664 // Clear the funcref afterwards, so that deleting it while
3665 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003666 if (evaluate)
3667 clear_tv(&functv);
3668
3669 return ret;
3670}
3671
3672/*
3673 * Evaluate "->method()".
3674 * "*arg" points to the '-'.
3675 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3676 */
3677 static int
3678eval_lambda(
3679 char_u **arg,
3680 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003681 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003682 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003683{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003684 int evaluate = evalarg != NULL
3685 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003686 typval_T base = *rettv;
3687 int ret;
3688
3689 // Skip over the ->.
3690 *arg += 2;
3691 rettv->v_type = VAR_UNKNOWN;
3692
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003693 if (**arg == '{')
3694 {
3695 // ->{lambda}()
3696 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3697 }
3698 else
3699 {
3700 // ->(lambda)()
3701 ++*arg;
3702 ret = eval1(arg, rettv, evalarg);
3703 *arg = skipwhite_and_linebreak(*arg, evalarg);
3704 if (**arg != ')')
3705 {
3706 emsg(_(e_missing_close));
3707 ret = FAIL;
3708 }
3709 ++*arg;
3710 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003711 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003712 return FAIL;
3713 else if (**arg != '(')
3714 {
3715 if (verbose)
3716 {
3717 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003718 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003719 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003721 }
3722 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003723 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003724 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003725 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003726 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003727
3728 // Clear the funcref afterwards, so that deleting it while
3729 // evaluating the arguments is possible (see test55).
3730 if (evaluate)
3731 clear_tv(&base);
3732
3733 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003734}
3735
3736/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003737 * Evaluate "->method()".
3738 * "*arg" points to the '-'.
3739 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3740 */
3741 static int
3742eval_method(
3743 char_u **arg,
3744 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003745 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003746 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003747{
3748 char_u *name;
3749 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003750 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003751 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003752 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003753 int evaluate = evalarg != NULL
3754 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003755
3756 // Skip over the ->.
3757 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003758 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003759
Bram Moolenaarac92e252019-08-03 21:58:38 +02003760 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003761 len = get_name_len(arg, &alias, evaluate, TRUE);
3762 if (alias != NULL)
3763 name = alias;
3764
3765 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003766 {
3767 if (verbose)
3768 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003769 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003770 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003771 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003772 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003773 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003774 if (**arg != '(')
3775 {
3776 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003778 ret = FAIL;
3779 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003780 else if (VIM_ISWHITE((*arg)[-1]))
3781 {
3782 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003783 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003784 ret = FAIL;
3785 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003786 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003787 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003788 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003789 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003790
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003791 // Clear the funcref afterwards, so that deleting it while
3792 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003793 if (evaluate)
3794 clear_tv(&base);
3795
Bram Moolenaarac92e252019-08-03 21:58:38 +02003796 return ret;
3797}
3798
3799/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003800 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3801 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003802 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3803 */
3804 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003805eval_index(
3806 char_u **arg,
3807 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003808 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003809 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003810{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003811 int evaluate = evalarg != NULL
3812 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003813 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003814 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003815 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003816 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003817 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003818 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003819
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003820 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3821 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003822
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003823 init_tv(&var1);
3824 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003825 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003826 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003827 /*
3828 * dict.name
3829 */
3830 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003831 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003832 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003833 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003834 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003835 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003836 }
3837 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003838 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003839 /*
3840 * something[idx]
3841 *
3842 * Get the (first) variable from inside the [].
3843 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003844 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003845 if (**arg == ':')
3846 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003847 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003848 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003849 else if (vim9 && **arg == ':')
3850 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003851 semsg(_(e_white_space_required_before_and_after_str_at_str),
3852 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003853 clear_tv(&var1);
3854 return FAIL;
3855 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003856 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003857 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003858 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003859 clear_tv(&var1);
3860 return FAIL;
3861 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003862
3863 /*
3864 * Get the second variable from inside the [:].
3865 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003866 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003867 if (**arg == ':')
3868 {
3869 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003870 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003871 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003872 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003873 semsg(_(e_white_space_required_before_and_after_str_at_str),
3874 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003875 if (!empty1)
3876 clear_tv(&var1);
3877 return FAIL;
3878 }
3879 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003880 if (**arg == ']')
3881 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003882 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003884 if (!empty1)
3885 clear_tv(&var1);
3886 return FAIL;
3887 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003888 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003889 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003890 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003891 if (!empty1)
3892 clear_tv(&var1);
3893 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003894 return FAIL;
3895 }
3896 }
3897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003898 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003899 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003900 if (**arg != ']')
3901 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003902 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003903 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003904 clear_tv(&var1);
3905 if (range)
3906 clear_tv(&var2);
3907 return FAIL;
3908 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003909 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003910 }
3911
3912 if (evaluate)
3913 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003914 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003915 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003916 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003917
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003918 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003919 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003920 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003921 clear_tv(&var2);
3922 return res;
3923 }
3924 return OK;
3925}
3926
3927/*
3928 * Check if "rettv" can have an [index] or [sli:ce]
3929 */
3930 int
3931check_can_index(typval_T *rettv, int evaluate, int verbose)
3932{
3933 switch (rettv->v_type)
3934 {
3935 case VAR_FUNC:
3936 case VAR_PARTIAL:
3937 if (verbose)
3938 emsg(_("E695: Cannot index a Funcref"));
3939 return FAIL;
3940 case VAR_FLOAT:
3941#ifdef FEAT_FLOAT
3942 if (verbose)
3943 emsg(_(e_float_as_string));
3944 return FAIL;
3945#endif
3946 case VAR_BOOL:
3947 case VAR_SPECIAL:
3948 case VAR_JOB:
3949 case VAR_CHANNEL:
3950 if (verbose)
3951 emsg(_(e_cannot_index_special_variable));
3952 return FAIL;
3953 case VAR_UNKNOWN:
3954 case VAR_ANY:
3955 case VAR_VOID:
3956 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003957 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003958 emsg(_(e_cannot_index_special_variable));
3959 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003960 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003961 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003962
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003963 case VAR_STRING:
3964 case VAR_LIST:
3965 case VAR_DICT:
3966 case VAR_BLOB:
3967 break;
3968 case VAR_NUMBER:
3969 if (in_vim9script())
3970 emsg(_(e_cannot_index_number));
3971 break;
3972 }
3973 return OK;
3974}
3975
3976/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01003977 * slice() function
3978 */
3979 void
3980f_slice(typval_T *argvars, typval_T *rettv)
3981{
3982 if (check_can_index(argvars, TRUE, FALSE) == OK)
3983 {
3984 copy_tv(argvars, rettv);
3985 eval_index_inner(rettv, TRUE, argvars + 1,
3986 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
3987 TRUE, NULL, 0, FALSE);
3988 }
3989}
3990
3991/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003992 * Apply index or range to "rettv".
3993 * "var1" is the first index, NULL for [:expr].
3994 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01003995 * "exclusive" is TRUE for slice(): second index is exclusive, use character
3996 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003997 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3998 */
3999 int
4000eval_index_inner(
4001 typval_T *rettv,
4002 int is_range,
4003 typval_T *var1,
4004 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004005 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004006 char_u *key,
4007 int keylen,
4008 int verbose)
4009{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004010 varnumber_T n1, n2 = 0;
4011 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004012
4013 n1 = 0;
4014 if (var1 != NULL && rettv->v_type != VAR_DICT)
4015 n1 = tv_get_number(var1);
4016
4017 if (is_range)
4018 {
4019 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004020 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004021 if (verbose)
4022 emsg(_(e_cannot_slice_dictionary));
4023 return FAIL;
4024 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004025 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004026 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004027 else
4028 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004029 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004030
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004031 switch (rettv->v_type)
4032 {
4033 case VAR_UNKNOWN:
4034 case VAR_ANY:
4035 case VAR_VOID:
4036 case VAR_FUNC:
4037 case VAR_PARTIAL:
4038 case VAR_FLOAT:
4039 case VAR_BOOL:
4040 case VAR_SPECIAL:
4041 case VAR_JOB:
4042 case VAR_CHANNEL:
4043 break; // not evaluating, skipping over subscript
4044
4045 case VAR_NUMBER:
4046 case VAR_STRING:
4047 {
4048 char_u *s = tv_get_string(rettv);
4049
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004050 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004051 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004052 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004053 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004054 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004055 else
4056 s = char_from_string(s, n1);
4057 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004058 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004059 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004060 // The resulting variable is a substring. If the indexes
4061 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004062 if (n1 < 0)
4063 {
4064 n1 = len + n1;
4065 if (n1 < 0)
4066 n1 = 0;
4067 }
4068 if (n2 < 0)
4069 n2 = len + n2;
4070 else if (n2 >= len)
4071 n2 = len;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004072 if (exclusive)
4073 --n2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004074 if (n1 >= len || n2 < 0 || n1 > n2)
4075 s = NULL;
4076 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004077 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004078 }
4079 else
4080 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004081 // The resulting variable is a string of a single
4082 // character. If the index is too big or negative the
4083 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004084 if (n1 >= len || n1 < 0)
4085 s = NULL;
4086 else
4087 s = vim_strnsave(s + n1, 1);
4088 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004089 clear_tv(rettv);
4090 rettv->v_type = VAR_STRING;
4091 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004092 }
4093 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004094
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004095 case VAR_BLOB:
4096 len = blob_len(rettv->vval.v_blob);
4097 if (is_range)
4098 {
4099 // The resulting variable is a sub-blob. If the indexes
4100 // are out of range the result is empty.
4101 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004102 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004103 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004104 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004105 n1 = 0;
4106 }
4107 if (n2 < 0)
4108 n2 = len + n2;
4109 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004110 n2 = len - (exclusive ? 0 : 1);
4111 if (exclusive)
4112 --n2;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004113 if (n1 >= len || n2 < 0 || n1 > n2)
4114 {
4115 clear_tv(rettv);
4116 rettv->v_type = VAR_BLOB;
4117 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004118 }
4119 else
4120 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004121 blob_T *blob = blob_alloc();
4122 long i;
4123
4124 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004125 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004126 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004127 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004128 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004129 return FAIL;
4130 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004131 blob->bv_ga.ga_len = n2 - n1 + 1;
4132 for (i = n1; i <= n2; i++)
4133 blob_set(blob, i - n1,
4134 blob_get(rettv->vval.v_blob, i));
4135
4136 clear_tv(rettv);
4137 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004138 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004139 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004140 }
4141 else
4142 {
4143 // The resulting variable is a byte value.
4144 // If the index is too big or negative that is an error.
4145 if (n1 < 0)
4146 n1 = len + n1;
4147 if (n1 < len && n1 >= 0)
4148 {
4149 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004150
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004151 clear_tv(rettv);
4152 rettv->v_type = VAR_NUMBER;
4153 rettv->vval.v_number = v;
4154 }
4155 else
4156 semsg(_(e_blobidx), n1);
4157 }
4158 break;
4159
4160 case VAR_LIST:
4161 if (var1 == NULL)
4162 n1 = 0;
4163 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004164 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004165 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004166 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004167 return FAIL;
4168 break;
4169
4170 case VAR_DICT:
4171 {
4172 dictitem_T *item;
4173 typval_T tmp;
4174
4175 if (key == NULL)
4176 {
4177 key = tv_get_string_chk(var1);
4178 if (key == NULL)
4179 return FAIL;
4180 }
4181
4182 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4183
4184 if (item == NULL && verbose)
4185 semsg(_(e_dictkey), key);
4186 if (item == NULL)
4187 return FAIL;
4188
4189 copy_tv(&item->di_tv, &tmp);
4190 clear_tv(rettv);
4191 *rettv = tmp;
4192 }
4193 break;
4194 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004195 return OK;
4196}
4197
4198/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004199 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004200 */
4201 char_u *
4202partial_name(partial_T *pt)
4203{
4204 if (pt->pt_name != NULL)
4205 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004206 if (pt->pt_func != NULL)
4207 return pt->pt_func->uf_name;
4208 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004209}
4210
Bram Moolenaarddecc252016-04-06 22:59:37 +02004211 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004212partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004213{
4214 int i;
4215
4216 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004217 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004218 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004219 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004220 if (pt->pt_name != NULL)
4221 {
4222 func_unref(pt->pt_name);
4223 vim_free(pt->pt_name);
4224 }
4225 else
4226 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004227
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004228 // Decrease the reference count for the context of a closure. If down
4229 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004230 if (pt->pt_funcstack != NULL)
4231 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004232 --pt->pt_funcstack->fs_refcount;
4233 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004234 }
4235
Bram Moolenaarddecc252016-04-06 22:59:37 +02004236 vim_free(pt);
4237}
4238
4239/*
4240 * Unreference a closure: decrement the reference count and free it when it
4241 * becomes zero.
4242 */
4243 void
4244partial_unref(partial_T *pt)
4245{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004246 if (pt != NULL)
4247 {
4248 if (--pt->pt_refcount <= 0)
4249 partial_free(pt);
4250
4251 // If the reference count goes down to one, the funcstack may be the
4252 // only reference and can be freed if no other partials reference it.
4253 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4254 funcstack_check_refcount(pt->pt_funcstack);
4255 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004256}
4257
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004258/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004259 * Return the next (unique) copy ID.
4260 * Used for serializing nested structures.
4261 */
4262 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004263get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004264{
4265 current_copyID += COPYID_INC;
4266 return current_copyID;
4267}
4268
4269/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004270 * Garbage collection for lists and dictionaries.
4271 *
4272 * We use reference counts to be able to free most items right away when they
4273 * are no longer used. But for composite items it's possible that it becomes
4274 * unused while the reference count is > 0: When there is a recursive
4275 * reference. Example:
4276 * :let l = [1, 2, 3]
4277 * :let d = {9: l}
4278 * :let l[1] = d
4279 *
4280 * Since this is quite unusual we handle this with garbage collection: every
4281 * once in a while find out which lists and dicts are not referenced from any
4282 * variable.
4283 *
4284 * Here is a good reference text about garbage collection (refers to Python
4285 * but it applies to all reference-counting mechanisms):
4286 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004287 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004288
4289/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004290 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004291 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004292 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004293 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004294 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004295garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004296{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004297 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004298 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004299 buf_T *buf;
4300 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004301 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004302 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004303
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004304 if (!testing)
4305 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004306 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004307 want_garbage_collect = FALSE;
4308 may_garbage_collect = FALSE;
4309 garbage_collect_at_exit = FALSE;
4310 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004311
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004312 // The execution stack can grow big, limit the size.
4313 if (exestack.ga_maxlen - exestack.ga_len > 500)
4314 {
4315 size_t new_len;
4316 char_u *pp;
4317 int n;
4318
4319 // Keep 150% of the current size, with a minimum of the growth size.
4320 n = exestack.ga_len / 2;
4321 if (n < exestack.ga_growsize)
4322 n = exestack.ga_growsize;
4323
4324 // Don't make it bigger though.
4325 if (exestack.ga_len + n < exestack.ga_maxlen)
4326 {
4327 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4328 pp = vim_realloc(exestack.ga_data, new_len);
4329 if (pp == NULL)
4330 return FAIL;
4331 exestack.ga_maxlen = exestack.ga_len + n;
4332 exestack.ga_data = pp;
4333 }
4334 }
4335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004336 // We advance by two because we add one for items referenced through
4337 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004338 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004339
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004340 /*
4341 * 1. Go through all accessible variables and mark all lists and dicts
4342 * with copyID.
4343 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004344
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004345 // Don't free variables in the previous_funccal list unless they are only
4346 // referenced through previous_funccal. This must be first, because if
4347 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004348 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004349
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004350 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004351 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004352
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004353 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004354 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004355 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4356 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004358 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004359 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004360 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4361 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004362 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004363 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4364 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004365#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004366 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004367 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4368 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004369 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004370 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004371 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4372 NULL, NULL);
4373#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004375 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004376 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004377 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4378 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004379 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004380 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004382 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004383 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004384
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004385 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004386 abort = abort || set_ref_in_functions(copyID);
4387
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004388 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004389 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004390
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004391 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004392 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004393
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004394 // callbacks in buffers
4395 abort = abort || set_ref_in_buffers(copyID);
4396
Bram Moolenaar1dced572012-04-05 16:54:08 +02004397#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004398 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004399#endif
4400
Bram Moolenaardb913952012-06-29 12:54:53 +02004401#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004402 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004403#endif
4404
4405#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004406 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004407#endif
4408
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004409#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004410 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004411 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004412#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004413#ifdef FEAT_NETBEANS_INTG
4414 abort = abort || set_ref_in_nb_channel(copyID);
4415#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004416
Bram Moolenaare3188e22016-05-31 21:13:04 +02004417#ifdef FEAT_TIMERS
4418 abort = abort || set_ref_in_timer(copyID);
4419#endif
4420
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004421#ifdef FEAT_QUICKFIX
4422 abort = abort || set_ref_in_quickfix(copyID);
4423#endif
4424
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004425#ifdef FEAT_TERMINAL
4426 abort = abort || set_ref_in_term(copyID);
4427#endif
4428
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004429#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004430 abort = abort || set_ref_in_popups(copyID);
4431#endif
4432
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004433 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004434 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004435 /*
4436 * 2. Free lists and dictionaries that are not referenced.
4437 */
4438 did_free = free_unref_items(copyID);
4439
4440 /*
4441 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004442 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004443 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004444 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004445 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004446 else if (p_verbose > 0)
4447 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004448 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004449 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004450
4451 return did_free;
4452}
4453
4454/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004455 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004456 */
4457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004458free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004459{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004460 int did_free = FALSE;
4461
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004462 // Let all "free" functions know that we are here. This means no
4463 // dictionaries, lists, channels or jobs are to be freed, because we will
4464 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004465 in_free_unref_items = TRUE;
4466
4467 /*
4468 * PASS 1: free the contents of the items. We don't free the items
4469 * themselves yet, so that it is possible to decrement refcount counters
4470 */
4471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004472 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004473 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004475 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004476 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004477
4478#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 // Go through the list of jobs and free items without the copyID. This
4480 // must happen before doing channels, because jobs refer to channels, but
4481 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004482 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4483
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004484 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004485 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4486#endif
4487
4488 /*
4489 * PASS 2: free the items themselves.
4490 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004491 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004492 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004493
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004494#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004495 // Go through the list of jobs and free items without the copyID. This
4496 // must happen before doing channels, because jobs refer to channels, but
4497 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004498 free_unused_jobs(copyID, COPYID_MASK);
4499
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004500 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004501 free_unused_channels(copyID, COPYID_MASK);
4502#endif
4503
4504 in_free_unref_items = FALSE;
4505
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004506 return did_free;
4507}
4508
4509/*
4510 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004511 * "list_stack" is used to add lists to be marked. Can be NULL.
4512 *
4513 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004514 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004516set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004517{
4518 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004519 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004520 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004521 hashtab_T *cur_ht;
4522 ht_stack_T *ht_stack = NULL;
4523 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004524
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004525 cur_ht = ht;
4526 for (;;)
4527 {
4528 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004529 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004530 // Mark each item in the hashtab. If the item contains a hashtab
4531 // it is added to ht_stack, if it contains a list it is added to
4532 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004533 todo = (int)cur_ht->ht_used;
4534 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4535 if (!HASHITEM_EMPTY(hi))
4536 {
4537 --todo;
4538 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4539 &ht_stack, list_stack);
4540 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004541 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004542
4543 if (ht_stack == NULL)
4544 break;
4545
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004546 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004547 cur_ht = ht_stack->ht;
4548 tempitem = ht_stack;
4549 ht_stack = ht_stack->prev;
4550 free(tempitem);
4551 }
4552
4553 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004554}
4555
4556/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004557 * Mark a dict and its items with "copyID".
4558 * Returns TRUE if setting references failed somehow.
4559 */
4560 int
4561set_ref_in_dict(dict_T *d, int copyID)
4562{
4563 if (d != NULL && d->dv_copyID != copyID)
4564 {
4565 d->dv_copyID = copyID;
4566 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4567 }
4568 return FALSE;
4569}
4570
4571/*
4572 * Mark a list and its items with "copyID".
4573 * Returns TRUE if setting references failed somehow.
4574 */
4575 int
4576set_ref_in_list(list_T *ll, int copyID)
4577{
4578 if (ll != NULL && ll->lv_copyID != copyID)
4579 {
4580 ll->lv_copyID = copyID;
4581 return set_ref_in_list_items(ll, copyID, NULL);
4582 }
4583 return FALSE;
4584}
4585
4586/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004587 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004588 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4589 *
4590 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004591 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004592 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004593set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004594{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004595 listitem_T *li;
4596 int abort = FALSE;
4597 list_T *cur_l;
4598 list_stack_T *list_stack = NULL;
4599 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004600
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004601 cur_l = l;
4602 for (;;)
4603 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004604 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004605 // Mark each item in the list. If the item contains a hashtab
4606 // it is added to ht_stack, if it contains a list it is added to
4607 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004608 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4609 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4610 ht_stack, &list_stack);
4611 if (list_stack == NULL)
4612 break;
4613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004614 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004615 cur_l = list_stack->list;
4616 tempitem = list_stack;
4617 list_stack = list_stack->prev;
4618 free(tempitem);
4619 }
4620
4621 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004622}
4623
4624/*
4625 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004626 * "list_stack" is used to add lists to be marked. Can be NULL.
4627 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4628 *
4629 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004630 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004632set_ref_in_item(
4633 typval_T *tv,
4634 int copyID,
4635 ht_stack_T **ht_stack,
4636 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004637{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004638 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004639
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004640 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004641 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004642 dict_T *dd = tv->vval.v_dict;
4643
Bram Moolenaara03f2332016-02-06 18:09:59 +01004644 if (dd != NULL && dd->dv_copyID != copyID)
4645 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004646 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004647 dd->dv_copyID = copyID;
4648 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004649 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004650 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4651 }
4652 else
4653 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004654 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4655
Bram Moolenaara03f2332016-02-06 18:09:59 +01004656 if (newitem == NULL)
4657 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004658 else
4659 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004660 newitem->ht = &dd->dv_hashtab;
4661 newitem->prev = *ht_stack;
4662 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004663 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004664 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004665 }
4666 }
4667 else if (tv->v_type == VAR_LIST)
4668 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004669 list_T *ll = tv->vval.v_list;
4670
Bram Moolenaara03f2332016-02-06 18:09:59 +01004671 if (ll != NULL && ll->lv_copyID != copyID)
4672 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004673 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004674 ll->lv_copyID = copyID;
4675 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004676 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004677 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004678 }
4679 else
4680 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004681 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4682
Bram Moolenaara03f2332016-02-06 18:09:59 +01004683 if (newitem == NULL)
4684 abort = TRUE;
4685 else
4686 {
4687 newitem->list = ll;
4688 newitem->prev = *list_stack;
4689 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004690 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004691 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004692 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004693 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004694 else if (tv->v_type == VAR_FUNC)
4695 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004696 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004697 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004698 else if (tv->v_type == VAR_PARTIAL)
4699 {
4700 partial_T *pt = tv->vval.v_partial;
4701 int i;
4702
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004703 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004704 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004705 // Didn't see this partial yet.
4706 pt->pt_copyID = copyID;
4707
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004708 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004709
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004710 if (pt->pt_dict != NULL)
4711 {
4712 typval_T dtv;
4713
4714 dtv.v_type = VAR_DICT;
4715 dtv.vval.v_dict = pt->pt_dict;
4716 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4717 }
4718
4719 for (i = 0; i < pt->pt_argc; ++i)
4720 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4721 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004722 if (pt->pt_funcstack != NULL)
4723 {
4724 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4725
4726 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4727 abort = abort || set_ref_in_item(stack + i, copyID,
4728 ht_stack, list_stack);
4729 }
4730
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004731 }
4732 }
4733#ifdef FEAT_JOB_CHANNEL
4734 else if (tv->v_type == VAR_JOB)
4735 {
4736 job_T *job = tv->vval.v_job;
4737 typval_T dtv;
4738
4739 if (job != NULL && job->jv_copyID != copyID)
4740 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004741 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004742 if (job->jv_channel != NULL)
4743 {
4744 dtv.v_type = VAR_CHANNEL;
4745 dtv.vval.v_channel = job->jv_channel;
4746 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4747 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004748 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004749 {
4750 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004751 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004752 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4753 }
4754 }
4755 }
4756 else if (tv->v_type == VAR_CHANNEL)
4757 {
4758 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004759 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004760 typval_T dtv;
4761 jsonq_T *jq;
4762 cbq_T *cq;
4763
4764 if (ch != NULL && ch->ch_copyID != copyID)
4765 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004766 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004767 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004768 {
4769 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4770 jq = jq->jq_next)
4771 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4772 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4773 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004774 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004775 {
4776 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004777 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004778 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4779 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004780 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004781 {
4782 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004783 dtv.vval.v_partial =
4784 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004785 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4786 }
4787 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004788 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004789 {
4790 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004791 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004792 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4793 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004794 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004795 {
4796 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004797 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004798 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4799 }
4800 }
4801 }
4802#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004803 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004804}
4805
Bram Moolenaar8c711452005-01-14 21:53:12 +00004806/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004807 * Return a string with the string representation of a variable.
4808 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004809 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004810 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004811 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004812 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004813 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004814 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4815 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004816 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004817 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004818 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004819echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004820 typval_T *tv,
4821 char_u **tofree,
4822 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004823 int copyID,
4824 int echo_style,
4825 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004826 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004828 static int recurse = 0;
4829 char_u *r = NULL;
4830
Bram Moolenaar33570922005-01-25 22:26:29 +00004831 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004832 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004833 if (!did_echo_string_emsg)
4834 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004835 // Only give this message once for a recursive call to avoid
4836 // flooding the user with errors. And stop iterating over lists
4837 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004838 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004839 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004841 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004842 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004843 }
4844 ++recurse;
4845
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004846 switch (tv->v_type)
4847 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004848 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004849 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004850 {
4851 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004852 r = tv->vval.v_string;
4853 if (r == NULL)
4854 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004855 }
4856 else
4857 {
4858 *tofree = string_quote(tv->vval.v_string, FALSE);
4859 r = *tofree;
4860 }
4861 break;
4862
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004863 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004864 if (echo_style)
4865 {
4866 *tofree = NULL;
4867 r = tv->vval.v_string;
4868 }
4869 else
4870 {
4871 *tofree = string_quote(tv->vval.v_string, TRUE);
4872 r = *tofree;
4873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004874 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004875
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004876 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004877 {
4878 partial_T *pt = tv->vval.v_partial;
4879 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004880 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004881 garray_T ga;
4882 int i;
4883 char_u *tf;
4884
4885 ga_init2(&ga, 1, 100);
4886 ga_concat(&ga, (char_u *)"function(");
4887 if (fname != NULL)
4888 {
4889 ga_concat(&ga, fname);
4890 vim_free(fname);
4891 }
4892 if (pt != NULL && pt->pt_argc > 0)
4893 {
4894 ga_concat(&ga, (char_u *)", [");
4895 for (i = 0; i < pt->pt_argc; ++i)
4896 {
4897 if (i > 0)
4898 ga_concat(&ga, (char_u *)", ");
4899 ga_concat(&ga,
4900 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4901 vim_free(tf);
4902 }
4903 ga_concat(&ga, (char_u *)"]");
4904 }
4905 if (pt != NULL && pt->pt_dict != NULL)
4906 {
4907 typval_T dtv;
4908
4909 ga_concat(&ga, (char_u *)", ");
4910 dtv.v_type = VAR_DICT;
4911 dtv.vval.v_dict = pt->pt_dict;
4912 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4913 vim_free(tf);
4914 }
4915 ga_concat(&ga, (char_u *)")");
4916
4917 *tofree = ga.ga_data;
4918 r = *tofree;
4919 break;
4920 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004921
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004922 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004923 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004924 break;
4925
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004926 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004927 if (tv->vval.v_list == NULL)
4928 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004929 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004930 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004931 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004932 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004933 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4934 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004935 {
4936 *tofree = NULL;
4937 r = (char_u *)"[...]";
4938 }
4939 else
4940 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004941 int old_copyID = tv->vval.v_list->lv_copyID;
4942
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004943 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004944 *tofree = list2string(tv, copyID, restore_copyID);
4945 if (restore_copyID)
4946 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004947 r = *tofree;
4948 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004949 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004950
Bram Moolenaar8c711452005-01-14 21:53:12 +00004951 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004952 if (tv->vval.v_dict == NULL)
4953 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004954 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004955 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004956 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004957 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004958 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4959 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004960 {
4961 *tofree = NULL;
4962 r = (char_u *)"{...}";
4963 }
4964 else
4965 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004966 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004967
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004968 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004969 *tofree = dict2string(tv, copyID, restore_copyID);
4970 if (restore_copyID)
4971 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004972 r = *tofree;
4973 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004974 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004975
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004976 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004977 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004978 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004980 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004981 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004982 break;
4983
Bram Moolenaar835dc632016-02-07 14:27:38 +01004984 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004985 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004986 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004987 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004988 if (composite_val)
4989 {
4990 *tofree = string_quote(r, FALSE);
4991 r = *tofree;
4992 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004993 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004994
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004996#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 *tofree = NULL;
4998 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4999 r = numbuf;
5000 break;
5001#endif
5002
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005003 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005004 case VAR_SPECIAL:
5005 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005006 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005007 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005008 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005009
Bram Moolenaar8502c702014-06-17 12:51:16 +02005010 if (--recurse == 0)
5011 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005012 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005013}
5014
5015/*
5016 * Return a string with the string representation of a variable.
5017 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5018 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005019 * Does not put quotes around strings, as ":echo" displays values.
5020 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5021 * May return NULL.
5022 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005023 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005024echo_string(
5025 typval_T *tv,
5026 char_u **tofree,
5027 char_u *numbuf,
5028 int copyID)
5029{
5030 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5031}
5032
5033/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005034 * Return string "str" in ' quotes, doubling ' characters.
5035 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005037 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005038 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005039string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005040{
Bram Moolenaar33570922005-01-25 22:26:29 +00005041 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005042 char_u *p, *r, *s;
5043
Bram Moolenaar33570922005-01-25 22:26:29 +00005044 len = (function ? 13 : 3);
5045 if (str != NULL)
5046 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005047 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005048 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005049 if (*p == '\'')
5050 ++len;
5051 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005052 s = r = alloc(len);
5053 if (r != NULL)
5054 {
5055 if (function)
5056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 r += 10;
5059 }
5060 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005062 if (str != NULL)
5063 for (p = str; *p != NUL; )
5064 {
5065 if (*p == '\'')
5066 *r++ = '\'';
5067 MB_COPY_CHAR(p, r);
5068 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 if (function)
5071 *r++ = ')';
5072 *r++ = NUL;
5073 }
5074 return s;
5075}
5076
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005077#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005078/*
5079 * Convert the string "text" to a floating point number.
5080 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5081 * this always uses a decimal point.
5082 * Returns the length of the text that was consumed.
5083 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005085string2float(
5086 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005087 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005088{
5089 char *s = (char *)text;
5090 float_T f;
5091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005092 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005093 if (STRNICMP(text, "inf", 3) == 0)
5094 {
5095 *value = INFINITY;
5096 return 3;
5097 }
5098 if (STRNICMP(text, "-inf", 3) == 0)
5099 {
5100 *value = -INFINITY;
5101 return 4;
5102 }
5103 if (STRNICMP(text, "nan", 3) == 0)
5104 {
5105 *value = NAN;
5106 return 3;
5107 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005108 f = strtod(s, &s);
5109 *value = f;
5110 return (int)((char_u *)s - text);
5111}
5112#endif
5113
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005114/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005115 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5116 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005117 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005118 */
5119 int
5120buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5121{
5122 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005123 char_u *t;
5124 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005125
5126 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5127 return -1;
5128
5129 if (lnum > buf->b_ml.ml_line_count)
5130 lnum = buf->b_ml.ml_line_count;
5131
5132 str = ml_get_buf(buf, lnum, FALSE);
5133 if (str == NULL)
5134 return -1;
5135
5136 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005137 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005138
Bram Moolenaar91458462021-01-13 20:08:38 +01005139 // count the number of characters
5140 t = str;
5141 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5142 t += mb_ptr2len(t);
5143
5144 // In insert mode, when the cursor is at the end of a non-empty line,
5145 // byteidx points to the NUL character immediately past the end of the
5146 // string. In this case, add one to the character count.
5147 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5148 count++;
5149
5150 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005151}
5152
5153/*
5154 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005155 * byte index. Works only for loaded buffers. Returns -1 on failure.
5156 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005157 */
5158 int
5159buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5160{
5161 char_u *str;
5162 char_u *t;
5163
5164 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5165 return -1;
5166
5167 if (lnum > buf->b_ml.ml_line_count)
5168 lnum = buf->b_ml.ml_line_count;
5169
5170 str = ml_get_buf(buf, lnum, FALSE);
5171 if (str == NULL)
5172 return -1;
5173
5174 // Convert the character offset to a byte offset
5175 t = str;
5176 while (*t != NUL && --charidx > 0)
5177 t += mb_ptr2len(t);
5178
Bram Moolenaar91458462021-01-13 20:08:38 +01005179 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005180}
5181
5182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005184 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005186 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005187var2fpos(
5188 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005189 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005190 int *fnum, // set to fnum for '0, 'A, etc.
5191 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005193 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005195 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005197 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005198 if (varp->v_type == VAR_LIST)
5199 {
5200 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005201 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005202 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005203 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005204
5205 l = varp->vval.v_list;
5206 if (l == NULL)
5207 return NULL;
5208
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005209 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005210 pos.lnum = list_find_nr(l, 0L, &error);
5211 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005212 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005213 if (charcol)
5214 len = (long)mb_charlen(ml_get(pos.lnum));
5215 else
5216 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005217
Bram Moolenaarec65d772020-08-20 22:29:12 +02005218 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005219 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005220 li = list_find(l, 1L);
5221 if (li != NULL && li->li_tv.v_type == VAR_STRING
5222 && li->li_tv.vval.v_string != NULL
5223 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005224 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005225 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005226 }
5227 else
5228 {
5229 pos.col = list_find_nr(l, 1L, &error);
5230 if (error)
5231 return NULL;
5232 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005233
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005234 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005235 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005237 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005238
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005239 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005240 pos.coladd = list_find_nr(l, 2L, &error);
5241 if (error)
5242 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005243
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005244 return &pos;
5245 }
5246
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005247 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005248 if (name == NULL)
5249 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005250 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005251 {
5252 pos = curwin->w_cursor;
5253 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005254 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005255 return &pos;
5256 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005257 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005258 {
5259 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005260 pos = VIsual;
5261 else
5262 pos = curwin->w_cursor;
5263 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005264 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005265 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005266 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005267 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005269 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5271 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005272 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005273 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 return pp;
5275 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005276
Bram Moolenaara5525202006-03-02 22:52:09 +00005277 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005278
Bram Moolenaar477933c2007-07-17 14:32:23 +00005279 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005280 {
5281 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005282 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005283 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005284 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005285 // In silent Ex mode topline is zero, but that's not a valid line
5286 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005287 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005288 return &pos;
5289 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005290 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005291 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005292 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005293 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005294 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005295 return &pos;
5296 }
5297 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005298 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005300 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {
5302 pos.lnum = curbuf->b_ml.ml_line_count;
5303 pos.col = 0;
5304 }
5305 else
5306 {
5307 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005308 if (charcol)
5309 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5310 else
5311 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
5313 return &pos;
5314 }
5315 return NULL;
5316}
5317
5318/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005319 * Convert list in "arg" into a position and optional file number.
5320 * When "fnump" is NULL there is no file number, only 3 items.
5321 * Note that the column is passed on as-is, the caller may want to decrement
5322 * it to use 1 for the first column.
5323 * Return FAIL when conversion is not possible, doesn't check the position for
5324 * validity.
5325 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005326 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005327list2fpos(
5328 typval_T *arg,
5329 pos_T *posp,
5330 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005331 colnr_T *curswantp,
5332 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005333{
5334 list_T *l = arg->vval.v_list;
5335 long i = 0;
5336 long n;
5337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005338 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5339 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005340 if (arg->v_type != VAR_LIST
5341 || l == NULL
5342 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005343 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005344 return FAIL;
5345
5346 if (fnump != NULL)
5347 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005348 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005349 if (n < 0)
5350 return FAIL;
5351 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005352 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005353 *fnump = n;
5354 }
5355
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005356 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005357 if (n < 0)
5358 return FAIL;
5359 posp->lnum = n;
5360
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005361 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005362 if (n < 0)
5363 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005364 // If character position is specified, then convert to byte position
5365 if (charcol)
5366 {
5367 buf_T *buf;
5368
5369 // Get the text for the specified line in a loaded buffer
5370 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5371 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5372 return FAIL;
5373
Bram Moolenaar91458462021-01-13 20:08:38 +01005374 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005375 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005376 posp->col = n;
5377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005379 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005380 posp->coladd = 0;
5381 else
5382 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005383
Bram Moolenaar493c1782014-05-28 14:34:46 +02005384 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005385 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005386
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005387 return OK;
5388}
5389
5390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 * Get the length of an environment variable name.
5392 * Advance "arg" to the first character after the name.
5393 * Return 0 for error.
5394 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005395 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005396get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398 char_u *p;
5399 int len;
5400
5401 for (p = *arg; vim_isIDc(*p); ++p)
5402 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005403 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 return 0;
5405
5406 len = (int)(p - *arg);
5407 *arg = p;
5408 return len;
5409}
5410
5411/*
5412 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005413 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 * Return 0 if something is wrong.
5415 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005417get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
5419 char_u *p;
5420 int len;
5421
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005422 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005424 {
5425 if (*p == ':')
5426 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005427 // "s:" is start of "s:var", but "n:" is not and can be used in
5428 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005429 len = (int)(p - *arg);
5430 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5431 || len > 1)
5432 break;
5433 }
5434 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005435 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 return 0;
5437
5438 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005439 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
5441 return len;
5442}
5443
5444/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005445 * Get the length of the name of a variable or function.
5446 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005448 * Return -1 if curly braces expansion failed.
5449 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 * If the name contains 'magic' {}'s, expand them and return the
5451 * expanded name in an allocated string via 'alias' - caller must free.
5452 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005454get_name_len(
5455 char_u **arg,
5456 char_u **alias,
5457 int evaluate,
5458 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459{
5460 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 char_u *p;
5462 char_u *expr_start;
5463 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005465 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
5467 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5468 && (*arg)[2] == (int)KE_SNR)
5469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005470 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 *arg += 3;
5472 return get_id_len(arg) + 3;
5473 }
5474 len = eval_fname_script(*arg);
5475 if (len > 0)
5476 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005477 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 *arg += len;
5479 }
5480
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005482 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005484 p = find_name_end(*arg, &expr_start, &expr_end,
5485 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 if (expr_start != NULL)
5487 {
5488 char_u *temp_string;
5489
5490 if (!evaluate)
5491 {
5492 len += (int)(p - *arg);
5493 *arg = skipwhite(p);
5494 return len;
5495 }
5496
5497 /*
5498 * Include any <SID> etc in the expanded string:
5499 * Thus the -len here.
5500 */
5501 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5502 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005503 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 *alias = temp_string;
5505 *arg = skipwhite(p);
5506 return (int)STRLEN(temp_string);
5507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
5509 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005510 // Only give an error when there is something, otherwise it will be
5511 // reported at a higher level.
5512 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005513 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514
5515 return len;
5516}
5517
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518/*
5519 * Find the end of a variable or function name, taking care of magic braces.
5520 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5521 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005522 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 * Return a pointer to just after the name. Equal to "arg" if there is no
5524 * valid name.
5525 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005526 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005527find_name_end(
5528 char_u *arg,
5529 char_u **expr_start,
5530 char_u **expr_end,
5531 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 int mb_nest = 0;
5534 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005536 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005537 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 *expr_start = NULL;
5542 *expr_end = NULL;
5543 }
5544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005545 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005546 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5547 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005548 return arg;
5549
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 for (p = arg; *p != NUL
5551 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005552 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005553 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005554 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005556 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005558 if (*p == '\'')
5559 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005560 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005561 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005562 ;
5563 if (*p == NUL)
5564 break;
5565 }
5566 else if (*p == '"')
5567 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005568 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005569 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005570 if (*p == '\\' && p[1] != NUL)
5571 ++p;
5572 if (*p == NUL)
5573 break;
5574 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005575 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005577 // "s:" is start of "s:var", but "n:" is not and can be used in
5578 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005579 len = (int)(p - arg);
5580 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005581 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005582 break;
5583 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005584
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005587 if (*p == '[')
5588 ++br_nest;
5589 else if (*p == ']')
5590 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005592
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005593 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005595 if (*p == '{')
5596 {
5597 mb_nest++;
5598 if (expr_start != NULL && *expr_start == NULL)
5599 *expr_start = p;
5600 }
5601 else if (*p == '}')
5602 {
5603 mb_nest--;
5604 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5605 *expr_end = p;
5606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 }
5609
5610 return p;
5611}
5612
5613/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005614 * Expands out the 'magic' {}'s in a variable/function name.
5615 * Note that this can call itself recursively, to deal with
5616 * constructs like foo{bar}{baz}{bam}
5617 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5618 * "in_start" ^
5619 * "expr_start" ^
5620 * "expr_end" ^
5621 * "in_end" ^
5622 *
5623 * Returns a new allocated string, which the caller must free.
5624 * Returns NULL for failure.
5625 */
5626 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005627make_expanded_name(
5628 char_u *in_start,
5629 char_u *expr_start,
5630 char_u *expr_end,
5631 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005632{
5633 char_u c1;
5634 char_u *retval = NULL;
5635 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005636
5637 if (expr_end == NULL || in_end == NULL)
5638 return NULL;
5639 *expr_start = NUL;
5640 *expr_end = NUL;
5641 c1 = *in_end;
5642 *in_end = NUL;
5643
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005644 temp_result = eval_to_string(expr_start + 1, FALSE);
5645 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005646 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005647 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5648 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005649 if (retval != NULL)
5650 {
5651 STRCPY(retval, in_start);
5652 STRCAT(retval, temp_result);
5653 STRCAT(retval, expr_end + 1);
5654 }
5655 }
5656 vim_free(temp_result);
5657
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005658 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005659 *expr_start = '{';
5660 *expr_end = '}';
5661
5662 if (retval != NULL)
5663 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005664 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005665 if (expr_start != NULL)
5666 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005667 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005668 temp_result = make_expanded_name(retval, expr_start,
5669 expr_end, temp_result);
5670 vim_free(retval);
5671 retval = temp_result;
5672 }
5673 }
5674
5675 return retval;
5676}
5677
5678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005680 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005682 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005683eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005685 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005686}
5687
5688/*
5689 * Return TRUE if character "c" can be used as the first character in a
5690 * variable or function name (excluding '{' and '}').
5691 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005692 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005693eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005694{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005695 return ASCII_ISALPHA(c) || c == '_';
5696}
5697
5698/*
5699 * Return TRUE if character "c" can be used as the first character of a
5700 * dictionary key.
5701 */
5702 int
5703eval_isdictc(int c)
5704{
5705 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706}
5707
5708/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005709 * Handle:
5710 * - expr[expr], expr[expr:expr] subscript
5711 * - ".name" lookup
5712 * - function call with Funcref variable: func(expr)
5713 * - method call: var->method()
5714 *
5715 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005716 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005717 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005718handle_subscript(
5719 char_u **arg,
5720 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005721 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005722 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005723{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005724 int evaluate = evalarg != NULL
5725 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005726 int ret = OK;
5727 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005728 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005729 int getnext;
5730 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005731
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005732 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005733 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005734 // When at the end of the line and ".name" or "->{" or "->X" follows in
5735 // the next line then consume the line break.
5736 p = eval_next_non_blank(*arg, evalarg, &getnext);
5737 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005738 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005739 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005740 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005741 {
5742 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005743 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005744 check_white = FALSE;
5745 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005746
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005747 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5748 || rettv->v_type == VAR_PARTIAL))
5749 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005750 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005751 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5752 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005753
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005754 // Stop the expression evaluation when immediately aborting on
5755 // error, or when an interrupt occurred or an exception was thrown
5756 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005757 if (aborting())
5758 {
5759 if (ret == OK)
5760 clear_tv(rettv);
5761 ret = FAIL;
5762 }
5763 dict_unref(selfdict);
5764 selfdict = NULL;
5765 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005766 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005767 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005768 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005769 if (ret == OK)
5770 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005771 if (((*arg)[2] == '{' && !in_vim9script()) || (*arg)[2] == '(')
5772 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005773 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005774 else
5775 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005776 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005777 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005778 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005779 // "." is ".name" lookup when we found a dict or when evaluating and
5780 // scriptversion is at least 2, where string concatenation is "..".
5781 else if (**arg == '['
5782 || (**arg == '.' && (rettv->v_type == VAR_DICT
5783 || (!evaluate
5784 && (*arg)[1] != '.'
5785 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005786 {
5787 dict_unref(selfdict);
5788 if (rettv->v_type == VAR_DICT)
5789 {
5790 selfdict = rettv->vval.v_dict;
5791 if (selfdict != NULL)
5792 ++selfdict->dv_refcount;
5793 }
5794 else
5795 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005796 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005797 {
5798 clear_tv(rettv);
5799 ret = FAIL;
5800 }
5801 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005802 else
5803 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005804 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005805
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005806 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5807 // Don't do this when "Func" is already a partial that was bound
5808 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005809 if (selfdict != NULL
5810 && (rettv->v_type == VAR_FUNC
5811 || (rettv->v_type == VAR_PARTIAL
5812 && (rettv->vval.v_partial->pt_auto
5813 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005814 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005815
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005816 dict_unref(selfdict);
5817 return ret;
5818}
5819
5820/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005821 * Make a copy of an item.
5822 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005823 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5824 * reference to an already copied list/dict can be used.
5825 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005826 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005828item_copy(
5829 typval_T *from,
5830 typval_T *to,
5831 int deep,
5832 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005833{
5834 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005835 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005836
Bram Moolenaar33570922005-01-25 22:26:29 +00005837 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005838 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005839 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005840 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005841 }
5842 ++recurse;
5843
5844 switch (from->v_type)
5845 {
5846 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005847 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005848 case VAR_STRING:
5849 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005850 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005851 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005852 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005853 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005854 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005855 copy_tv(from, to);
5856 break;
5857 case VAR_LIST:
5858 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005859 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005860 if (from->vval.v_list == NULL)
5861 to->vval.v_list = NULL;
5862 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005864 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005865 to->vval.v_list = from->vval.v_list->lv_copylist;
5866 ++to->vval.v_list->lv_refcount;
5867 }
5868 else
5869 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5870 if (to->vval.v_list == NULL)
5871 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005872 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005873 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005874 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005875 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005876 case VAR_DICT:
5877 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005878 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005879 if (from->vval.v_dict == NULL)
5880 to->vval.v_dict = NULL;
5881 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5882 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005883 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005884 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5885 ++to->vval.v_dict->dv_refcount;
5886 }
5887 else
5888 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5889 if (to->vval.v_dict == NULL)
5890 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005891 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005892 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005893 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005894 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005895 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005896 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005897 }
5898 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005899 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005900}
5901
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005902 void
5903echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5904{
5905 char_u *tofree;
5906 char_u numbuf[NUMBUFLEN];
5907 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5908
5909 if (*atstart)
5910 {
5911 *atstart = FALSE;
5912 // Call msg_start() after eval1(), evaluating the expression
5913 // may cause a message to appear.
5914 if (with_space)
5915 {
5916 // Mark the saved text as finishing the line, so that what
5917 // follows is displayed on a new line when scrolling back
5918 // at the more prompt.
5919 msg_sb_eol();
5920 msg_start();
5921 }
5922 }
5923 else if (with_space)
5924 msg_puts_attr(" ", echo_attr);
5925
5926 if (p != NULL)
5927 for ( ; *p != NUL && !got_int; ++p)
5928 {
5929 if (*p == '\n' || *p == '\r' || *p == TAB)
5930 {
5931 if (*p != TAB && *needclr)
5932 {
5933 // remove any text still there from the command
5934 msg_clr_eos();
5935 *needclr = FALSE;
5936 }
5937 msg_putchar_attr(*p, echo_attr);
5938 }
5939 else
5940 {
5941 if (has_mbyte)
5942 {
5943 int i = (*mb_ptr2len)(p);
5944
5945 (void)msg_outtrans_len_attr(p, i, echo_attr);
5946 p += i - 1;
5947 }
5948 else
5949 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5950 }
5951 }
5952 vim_free(tofree);
5953}
5954
Bram Moolenaare9a41262005-01-15 22:18:47 +00005955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 * ":echo expr1 ..." print each argument separated with a space, add a
5957 * newline at the end.
5958 * ":echon expr1 ..." print each argument plain.
5959 */
5960 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005961ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
5963 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005964 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 char_u *p;
5966 int needclr = TRUE;
5967 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005968 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005969 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005970 evalarg_T evalarg;
5971
Bram Moolenaare707c882020-07-01 13:07:37 +02005972 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973
5974 if (eap->skip)
5975 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005976 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005978 // If eval1() causes an error message the text from the command may
5979 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005980 need_clr_eos = needclr;
5981
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005983 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 {
5985 /*
5986 * Report the invalid expression unless the expression evaluation
5987 * has been cancelled due to an aborting error, an interrupt, or an
5988 * exception.
5989 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005990 if (!aborting() && did_emsg == did_emsg_before
5991 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005992 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005993 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 break;
5995 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005996 need_clr_eos = FALSE;
5997
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006000
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006001 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 arg = skipwhite(arg);
6003 }
6004 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006005 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
6007 if (eap->skip)
6008 --emsg_skip;
6009 else
6010 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006011 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 if (needclr)
6013 msg_clr_eos();
6014 if (eap->cmdidx == CMD_echo)
6015 msg_end();
6016 }
6017}
6018
6019/*
6020 * ":echohl {name}".
6021 */
6022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006023ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006025 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026}
6027
6028/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006029 * Returns the :echo attribute
6030 */
6031 int
6032get_echo_attr(void)
6033{
6034 return echo_attr;
6035}
6036
6037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 * ":execute expr1 ..." execute the result of an expression.
6039 * ":echomsg expr1 ..." Print a message
6040 * ":echoerr expr1 ..." Print an error
6041 * Each gets spaces around each argument and a newline at the end for
6042 * echo commands
6043 */
6044 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006045ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
6047 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 int ret = OK;
6050 char_u *p;
6051 garray_T ga;
6052 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053
6054 ga_init2(&ga, 1, 80);
6055
6056 if (eap->skip)
6057 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006058 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006060 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006061 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063
6064 if (!eap->skip)
6065 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006066 char_u buf[NUMBUFLEN];
6067
6068 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006069 {
6070 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6071 {
6072 emsg(_(e_inval_string));
6073 p = NULL;
6074 }
6075 else
6076 p = tv_get_string_buf(&rettv, buf);
6077 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006078 else
6079 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006080 if (p == NULL)
6081 {
6082 clear_tv(&rettv);
6083 ret = FAIL;
6084 break;
6085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 len = (int)STRLEN(p);
6087 if (ga_grow(&ga, len + 2) == FAIL)
6088 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006089 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 ret = FAIL;
6091 break;
6092 }
6093 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 ga.ga_len += len;
6097 }
6098
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006099 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 arg = skipwhite(arg);
6101 }
6102
6103 if (ret != FAIL && ga.ga_data != NULL)
6104 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006105 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6106 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006107 // Mark the already saved text as finishing the line, so that what
6108 // follows is displayed on a new line when scrolling back at the
6109 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006110 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006111 }
6112
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006114 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006115 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006116 out_flush();
6117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 else if (eap->cmdidx == CMD_echoerr)
6119 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006120 int save_did_emsg = did_emsg;
6121
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006122 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006123 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (!force_abort)
6125 did_emsg = save_did_emsg;
6126 }
6127 else if (eap->cmdidx == CMD_execute)
6128 do_cmdline((char_u *)ga.ga_data,
6129 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6130 }
6131
6132 ga_clear(&ga);
6133
6134 if (eap->skip)
6135 --emsg_skip;
6136
6137 eap->nextcmd = check_nextcmd(arg);
6138}
6139
6140/*
6141 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6142 * "arg" points to the "&" or '+' when called, to "option" when returning.
6143 * Returns NULL when no option name found. Otherwise pointer to the char
6144 * after the option name.
6145 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006146 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006147find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 char_u *p = *arg;
6150
6151 ++p;
6152 if (*p == 'g' && p[1] == ':')
6153 {
6154 *opt_flags = OPT_GLOBAL;
6155 p += 2;
6156 }
6157 else if (*p == 'l' && p[1] == ':')
6158 {
6159 *opt_flags = OPT_LOCAL;
6160 p += 2;
6161 }
6162 else
6163 *opt_flags = 0;
6164
6165 if (!ASCII_ISALPHA(*p))
6166 return NULL;
6167 *arg = p;
6168
6169 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006170 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 else
6172 while (ASCII_ISALPHA(*p))
6173 ++p;
6174 return p;
6175}
6176
6177/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006178 * Display script name where an item was last set.
6179 * Should only be invoked when 'verbose' is non-zero.
6180 */
6181 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006182last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006183{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006184 char_u *p;
6185
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006186 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006187 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006188 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006189 if (p != NULL)
6190 {
6191 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006192 msg_puts(_("\n\tLast set from "));
6193 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006194 if (script_ctx.sc_lnum > 0)
6195 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006196 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006197 msg_outnum((long)script_ctx.sc_lnum);
6198 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006199 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006200 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006201 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006202 }
6203}
6204
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006205#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
6207/*
6208 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006209 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 * "flags" can be "g" to do a global substitute.
6211 * Returns an allocated string, NULL for error.
6212 */
6213 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006214do_string_sub(
6215 char_u *str,
6216 char_u *pat,
6217 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006218 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006219 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
6221 int sublen;
6222 regmatch_T regmatch;
6223 int i;
6224 int do_all;
6225 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006226 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 garray_T ga;
6228 char_u *ret;
6229 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006230 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006232 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006234 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
6236 ga_init2(&ga, 1, 200);
6237
6238 do_all = (flags[0] == 'g');
6239
6240 regmatch.rm_ic = p_ic;
6241 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6242 if (regmatch.regprog != NULL)
6243 {
6244 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006245 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6247 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006248 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006249 if (regmatch.startp[0] == regmatch.endp[0])
6250 {
6251 if (zero_width == regmatch.startp[0])
6252 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006253 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006254 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006255 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6256 (size_t)i);
6257 ga.ga_len += i;
6258 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006259 continue;
6260 }
6261 zero_width = regmatch.startp[0];
6262 }
6263
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 /*
6265 * Get some space for a temporary buffer to do the substitution
6266 * into. It will contain:
6267 * - The text up to where the match is.
6268 * - The substituted text.
6269 * - The text after the match.
6270 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006271 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006272 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6274 {
6275 ga_clear(&ga);
6276 break;
6277 }
6278
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006279 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 i = (int)(regmatch.startp[0] - tail);
6281 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006282 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006283 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 + ga.ga_len + i, TRUE, TRUE, FALSE);
6285 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006286 tail = regmatch.endp[0];
6287 if (*tail == NUL)
6288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (!do_all)
6290 break;
6291 }
6292
6293 if (ga.ga_data != NULL)
6294 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6295
Bram Moolenaar473de612013-06-08 18:19:48 +02006296 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 }
6298
6299 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6300 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006301 if (p_cpo == empty_option)
6302 p_cpo = save_cpo;
6303 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006304 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006305 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006306 // If it's still empty it was changed and restored, need to restore in
6307 // the complicated way.
6308 if (*p_cpo == NUL)
6309 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006310 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313 return ret;
6314}