blob: bf01ae0fdff6facf9bb38c7c2f7cee12321fb6a5 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000044} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045
Bram Moolenaar48e697e2016-01-23 22:17:30 +010046static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020047static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
48static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
52static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020053static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020056static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020057
Bram Moolenaare21c1582019-03-02 11:57:09 +010058/*
59 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010060 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010061 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020062 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010064{
65 varnumber_T result;
66
Bram Moolenaar99880f92021-01-20 21:23:14 +010067 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010068 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 if (failed != NULL)
73 *failed = TRUE;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 if (n1 == 0)
76 result = VARNUM_MIN; // similar to NaN
77 else if (n1 < 0)
78 result = -VARNUM_MAX;
79 else
80 result = VARNUM_MAX;
81 }
82 else
83 result = n1 / n2;
84
85 return result;
86}
87
88/*
89 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010090 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010091 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020092 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010094{
Bram Moolenaar99880f92021-01-20 21:23:14 +010095 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 if (failed != NULL)
99 *failed = TRUE;
100 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100101 return (n2 == 0) ? 0 : (n1 % n2);
102}
103
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100104#if defined(EBCDIC) || defined(PROTO)
105/*
106 * Compare struct fst by function name.
107 */
108 static int
109compare_func_name(const void *s1, const void *s2)
110{
111 struct fst *p1 = (struct fst *)s1;
112 struct fst *p2 = (struct fst *)s2;
113
114 return STRCMP(p1->f_name, p2->f_name);
115}
116
117/*
118 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100119 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100120 * On machines using EBCDIC we have to sort it.
121 */
122 static void
123sortFunctions(void)
124{
125 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
126
127 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
128}
129#endif
130
Bram Moolenaar33570922005-01-25 22:26:29 +0000131/*
132 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000133 */
134 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100135eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000136{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200137 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200138 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000139
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200140#ifdef EBCDIC
141 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100142 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200143 */
144 sortFunctions();
145#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000146}
147
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000148#if defined(EXITFREE) || defined(PROTO)
149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100150eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200152 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100153 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200154 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000155
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200156 // autoloaded script names
157 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000158
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200159 // unreferenced lists and dicts
160 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200161
162 // functions not garbage collected
163 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000164}
165#endif
166
Bram Moolenaare6b53242020-07-01 17:28:33 +0200167 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200168fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
169{
170 CLEAR_FIELD(*evalarg);
171 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
172 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
173 {
174 evalarg->eval_getline = eap->getline;
175 evalarg->eval_cookie = eap->cookie;
176 }
177}
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179/*
180 * Top level evaluation function, returning a boolean.
181 * Sets "error" to TRUE if there was an error.
182 * Return TRUE or FALSE.
183 */
184 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100185eval_to_bool(
186 char_u *arg,
187 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200188 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100189 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
Bram Moolenaar33570922005-01-25 22:26:29 +0000191 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200192 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200193 evalarg_T evalarg;
194
Bram Moolenaar37c83712020-06-30 21:18:36 +0200195 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
197 if (skip)
198 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200199 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 else
202 {
203 *error = FALSE;
204 if (!skip)
205 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200206 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200207 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200208 else
209 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000210 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 }
212 }
213 if (skip)
214 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200217 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
219
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100220/*
221 * Call eval1() and give an error message if not done at a lower level.
222 */
223 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200224eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100225{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100226 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100227 int ret;
228 int did_emsg_before = did_emsg;
229 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200230 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100231
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
233
234 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100235 if (ret == FAIL)
236 {
237 // Report the invalid expression unless the expression evaluation has
238 // been cancelled due to an aborting error, an interrupt, or an
239 // exception, or we already gave a more specific error.
240 // Also check called_emsg for when using assert_fails().
241 if (!aborting() && did_emsg == did_emsg_before
242 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100243 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100244 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200245 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100246 return ret;
247}
248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200250 * Return whether a typval is a valid expression to pass to eval_expr_typval()
251 * or eval_expr_to_bool(). An empty string returns FALSE;
252 */
253 int
254eval_expr_valid_arg(typval_T *tv)
255{
256 return tv->v_type != VAR_UNKNOWN
257 && (tv->v_type != VAR_STRING
258 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
259}
260
261/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 * Evaluate an expression, which can be a function, partial or string.
263 * Pass arguments "argv[argc]".
264 * Return the result in "rettv" and OK or FAIL.
265 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200266 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100267eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
268{
269 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100270 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200271 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100272
273 if (expr->v_type == VAR_FUNC)
274 {
275 s = expr->vval.v_string;
276 if (s == NULL || *s == NUL)
277 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200278 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200279 funcexe.evaluate = TRUE;
280 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100281 return FAIL;
282 }
283 else if (expr->v_type == VAR_PARTIAL)
284 {
285 partial_T *partial = expr->vval.v_partial;
286
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200287 if (partial == NULL)
288 return FAIL;
289
Bram Moolenaar822ba242020-05-24 23:00:18 +0200290 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200291 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200293 if (call_def_function(partial->pt_func, argc, argv,
294 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303 funcexe.evaluate = TRUE;
304 funcexe.partial = partial;
305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
309 else
310 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100311 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100312 if (s == NULL)
313 return FAIL;
314 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200315 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100316 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200317 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100318 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200319 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100320 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100321 return FAIL;
322 }
323 }
324 return OK;
325}
326
327/*
328 * Like eval_to_bool() but using a typval_T instead of a string.
329 * Works for string, funcref and partial.
330 */
331 int
332eval_expr_to_bool(typval_T *expr, int *error)
333{
334 typval_T rettv;
335 int res;
336
337 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
338 {
339 *error = TRUE;
340 return FALSE;
341 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200342 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100343 clear_tv(&rettv);
344 return res;
345}
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347/*
348 * Top level evaluation function, returning a string. If "skip" is TRUE,
349 * only parsing to "nextcmd" is done, without reporting errors. Return
350 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
351 */
352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100353eval_to_string_skip(
354 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200355 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100356 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357{
Bram Moolenaar33570922005-01-25 22:26:29 +0000358 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200360 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar37c83712020-06-30 21:18:36 +0200362 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (skip)
364 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200365 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 retval = NULL;
367 else
368 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100369 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000370 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 }
372 if (skip)
373 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200374 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
376 return retval;
377}
378
379/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000380 * Skip over an expression at "*pp".
381 * Return FAIL for an error, OK otherwise.
382 */
383 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200384skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000385{
Bram Moolenaar33570922005-01-25 22:26:29 +0000386 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000387
388 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200389 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000390}
391
392/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200393 * Skip over an expression at "*pp".
394 * If in Vim9 script and line breaks are encountered, the lines are
395 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200396 * "arg" is advanced to just after the expression.
397 * "start" is set to the start of the expression, "end" to just after the end.
398 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 * Return FAIL for an error, OK otherwise.
400 */
401 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200402skip_expr_concatenate(
403 char_u **arg,
404 char_u **start,
405 char_u **end,
406 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200407{
408 typval_T rettv;
409 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200410 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200411 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200413 int evaluate = evalarg == NULL
414 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200416 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200417 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200418 {
419 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200420 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200421 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422 ++gap->ga_len;
423 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200424 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200425
426 // Don't evaluate the expression.
427 if (evalarg != NULL)
428 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200429 *arg = skipwhite(*arg);
430 res = eval1(arg, &rettv, evalarg);
431 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200432 if (evalarg != NULL)
433 evalarg->eval_flags = save_flags;
434
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200435 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200437 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200438 if (evalarg->eval_ga.ga_len == 1)
439 {
440 // just one line, no need to concatenate
441 ga_clear(gap);
442 gap->ga_itemsize = 0;
443 }
444 else
445 {
446 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200447 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200448
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200449 // Line breaks encountered, concatenate all the lines.
450 *((char_u **)gap->ga_data) = *start;
451 p = ga_concat_strings(gap, "");
452
453 // free the lines only when using getsourceline()
454 if (evalarg->eval_cookie != NULL)
455 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200456 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200457 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200458 // Do not free the last line, "arg" points into it, free it
459 // later.
460 vim_free(evalarg->eval_tofree);
461 evalarg->eval_tofree =
462 ((char_u **)gap->ga_data)[gap->ga_len - 1];
463 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200464 ga_clear_strings(gap);
465 }
466 else
467 ga_clear(gap);
468 gap->ga_itemsize = 0;
469 if (p == NULL)
470 return FAIL;
471 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200472 vim_free(evalarg->eval_tofree_lambda);
473 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200474 // Compute "end" relative to the end.
475 *end = *start + STRLEN(*start) - endoff;
476 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200477 }
478
479 return res;
480}
481
482/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100483 * Convert "tv" to a string.
484 * When "convert" is TRUE convert a List into a sequence of lines and convert
485 * a Float to a String.
486 * Returns an allocated string (NULL when out of memory).
487 */
488 char_u *
489typval2string(typval_T *tv, int convert)
490{
491 garray_T ga;
492 char_u *retval;
493#ifdef FEAT_FLOAT
494 char_u numbuf[NUMBUFLEN];
495#endif
496
497 if (convert && tv->v_type == VAR_LIST)
498 {
499 ga_init2(&ga, (int)sizeof(char), 80);
500 if (tv->vval.v_list != NULL)
501 {
502 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
503 if (tv->vval.v_list->lv_len > 0)
504 ga_append(&ga, NL);
505 }
506 ga_append(&ga, NUL);
507 retval = (char_u *)ga.ga_data;
508 }
509#ifdef FEAT_FLOAT
510 else if (convert && tv->v_type == VAR_FLOAT)
511 {
512 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
513 retval = vim_strsave(numbuf);
514 }
515#endif
516 else
517 retval = vim_strsave(tv_get_string(tv));
518 return retval;
519}
520
521/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200522 * Top level evaluation function, returning a string. Does not handle line
523 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000524 * When "convert" is TRUE convert a List into a sequence of lines and convert
525 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 * Return pointer to allocated memory, or NULL for failure.
527 */
528 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100529eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100530 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100531 int convert,
532 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
Bram Moolenaar33570922005-01-25 22:26:29 +0000534 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100536 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100538 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
539 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 retval = NULL;
541 else
542 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100543 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000544 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100546 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
548 return retval;
549}
550
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100551 char_u *
552eval_to_string(
553 char_u *arg,
554 int convert)
555{
556 return eval_to_string_eap(arg, convert, NULL);
557}
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000560 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200561 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200562 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 */
564 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100565eval_to_string_safe(
566 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100567 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
569 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200570 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200571 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200573 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200574 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000575 if (use_sandbox)
576 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200577 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200578 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000579 if (use_sandbox)
580 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200581 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200582 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200583 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 return retval;
585}
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587/*
588 * Top level evaluation function, returning a number.
589 * Evaluates "expr" silently.
590 * Returns -1 for an error.
591 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200592 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100593eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
Bram Moolenaar33570922005-01-25 22:26:29 +0000595 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200596 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000597 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
599 ++emsg_off;
600
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200601 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 retval = -1;
603 else
604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100605 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000606 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608 --emsg_off;
609
610 return retval;
611}
612
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000613/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000614 * Top level evaluation function.
615 * Returns an allocated typval_T with the result.
616 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000617 */
618 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200619eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620{
621 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200622 evalarg_T evalarg;
623
624 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000625
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200626 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200627 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100628 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000629
Bram Moolenaar37c83712020-06-30 21:18:36 +0200630 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000631 return tv;
632}
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100635 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200636 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
637 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000638 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641call_vim_function(
642 char_u *func,
643 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200644 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200645 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000647 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200648 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100650 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200651 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200652 funcexe.firstline = curwin->w_cursor.lnum;
653 funcexe.lastline = curwin->w_cursor.lnum;
654 funcexe.evaluate = TRUE;
655 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000656 if (ret == FAIL)
657 clear_tv(rettv);
658
659 return ret;
660}
661
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100662/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100663 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100664 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200665 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
666 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100667 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200668 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100669call_func_retnr(
670 char_u *func,
671 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200672 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100673{
674 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200675 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100676
Bram Moolenaarded27a12018-08-01 19:06:03 +0200677 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100678 return -1;
679
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100680 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100681 clear_tv(&rettv);
682 return retval;
683}
684
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000685/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100686 * Call Vim script function like call_func_retnr() and drop the result.
687 * Returns FAIL when calling the function fails.
688 */
689 int
690call_func_noret(
691 char_u *func,
692 int argc,
693 typval_T *argv)
694{
695 typval_T rettv;
696
697 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
698 return FAIL;
699 clear_tv(&rettv);
700 return OK;
701}
702
703/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100704 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100705 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000706 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707 */
708 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100709call_func_retstr(
710 char_u *func,
711 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200712 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000713{
714 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000715 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000716
Bram Moolenaarded27a12018-08-01 19:06:03 +0200717 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718 return NULL;
719
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100720 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000721 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 return retval;
723}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000724
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000725/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100726 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100727 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000728 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000729 */
730 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100731call_func_retlist(
732 char_u *func,
733 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200734 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000735{
736 typval_T rettv;
737
Bram Moolenaarded27a12018-08-01 19:06:03 +0200738 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000739 return NULL;
740
741 if (rettv.v_type != VAR_LIST)
742 {
743 clear_tv(&rettv);
744 return NULL;
745 }
746
747 return rettv.vval.v_list;
748}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750#ifdef FEAT_FOLDING
751/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100752 * Evaluate "arg", which is 'foldexpr'.
753 * Note: caller must set "curwin" to match "arg".
754 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
755 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
757 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100758eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
Bram Moolenaar33570922005-01-25 22:26:29 +0000760 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200761 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000763 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
764 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000767 if (use_sandbox)
768 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200769 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200771 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 retval = 0;
773 else
774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100775 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000776 if (tv.v_type == VAR_NUMBER)
777 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000778 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 retval = 0;
780 else
781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100782 // If the result is a string, check if there is a non-digit before
783 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000784 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (!VIM_ISDIGIT(*s) && *s != '-')
786 *cp = *s++;
787 retval = atol((char *)s);
788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000789 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000792 if (use_sandbox)
793 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200794 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200795 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200797 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798}
799#endif
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000802 * Get an lval: variable, Dict item or List item that can be assigned a value
803 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
804 * "name.key", "name.key[expr]" etc.
805 * Indexing only works if "name" is an existing List or Dictionary.
806 * "name" points to the start of the name.
807 * If "rettv" is not NULL it points to the value to be assigned.
808 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
809 * wrong; must end in space or cmd separator.
810 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100811 * flags:
812 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100813 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100814 * GLV_NO_AUTOLOAD: do not use script autoloading
815 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000817 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000818 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000819 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200820 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100821get_lval(
822 char_u *name,
823 typval_T *rettv,
824 lval_T *lp,
825 int unlet,
826 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100827 int flags, // GLV_ values
828 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000830 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000831 char_u *expr_start, *expr_end;
832 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000833 dictitem_T *v;
834 typval_T var1;
835 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000837 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000838 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000839 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100840 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100841 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100842 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100844 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200845 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000846
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100847 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100849 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200851 lp->ll_name_end = find_name_end(name, NULL, NULL,
852 FNE_INCL_BR | fne_flags);
853 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000854 }
855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100856 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000857 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 if (expr_start != NULL)
860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100861 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100862 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000863 && *p != '[' && *p != '.')
864 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200865 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 return NULL;
867 }
868
869 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
870 if (lp->ll_exp_name == NULL)
871 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100872 // Report an invalid expression in braces, unless the
873 // expression evaluation has been cancelled due to an
874 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 if (!aborting() && !quiet)
876 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000877 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100878 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 return NULL;
880 }
881 }
882 lp->ll_name = lp->ll_exp_name;
883 }
884 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 lp->ll_name = name;
887
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200888 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200890 // "a: type" is declaring variable "a" with a type, not "a:".
891 if (p == name + 2 && p[-1] == ':')
892 {
893 --p;
894 lp->ll_name_end = p;
895 }
896 if (*p == ':')
897 {
898 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
899 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200901 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100902 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
903 if (lp->ll_type == NULL && !quiet)
904 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200905 lp->ll_name_end = tp;
906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907 }
908 }
909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100910 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
912 return p;
913
914 cc = *p;
915 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100916 // When we would write to the variable pass &ht and prevent autoload.
917 writing = !(flags & GLV_READ_ONLY);
918 v = find_var(lp->ll_name, writing ? &ht : NULL,
919 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000920 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200921 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000923 if (v == NULL)
924 return NULL;
925
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100926 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
927 {
928 if (!quiet)
929 semsg(_(e_variable_already_declared), lp->ll_name);
930 return NULL;
931 }
932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 /*
934 * Loop until no more [idx] or .key is following.
935 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000936 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100937 var1.v_type = VAR_UNKNOWN;
938 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000939 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200942 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100943 && !(lp->ll_tv->v_type == VAR_BLOB
944 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000946 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100947 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000949 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000950 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100953 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000955 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000956
Bram Moolenaar10c65862020-10-08 21:16:42 +0200957 if (in_vim9script() && lp->ll_valtype == NULL
958 && lp->ll_tv == &v->di_tv
959 && ht != NULL && ht == get_script_local_ht())
960 {
961 svar_T *sv = find_typval_in_script(lp->ll_tv);
962
963 // Vim9 script local variable: get the type
964 if (sv != NULL)
965 lp->ll_valtype = sv->sv_type;
966 }
967
Bram Moolenaar8c711452005-01-14 21:53:12 +0000968 len = -1;
969 if (*p == '.')
970 {
971 key = p + 1;
972 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
973 ;
974 if (len == 0)
975 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100977 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000979 }
980 p = key + len;
981 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000982 else
983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100984 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000985 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000986 if (*p == ':')
987 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000988 else
989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200991 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100993 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000994 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100995 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000996 clear_tv(&var1);
997 return NULL;
998 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200999 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 }
1001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001003 if (*p == ':')
1004 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001008 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001009 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001011 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001012 if (rettv != NULL
1013 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001014 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001015 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001016 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001019 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001020 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001022 }
1023 p = skipwhite(p + 1);
1024 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001026 else
1027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001029 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001030 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001031 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001032 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001035 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001036 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001037 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001038 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001039 clear_tv(&var2);
1040 return NULL;
1041 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001042 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001043 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001044 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001047
Bram Moolenaar8c711452005-01-14 21:53:12 +00001048 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001049 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001051 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001052 clear_tv(&var1);
1053 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001055 }
1056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001057 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001058 ++p;
1059 }
1060
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 {
1063 if (len == -1)
1064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001065 // "[key]": get key from "var1"
1066 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001067 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001070 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 }
1072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001073 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001074
1075 // a NULL dict is equivalent with an empty dict
1076 if (lp->ll_tv->vval.v_dict == NULL)
1077 {
1078 lp->ll_tv->vval.v_dict = dict_alloc();
1079 if (lp->ll_tv->vval.v_dict == NULL)
1080 {
1081 clear_tv(&var1);
1082 return NULL;
1083 }
1084 ++lp->ll_tv->vval.v_dict->dv_refcount;
1085 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001086 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001087
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001088 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001090 // When assigning to a scope dictionary check that a function and
1091 // variable name is valid (only variable name unless it is l: or
1092 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001093 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001094 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001095 int prevval;
1096 int wrong;
1097
1098 if (len != -1)
1099 {
1100 prevval = key[len];
1101 key[len] = NUL;
1102 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001103 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001104 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001105 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1106 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001107 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001108 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001109 if (len != -1)
1110 key[len] = prevval;
1111 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001112 {
1113 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001114 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001115 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001116 }
1117
Bram Moolenaar10c65862020-10-08 21:16:42 +02001118 if (lp->ll_valtype != NULL)
1119 // use the type of the member
1120 lp->ll_valtype = lp->ll_valtype->tt_member;
1121
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001123 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001124 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001125 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001126 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001128 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001129 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001130 return NULL;
1131 }
1132
Bram Moolenaar31b81602019-02-10 22:14:27 +01001133 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001137 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001138 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001140 }
1141 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001145 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001147 p = NULL;
1148 break;
1149 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001150 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001151 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001152 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1153 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001154 {
1155 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001156 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001157 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001158
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001159 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001161 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001162 else if (lp->ll_tv->v_type == VAR_BLOB)
1163 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001164 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1165
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001166 /*
1167 * Get the number and item for the only or first index of the List.
1168 */
1169 if (empty1)
1170 lp->ll_n1 = 0;
1171 else
1172 // is number or string
1173 lp->ll_n1 = (long)tv_get_number(&var1);
1174 clear_tv(&var1);
1175
1176 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001177 || lp->ll_n1 > bloblen
1178 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 {
1180 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001181 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001182 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001183 return NULL;
1184 }
1185 if (lp->ll_range && !lp->ll_empty2)
1186 {
1187 lp->ll_n2 = (long)tv_get_number(&var2);
1188 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001189 if (lp->ll_n2 < 0
1190 || lp->ll_n2 >= bloblen
1191 || lp->ll_n2 < lp->ll_n1)
1192 {
1193 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001194 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001195 return NULL;
1196 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001197 }
1198 lp->ll_blob = lp->ll_tv->vval.v_blob;
1199 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001200 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 else
1203 {
1204 /*
1205 * Get the number and item for the only or first index of the List.
1206 */
1207 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001209 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001210 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001211 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001212 clear_tv(&var1);
1213
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001214 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001216 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001217 if (lp->ll_li == NULL)
1218 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001219 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001220 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001223 }
1224
Bram Moolenaar10c65862020-10-08 21:16:42 +02001225 if (lp->ll_valtype != NULL)
1226 // use the type of the member
1227 lp->ll_valtype = lp->ll_valtype->tt_member;
1228
Bram Moolenaar8c711452005-01-14 21:53:12 +00001229 /*
1230 * May need to find the item or absolute index for the second
1231 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 * When no index given: "lp->ll_empty2" is TRUE.
1233 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001237 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001238 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001244 {
1245 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001246 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001248 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 }
1251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001252 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253 if (lp->ll_n1 < 0)
1254 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1255 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001256 {
1257 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001258 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001260 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001261 }
1262
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001265 }
1266
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001267 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 return p;
1270}
1271
1272/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001273 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001274 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001276clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277{
1278 vim_free(lp->ll_exp_name);
1279 vim_free(lp->ll_newkey);
1280}
1281
1282/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001283 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001284 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001285 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1286 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001289set_var_lval(
1290 lval_T *lp,
1291 char_u *endp,
1292 typval_T *rettv,
1293 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001294 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1295 char_u *op,
1296 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001297{
1298 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001299 listitem_T *ri;
1300 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001301
1302 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001304 cc = *endp;
1305 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001306 if (lp->ll_blob != NULL)
1307 {
1308 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001309
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001310 if (op != NULL && *op != '=')
1311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001312 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001313 return;
1314 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001315 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001316 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001317
1318 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1319 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001320 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001321
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001322 if (lp->ll_empty2)
1323 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1324
1325 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001327 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001328 return;
1329 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001330 if (lp->ll_empty2)
1331 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001332
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001333 ir = 0;
1334 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1335 blob_set(lp->ll_blob, il,
1336 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001337 }
1338 else
1339 {
1340 val = (int)tv_get_number_chk(rettv, &error);
1341 if (!error)
1342 {
1343 garray_T *gap = &lp->ll_blob->bv_ga;
1344
1345 // Allow for appending a byte. Setting a byte beyond
1346 // the end is an error otherwise.
1347 if (lp->ll_n1 < gap->ga_len
1348 || (lp->ll_n1 == gap->ga_len
1349 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1350 {
1351 blob_set(lp->ll_blob, lp->ll_n1, val);
1352 if (lp->ll_n1 == gap->ga_len)
1353 ++gap->ga_len;
1354 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001355 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001356 }
1357 }
1358 }
1359 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001360 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001361 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001362
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001363 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001364 {
1365 emsg(_(e_cannot_mod));
1366 *endp = cc;
1367 return;
1368 }
1369
Bram Moolenaarff697e62019-02-12 22:28:33 +01001370 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001371 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001372 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001373 &tv, &di, TRUE, FALSE) == OK)
1374 {
1375 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001376 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1377 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001378 && tv_op(&tv, rettv, op) == OK)
1379 set_var(lp->ll_name, &tv, FALSE);
1380 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001383 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001384 {
1385 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001386 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001387 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001388 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1389 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001390 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001391 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001392 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001393 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001394 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001395 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001396 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001397 else if (lp->ll_range)
1398 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001399 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001400 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001401
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001402 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001403 {
1404 emsg(_("E996: Cannot lock a range"));
1405 return;
1406 }
1407
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001408 /*
1409 * Check whether any of the list items is locked
1410 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001411 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001412 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001413 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001414 return;
1415 ri = ri->li_next;
1416 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1417 break;
1418 ll_li = ll_li->li_next;
1419 ++ll_n1;
1420 }
1421
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 /*
1423 * Assign the List values to the list items.
1424 */
1425 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001426 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001427 if (op != NULL && *op != '=')
1428 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1429 else
1430 {
1431 clear_tv(&lp->ll_li->li_tv);
1432 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1433 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001434 ri = ri->li_next;
1435 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1436 break;
1437 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001438 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001439 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001440 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001441 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001442 ri = NULL;
1443 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001444 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001445 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 lp->ll_li = lp->ll_li->li_next;
1447 ++lp->ll_n1;
1448 }
1449 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001450 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001451 else if (lp->ll_empty2
1452 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001453 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001454 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001455 }
1456 else
1457 {
1458 /*
1459 * Assign to a List or Dictionary item.
1460 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001461 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001462 {
1463 emsg(_("E996: Cannot lock a list or dict"));
1464 return;
1465 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001466
1467 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001468 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001469 return;
1470
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471 if (lp->ll_newkey != NULL)
1472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001473 if (op != NULL && *op != '=')
1474 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001475 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001476 return;
1477 }
1478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001479 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001480 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001481 if (di == NULL)
1482 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001483 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1484 {
1485 vim_free(di);
1486 return;
1487 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001488 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001489 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490 else if (op != NULL && *op != '=')
1491 {
1492 tv_op(lp->ll_tv, rettv, op);
1493 return;
1494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001496 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001497
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001498 /*
1499 * Assign the value to the variable or list item.
1500 */
1501 if (copy)
1502 copy_tv(rettv, lp->ll_tv);
1503 else
1504 {
1505 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001506 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001507 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001508 }
1509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001510}
1511
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001512/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001513 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1514 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 * Returns OK or FAIL.
1516 */
1517 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001518tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001519{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001520 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521 char_u numbuf[NUMBUFLEN];
1522 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001523 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001525 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001526 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001527 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001528 {
1529 switch (tv1->v_type)
1530 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001531 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001532 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001534 case VAR_DICT:
1535 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001536 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001537 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001538 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001539 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001540 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541 break;
1542
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001543 case VAR_BLOB:
1544 if (*op != '+' || tv2->v_type != VAR_BLOB)
1545 break;
1546 // BLOB += BLOB
1547 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1548 {
1549 blob_T *b1 = tv1->vval.v_blob;
1550 blob_T *b2 = tv2->vval.v_blob;
1551 int i, len = blob_len(b2);
1552 for (i = 0; i < len; i++)
1553 ga_append(&b1->bv_ga, blob_get(b2, i));
1554 }
1555 return OK;
1556
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001557 case VAR_LIST:
1558 if (*op != '+' || tv2->v_type != VAR_LIST)
1559 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001560 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001561 if (tv2->vval.v_list != NULL)
1562 {
1563 if (tv1->vval.v_list == NULL)
1564 {
1565 tv1->vval.v_list = tv2->vval.v_list;
1566 ++tv1->vval.v_list->lv_refcount;
1567 }
1568 else
1569 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1570 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001571 return OK;
1572
1573 case VAR_NUMBER:
1574 case VAR_STRING:
1575 if (tv2->v_type == VAR_LIST)
1576 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001577 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001578 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001579 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001580 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001581#ifdef FEAT_FLOAT
1582 if (tv2->v_type == VAR_FLOAT)
1583 {
1584 float_T f = n;
1585
Bram Moolenaarff697e62019-02-12 22:28:33 +01001586 if (*op == '%')
1587 break;
1588 switch (*op)
1589 {
1590 case '+': f += tv2->vval.v_float; break;
1591 case '-': f -= tv2->vval.v_float; break;
1592 case '*': f *= tv2->vval.v_float; break;
1593 case '/': f /= tv2->vval.v_float; break;
1594 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001595 clear_tv(tv1);
1596 tv1->v_type = VAR_FLOAT;
1597 tv1->vval.v_float = f;
1598 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001599 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600#endif
1601 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001602 switch (*op)
1603 {
1604 case '+': n += tv_get_number(tv2); break;
1605 case '-': n -= tv_get_number(tv2); break;
1606 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001607 case '/': n = num_divide(n, tv_get_number(tv2),
1608 &failed); break;
1609 case '%': n = num_modulus(n, tv_get_number(tv2),
1610 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001611 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001612 clear_tv(tv1);
1613 tv1->v_type = VAR_NUMBER;
1614 tv1->vval.v_number = n;
1615 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001616 }
1617 else
1618 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001619 if (tv2->v_type == VAR_FLOAT)
1620 break;
1621
Bram Moolenaarff697e62019-02-12 22:28:33 +01001622 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001623 s = tv_get_string(tv1);
1624 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001625 clear_tv(tv1);
1626 tv1->v_type = VAR_STRING;
1627 tv1->vval.v_string = s;
1628 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001629 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001630
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001631 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001632#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001633 {
1634 float_T f;
1635
Bram Moolenaarff697e62019-02-12 22:28:33 +01001636 if (*op == '%' || *op == '.'
1637 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001638 && tv2->v_type != VAR_NUMBER
1639 && tv2->v_type != VAR_STRING))
1640 break;
1641 if (tv2->v_type == VAR_FLOAT)
1642 f = tv2->vval.v_float;
1643 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001644 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001645 switch (*op)
1646 {
1647 case '+': tv1->vval.v_float += f; break;
1648 case '-': tv1->vval.v_float -= f; break;
1649 case '*': tv1->vval.v_float *= f; break;
1650 case '/': tv1->vval.v_float /= f; break;
1651 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001652 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001653#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001654 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001655 }
1656 }
1657
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001658 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001659 return FAIL;
1660}
1661
1662/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663 * Evaluate the expression used in a ":for var in expr" command.
1664 * "arg" points to "var".
1665 * Set "*errp" to TRUE for an error, FALSE otherwise;
1666 * Return a pointer that holds the info. Null when there is an error.
1667 */
1668 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001669eval_for_line(
1670 char_u *arg,
1671 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001672 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001673 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674{
Bram Moolenaar33570922005-01-25 22:26:29 +00001675 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001676 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001677 typval_T tv;
1678 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001679 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001681 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001683 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 if (fi == NULL)
1685 return NULL;
1686
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001687 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 if (expr == NULL)
1689 return fi;
1690
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001691 expr = skipwhite_and_linebreak(expr, evalarg);
1692 if (expr[0] != 'i' || expr[1] != 'n'
1693 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 return fi;
1697 }
1698
1699 if (skip)
1700 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001701 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1702 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703 {
1704 *errp = FALSE;
1705 if (!skip)
1706 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001707 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001708 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001709 l = tv.vval.v_list;
1710 if (l == NULL)
1711 {
1712 // a null list is like an empty list: do nothing
1713 clear_tv(&tv);
1714 }
1715 else
1716 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001717 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001718 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001720 // No need to increment the refcount, it's already set for
1721 // the list being used in "tv".
1722 fi->fi_list = l;
1723 list_add_watch(l, &fi->fi_lw);
1724 fi->fi_lw.lw_item = l->lv_first;
1725 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001726 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001727 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001728 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001729 fi->fi_bi = 0;
1730 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001731 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001732 typval_T btv;
1733
1734 // Make a copy, so that the iteration still works when the
1735 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001737 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001738 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001739 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001740 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741 else
1742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001743 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001744 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001745 }
1746 }
1747 }
1748 if (skip)
1749 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001750 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751
1752 return fi;
1753}
1754
1755/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001756 * Used when looping over a :for line, skip the "in expr" part.
1757 */
1758 void
1759skip_for_lines(void *fi_void, evalarg_T *evalarg)
1760{
1761 forinfo_T *fi = (forinfo_T *)fi_void;
1762 int i;
1763
1764 for (i = 0; i < fi->fi_break_count; ++i)
1765 eval_next_line(evalarg);
1766}
1767
1768/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 * Use the first item in a ":for" list. Advance to the next.
1770 * Assign the values to the variable (list). "arg" points to the first one.
1771 * Return TRUE when a valid item was found, FALSE when at end of list or
1772 * something wrong.
1773 */
1774 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001777 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 int result;
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001779 int flag = in_vim9script() ? ASSIGN_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001780 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001782 if (fi->fi_blob != NULL)
1783 {
1784 typval_T tv;
1785
1786 if (fi->fi_bi >= blob_len(fi->fi_blob))
1787 return FALSE;
1788 tv.v_type = VAR_NUMBER;
1789 tv.v_lock = VAR_FIXED;
1790 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1791 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001792 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001793 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001794 }
1795
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 item = fi->fi_lw.lw_item;
1797 if (item == NULL)
1798 result = FALSE;
1799 else
1800 {
1801 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001802 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001803 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 }
1805 return result;
1806}
1807
1808/*
1809 * Free the structure used to store info used by ":for".
1810 */
1811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001812free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813{
Bram Moolenaar33570922005-01-25 22:26:29 +00001814 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001816 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001817 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001819 list_unref(fi->fi_list);
1820 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001821 if (fi != NULL && fi->fi_blob != NULL)
1822 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 vim_free(fi);
1824}
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001827set_context_for_expression(
1828 expand_T *xp,
1829 char_u *arg,
1830 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001832 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001836 if (cmdidx == CMD_let || cmdidx == CMD_var
1837 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838 {
1839 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001840 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001842 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001843 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 {
1845 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001846 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001847 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 break;
1849 }
1850 return;
1851 }
1852 }
1853 else
1854 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1855 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 while ((xp->xp_pattern = vim_strpbrk(arg,
1857 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1858 {
1859 c = *xp->xp_pattern;
1860 if (c == '&')
1861 {
1862 c = xp->xp_pattern[1];
1863 if (c == '&')
1864 {
1865 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001866 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001871 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1872 xp->xp_pattern += 2;
1873
1874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 else if (c == '$')
1877 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001878 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 xp->xp_context = EXPAND_ENV_VARS;
1880 }
1881 else if (c == '=')
1882 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001883 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 xp->xp_context = EXPAND_EXPRESSION;
1885 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001886 else if (c == '#'
1887 && xp->xp_context == EXPAND_EXPRESSION)
1888 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001889 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001890 break;
1891 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001892 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 && xp->xp_context == EXPAND_FUNCTIONS
1894 && vim_strchr(xp->xp_pattern, '(') == NULL)
1895 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001896 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 break;
1898 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001899 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001901 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 {
1903 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1904 if (c == '\\' && xp->xp_pattern[1] != NUL)
1905 ++xp->xp_pattern;
1906 xp->xp_context = EXPAND_NOTHING;
1907 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001908 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001910 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1912 /* skip */ ;
1913 xp->xp_context = EXPAND_NOTHING;
1914 }
1915 else if (c == '|')
1916 {
1917 if (xp->xp_pattern[1] == '|')
1918 {
1919 ++xp->xp_pattern;
1920 xp->xp_context = EXPAND_EXPRESSION;
1921 }
1922 else
1923 xp->xp_context = EXPAND_COMMANDS;
1924 }
1925 else
1926 xp->xp_context = EXPAND_EXPRESSION;
1927 }
1928 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001929 // Doesn't look like something valid, expand as an expression
1930 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 arg = xp->xp_pattern;
1933 if (*arg != NUL)
1934 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1935 /* skip */ ;
1936 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001937
1938 // ":exe one two" completes "two"
1939 if ((cmdidx == CMD_execute
1940 || cmdidx == CMD_echo
1941 || cmdidx == CMD_echon
1942 || cmdidx == CMD_echomsg)
1943 && xp->xp_context == EXPAND_EXPRESSION)
1944 {
1945 for (;;)
1946 {
1947 char_u *n = skiptowhite(arg);
1948
1949 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1950 break;
1951 arg = skipwhite(n);
1952 }
1953 }
1954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 xp->xp_pattern = arg;
1956}
1957
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001959 * Return TRUE if "pat" matches "text".
1960 * Does not use 'cpo' and always uses 'magic'.
1961 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001962 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001963pattern_match(char_u *pat, char_u *text, int ic)
1964{
1965 int matches = FALSE;
1966 char_u *save_cpo;
1967 regmatch_T regmatch;
1968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001969 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001970 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001971 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001972 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1973 if (regmatch.regprog != NULL)
1974 {
1975 regmatch.rm_ic = ic;
1976 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1977 vim_regfree(regmatch.regprog);
1978 }
1979 p_cpo = save_cpo;
1980 return matches;
1981}
1982
1983/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001984 * Handle a name followed by "(". Both for just "name(arg)" and for
1985 * "expr->name(arg)".
1986 * Returns OK or FAIL.
1987 */
1988 static int
1989eval_func(
1990 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001991 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001992 char_u *name,
1993 int name_len,
1994 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001995 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001996 typval_T *basetv) // "expr" for "expr->name(arg)"
1997{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001998 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001999 char_u *s = name;
2000 int len = name_len;
2001 partial_T *partial;
2002 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002003 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002004
2005 if (!evaluate)
2006 check_vars(s, len);
2007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002008 // If "s" is the name of a variable of type VAR_FUNC
2009 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002010 s = deref_func_name(s, &len, &partial,
2011 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002012
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002013 // Need to make a copy, in case evaluating the arguments makes
2014 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002015 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002016 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002017 ret = FAIL;
2018 else
2019 {
2020 funcexe_T funcexe;
2021
2022 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002023 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002024 funcexe.firstline = curwin->w_cursor.lnum;
2025 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002026 funcexe.evaluate = evaluate;
2027 funcexe.partial = partial;
2028 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002029 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002030 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002031 }
2032 vim_free(s);
2033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002034 // If evaluate is FALSE rettv->v_type was not set in
2035 // get_func_tv, but it's needed in handle_subscript() to parse
2036 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002037 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2038 {
2039 rettv->vval.v_string = NULL;
2040 rettv->v_type = VAR_FUNC;
2041 }
2042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002043 // Stop the expression evaluation when immediately
2044 // aborting on error, or when an interrupt occurred or
2045 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002046 if (evaluate && aborting())
2047 {
2048 if (ret == OK)
2049 clear_tv(rettv);
2050 ret = FAIL;
2051 }
2052 return ret;
2053}
2054
2055/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002056 * Get the next line source line without advancing. But do skip over comment
2057 * lines.
2058 */
2059 static char_u *
2060getline_peek_skip_comments(evalarg_T *evalarg)
2061{
2062 for (;;)
2063 {
2064 char_u *next = getline_peek(evalarg->eval_getline,
2065 evalarg->eval_cookie);
2066 char_u *p;
2067
2068 if (next == NULL)
2069 break;
2070 p = skipwhite(next);
2071 if (*p != NUL && !vim9_comment_start(p))
2072 return next;
2073 (void)eval_next_line(evalarg);
2074 }
2075 return NULL;
2076}
2077
2078/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002079 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2080 * comment) and there is a next line, return the next line (skipping blanks)
2081 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002082 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2083 * "arg" must point somewhere inside a line, not at the start.
2084 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002085 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002086eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2087{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002088 char_u *p = skipwhite(arg);
2089
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002091 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002092 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002093 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002094 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002095 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002096 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002097
2098 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002099 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002100 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002101 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002102
Bram Moolenaar9d489562020-07-30 20:08:50 +02002103 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002104 {
2105 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002106 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002107 }
2108 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002109 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002110}
2111
2112/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002113 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002114 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002115 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002116eval_next_line(evalarg_T *evalarg)
2117{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002118 garray_T *gap = &evalarg->eval_ga;
2119 char_u *line;
2120
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002121 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002122 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2123 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002124 else
2125 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002126 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002127 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2128 {
2129 // Going to concatenate the lines after parsing.
2130 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2131 ++gap->ga_len;
2132 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002133 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002134 {
2135 vim_free(evalarg->eval_tofree);
2136 evalarg->eval_tofree = line;
2137 }
2138 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002139}
2140
Bram Moolenaare6b53242020-07-01 17:28:33 +02002141/*
2142 * Call eval_next_non_blank() and get the next line if needed.
2143 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002144 char_u *
2145skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2146{
2147 int getnext;
2148 char_u *p = skipwhite(arg);
2149
Bram Moolenaar962d7212020-07-04 14:15:00 +02002150 if (evalarg == NULL)
2151 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002152 eval_next_non_blank(p, evalarg, &getnext);
2153 if (getnext)
2154 return eval_next_line(evalarg);
2155 return p;
2156}
2157
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002158/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002159 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002160 */
2161 void
2162clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2163{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002164 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002165 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002166 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002167 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002168 if (eap != NULL)
2169 {
2170 // We may need to keep the original command line, e.g. for
2171 // ":let" it has the variable names. But we may also need the
2172 // new one, "nextcmd" points into it. Keep both.
2173 vim_free(eap->cmdline_tofree);
2174 eap->cmdline_tofree = *eap->cmdlinep;
2175 *eap->cmdlinep = evalarg->eval_tofree;
2176 }
2177 else
2178 vim_free(evalarg->eval_tofree);
2179 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002180 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002181
2182 vim_free(evalarg->eval_tofree_lambda);
2183 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002184 }
2185}
2186
2187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2191 */
2192
2193/*
2194 * Handle zero level expression.
2195 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002197 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002198 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 * Return OK or FAIL.
2200 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002202eval0(
2203 char_u *arg,
2204 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002205 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002206 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207{
2208 int ret;
2209 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002210 int did_emsg_before = did_emsg;
2211 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002212 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
2214 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002215 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002216 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002217
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002218 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
2220 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 /*
2223 * Report the invalid expression unless the expression evaluation has
2224 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002225 * exception, or we already gave a more specific error.
2226 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002228 if (!aborting()
2229 && did_emsg == did_emsg_before
2230 && called_emsg == called_emsg_before
2231 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002232 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002233
2234 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002235 // a next command to avoid more errors, unless "|" is following, which
2236 // could only be a command separator.
2237 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2238 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002239 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002241
2242 if (eap != NULL)
2243 eap->nextcmd = check_nextcmd(p);
2244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 return ret;
2246}
2247
2248/*
2249 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002250 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002251 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 *
2253 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002254 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002256 * Note: "rettv.v_lock" is not set.
2257 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 * Return OK or FAIL.
2259 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002260 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002261eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002263 char_u *p;
2264 int getnext;
2265
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002266 CLEAR_POINTER(rettv);
2267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 /*
2269 * Get the first variable.
2270 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002271 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 return FAIL;
2273
Bram Moolenaar793648f2020-06-26 21:28:25 +02002274 p = eval_next_non_blank(*arg, evalarg, &getnext);
2275 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002277 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002278 int result;
2279 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002280 evalarg_T *evalarg_used = evalarg;
2281 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002282 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002283 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002284 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002285
2286 if (evalarg == NULL)
2287 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002288 CLEAR_FIELD(local_evalarg);
2289 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002290 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002291 orig_flags = evalarg_used->eval_flags;
2292 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002293
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002294 if (getnext)
2295 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002296 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002297 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002298 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002299 {
2300 error_white_both(p, 1);
2301 clear_tv(rettv);
2302 return FAIL;
2303 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002304 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002305 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002308 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002310 int error = FALSE;
2311
Bram Moolenaar13106602020-10-04 16:06:05 +02002312 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002313 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002314 else if (vim9script)
2315 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002316 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002318 if (error || !op_falsy || !result)
2319 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002320 if (error)
2321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 }
2323
2324 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002325 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002327 if (op_falsy)
2328 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002329 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002330 {
2331 error_white_both(p, 1);
2332 clear_tv(rettv);
2333 return FAIL;
2334 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002335 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002336 evalarg_used->eval_flags = (op_falsy ? !result : result)
2337 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2338 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002339 {
2340 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002342 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002343 if (!op_falsy || !result)
2344 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002346 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002348 /*
2349 * Check for the ":".
2350 */
2351 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2352 if (*p != ':')
2353 {
2354 emsg(_(e_missing_colon));
2355 if (evaluate && result)
2356 clear_tv(rettv);
2357 evalarg_used->eval_flags = orig_flags;
2358 return FAIL;
2359 }
2360 if (getnext)
2361 *arg = eval_next_line(evalarg_used);
2362 else
2363 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002364 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002365 {
2366 error_white_both(p, 1);
2367 clear_tv(rettv);
2368 evalarg_used->eval_flags = orig_flags;
2369 return FAIL;
2370 }
2371 *arg = p;
2372 }
2373
2374 /*
2375 * Get the third variable. Recursive!
2376 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002377 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002378 {
2379 error_white_both(p, 1);
2380 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002381 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002382 return FAIL;
2383 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002384 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2385 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002386 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002387 if (eval1(arg, &var2, evalarg_used) == FAIL)
2388 {
2389 if (evaluate && result)
2390 clear_tv(rettv);
2391 evalarg_used->eval_flags = orig_flags;
2392 return FAIL;
2393 }
2394 if (evaluate && !result)
2395 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002397
2398 if (evalarg == NULL)
2399 clear_evalarg(&local_evalarg, NULL);
2400 else
2401 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403
2404 return OK;
2405}
2406
2407/*
2408 * Handle first level expression:
2409 * expr2 || expr2 || expr2 logical OR
2410 *
2411 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002412 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 *
2414 * Return OK or FAIL.
2415 */
2416 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002417eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002419 char_u *p;
2420 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422 /*
2423 * Get the first variable.
2424 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002425 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 return FAIL;
2427
2428 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002429 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002431 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002432 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002434 evalarg_T *evalarg_used = evalarg;
2435 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002436 int evaluate;
2437 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 long result = FALSE;
2439 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002440 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002441 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002442
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002443 if (evalarg == NULL)
2444 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002445 CLEAR_FIELD(local_evalarg);
2446 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002447 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002448 orig_flags = evalarg_used->eval_flags;
2449 evaluate = orig_flags & EVAL_EVALUATE;
2450 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002451 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002452 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002453 result = tv_get_bool_chk(rettv, &error);
2454 else if (tv_get_number_chk(rettv, &error) != 0)
2455 result = TRUE;
2456 clear_tv(rettv);
2457 if (error)
2458 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460
2461 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002462 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002464 while (p[0] == '|' && p[1] == '|')
2465 {
2466 if (getnext)
2467 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002468 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002469 {
2470 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2471 {
2472 error_white_both(p, 2);
2473 clear_tv(rettv);
2474 return FAIL;
2475 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002476 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002477 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002478
2479 /*
2480 * Get the second variable.
2481 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002482 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2483 {
2484 error_white_both(p, 2);
2485 clear_tv(rettv);
2486 return FAIL;
2487 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002488 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2489 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002490 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002491 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002493
2494 /*
2495 * Compute the result.
2496 */
2497 if (evaluate && !result)
2498 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002499 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002500 result = tv_get_bool_chk(&var2, &error);
2501 else if (tv_get_number_chk(&var2, &error) != 0)
2502 result = TRUE;
2503 clear_tv(&var2);
2504 if (error)
2505 return FAIL;
2506 }
2507 if (evaluate)
2508 {
2509 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002510 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002511 rettv->v_type = VAR_BOOL;
2512 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002513 }
2514 else
2515 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002516 rettv->v_type = VAR_NUMBER;
2517 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002518 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002519 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002520
2521 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002523
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002524 if (evalarg == NULL)
2525 clear_evalarg(&local_evalarg, NULL);
2526 else
2527 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
2529
2530 return OK;
2531}
2532
2533/*
2534 * Handle second level expression:
2535 * expr3 && expr3 && expr3 logical AND
2536 *
2537 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002538 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 *
2540 * Return OK or FAIL.
2541 */
2542 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002543eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002545 char_u *p;
2546 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
2548 /*
2549 * Get the first variable.
2550 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002551 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 return FAIL;
2553
2554 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002555 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002557 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002558 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002560 evalarg_T *evalarg_used = evalarg;
2561 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002562 int orig_flags;
2563 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002564 long result = TRUE;
2565 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002566 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002567 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002568
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002569 if (evalarg == NULL)
2570 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002571 CLEAR_FIELD(local_evalarg);
2572 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002573 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002574 orig_flags = evalarg_used->eval_flags;
2575 evaluate = orig_flags & EVAL_EVALUATE;
2576 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002577 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002578 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002579 result = tv_get_bool_chk(rettv, &error);
2580 else if (tv_get_number_chk(rettv, &error) == 0)
2581 result = FALSE;
2582 clear_tv(rettv);
2583 if (error)
2584 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586
2587 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002588 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002590 while (p[0] == '&' && p[1] == '&')
2591 {
2592 if (getnext)
2593 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002594 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002595 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002596 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002597 {
2598 error_white_both(p, 2);
2599 clear_tv(rettv);
2600 return FAIL;
2601 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002602 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002603 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002604
2605 /*
2606 * Get the second variable.
2607 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002608 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2609 {
2610 error_white_both(p, 2);
2611 clear_tv(rettv);
2612 return FAIL;
2613 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2615 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002616 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002617 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002618 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002619 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620
2621 /*
2622 * Compute the result.
2623 */
2624 if (evaluate && result)
2625 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002626 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002627 result = tv_get_bool_chk(&var2, &error);
2628 else if (tv_get_number_chk(&var2, &error) == 0)
2629 result = FALSE;
2630 clear_tv(&var2);
2631 if (error)
2632 return FAIL;
2633 }
2634 if (evaluate)
2635 {
2636 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002637 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002638 rettv->v_type = VAR_BOOL;
2639 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002640 }
2641 else
2642 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002643 rettv->v_type = VAR_NUMBER;
2644 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002645 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002646 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002647
2648 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002650
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002651 if (evalarg == NULL)
2652 clear_evalarg(&local_evalarg, NULL);
2653 else
2654 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
2656
2657 return OK;
2658}
2659
2660/*
2661 * Handle third level expression:
2662 * var1 == var2
2663 * var1 =~ var2
2664 * var1 != var2
2665 * var1 !~ var2
2666 * var1 > var2
2667 * var1 >= var2
2668 * var1 < var2
2669 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002670 * var1 is var2
2671 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 *
2673 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002674 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 *
2676 * Return OK or FAIL.
2677 */
2678 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002679eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002682 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002683 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002685 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687 /*
2688 * Get the first variable.
2689 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002690 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 return FAIL;
2692
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002693 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002694 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002697 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002699 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002701 typval_T var2;
2702 int ic;
2703 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002704 int evaluate = evalarg == NULL
2705 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002706
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002707 if (getnext)
2708 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002709 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2710 {
2711 error_white_both(p, len);
2712 clear_tv(rettv);
2713 return FAIL;
2714 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002715
Bram Moolenaar696ba232020-07-29 21:20:41 +02002716 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2717 {
2718 semsg(_(e_invexpr2), p);
2719 clear_tv(rettv);
2720 return FAIL;
2721 }
2722
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002723 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 if (p[len] == '?')
2725 {
2726 ic = TRUE;
2727 ++len;
2728 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002729 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 else if (p[len] == '#')
2731 {
2732 ic = FALSE;
2733 ++len;
2734 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002735 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002737 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 /*
2740 * Get the second variable.
2741 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002742 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2743 {
2744 error_white_both(p, 1);
2745 clear_tv(rettv);
2746 return FAIL;
2747 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002748 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002749 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002751 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 return FAIL;
2753 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002754 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002755 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002756 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002757
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002758 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002759 {
2760 ret = FAIL;
2761 clear_tv(rettv);
2762 }
2763 else
2764 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002765 clear_tv(&var2);
2766 return ret;
2767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769
2770 return OK;
2771}
2772
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002773/*
2774 * Make a copy of blob "tv1" and append blob "tv2".
2775 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 void
2777eval_addblob(typval_T *tv1, typval_T *tv2)
2778{
2779 blob_T *b1 = tv1->vval.v_blob;
2780 blob_T *b2 = tv2->vval.v_blob;
2781 blob_T *b = blob_alloc();
2782 int i;
2783
2784 if (b != NULL)
2785 {
2786 for (i = 0; i < blob_len(b1); i++)
2787 ga_append(&b->bv_ga, blob_get(b1, i));
2788 for (i = 0; i < blob_len(b2); i++)
2789 ga_append(&b->bv_ga, blob_get(b2, i));
2790
2791 clear_tv(tv1);
2792 rettv_blob_set(tv1, b);
2793 }
2794}
2795
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002796/*
2797 * Make a copy of list "tv1" and append list "tv2".
2798 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 int
2800eval_addlist(typval_T *tv1, typval_T *tv2)
2801{
2802 typval_T var3;
2803
2804 // concatenate Lists
2805 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2806 {
2807 clear_tv(tv1);
2808 clear_tv(tv2);
2809 return FAIL;
2810 }
2811 clear_tv(tv1);
2812 *tv1 = var3;
2813 return OK;
2814}
2815
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816/*
2817 * Handle fourth level expression:
2818 * + number addition
2819 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002820 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002821 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 *
2823 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002824 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 *
2826 * Return OK or FAIL.
2827 */
2828 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002829eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 /*
2832 * Get the first variable.
2833 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002834 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 return FAIL;
2836
2837 /*
2838 * Repeat computing, until no '+', '-' or '.' is following.
2839 */
2840 for (;;)
2841 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002842 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002843 int getnext;
2844 char_u *p;
2845 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002846 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002847 int concat;
2848 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002849 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002850
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002851 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002852 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002853 p = eval_next_non_blank(*arg, evalarg, &getnext);
2854 op = *p;
2855 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002856 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002858
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002859 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002860 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002861 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002862 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002863 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002864 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002865 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002866 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002867 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002868 clear_tv(rettv);
2869 return FAIL;
2870 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002871 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002872 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002873 if ((op != '+' || (rettv->v_type != VAR_LIST
2874 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002875#ifdef FEAT_FLOAT
2876 && (op == '.' || rettv->v_type != VAR_FLOAT)
2877#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002878 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002879 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002880 int error = FALSE;
2881
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002882 // For "list + ...", an illegal use of the first operand as
2883 // a number cannot be determined before evaluating the 2nd
2884 // operand: if this is also a list, all is ok.
2885 // For "something . ...", "something - ..." or "non-list + ...",
2886 // we know that the first operand needs to be a string or number
2887 // without evaluating the 2nd operand. So check before to avoid
2888 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002889 if (op != '.')
2890 tv_get_number_chk(rettv, &error);
2891 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002892 {
2893 clear_tv(rettv);
2894 return FAIL;
2895 }
2896 }
2897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 /*
2899 * Get the second variable.
2900 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002901 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002902 {
2903 error_white_both(p, oplen);
2904 clear_tv(rettv);
2905 return FAIL;
2906 }
2907 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002908 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002910 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 return FAIL;
2912 }
2913
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002914 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 {
2916 /*
2917 * Compute the result.
2918 */
2919 if (op == '.')
2920 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002921 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2922 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002923 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002924
Bram Moolenaar2e086612020-08-25 22:37:48 +02002925 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002926 || var2.v_type == VAR_CHANNEL
2927 || var2.v_type == VAR_JOB))
2928 emsg(_(e_inval_string));
2929#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002930 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002931 {
2932 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2933 var2.vval.v_float);
2934 s2 = buf2;
2935 }
2936#endif
2937 else
2938 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002939 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002940 {
2941 clear_tv(rettv);
2942 clear_tv(&var2);
2943 return FAIL;
2944 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002946 clear_tv(rettv);
2947 rettv->v_type = VAR_STRING;
2948 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002950 else if (op == '+' && rettv->v_type == VAR_BLOB
2951 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002953 else if (op == '+' && rettv->v_type == VAR_LIST
2954 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002955 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002957 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 else
2960 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002961 int error = FALSE;
2962 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002963#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002964 float_T f1 = 0, f2 = 0;
2965
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002966 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002967 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002968 f1 = rettv->vval.v_float;
2969 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002970 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002971 else
2972#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002973 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002974 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002975 if (error)
2976 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002977 // This can only happen for "list + non-list". For
2978 // "non-list + ..." or "something - ...", we returned
2979 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002980 clear_tv(rettv);
2981 return FAIL;
2982 }
2983#ifdef FEAT_FLOAT
2984 if (var2.v_type == VAR_FLOAT)
2985 f1 = n1;
2986#endif
2987 }
2988#ifdef FEAT_FLOAT
2989 if (var2.v_type == VAR_FLOAT)
2990 {
2991 f2 = var2.vval.v_float;
2992 n2 = 0;
2993 }
2994 else
2995#endif
2996 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002997 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002998 if (error)
2999 {
3000 clear_tv(rettv);
3001 clear_tv(&var2);
3002 return FAIL;
3003 }
3004#ifdef FEAT_FLOAT
3005 if (rettv->v_type == VAR_FLOAT)
3006 f2 = n2;
3007#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003009 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003010
3011#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003012 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003013 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3014 {
3015 if (op == '+')
3016 f1 = f1 + f2;
3017 else
3018 f1 = f1 - f2;
3019 rettv->v_type = VAR_FLOAT;
3020 rettv->vval.v_float = f1;
3021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003023#endif
3024 {
3025 if (op == '+')
3026 n1 = n1 + n2;
3027 else
3028 n1 = n1 - n2;
3029 rettv->v_type = VAR_NUMBER;
3030 rettv->vval.v_number = n1;
3031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003033 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 }
3035 }
3036 return OK;
3037}
3038
3039/*
3040 * Handle fifth level expression:
3041 * * number multiplication
3042 * / number division
3043 * % number modulo
3044 *
3045 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003046 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 *
3048 * Return OK or FAIL.
3049 */
3050 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003051eval6(
3052 char_u **arg,
3053 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003054 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003055 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003057#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003058 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
3061 /*
3062 * Get the first variable.
3063 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003064 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 return FAIL;
3066
3067 /*
3068 * Repeat computing, until no '*', '/' or '%' is following.
3069 */
3070 for (;;)
3071 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003072 int evaluate;
3073 int getnext;
3074 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003075 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003076 int op;
3077 varnumber_T n1, n2;
3078#ifdef FEAT_FLOAT
3079 float_T f1, f2;
3080#endif
3081 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003082
Bram Moolenaar9d489562020-07-30 20:08:50 +02003083 p = eval_next_non_blank(*arg, evalarg, &getnext);
3084 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003085 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003087
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003088 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003089 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003090 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003091 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003092 {
3093 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3094 {
3095 error_white_both(p, 1);
3096 clear_tv(rettv);
3097 return FAIL;
3098 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003099 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003102#ifdef FEAT_FLOAT
3103 f1 = 0;
3104 f2 = 0;
3105#endif
3106 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003107 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003109#ifdef FEAT_FLOAT
3110 if (rettv->v_type == VAR_FLOAT)
3111 {
3112 f1 = rettv->vval.v_float;
3113 use_float = TRUE;
3114 n1 = 0;
3115 }
3116 else
3117#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003118 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003119 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003120 if (error)
3121 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 }
3123 else
3124 n1 = 0;
3125
3126 /*
3127 * Get the second variable.
3128 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003129 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3130 {
3131 error_white_both(p, 1);
3132 clear_tv(rettv);
3133 return FAIL;
3134 }
3135 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003136 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 return FAIL;
3138
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003139 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141#ifdef FEAT_FLOAT
3142 if (var2.v_type == VAR_FLOAT)
3143 {
3144 if (!use_float)
3145 {
3146 f1 = n1;
3147 use_float = TRUE;
3148 }
3149 f2 = var2.vval.v_float;
3150 n2 = 0;
3151 }
3152 else
3153#endif
3154 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003155 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003156 clear_tv(&var2);
3157 if (error)
3158 return FAIL;
3159#ifdef FEAT_FLOAT
3160 if (use_float)
3161 f2 = n2;
3162#endif
3163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
3165 /*
3166 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003167 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169#ifdef FEAT_FLOAT
3170 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003172 if (op == '*')
3173 f1 = f1 * f2;
3174 else if (op == '/')
3175 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003176# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003177 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003178 if (f2 == 0.0)
3179 {
3180 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003181 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003182 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003183 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003184 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003185 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003186 }
3187 else
3188 f1 = f1 / f2;
3189# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003190 // We rely on the floating point library to handle divide
3191 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003193# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003196 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003198 return FAIL;
3199 }
3200 rettv->v_type = VAR_FLOAT;
3201 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
3203 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003206 int failed = FALSE;
3207
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003208 if (op == '*')
3209 n1 = n1 * n2;
3210 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003211 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003213 n1 = num_modulus(n1, n2, &failed);
3214 if (failed)
3215 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003216
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003217 rettv->v_type = VAR_NUMBER;
3218 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 }
3221 }
3222
3223 return OK;
3224}
3225
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003226 int
3227eval_leader(char_u **arg, int vim9)
3228{
3229 char_u *s = *arg;
3230 char_u *p = *arg;
3231
3232 while (*p == '!' || *p == '-' || *p == '+')
3233 {
3234 char_u *n = skipwhite(p + 1);
3235
3236 // ++, --, -+ and +- are not accepted in Vim9 script
3237 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3238 {
3239 semsg(_(e_invexpr2), s);
3240 return FAIL;
3241 }
3242 p = n;
3243 }
3244 *arg = p;
3245 return OK;
3246}
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248/*
3249 * Handle sixth level expression:
3250 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003251 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003252 * "string" string constant
3253 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 * &option-name option value
3255 * @r register contents
3256 * identifier variable value
3257 * function() function call
3258 * $VAR environment variable
3259 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003260 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003262 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003263 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 *
3265 * Also handle:
3266 * ! in front logical NOT
3267 * - in front unary minus
3268 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003269 * trailing [] subscript in String or List
3270 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003271 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 *
3273 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003274 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 *
3276 * Return OK or FAIL.
3277 */
3278 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003279eval7(
3280 char_u **arg,
3281 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003282 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003285 int evaluate = evalarg != NULL
3286 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 int len;
3288 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 char_u *start_leader, *end_leader;
3290 int ret = OK;
3291 char_u *alias;
3292
3293 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003294 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003295 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003297 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298
3299 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003300 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 */
3302 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003303 if (eval_leader(arg, in_vim9script()) == FAIL)
3304 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 end_leader = *arg;
3306
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003307 if (**arg == '.' && (!isdigit(*(*arg + 1))
3308#ifdef FEAT_FLOAT
3309 || current_sctx.sc_version < 2
3310#endif
3311 ))
3312 {
3313 semsg(_(e_invexpr2), *arg);
3314 ++*arg;
3315 return FAIL;
3316 }
3317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 switch (**arg)
3319 {
3320 /*
3321 * Number constant.
3322 */
3323 case '0':
3324 case '1':
3325 case '2':
3326 case '3':
3327 case '4':
3328 case '5':
3329 case '6':
3330 case '7':
3331 case '8':
3332 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003333 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003334
3335 // Apply prefixed "-" and "+" now. Matters especially when
3336 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003337 if (ret == OK && evaluate && end_leader > start_leader
3338 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003339 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 break;
3341
3342 /*
3343 * String constant: "string".
3344 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003345 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 break;
3347
3348 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003349 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003351 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003352 break;
3353
3354 /*
3355 * List: [expr, expr]
3356 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003357 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 break;
3359
3360 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003361 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003362 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003363 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003364 {
3365 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003366 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003367 }
3368 else
3369 ret = NOTDONE;
3370 break;
3371
3372 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003373 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003374 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003375 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003376 case '{': if (in_vim9script())
3377 ret = NOTDONE;
3378 else
3379 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003380 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003381 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003382 break;
3383
3384 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003385 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003387 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 break;
3389
3390 /*
3391 * Environment variable: $VAR.
3392 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003393 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 break;
3395
3396 /*
3397 * Register contents: @r.
3398 */
3399 case '@': ++*arg;
3400 if (evaluate)
3401 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003402 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003403 rettv->vval.v_string = get_reg_contents(**arg,
3404 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 }
3406 if (**arg != NUL)
3407 ++*arg;
3408 break;
3409
3410 /*
3411 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003412 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003414 case '(': ret = NOTDONE;
3415 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003416 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003417 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003418 if (ret == OK && evaluate)
3419 {
3420 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3421
3422 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003423 if (compile_def_function(ufunc,
3424 TRUE, PROFILING(ufunc), NULL) == FAIL)
3425 {
3426 clear_tv(rettv);
3427 ret = FAIL;
3428 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003429 }
3430 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003431 if (ret == NOTDONE)
3432 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003433 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003434 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003435
Bram Moolenaar9215f012020-06-27 21:18:00 +02003436 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003437 if (**arg == ')')
3438 ++*arg;
3439 else if (ret == OK)
3440 {
3441 emsg(_(e_missing_close));
3442 clear_tv(rettv);
3443 ret = FAIL;
3444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446 break;
3447
Bram Moolenaar8c711452005-01-14 21:53:12 +00003448 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 break;
3450 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003451
3452 if (ret == NOTDONE)
3453 {
3454 /*
3455 * Must be a variable or function name.
3456 * Can also be a curly-braces kind of name: {expr}.
3457 */
3458 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003459 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003460 if (alias != NULL)
3461 s = alias;
3462
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003463 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003464 ret = FAIL;
3465 else
3466 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003467 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3468
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003469 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3470 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003471 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003472 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003473 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003474 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003475 else if (flags & EVAL_CONSTANT)
3476 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003477 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003478 {
3479 // get the value of "true", "false" or a variable
3480 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3481 {
3482 rettv->v_type = VAR_BOOL;
3483 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003484 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003485 }
3486 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003487 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003488 {
3489 rettv->v_type = VAR_BOOL;
3490 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003491 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003492 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003493 else if (len == 4 && in_vim9script()
3494 && STRNCMP(s, "null", 4) == 0)
3495 {
3496 rettv->v_type = VAR_SPECIAL;
3497 rettv->vval.v_number = VVAL_NULL;
3498 ret = OK;
3499 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003500 else
3501 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3502 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003503 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003504 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003505 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003506 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003507 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003508 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003509 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003510 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003511 }
3512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003513 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3514 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003515 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003516 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
3518 /*
3519 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3520 */
3521 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003522 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003523 return ret;
3524}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003525
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003526/*
3527 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003528 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003529 * Adjusts "end_leaderp" until it is at "start_leader".
3530 */
3531 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003532eval7_leader(
3533 typval_T *rettv,
3534 int numeric_only,
3535 char_u *start_leader,
3536 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003537{
3538 char_u *end_leader = *end_leaderp;
3539 int ret = OK;
3540 int error = FALSE;
3541 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003542 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003543#ifdef FEAT_FLOAT
3544 float_T f = 0.0;
3545
3546 if (rettv->v_type == VAR_FLOAT)
3547 f = rettv->vval.v_float;
3548 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003549#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003550 {
3551 while (VIM_ISWHITE(end_leader[-1]))
3552 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003553 if (in_vim9script() && end_leader[-1] == '!')
3554 val = tv2bool(rettv);
3555 else
3556 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003557 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003558 if (error)
3559 {
3560 clear_tv(rettv);
3561 ret = FAIL;
3562 }
3563 else
3564 {
3565 while (end_leader > start_leader)
3566 {
3567 --end_leader;
3568 if (*end_leader == '!')
3569 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003570 if (numeric_only)
3571 {
3572 ++end_leader;
3573 break;
3574 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003575#ifdef FEAT_FLOAT
3576 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003577 {
3578 if (in_vim9script())
3579 {
3580 rettv->v_type = VAR_BOOL;
3581 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3582 }
3583 else
3584 f = !f;
3585 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003586 else
3587#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003588 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003589 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003590 type = VAR_BOOL;
3591 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003592 }
3593 else if (*end_leader == '-')
3594 {
3595#ifdef FEAT_FLOAT
3596 if (rettv->v_type == VAR_FLOAT)
3597 f = -f;
3598 else
3599#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003600 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003601 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003602 type = VAR_NUMBER;
3603 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003604 }
3605 }
3606#ifdef FEAT_FLOAT
3607 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003609 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003610 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003612 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003613#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003614 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003615 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003616 if (in_vim9script())
3617 rettv->v_type = type;
3618 else
3619 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003620 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003623 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 return ret;
3625}
3626
3627/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003628 * Call the function referred to in "rettv".
3629 */
3630 static int
3631call_func_rettv(
3632 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003633 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003634 typval_T *rettv,
3635 int evaluate,
3636 dict_T *selfdict,
3637 typval_T *basetv)
3638{
3639 partial_T *pt = NULL;
3640 funcexe_T funcexe;
3641 typval_T functv;
3642 char_u *s;
3643 int ret;
3644
3645 // need to copy the funcref so that we can clear rettv
3646 if (evaluate)
3647 {
3648 functv = *rettv;
3649 rettv->v_type = VAR_UNKNOWN;
3650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003651 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003652 if (functv.v_type == VAR_PARTIAL)
3653 {
3654 pt = functv.vval.v_partial;
3655 s = partial_name(pt);
3656 }
3657 else
3658 s = functv.vval.v_string;
3659 }
3660 else
3661 s = (char_u *)"";
3662
Bram Moolenaara80faa82020-04-12 19:37:17 +02003663 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003664 funcexe.firstline = curwin->w_cursor.lnum;
3665 funcexe.lastline = curwin->w_cursor.lnum;
3666 funcexe.evaluate = evaluate;
3667 funcexe.partial = pt;
3668 funcexe.selfdict = selfdict;
3669 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003670 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003672 // Clear the funcref afterwards, so that deleting it while
3673 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003674 if (evaluate)
3675 clear_tv(&functv);
3676
3677 return ret;
3678}
3679
3680/*
3681 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003682 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003683 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3684 */
3685 static int
3686eval_lambda(
3687 char_u **arg,
3688 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003689 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003690 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003691{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003692 int evaluate = evalarg != NULL
3693 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003694 typval_T base = *rettv;
3695 int ret;
3696
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003697 rettv->v_type = VAR_UNKNOWN;
3698
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003699 if (**arg == '{')
3700 {
3701 // ->{lambda}()
3702 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3703 }
3704 else
3705 {
3706 // ->(lambda)()
3707 ++*arg;
3708 ret = eval1(arg, rettv, evalarg);
3709 *arg = skipwhite_and_linebreak(*arg, evalarg);
3710 if (**arg != ')')
3711 {
3712 emsg(_(e_missing_close));
3713 ret = FAIL;
3714 }
3715 ++*arg;
3716 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003717 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003718 return FAIL;
3719 else if (**arg != '(')
3720 {
3721 if (verbose)
3722 {
3723 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003724 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003725 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003727 }
3728 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003729 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003730 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003731 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003732 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003733
3734 // Clear the funcref afterwards, so that deleting it while
3735 // evaluating the arguments is possible (see test55).
3736 if (evaluate)
3737 clear_tv(&base);
3738
3739 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003740}
3741
3742/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003743 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003744 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003745 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3746 */
3747 static int
3748eval_method(
3749 char_u **arg,
3750 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003751 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003752 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003753{
3754 char_u *name;
3755 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003756 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003757 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003758 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003759 int evaluate = evalarg != NULL
3760 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003761
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003762 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003763
Bram Moolenaarac92e252019-08-03 21:58:38 +02003764 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003765 len = get_name_len(arg, &alias, evaluate, TRUE);
3766 if (alias != NULL)
3767 name = alias;
3768
3769 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003770 {
3771 if (verbose)
3772 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003773 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003774 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003775 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003776 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003777 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003778 if (**arg != '(')
3779 {
3780 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003782 ret = FAIL;
3783 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003784 else if (VIM_ISWHITE((*arg)[-1]))
3785 {
3786 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003787 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003788 ret = FAIL;
3789 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003790 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003791 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003792 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003793 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003794
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003795 // Clear the funcref afterwards, so that deleting it while
3796 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003797 if (evaluate)
3798 clear_tv(&base);
3799
Bram Moolenaarac92e252019-08-03 21:58:38 +02003800 return ret;
3801}
3802
3803/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003804 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3805 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003806 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3807 */
3808 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003809eval_index(
3810 char_u **arg,
3811 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003812 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003813 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003814{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003815 int evaluate = evalarg != NULL
3816 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003817 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003818 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003819 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003820 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003821 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003822 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003823
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003824 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3825 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003826
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003827 init_tv(&var1);
3828 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003829 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003830 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003831 /*
3832 * dict.name
3833 */
3834 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003835 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003836 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003837 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003838 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003839 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003840 }
3841 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003842 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003843 /*
3844 * something[idx]
3845 *
3846 * Get the (first) variable from inside the [].
3847 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003848 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003849 if (**arg == ':')
3850 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003851 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003852 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003853 else if (vim9 && **arg == ':')
3854 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003855 semsg(_(e_white_space_required_before_and_after_str_at_str),
3856 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003857 clear_tv(&var1);
3858 return FAIL;
3859 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003860 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003861 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003862#ifdef FEAT_FLOAT
3863 // allow for indexing with float
3864 if (vim9 && rettv->v_type == VAR_DICT
3865 && var1.v_type == VAR_FLOAT)
3866 {
3867 var1.vval.v_string = typval_tostring(&var1, TRUE);
3868 var1.v_type = VAR_STRING;
3869 }
3870#endif
3871 if (tv_get_string_chk(&var1) == NULL)
3872 {
3873 // not a number or string
3874 clear_tv(&var1);
3875 return FAIL;
3876 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003877 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003878
3879 /*
3880 * Get the second variable from inside the [:].
3881 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003882 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003883 if (**arg == ':')
3884 {
3885 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003886 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003887 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003888 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003889 semsg(_(e_white_space_required_before_and_after_str_at_str),
3890 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003891 if (!empty1)
3892 clear_tv(&var1);
3893 return FAIL;
3894 }
3895 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003896 if (**arg == ']')
3897 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003898 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003900 if (!empty1)
3901 clear_tv(&var1);
3902 return FAIL;
3903 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003904 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003905 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003906 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003907 if (!empty1)
3908 clear_tv(&var1);
3909 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003910 return FAIL;
3911 }
3912 }
3913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003914 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003915 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003916 if (**arg != ']')
3917 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003918 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003919 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003920 clear_tv(&var1);
3921 if (range)
3922 clear_tv(&var2);
3923 return FAIL;
3924 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003925 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003926 }
3927
3928 if (evaluate)
3929 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003930 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003931 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003932 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003933
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003934 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003935 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003936 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003937 clear_tv(&var2);
3938 return res;
3939 }
3940 return OK;
3941}
3942
3943/*
3944 * Check if "rettv" can have an [index] or [sli:ce]
3945 */
3946 int
3947check_can_index(typval_T *rettv, int evaluate, int verbose)
3948{
3949 switch (rettv->v_type)
3950 {
3951 case VAR_FUNC:
3952 case VAR_PARTIAL:
3953 if (verbose)
3954 emsg(_("E695: Cannot index a Funcref"));
3955 return FAIL;
3956 case VAR_FLOAT:
3957#ifdef FEAT_FLOAT
3958 if (verbose)
3959 emsg(_(e_float_as_string));
3960 return FAIL;
3961#endif
3962 case VAR_BOOL:
3963 case VAR_SPECIAL:
3964 case VAR_JOB:
3965 case VAR_CHANNEL:
3966 if (verbose)
3967 emsg(_(e_cannot_index_special_variable));
3968 return FAIL;
3969 case VAR_UNKNOWN:
3970 case VAR_ANY:
3971 case VAR_VOID:
3972 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003973 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003974 emsg(_(e_cannot_index_special_variable));
3975 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003976 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003977 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003978
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003979 case VAR_STRING:
3980 case VAR_LIST:
3981 case VAR_DICT:
3982 case VAR_BLOB:
3983 break;
3984 case VAR_NUMBER:
3985 if (in_vim9script())
3986 emsg(_(e_cannot_index_number));
3987 break;
3988 }
3989 return OK;
3990}
3991
3992/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01003993 * slice() function
3994 */
3995 void
3996f_slice(typval_T *argvars, typval_T *rettv)
3997{
3998 if (check_can_index(argvars, TRUE, FALSE) == OK)
3999 {
4000 copy_tv(argvars, rettv);
4001 eval_index_inner(rettv, TRUE, argvars + 1,
4002 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4003 TRUE, NULL, 0, FALSE);
4004 }
4005}
4006
4007/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004008 * Apply index or range to "rettv".
4009 * "var1" is the first index, NULL for [:expr].
4010 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004011 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4012 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004013 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4014 */
4015 int
4016eval_index_inner(
4017 typval_T *rettv,
4018 int is_range,
4019 typval_T *var1,
4020 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004021 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004022 char_u *key,
4023 int keylen,
4024 int verbose)
4025{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004026 varnumber_T n1, n2 = 0;
4027 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004028
4029 n1 = 0;
4030 if (var1 != NULL && rettv->v_type != VAR_DICT)
4031 n1 = tv_get_number(var1);
4032
4033 if (is_range)
4034 {
4035 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004036 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004037 if (verbose)
4038 emsg(_(e_cannot_slice_dictionary));
4039 return FAIL;
4040 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004041 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004042 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004043 else
4044 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004045 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004046
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004047 switch (rettv->v_type)
4048 {
4049 case VAR_UNKNOWN:
4050 case VAR_ANY:
4051 case VAR_VOID:
4052 case VAR_FUNC:
4053 case VAR_PARTIAL:
4054 case VAR_FLOAT:
4055 case VAR_BOOL:
4056 case VAR_SPECIAL:
4057 case VAR_JOB:
4058 case VAR_CHANNEL:
4059 break; // not evaluating, skipping over subscript
4060
4061 case VAR_NUMBER:
4062 case VAR_STRING:
4063 {
4064 char_u *s = tv_get_string(rettv);
4065
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004066 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004067 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004068 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004069 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004070 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004071 else
4072 s = char_from_string(s, n1);
4073 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004074 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004075 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004076 // The resulting variable is a substring. If the indexes
4077 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004078 if (n1 < 0)
4079 {
4080 n1 = len + n1;
4081 if (n1 < 0)
4082 n1 = 0;
4083 }
4084 if (n2 < 0)
4085 n2 = len + n2;
4086 else if (n2 >= len)
4087 n2 = len;
4088 if (n1 >= len || n2 < 0 || n1 > n2)
4089 s = NULL;
4090 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004091 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004092 }
4093 else
4094 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004095 // The resulting variable is a string of a single
4096 // character. If the index is too big or negative the
4097 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004098 if (n1 >= len || n1 < 0)
4099 s = NULL;
4100 else
4101 s = vim_strnsave(s + n1, 1);
4102 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004103 clear_tv(rettv);
4104 rettv->v_type = VAR_STRING;
4105 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004106 }
4107 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004108
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004109 case VAR_BLOB:
4110 len = blob_len(rettv->vval.v_blob);
4111 if (is_range)
4112 {
4113 // The resulting variable is a sub-blob. If the indexes
4114 // are out of range the result is empty.
4115 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004116 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004117 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004118 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004119 n1 = 0;
4120 }
4121 if (n2 < 0)
4122 n2 = len + n2;
4123 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004124 n2 = len - (exclusive ? 0 : 1);
4125 if (exclusive)
4126 --n2;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004127 if (n1 >= len || n2 < 0 || n1 > n2)
4128 {
4129 clear_tv(rettv);
4130 rettv->v_type = VAR_BLOB;
4131 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004132 }
4133 else
4134 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004135 blob_T *blob = blob_alloc();
4136 long i;
4137
4138 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004139 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004140 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004141 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004142 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004143 return FAIL;
4144 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004145 blob->bv_ga.ga_len = n2 - n1 + 1;
4146 for (i = n1; i <= n2; i++)
4147 blob_set(blob, i - n1,
4148 blob_get(rettv->vval.v_blob, i));
4149
4150 clear_tv(rettv);
4151 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004152 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004153 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004154 }
4155 else
4156 {
4157 // The resulting variable is a byte value.
4158 // If the index is too big or negative that is an error.
4159 if (n1 < 0)
4160 n1 = len + n1;
4161 if (n1 < len && n1 >= 0)
4162 {
4163 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004164
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004165 clear_tv(rettv);
4166 rettv->v_type = VAR_NUMBER;
4167 rettv->vval.v_number = v;
4168 }
4169 else
4170 semsg(_(e_blobidx), n1);
4171 }
4172 break;
4173
4174 case VAR_LIST:
4175 if (var1 == NULL)
4176 n1 = 0;
4177 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004178 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004179 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004180 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004181 return FAIL;
4182 break;
4183
4184 case VAR_DICT:
4185 {
4186 dictitem_T *item;
4187 typval_T tmp;
4188
4189 if (key == NULL)
4190 {
4191 key = tv_get_string_chk(var1);
4192 if (key == NULL)
4193 return FAIL;
4194 }
4195
4196 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4197
4198 if (item == NULL && verbose)
4199 semsg(_(e_dictkey), key);
4200 if (item == NULL)
4201 return FAIL;
4202
4203 copy_tv(&item->di_tv, &tmp);
4204 clear_tv(rettv);
4205 *rettv = tmp;
4206 }
4207 break;
4208 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004209 return OK;
4210}
4211
4212/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004213 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004214 */
4215 char_u *
4216partial_name(partial_T *pt)
4217{
4218 if (pt->pt_name != NULL)
4219 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004220 if (pt->pt_func != NULL)
4221 return pt->pt_func->uf_name;
4222 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004223}
4224
Bram Moolenaarddecc252016-04-06 22:59:37 +02004225 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004226partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004227{
4228 int i;
4229
4230 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004231 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004232 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004233 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004234 if (pt->pt_name != NULL)
4235 {
4236 func_unref(pt->pt_name);
4237 vim_free(pt->pt_name);
4238 }
4239 else
4240 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004241
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004242 // Decrease the reference count for the context of a closure. If down
4243 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004244 if (pt->pt_funcstack != NULL)
4245 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004246 --pt->pt_funcstack->fs_refcount;
4247 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004248 }
4249
Bram Moolenaarddecc252016-04-06 22:59:37 +02004250 vim_free(pt);
4251}
4252
4253/*
4254 * Unreference a closure: decrement the reference count and free it when it
4255 * becomes zero.
4256 */
4257 void
4258partial_unref(partial_T *pt)
4259{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004260 if (pt != NULL)
4261 {
4262 if (--pt->pt_refcount <= 0)
4263 partial_free(pt);
4264
4265 // If the reference count goes down to one, the funcstack may be the
4266 // only reference and can be freed if no other partials reference it.
4267 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4268 funcstack_check_refcount(pt->pt_funcstack);
4269 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004270}
4271
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004272/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004273 * Return the next (unique) copy ID.
4274 * Used for serializing nested structures.
4275 */
4276 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004277get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004278{
4279 current_copyID += COPYID_INC;
4280 return current_copyID;
4281}
4282
4283/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004284 * Garbage collection for lists and dictionaries.
4285 *
4286 * We use reference counts to be able to free most items right away when they
4287 * are no longer used. But for composite items it's possible that it becomes
4288 * unused while the reference count is > 0: When there is a recursive
4289 * reference. Example:
4290 * :let l = [1, 2, 3]
4291 * :let d = {9: l}
4292 * :let l[1] = d
4293 *
4294 * Since this is quite unusual we handle this with garbage collection: every
4295 * once in a while find out which lists and dicts are not referenced from any
4296 * variable.
4297 *
4298 * Here is a good reference text about garbage collection (refers to Python
4299 * but it applies to all reference-counting mechanisms):
4300 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004301 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004302
4303/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004304 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004305 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004306 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004307 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004308 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004309garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004310{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004311 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004312 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004313 buf_T *buf;
4314 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004315 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004316 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004317
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004318 if (!testing)
4319 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004320 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004321 want_garbage_collect = FALSE;
4322 may_garbage_collect = FALSE;
4323 garbage_collect_at_exit = FALSE;
4324 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004325
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004326 // The execution stack can grow big, limit the size.
4327 if (exestack.ga_maxlen - exestack.ga_len > 500)
4328 {
4329 size_t new_len;
4330 char_u *pp;
4331 int n;
4332
4333 // Keep 150% of the current size, with a minimum of the growth size.
4334 n = exestack.ga_len / 2;
4335 if (n < exestack.ga_growsize)
4336 n = exestack.ga_growsize;
4337
4338 // Don't make it bigger though.
4339 if (exestack.ga_len + n < exestack.ga_maxlen)
4340 {
4341 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4342 pp = vim_realloc(exestack.ga_data, new_len);
4343 if (pp == NULL)
4344 return FAIL;
4345 exestack.ga_maxlen = exestack.ga_len + n;
4346 exestack.ga_data = pp;
4347 }
4348 }
4349
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004350 // We advance by two because we add one for items referenced through
4351 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004352 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004353
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004354 /*
4355 * 1. Go through all accessible variables and mark all lists and dicts
4356 * with copyID.
4357 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004358
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004359 // Don't free variables in the previous_funccal list unless they are only
4360 // referenced through previous_funccal. This must be first, because if
4361 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004362 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004363
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004364 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004365 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004367 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004368 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004369 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4370 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004371
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004372 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004373 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004374 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4375 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004376 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004377 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4378 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004379#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004380 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004381 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4382 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004383 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004384 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004385 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4386 NULL, NULL);
4387#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004388
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004389 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004390 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004391 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4392 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004393 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004394 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004395
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004396 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004397 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004398
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004399 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004400 abort = abort || set_ref_in_functions(copyID);
4401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004402 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004403 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004404
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004405 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004406 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004407
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004408 // callbacks in buffers
4409 abort = abort || set_ref_in_buffers(copyID);
4410
Bram Moolenaar1dced572012-04-05 16:54:08 +02004411#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004412 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004413#endif
4414
Bram Moolenaardb913952012-06-29 12:54:53 +02004415#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004416 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004417#endif
4418
4419#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004420 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004421#endif
4422
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004423#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004424 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004425 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004426#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004427#ifdef FEAT_NETBEANS_INTG
4428 abort = abort || set_ref_in_nb_channel(copyID);
4429#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004430
Bram Moolenaare3188e22016-05-31 21:13:04 +02004431#ifdef FEAT_TIMERS
4432 abort = abort || set_ref_in_timer(copyID);
4433#endif
4434
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004435#ifdef FEAT_QUICKFIX
4436 abort = abort || set_ref_in_quickfix(copyID);
4437#endif
4438
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004439#ifdef FEAT_TERMINAL
4440 abort = abort || set_ref_in_term(copyID);
4441#endif
4442
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004443#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004444 abort = abort || set_ref_in_popups(copyID);
4445#endif
4446
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004447 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004448 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004449 /*
4450 * 2. Free lists and dictionaries that are not referenced.
4451 */
4452 did_free = free_unref_items(copyID);
4453
4454 /*
4455 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004456 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004457 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004458 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004459 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004460 else if (p_verbose > 0)
4461 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004462 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004463 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004464
4465 return did_free;
4466}
4467
4468/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004469 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004470 */
4471 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004472free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004473{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004474 int did_free = FALSE;
4475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004476 // Let all "free" functions know that we are here. This means no
4477 // dictionaries, lists, channels or jobs are to be freed, because we will
4478 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004479 in_free_unref_items = TRUE;
4480
4481 /*
4482 * PASS 1: free the contents of the items. We don't free the items
4483 * themselves yet, so that it is possible to decrement refcount counters
4484 */
4485
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004486 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004487 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004489 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004490 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004491
4492#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004493 // Go through the list of jobs and free items without the copyID. This
4494 // must happen before doing channels, because jobs refer to channels, but
4495 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004496 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4497
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004498 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004499 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4500#endif
4501
4502 /*
4503 * PASS 2: free the items themselves.
4504 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004505 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004506 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004507
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004508#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004509 // Go through the list of jobs and free items without the copyID. This
4510 // must happen before doing channels, because jobs refer to channels, but
4511 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004512 free_unused_jobs(copyID, COPYID_MASK);
4513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004514 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004515 free_unused_channels(copyID, COPYID_MASK);
4516#endif
4517
4518 in_free_unref_items = FALSE;
4519
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004520 return did_free;
4521}
4522
4523/*
4524 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004525 * "list_stack" is used to add lists to be marked. Can be NULL.
4526 *
4527 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004528 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004529 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004530set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004531{
4532 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004533 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004534 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004535 hashtab_T *cur_ht;
4536 ht_stack_T *ht_stack = NULL;
4537 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004538
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004539 cur_ht = ht;
4540 for (;;)
4541 {
4542 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004543 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004544 // Mark each item in the hashtab. If the item contains a hashtab
4545 // it is added to ht_stack, if it contains a list it is added to
4546 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004547 todo = (int)cur_ht->ht_used;
4548 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4549 if (!HASHITEM_EMPTY(hi))
4550 {
4551 --todo;
4552 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4553 &ht_stack, list_stack);
4554 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004555 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004556
4557 if (ht_stack == NULL)
4558 break;
4559
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004560 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004561 cur_ht = ht_stack->ht;
4562 tempitem = ht_stack;
4563 ht_stack = ht_stack->prev;
4564 free(tempitem);
4565 }
4566
4567 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004568}
4569
4570/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004571 * Mark a dict and its items with "copyID".
4572 * Returns TRUE if setting references failed somehow.
4573 */
4574 int
4575set_ref_in_dict(dict_T *d, int copyID)
4576{
4577 if (d != NULL && d->dv_copyID != copyID)
4578 {
4579 d->dv_copyID = copyID;
4580 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4581 }
4582 return FALSE;
4583}
4584
4585/*
4586 * Mark a list and its items with "copyID".
4587 * Returns TRUE if setting references failed somehow.
4588 */
4589 int
4590set_ref_in_list(list_T *ll, int copyID)
4591{
4592 if (ll != NULL && ll->lv_copyID != copyID)
4593 {
4594 ll->lv_copyID = copyID;
4595 return set_ref_in_list_items(ll, copyID, NULL);
4596 }
4597 return FALSE;
4598}
4599
4600/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004601 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004602 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4603 *
4604 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004605 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004606 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004607set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004608{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004609 listitem_T *li;
4610 int abort = FALSE;
4611 list_T *cur_l;
4612 list_stack_T *list_stack = NULL;
4613 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004614
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004615 cur_l = l;
4616 for (;;)
4617 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004618 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004619 // Mark each item in the list. If the item contains a hashtab
4620 // it is added to ht_stack, if it contains a list it is added to
4621 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004622 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4623 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4624 ht_stack, &list_stack);
4625 if (list_stack == NULL)
4626 break;
4627
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004628 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004629 cur_l = list_stack->list;
4630 tempitem = list_stack;
4631 list_stack = list_stack->prev;
4632 free(tempitem);
4633 }
4634
4635 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004636}
4637
4638/*
4639 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004640 * "list_stack" is used to add lists to be marked. Can be NULL.
4641 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4642 *
4643 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004644 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004645 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004646set_ref_in_item(
4647 typval_T *tv,
4648 int copyID,
4649 ht_stack_T **ht_stack,
4650 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004651{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004652 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004653
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004654 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004655 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004656 dict_T *dd = tv->vval.v_dict;
4657
Bram Moolenaara03f2332016-02-06 18:09:59 +01004658 if (dd != NULL && dd->dv_copyID != copyID)
4659 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004660 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004661 dd->dv_copyID = copyID;
4662 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004663 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004664 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4665 }
4666 else
4667 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004668 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4669
Bram Moolenaara03f2332016-02-06 18:09:59 +01004670 if (newitem == NULL)
4671 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004672 else
4673 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004674 newitem->ht = &dd->dv_hashtab;
4675 newitem->prev = *ht_stack;
4676 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004677 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004678 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004679 }
4680 }
4681 else if (tv->v_type == VAR_LIST)
4682 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004683 list_T *ll = tv->vval.v_list;
4684
Bram Moolenaara03f2332016-02-06 18:09:59 +01004685 if (ll != NULL && ll->lv_copyID != copyID)
4686 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004687 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004688 ll->lv_copyID = copyID;
4689 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004690 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004691 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004692 }
4693 else
4694 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004695 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4696
Bram Moolenaara03f2332016-02-06 18:09:59 +01004697 if (newitem == NULL)
4698 abort = TRUE;
4699 else
4700 {
4701 newitem->list = ll;
4702 newitem->prev = *list_stack;
4703 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004704 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004705 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004706 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004707 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004708 else if (tv->v_type == VAR_FUNC)
4709 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004710 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004711 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004712 else if (tv->v_type == VAR_PARTIAL)
4713 {
4714 partial_T *pt = tv->vval.v_partial;
4715 int i;
4716
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004717 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004718 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004719 // Didn't see this partial yet.
4720 pt->pt_copyID = copyID;
4721
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004722 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004723
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004724 if (pt->pt_dict != NULL)
4725 {
4726 typval_T dtv;
4727
4728 dtv.v_type = VAR_DICT;
4729 dtv.vval.v_dict = pt->pt_dict;
4730 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4731 }
4732
4733 for (i = 0; i < pt->pt_argc; ++i)
4734 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4735 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004736 if (pt->pt_funcstack != NULL)
4737 {
4738 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4739
4740 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4741 abort = abort || set_ref_in_item(stack + i, copyID,
4742 ht_stack, list_stack);
4743 }
4744
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004745 }
4746 }
4747#ifdef FEAT_JOB_CHANNEL
4748 else if (tv->v_type == VAR_JOB)
4749 {
4750 job_T *job = tv->vval.v_job;
4751 typval_T dtv;
4752
4753 if (job != NULL && job->jv_copyID != copyID)
4754 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004755 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004756 if (job->jv_channel != NULL)
4757 {
4758 dtv.v_type = VAR_CHANNEL;
4759 dtv.vval.v_channel = job->jv_channel;
4760 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4761 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004762 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004763 {
4764 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004765 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004766 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4767 }
4768 }
4769 }
4770 else if (tv->v_type == VAR_CHANNEL)
4771 {
4772 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004773 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004774 typval_T dtv;
4775 jsonq_T *jq;
4776 cbq_T *cq;
4777
4778 if (ch != NULL && ch->ch_copyID != copyID)
4779 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004780 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004781 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004782 {
4783 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4784 jq = jq->jq_next)
4785 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4786 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4787 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004788 if (cq->cq_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 = cq->cq_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_part[part].ch_callback.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 =
4798 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004799 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4800 }
4801 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004802 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004803 {
4804 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004805 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004806 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4807 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004808 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004809 {
4810 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004811 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004812 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4813 }
4814 }
4815 }
4816#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004817 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004818}
4819
Bram Moolenaar8c711452005-01-14 21:53:12 +00004820/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004821 * Return a string with the string representation of a variable.
4822 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004823 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004824 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004825 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004826 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004827 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004828 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4829 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004830 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004831 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004832 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004833echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004834 typval_T *tv,
4835 char_u **tofree,
4836 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004837 int copyID,
4838 int echo_style,
4839 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004840 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004841{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004842 static int recurse = 0;
4843 char_u *r = NULL;
4844
Bram Moolenaar33570922005-01-25 22:26:29 +00004845 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004846 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004847 if (!did_echo_string_emsg)
4848 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004849 // Only give this message once for a recursive call to avoid
4850 // flooding the user with errors. And stop iterating over lists
4851 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004852 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004853 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004854 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004855 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004856 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004857 }
4858 ++recurse;
4859
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860 switch (tv->v_type)
4861 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004862 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004863 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004864 {
4865 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004866 r = tv->vval.v_string;
4867 if (r == NULL)
4868 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004869 }
4870 else
4871 {
4872 *tofree = string_quote(tv->vval.v_string, FALSE);
4873 r = *tofree;
4874 }
4875 break;
4876
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004877 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004878 if (echo_style)
4879 {
4880 *tofree = NULL;
4881 r = tv->vval.v_string;
4882 }
4883 else
4884 {
4885 *tofree = string_quote(tv->vval.v_string, TRUE);
4886 r = *tofree;
4887 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004888 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004889
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004890 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004891 {
4892 partial_T *pt = tv->vval.v_partial;
4893 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004894 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004895 garray_T ga;
4896 int i;
4897 char_u *tf;
4898
4899 ga_init2(&ga, 1, 100);
4900 ga_concat(&ga, (char_u *)"function(");
4901 if (fname != NULL)
4902 {
4903 ga_concat(&ga, fname);
4904 vim_free(fname);
4905 }
4906 if (pt != NULL && pt->pt_argc > 0)
4907 {
4908 ga_concat(&ga, (char_u *)", [");
4909 for (i = 0; i < pt->pt_argc; ++i)
4910 {
4911 if (i > 0)
4912 ga_concat(&ga, (char_u *)", ");
4913 ga_concat(&ga,
4914 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4915 vim_free(tf);
4916 }
4917 ga_concat(&ga, (char_u *)"]");
4918 }
4919 if (pt != NULL && pt->pt_dict != NULL)
4920 {
4921 typval_T dtv;
4922
4923 ga_concat(&ga, (char_u *)", ");
4924 dtv.v_type = VAR_DICT;
4925 dtv.vval.v_dict = pt->pt_dict;
4926 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4927 vim_free(tf);
4928 }
4929 ga_concat(&ga, (char_u *)")");
4930
4931 *tofree = ga.ga_data;
4932 r = *tofree;
4933 break;
4934 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004935
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004936 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004937 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004938 break;
4939
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004940 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004941 if (tv->vval.v_list == NULL)
4942 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004943 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004944 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004945 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004946 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004947 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4948 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004949 {
4950 *tofree = NULL;
4951 r = (char_u *)"[...]";
4952 }
4953 else
4954 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004955 int old_copyID = tv->vval.v_list->lv_copyID;
4956
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004957 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004958 *tofree = list2string(tv, copyID, restore_copyID);
4959 if (restore_copyID)
4960 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004961 r = *tofree;
4962 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004963 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004964
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004966 if (tv->vval.v_dict == NULL)
4967 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004968 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004969 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004970 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004971 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004972 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4973 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004974 {
4975 *tofree = NULL;
4976 r = (char_u *)"{...}";
4977 }
4978 else
4979 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004980 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004981
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004982 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004983 *tofree = dict2string(tv, copyID, restore_copyID);
4984 if (restore_copyID)
4985 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004986 r = *tofree;
4987 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004988 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004989
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004990 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004991 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004992 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004993 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004994 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004995 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004996 break;
4997
Bram Moolenaar835dc632016-02-07 14:27:38 +01004998 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004999 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005000 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005001 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005002 if (composite_val)
5003 {
5004 *tofree = string_quote(r, FALSE);
5005 r = *tofree;
5006 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005007 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005008
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005010#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 *tofree = NULL;
5012 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5013 r = numbuf;
5014 break;
5015#endif
5016
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005017 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005018 case VAR_SPECIAL:
5019 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005020 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005021 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005022 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005023
Bram Moolenaar8502c702014-06-17 12:51:16 +02005024 if (--recurse == 0)
5025 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005026 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005027}
5028
5029/*
5030 * Return a string with the string representation of a variable.
5031 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5032 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005033 * Does not put quotes around strings, as ":echo" displays values.
5034 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5035 * May return NULL.
5036 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005037 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005038echo_string(
5039 typval_T *tv,
5040 char_u **tofree,
5041 char_u *numbuf,
5042 int copyID)
5043{
5044 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5045}
5046
5047/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005048 * Return string "str" in ' quotes, doubling ' characters.
5049 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005050 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005051 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005052 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005053string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005054{
Bram Moolenaar33570922005-01-25 22:26:29 +00005055 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005056 char_u *p, *r, *s;
5057
Bram Moolenaar33570922005-01-25 22:26:29 +00005058 len = (function ? 13 : 3);
5059 if (str != NULL)
5060 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005061 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005062 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005063 if (*p == '\'')
5064 ++len;
5065 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005066 s = r = alloc(len);
5067 if (r != NULL)
5068 {
5069 if (function)
5070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005072 r += 10;
5073 }
5074 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005076 if (str != NULL)
5077 for (p = str; *p != NUL; )
5078 {
5079 if (*p == '\'')
5080 *r++ = '\'';
5081 MB_COPY_CHAR(p, r);
5082 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005084 if (function)
5085 *r++ = ')';
5086 *r++ = NUL;
5087 }
5088 return s;
5089}
5090
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005091#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005092/*
5093 * Convert the string "text" to a floating point number.
5094 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5095 * this always uses a decimal point.
5096 * Returns the length of the text that was consumed.
5097 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005098 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005099string2float(
5100 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005101 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005102{
5103 char *s = (char *)text;
5104 float_T f;
5105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005106 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005107 if (STRNICMP(text, "inf", 3) == 0)
5108 {
5109 *value = INFINITY;
5110 return 3;
5111 }
5112 if (STRNICMP(text, "-inf", 3) == 0)
5113 {
5114 *value = -INFINITY;
5115 return 4;
5116 }
5117 if (STRNICMP(text, "nan", 3) == 0)
5118 {
5119 *value = NAN;
5120 return 3;
5121 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005122 f = strtod(s, &s);
5123 *value = f;
5124 return (int)((char_u *)s - text);
5125}
5126#endif
5127
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005128/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005129 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5130 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005131 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005132 */
5133 int
5134buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5135{
5136 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005137 char_u *t;
5138 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005139
5140 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5141 return -1;
5142
5143 if (lnum > buf->b_ml.ml_line_count)
5144 lnum = buf->b_ml.ml_line_count;
5145
5146 str = ml_get_buf(buf, lnum, FALSE);
5147 if (str == NULL)
5148 return -1;
5149
5150 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005151 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005152
Bram Moolenaar91458462021-01-13 20:08:38 +01005153 // count the number of characters
5154 t = str;
5155 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5156 t += mb_ptr2len(t);
5157
5158 // In insert mode, when the cursor is at the end of a non-empty line,
5159 // byteidx points to the NUL character immediately past the end of the
5160 // string. In this case, add one to the character count.
5161 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5162 count++;
5163
5164 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005165}
5166
5167/*
5168 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005169 * byte index. Works only for loaded buffers. Returns -1 on failure.
5170 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005171 */
5172 int
5173buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5174{
5175 char_u *str;
5176 char_u *t;
5177
5178 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5179 return -1;
5180
5181 if (lnum > buf->b_ml.ml_line_count)
5182 lnum = buf->b_ml.ml_line_count;
5183
5184 str = ml_get_buf(buf, lnum, FALSE);
5185 if (str == NULL)
5186 return -1;
5187
5188 // Convert the character offset to a byte offset
5189 t = str;
5190 while (*t != NUL && --charidx > 0)
5191 t += mb_ptr2len(t);
5192
Bram Moolenaar91458462021-01-13 20:08:38 +01005193 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005194}
5195
5196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005198 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005200 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005201var2fpos(
5202 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005203 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005204 int *fnum, // set to fnum for '0, 'A, etc.
5205 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005207 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005209 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005211 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005212 if (varp->v_type == VAR_LIST)
5213 {
5214 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005215 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005216 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005217 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005218
5219 l = varp->vval.v_list;
5220 if (l == NULL)
5221 return NULL;
5222
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005223 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005224 pos.lnum = list_find_nr(l, 0L, &error);
5225 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005226 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005227 if (charcol)
5228 len = (long)mb_charlen(ml_get(pos.lnum));
5229 else
5230 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005231
Bram Moolenaarec65d772020-08-20 22:29:12 +02005232 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005233 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005234 li = list_find(l, 1L);
5235 if (li != NULL && li->li_tv.v_type == VAR_STRING
5236 && li->li_tv.vval.v_string != NULL
5237 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005238 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005239 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005240 }
5241 else
5242 {
5243 pos.col = list_find_nr(l, 1L, &error);
5244 if (error)
5245 return NULL;
5246 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005247
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005248 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005249 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005250 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005251 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005252
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005253 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005254 pos.coladd = list_find_nr(l, 2L, &error);
5255 if (error)
5256 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005257
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005258 return &pos;
5259 }
5260
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005261 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005262 if (name == NULL)
5263 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005264 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005265 {
5266 pos = curwin->w_cursor;
5267 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005268 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005269 return &pos;
5270 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005271 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005272 {
5273 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005274 pos = VIsual;
5275 else
5276 pos = curwin->w_cursor;
5277 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005278 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005279 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005280 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005281 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005283 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5285 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005286 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005287 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 return pp;
5289 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005290
Bram Moolenaara5525202006-03-02 22:52:09 +00005291 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005292
Bram Moolenaar477933c2007-07-17 14:32:23 +00005293 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005294 {
5295 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005296 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005297 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005298 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005299 // In silent Ex mode topline is zero, but that's not a valid line
5300 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005301 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005302 return &pos;
5303 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005304 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005305 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005306 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005307 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005308 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005309 return &pos;
5310 }
5311 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005312 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005314 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 {
5316 pos.lnum = curbuf->b_ml.ml_line_count;
5317 pos.col = 0;
5318 }
5319 else
5320 {
5321 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005322 if (charcol)
5323 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5324 else
5325 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327 return &pos;
5328 }
5329 return NULL;
5330}
5331
5332/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005333 * Convert list in "arg" into a position and optional file number.
5334 * When "fnump" is NULL there is no file number, only 3 items.
5335 * Note that the column is passed on as-is, the caller may want to decrement
5336 * it to use 1 for the first column.
5337 * Return FAIL when conversion is not possible, doesn't check the position for
5338 * validity.
5339 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005340 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005341list2fpos(
5342 typval_T *arg,
5343 pos_T *posp,
5344 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005345 colnr_T *curswantp,
5346 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005347{
5348 list_T *l = arg->vval.v_list;
5349 long i = 0;
5350 long n;
5351
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005352 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5353 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005354 if (arg->v_type != VAR_LIST
5355 || l == NULL
5356 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005357 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005358 return FAIL;
5359
5360 if (fnump != NULL)
5361 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005362 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005363 if (n < 0)
5364 return FAIL;
5365 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005366 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005367 *fnump = n;
5368 }
5369
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005370 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005371 if (n < 0)
5372 return FAIL;
5373 posp->lnum = n;
5374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005375 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005376 if (n < 0)
5377 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005378 // If character position is specified, then convert to byte position
5379 if (charcol)
5380 {
5381 buf_T *buf;
5382
5383 // Get the text for the specified line in a loaded buffer
5384 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5385 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5386 return FAIL;
5387
Bram Moolenaar91458462021-01-13 20:08:38 +01005388 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005389 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005390 posp->col = n;
5391
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005392 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005393 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005394 posp->coladd = 0;
5395 else
5396 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005397
Bram Moolenaar493c1782014-05-28 14:34:46 +02005398 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005399 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005400
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005401 return OK;
5402}
5403
5404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 * Get the length of an environment variable name.
5406 * Advance "arg" to the first character after the name.
5407 * Return 0 for error.
5408 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005409 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005410get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 char_u *p;
5413 int len;
5414
5415 for (p = *arg; vim_isIDc(*p); ++p)
5416 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005417 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 return 0;
5419
5420 len = (int)(p - *arg);
5421 *arg = p;
5422 return len;
5423}
5424
5425/*
5426 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005427 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 * Return 0 if something is wrong.
5429 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005430 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005431get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
5433 char_u *p;
5434 int len;
5435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005436 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005438 {
5439 if (*p == ':')
5440 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005441 // "s:" is start of "s:var", but "n:" is not and can be used in
5442 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005443 len = (int)(p - *arg);
5444 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5445 || len > 1)
5446 break;
5447 }
5448 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005449 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 return 0;
5451
5452 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005453 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455 return len;
5456}
5457
5458/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005459 * Get the length of the name of a variable or function.
5460 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005462 * Return -1 if curly braces expansion failed.
5463 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 * If the name contains 'magic' {}'s, expand them and return the
5465 * expanded name in an allocated string via 'alias' - caller must free.
5466 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005467 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005468get_name_len(
5469 char_u **arg,
5470 char_u **alias,
5471 int evaluate,
5472 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473{
5474 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 char_u *p;
5476 char_u *expr_start;
5477 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005479 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
5481 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5482 && (*arg)[2] == (int)KE_SNR)
5483 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005484 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 *arg += 3;
5486 return get_id_len(arg) + 3;
5487 }
5488 len = eval_fname_script(*arg);
5489 if (len > 0)
5490 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005491 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 *arg += len;
5493 }
5494
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005496 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005498 p = find_name_end(*arg, &expr_start, &expr_end,
5499 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 if (expr_start != NULL)
5501 {
5502 char_u *temp_string;
5503
5504 if (!evaluate)
5505 {
5506 len += (int)(p - *arg);
5507 *arg = skipwhite(p);
5508 return len;
5509 }
5510
5511 /*
5512 * Include any <SID> etc in the expanded string:
5513 * Thus the -len here.
5514 */
5515 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5516 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005517 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 *alias = temp_string;
5519 *arg = skipwhite(p);
5520 return (int)STRLEN(temp_string);
5521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
5523 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005524 // Only give an error when there is something, otherwise it will be
5525 // reported at a higher level.
5526 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005527 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528
5529 return len;
5530}
5531
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532/*
5533 * Find the end of a variable or function name, taking care of magic braces.
5534 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5535 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005536 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 * Return a pointer to just after the name. Equal to "arg" if there is no
5538 * valid name.
5539 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005540 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005541find_name_end(
5542 char_u *arg,
5543 char_u **expr_start,
5544 char_u **expr_end,
5545 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005547 int mb_nest = 0;
5548 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005550 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005551 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 *expr_start = NULL;
5556 *expr_end = NULL;
5557 }
5558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005559 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005560 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5561 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005562 return arg;
5563
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 for (p = arg; *p != NUL
5565 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005566 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005567 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005568 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005570 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005572 if (*p == '\'')
5573 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005574 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005575 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005576 ;
5577 if (*p == NUL)
5578 break;
5579 }
5580 else if (*p == '"')
5581 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005582 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005583 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005584 if (*p == '\\' && p[1] != NUL)
5585 ++p;
5586 if (*p == NUL)
5587 break;
5588 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005589 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5590 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005591 // "s:" is start of "s:var", but "n:" is not and can be used in
5592 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005593 len = (int)(p - arg);
5594 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005595 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005596 break;
5597 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005598
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005601 if (*p == '[')
5602 ++br_nest;
5603 else if (*p == ']')
5604 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005606
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005607 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005609 if (*p == '{')
5610 {
5611 mb_nest++;
5612 if (expr_start != NULL && *expr_start == NULL)
5613 *expr_start = p;
5614 }
5615 else if (*p == '}')
5616 {
5617 mb_nest--;
5618 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5619 *expr_end = p;
5620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
5623
5624 return p;
5625}
5626
5627/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005628 * Expands out the 'magic' {}'s in a variable/function name.
5629 * Note that this can call itself recursively, to deal with
5630 * constructs like foo{bar}{baz}{bam}
5631 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5632 * "in_start" ^
5633 * "expr_start" ^
5634 * "expr_end" ^
5635 * "in_end" ^
5636 *
5637 * Returns a new allocated string, which the caller must free.
5638 * Returns NULL for failure.
5639 */
5640 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005641make_expanded_name(
5642 char_u *in_start,
5643 char_u *expr_start,
5644 char_u *expr_end,
5645 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005646{
5647 char_u c1;
5648 char_u *retval = NULL;
5649 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005650
5651 if (expr_end == NULL || in_end == NULL)
5652 return NULL;
5653 *expr_start = NUL;
5654 *expr_end = NUL;
5655 c1 = *in_end;
5656 *in_end = NUL;
5657
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005658 temp_result = eval_to_string(expr_start + 1, FALSE);
5659 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005660 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005661 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5662 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005663 if (retval != NULL)
5664 {
5665 STRCPY(retval, in_start);
5666 STRCAT(retval, temp_result);
5667 STRCAT(retval, expr_end + 1);
5668 }
5669 }
5670 vim_free(temp_result);
5671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005672 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005673 *expr_start = '{';
5674 *expr_end = '}';
5675
5676 if (retval != NULL)
5677 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005678 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005679 if (expr_start != NULL)
5680 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005681 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005682 temp_result = make_expanded_name(retval, expr_start,
5683 expr_end, temp_result);
5684 vim_free(retval);
5685 retval = temp_result;
5686 }
5687 }
5688
5689 return retval;
5690}
5691
5692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005694 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005696 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005697eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005699 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005700}
5701
5702/*
5703 * Return TRUE if character "c" can be used as the first character in a
5704 * variable or function name (excluding '{' and '}').
5705 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005706 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005707eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005708{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005709 return ASCII_ISALPHA(c) || c == '_';
5710}
5711
5712/*
5713 * Return TRUE if character "c" can be used as the first character of a
5714 * dictionary key.
5715 */
5716 int
5717eval_isdictc(int c)
5718{
5719 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720}
5721
5722/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005723 * Handle:
5724 * - expr[expr], expr[expr:expr] subscript
5725 * - ".name" lookup
5726 * - function call with Funcref variable: func(expr)
5727 * - method call: var->method()
5728 *
5729 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005730 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005731 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005732handle_subscript(
5733 char_u **arg,
5734 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005735 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005736 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005737{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005738 int evaluate = evalarg != NULL
5739 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005740 int ret = OK;
5741 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005742 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005743 int getnext;
5744 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005745
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005746 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005747 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005748 // When at the end of the line and ".name" or "->{" or "->X" follows in
5749 // the next line then consume the line break.
5750 p = eval_next_non_blank(*arg, evalarg, &getnext);
5751 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005752 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005753 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005754 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005755 {
5756 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005757 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005758 check_white = FALSE;
5759 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005760
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005761 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5762 || rettv->v_type == VAR_PARTIAL))
5763 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005764 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005765 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5766 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005767
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005768 // Stop the expression evaluation when immediately aborting on
5769 // error, or when an interrupt occurred or an exception was thrown
5770 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005771 if (aborting())
5772 {
5773 if (ret == OK)
5774 clear_tv(rettv);
5775 ret = FAIL;
5776 }
5777 dict_unref(selfdict);
5778 selfdict = NULL;
5779 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005780 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005781 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005782 *arg = skipwhite(p + 2);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005783 if (ret == OK)
5784 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005785 if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005786 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005787 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005788 else
5789 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005790 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005791 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005792 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005793 // "." is ".name" lookup when we found a dict or when evaluating and
5794 // scriptversion is at least 2, where string concatenation is "..".
5795 else if (**arg == '['
5796 || (**arg == '.' && (rettv->v_type == VAR_DICT
5797 || (!evaluate
5798 && (*arg)[1] != '.'
5799 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005800 {
5801 dict_unref(selfdict);
5802 if (rettv->v_type == VAR_DICT)
5803 {
5804 selfdict = rettv->vval.v_dict;
5805 if (selfdict != NULL)
5806 ++selfdict->dv_refcount;
5807 }
5808 else
5809 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005810 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005811 {
5812 clear_tv(rettv);
5813 ret = FAIL;
5814 }
5815 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005816 else
5817 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005818 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005819
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005820 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5821 // Don't do this when "Func" is already a partial that was bound
5822 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005823 if (selfdict != NULL
5824 && (rettv->v_type == VAR_FUNC
5825 || (rettv->v_type == VAR_PARTIAL
5826 && (rettv->vval.v_partial->pt_auto
5827 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005828 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005829
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005830 dict_unref(selfdict);
5831 return ret;
5832}
5833
5834/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005835 * Make a copy of an item.
5836 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005837 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5838 * reference to an already copied list/dict can be used.
5839 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005840 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005841 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005842item_copy(
5843 typval_T *from,
5844 typval_T *to,
5845 int deep,
5846 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005847{
5848 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005849 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005850
Bram Moolenaar33570922005-01-25 22:26:29 +00005851 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005852 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005853 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005854 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005855 }
5856 ++recurse;
5857
5858 switch (from->v_type)
5859 {
5860 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005861 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005862 case VAR_STRING:
5863 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005864 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005865 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005866 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005867 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005868 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005869 copy_tv(from, to);
5870 break;
5871 case VAR_LIST:
5872 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005873 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005874 if (from->vval.v_list == NULL)
5875 to->vval.v_list = NULL;
5876 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5877 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005878 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005879 to->vval.v_list = from->vval.v_list->lv_copylist;
5880 ++to->vval.v_list->lv_refcount;
5881 }
5882 else
5883 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5884 if (to->vval.v_list == NULL)
5885 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005886 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005887 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005888 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005889 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005890 case VAR_DICT:
5891 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005892 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005893 if (from->vval.v_dict == NULL)
5894 to->vval.v_dict = NULL;
5895 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5896 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005897 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005898 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5899 ++to->vval.v_dict->dv_refcount;
5900 }
5901 else
5902 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5903 if (to->vval.v_dict == NULL)
5904 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005905 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005906 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005907 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005908 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005909 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005910 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005911 }
5912 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005913 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005914}
5915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005916 void
5917echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5918{
5919 char_u *tofree;
5920 char_u numbuf[NUMBUFLEN];
5921 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5922
5923 if (*atstart)
5924 {
5925 *atstart = FALSE;
5926 // Call msg_start() after eval1(), evaluating the expression
5927 // may cause a message to appear.
5928 if (with_space)
5929 {
5930 // Mark the saved text as finishing the line, so that what
5931 // follows is displayed on a new line when scrolling back
5932 // at the more prompt.
5933 msg_sb_eol();
5934 msg_start();
5935 }
5936 }
5937 else if (with_space)
5938 msg_puts_attr(" ", echo_attr);
5939
5940 if (p != NULL)
5941 for ( ; *p != NUL && !got_int; ++p)
5942 {
5943 if (*p == '\n' || *p == '\r' || *p == TAB)
5944 {
5945 if (*p != TAB && *needclr)
5946 {
5947 // remove any text still there from the command
5948 msg_clr_eos();
5949 *needclr = FALSE;
5950 }
5951 msg_putchar_attr(*p, echo_attr);
5952 }
5953 else
5954 {
5955 if (has_mbyte)
5956 {
5957 int i = (*mb_ptr2len)(p);
5958
5959 (void)msg_outtrans_len_attr(p, i, echo_attr);
5960 p += i - 1;
5961 }
5962 else
5963 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5964 }
5965 }
5966 vim_free(tofree);
5967}
5968
Bram Moolenaare9a41262005-01-15 22:18:47 +00005969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 * ":echo expr1 ..." print each argument separated with a space, add a
5971 * newline at the end.
5972 * ":echon expr1 ..." print each argument plain.
5973 */
5974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005975ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
5977 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005978 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 char_u *p;
5980 int needclr = TRUE;
5981 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005982 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005983 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005984 evalarg_T evalarg;
5985
Bram Moolenaare707c882020-07-01 13:07:37 +02005986 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987
5988 if (eap->skip)
5989 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005990 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005992 // If eval1() causes an error message the text from the command may
5993 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005994 need_clr_eos = needclr;
5995
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005997 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 {
5999 /*
6000 * Report the invalid expression unless the expression evaluation
6001 * has been cancelled due to an aborting error, an interrupt, or an
6002 * exception.
6003 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006004 if (!aborting() && did_emsg == did_emsg_before
6005 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006006 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006007 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 break;
6009 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006010 need_clr_eos = FALSE;
6011
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006013 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006014
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006015 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 arg = skipwhite(arg);
6017 }
6018 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006019 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020
6021 if (eap->skip)
6022 --emsg_skip;
6023 else
6024 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006025 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 if (needclr)
6027 msg_clr_eos();
6028 if (eap->cmdidx == CMD_echo)
6029 msg_end();
6030 }
6031}
6032
6033/*
6034 * ":echohl {name}".
6035 */
6036 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006037ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006039 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040}
6041
6042/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006043 * Returns the :echo attribute
6044 */
6045 int
6046get_echo_attr(void)
6047{
6048 return echo_attr;
6049}
6050
6051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 * ":execute expr1 ..." execute the result of an expression.
6053 * ":echomsg expr1 ..." Print a message
6054 * ":echoerr expr1 ..." Print an error
6055 * Each gets spaces around each argument and a newline at the end for
6056 * echo commands
6057 */
6058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006059ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060{
6061 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 int ret = OK;
6064 char_u *p;
6065 garray_T ga;
6066 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
6068 ga_init2(&ga, 1, 80);
6069
6070 if (eap->skip)
6071 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006072 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006074 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006075 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077
6078 if (!eap->skip)
6079 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006080 char_u buf[NUMBUFLEN];
6081
6082 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006083 {
6084 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6085 {
6086 emsg(_(e_inval_string));
6087 p = NULL;
6088 }
6089 else
6090 p = tv_get_string_buf(&rettv, buf);
6091 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006092 else
6093 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006094 if (p == NULL)
6095 {
6096 clear_tv(&rettv);
6097 ret = FAIL;
6098 break;
6099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 len = (int)STRLEN(p);
6101 if (ga_grow(&ga, len + 2) == FAIL)
6102 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006103 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 ret = FAIL;
6105 break;
6106 }
6107 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 ga.ga_len += len;
6111 }
6112
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006113 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 arg = skipwhite(arg);
6115 }
6116
6117 if (ret != FAIL && ga.ga_data != NULL)
6118 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006119 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6120 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006121 // Mark the already saved text as finishing the line, so that what
6122 // follows is displayed on a new line when scrolling back at the
6123 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006124 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006125 }
6126
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006128 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006129 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006130 out_flush();
6131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 else if (eap->cmdidx == CMD_echoerr)
6133 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006134 int save_did_emsg = did_emsg;
6135
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006136 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006137 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 if (!force_abort)
6139 did_emsg = save_did_emsg;
6140 }
6141 else if (eap->cmdidx == CMD_execute)
6142 do_cmdline((char_u *)ga.ga_data,
6143 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6144 }
6145
6146 ga_clear(&ga);
6147
6148 if (eap->skip)
6149 --emsg_skip;
6150
6151 eap->nextcmd = check_nextcmd(arg);
6152}
6153
6154/*
6155 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6156 * "arg" points to the "&" or '+' when called, to "option" when returning.
6157 * Returns NULL when no option name found. Otherwise pointer to the char
6158 * after the option name.
6159 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006160 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006161find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162{
6163 char_u *p = *arg;
6164
6165 ++p;
6166 if (*p == 'g' && p[1] == ':')
6167 {
6168 *opt_flags = OPT_GLOBAL;
6169 p += 2;
6170 }
6171 else if (*p == 'l' && p[1] == ':')
6172 {
6173 *opt_flags = OPT_LOCAL;
6174 p += 2;
6175 }
6176 else
6177 *opt_flags = 0;
6178
6179 if (!ASCII_ISALPHA(*p))
6180 return NULL;
6181 *arg = p;
6182
6183 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006184 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 else
6186 while (ASCII_ISALPHA(*p))
6187 ++p;
6188 return p;
6189}
6190
6191/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006192 * Display script name where an item was last set.
6193 * Should only be invoked when 'verbose' is non-zero.
6194 */
6195 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006196last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006197{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006198 char_u *p;
6199
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006200 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006201 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006202 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006203 if (p != NULL)
6204 {
6205 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006206 msg_puts(_("\n\tLast set from "));
6207 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006208 if (script_ctx.sc_lnum > 0)
6209 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006210 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006211 msg_outnum((long)script_ctx.sc_lnum);
6212 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006213 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006214 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006215 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006216 }
6217}
6218
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006219#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
6221/*
6222 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006223 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 * "flags" can be "g" to do a global substitute.
6225 * Returns an allocated string, NULL for error.
6226 */
6227 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006228do_string_sub(
6229 char_u *str,
6230 char_u *pat,
6231 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006232 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006233 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234{
6235 int sublen;
6236 regmatch_T regmatch;
6237 int i;
6238 int do_all;
6239 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006240 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 garray_T ga;
6242 char_u *ret;
6243 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006244 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006246 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006248 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
6250 ga_init2(&ga, 1, 200);
6251
6252 do_all = (flags[0] == 'g');
6253
6254 regmatch.rm_ic = p_ic;
6255 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6256 if (regmatch.regprog != NULL)
6257 {
6258 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006259 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6261 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006262 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006263 if (regmatch.startp[0] == regmatch.endp[0])
6264 {
6265 if (zero_width == regmatch.startp[0])
6266 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006267 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006268 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006269 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6270 (size_t)i);
6271 ga.ga_len += i;
6272 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006273 continue;
6274 }
6275 zero_width = regmatch.startp[0];
6276 }
6277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 /*
6279 * Get some space for a temporary buffer to do the substitution
6280 * into. It will contain:
6281 * - The text up to where the match is.
6282 * - The substituted text.
6283 * - The text after the match.
6284 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006285 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006286 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6288 {
6289 ga_clear(&ga);
6290 break;
6291 }
6292
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006293 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 i = (int)(regmatch.startp[0] - tail);
6295 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006296 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006297 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 + ga.ga_len + i, TRUE, TRUE, FALSE);
6299 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006300 tail = regmatch.endp[0];
6301 if (*tail == NUL)
6302 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 if (!do_all)
6304 break;
6305 }
6306
6307 if (ga.ga_data != NULL)
6308 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6309
Bram Moolenaar473de612013-06-08 18:19:48 +02006310 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 }
6312
6313 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6314 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006315 if (p_cpo == empty_option)
6316 p_cpo = save_cpo;
6317 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006318 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006319 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006320 // If it's still empty it was changed and restored, need to restore in
6321 // the complicated way.
6322 if (*p_cpo == NUL)
6323 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006324 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326
6327 return ret;
6328}