blob: 785d8417a46731ff417ac71fb0e5413858a094ea [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000044} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045
Bram Moolenaar48e697e2016-01-23 22:17:30 +010046static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020047static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
48static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
52static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020053static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020056static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020057
Bram Moolenaare21c1582019-03-02 11:57:09 +010058/*
59 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010060 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010061 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020062 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010063num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010064{
65 varnumber_T result;
66
Bram Moolenaar99880f92021-01-20 21:23:14 +010067 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010068 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 if (failed != NULL)
73 *failed = TRUE;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 if (n1 == 0)
76 result = VARNUM_MIN; // similar to NaN
77 else if (n1 < 0)
78 result = -VARNUM_MAX;
79 else
80 result = VARNUM_MAX;
81 }
82 else
83 result = n1 / n2;
84
85 return result;
86}
87
88/*
89 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010090 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010091 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020092 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010093num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010094{
Bram Moolenaar99880f92021-01-20 21:23:14 +010095 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010096 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 if (failed != NULL)
99 *failed = TRUE;
100 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100101 return (n2 == 0) ? 0 : (n1 % n2);
102}
103
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100104#if defined(EBCDIC) || defined(PROTO)
105/*
106 * Compare struct fst by function name.
107 */
108 static int
109compare_func_name(const void *s1, const void *s2)
110{
111 struct fst *p1 = (struct fst *)s1;
112 struct fst *p2 = (struct fst *)s2;
113
114 return STRCMP(p1->f_name, p2->f_name);
115}
116
117/*
118 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100119 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100120 * On machines using EBCDIC we have to sort it.
121 */
122 static void
123sortFunctions(void)
124{
125 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
126
127 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
128}
129#endif
130
Bram Moolenaar33570922005-01-25 22:26:29 +0000131/*
132 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000133 */
134 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100135eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000136{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200137 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200138 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000139
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200140#ifdef EBCDIC
141 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100142 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200143 */
144 sortFunctions();
145#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000146}
147
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000148#if defined(EXITFREE) || defined(PROTO)
149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100150eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200152 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100153 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200154 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000155
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200156 // autoloaded script names
157 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000158
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200159 // unreferenced lists and dicts
160 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200161
162 // functions not garbage collected
163 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000164}
165#endif
166
Bram Moolenaare6b53242020-07-01 17:28:33 +0200167 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200168fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
169{
170 CLEAR_FIELD(*evalarg);
171 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
172 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
173 {
174 evalarg->eval_getline = eap->getline;
175 evalarg->eval_cookie = eap->cookie;
176 }
177}
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179/*
180 * Top level evaluation function, returning a boolean.
181 * Sets "error" to TRUE if there was an error.
182 * Return TRUE or FALSE.
183 */
184 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100185eval_to_bool(
186 char_u *arg,
187 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200188 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100189 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
Bram Moolenaar33570922005-01-25 22:26:29 +0000191 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200192 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200193 evalarg_T evalarg;
194
Bram Moolenaar37c83712020-06-30 21:18:36 +0200195 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196
197 if (skip)
198 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200199 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 else
202 {
203 *error = FALSE;
204 if (!skip)
205 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200206 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200207 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200208 else
209 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000210 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 }
212 }
213 if (skip)
214 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200215 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200217 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
219
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100220/*
221 * Call eval1() and give an error message if not done at a lower level.
222 */
223 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200224eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100225{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100226 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100227 int ret;
228 int did_emsg_before = did_emsg;
229 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200230 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100231
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
233
234 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100235 if (ret == FAIL)
236 {
237 // Report the invalid expression unless the expression evaluation has
238 // been cancelled due to an aborting error, an interrupt, or an
239 // exception, or we already gave a more specific error.
240 // Also check called_emsg for when using assert_fails().
241 if (!aborting() && did_emsg == did_emsg_before
242 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100243 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100244 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200245 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100246 return ret;
247}
248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200250 * Return whether a typval is a valid expression to pass to eval_expr_typval()
251 * or eval_expr_to_bool(). An empty string returns FALSE;
252 */
253 int
254eval_expr_valid_arg(typval_T *tv)
255{
256 return tv->v_type != VAR_UNKNOWN
257 && (tv->v_type != VAR_STRING
258 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
259}
260
261/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262 * Evaluate an expression, which can be a function, partial or string.
263 * Pass arguments "argv[argc]".
264 * Return the result in "rettv" and OK or FAIL.
265 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200266 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100267eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
268{
269 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100270 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200271 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100272
273 if (expr->v_type == VAR_FUNC)
274 {
275 s = expr->vval.v_string;
276 if (s == NULL || *s == NUL)
277 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200278 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200279 funcexe.evaluate = TRUE;
280 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100281 return FAIL;
282 }
283 else if (expr->v_type == VAR_PARTIAL)
284 {
285 partial_T *partial = expr->vval.v_partial;
286
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200287 if (partial == NULL)
288 return FAIL;
289
Bram Moolenaar822ba242020-05-24 23:00:18 +0200290 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200291 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200293 if (call_def_function(partial->pt_func, argc, argv,
294 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 return FAIL;
296 }
297 else
298 {
299 s = partial_name(partial);
300 if (s == NULL || *s == NUL)
301 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200302 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303 funcexe.evaluate = TRUE;
304 funcexe.partial = partial;
305 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
306 return FAIL;
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 }
309 else
310 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100311 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100312 if (s == NULL)
313 return FAIL;
314 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200315 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100316 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200317 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100318 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200319 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100320 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100321 return FAIL;
322 }
323 }
324 return OK;
325}
326
327/*
328 * Like eval_to_bool() but using a typval_T instead of a string.
329 * Works for string, funcref and partial.
330 */
331 int
332eval_expr_to_bool(typval_T *expr, int *error)
333{
334 typval_T rettv;
335 int res;
336
337 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
338 {
339 *error = TRUE;
340 return FALSE;
341 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200342 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100343 clear_tv(&rettv);
344 return res;
345}
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347/*
348 * Top level evaluation function, returning a string. If "skip" is TRUE,
349 * only parsing to "nextcmd" is done, without reporting errors. Return
350 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
351 */
352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100353eval_to_string_skip(
354 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200355 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100356 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357{
Bram Moolenaar33570922005-01-25 22:26:29 +0000358 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200360 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar37c83712020-06-30 21:18:36 +0200362 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (skip)
364 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200365 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 retval = NULL;
367 else
368 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100369 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000370 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 }
372 if (skip)
373 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200374 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375
376 return retval;
377}
378
379/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000380 * Skip over an expression at "*pp".
381 * Return FAIL for an error, OK otherwise.
382 */
383 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200384skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000385{
Bram Moolenaar33570922005-01-25 22:26:29 +0000386 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000387
388 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200389 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000390}
391
392/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200393 * Skip over an expression at "*pp".
394 * If in Vim9 script and line breaks are encountered, the lines are
395 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200396 * "arg" is advanced to just after the expression.
397 * "start" is set to the start of the expression, "end" to just after the end.
398 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 * Return FAIL for an error, OK otherwise.
400 */
401 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200402skip_expr_concatenate(
403 char_u **arg,
404 char_u **start,
405 char_u **end,
406 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200407{
408 typval_T rettv;
409 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200410 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200411 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200413 int evaluate = evalarg == NULL
414 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200416 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200417 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200418 {
419 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200420 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200421 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422 ++gap->ga_len;
423 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200424 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200425
426 // Don't evaluate the expression.
427 if (evalarg != NULL)
428 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200429 *arg = skipwhite(*arg);
430 res = eval1(arg, &rettv, evalarg);
431 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200432 if (evalarg != NULL)
433 evalarg->eval_flags = save_flags;
434
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200435 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200437 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200438 if (evalarg->eval_ga.ga_len == 1)
439 {
440 // just one line, no need to concatenate
441 ga_clear(gap);
442 gap->ga_itemsize = 0;
443 }
444 else
445 {
446 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200447 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200448
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200449 // Line breaks encountered, concatenate all the lines.
450 *((char_u **)gap->ga_data) = *start;
451 p = ga_concat_strings(gap, "");
452
453 // free the lines only when using getsourceline()
454 if (evalarg->eval_cookie != NULL)
455 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200456 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200457 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200458 // Do not free the last line, "arg" points into it, free it
459 // later.
460 vim_free(evalarg->eval_tofree);
461 evalarg->eval_tofree =
462 ((char_u **)gap->ga_data)[gap->ga_len - 1];
463 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200464 ga_clear_strings(gap);
465 }
466 else
467 ga_clear(gap);
468 gap->ga_itemsize = 0;
469 if (p == NULL)
470 return FAIL;
471 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200472 vim_free(evalarg->eval_tofree_lambda);
473 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200474 // Compute "end" relative to the end.
475 *end = *start + STRLEN(*start) - endoff;
476 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200477 }
478
479 return res;
480}
481
482/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100483 * Convert "tv" to a string.
484 * When "convert" is TRUE convert a List into a sequence of lines and convert
485 * a Float to a String.
486 * Returns an allocated string (NULL when out of memory).
487 */
488 char_u *
489typval2string(typval_T *tv, int convert)
490{
491 garray_T ga;
492 char_u *retval;
493#ifdef FEAT_FLOAT
494 char_u numbuf[NUMBUFLEN];
495#endif
496
497 if (convert && tv->v_type == VAR_LIST)
498 {
499 ga_init2(&ga, (int)sizeof(char), 80);
500 if (tv->vval.v_list != NULL)
501 {
502 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
503 if (tv->vval.v_list->lv_len > 0)
504 ga_append(&ga, NL);
505 }
506 ga_append(&ga, NUL);
507 retval = (char_u *)ga.ga_data;
508 }
509#ifdef FEAT_FLOAT
510 else if (convert && tv->v_type == VAR_FLOAT)
511 {
512 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
513 retval = vim_strsave(numbuf);
514 }
515#endif
516 else
517 retval = vim_strsave(tv_get_string(tv));
518 return retval;
519}
520
521/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200522 * Top level evaluation function, returning a string. Does not handle line
523 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000524 * When "convert" is TRUE convert a List into a sequence of lines and convert
525 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 * Return pointer to allocated memory, or NULL for failure.
527 */
528 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100529eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100530 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100531 int convert,
532 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
Bram Moolenaar33570922005-01-25 22:26:29 +0000534 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100536 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100538 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
539 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 retval = NULL;
541 else
542 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100543 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000544 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100546 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
548 return retval;
549}
550
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100551 char_u *
552eval_to_string(
553 char_u *arg,
554 int convert)
555{
556 return eval_to_string_eap(arg, convert, NULL);
557}
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000560 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200561 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200562 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 */
564 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100565eval_to_string_safe(
566 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100567 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
569 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200570 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200571 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200573 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200574 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000575 if (use_sandbox)
576 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200577 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200578 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000579 if (use_sandbox)
580 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200581 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200582 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200583 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 return retval;
585}
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587/*
588 * Top level evaluation function, returning a number.
589 * Evaluates "expr" silently.
590 * Returns -1 for an error.
591 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200592 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100593eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
Bram Moolenaar33570922005-01-25 22:26:29 +0000595 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200596 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000597 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
599 ++emsg_off;
600
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200601 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 retval = -1;
603 else
604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100605 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000606 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
608 --emsg_off;
609
610 return retval;
611}
612
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000613/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000614 * Top level evaluation function.
615 * Returns an allocated typval_T with the result.
616 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000617 */
618 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200619eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620{
621 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200622 evalarg_T evalarg;
623
624 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000625
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200626 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200627 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100628 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000629
Bram Moolenaar37c83712020-06-30 21:18:36 +0200630 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000631 return tv;
632}
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100635 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200636 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
637 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000638 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641call_vim_function(
642 char_u *func,
643 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200644 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200645 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000647 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200648 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100650 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200651 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200652 funcexe.firstline = curwin->w_cursor.lnum;
653 funcexe.lastline = curwin->w_cursor.lnum;
654 funcexe.evaluate = TRUE;
655 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000656 if (ret == FAIL)
657 clear_tv(rettv);
658
659 return ret;
660}
661
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100662/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100663 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100664 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200665 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
666 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100667 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200668 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100669call_func_retnr(
670 char_u *func,
671 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200672 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100673{
674 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200675 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100676
Bram Moolenaarded27a12018-08-01 19:06:03 +0200677 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100678 return -1;
679
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100680 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100681 clear_tv(&rettv);
682 return retval;
683}
684
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000685/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100686 * Call Vim script function like call_func_retnr() and drop the result.
687 * Returns FAIL when calling the function fails.
688 */
689 int
690call_func_noret(
691 char_u *func,
692 int argc,
693 typval_T *argv)
694{
695 typval_T rettv;
696
697 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
698 return FAIL;
699 clear_tv(&rettv);
700 return OK;
701}
702
703/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100704 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100705 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000706 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707 */
708 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100709call_func_retstr(
710 char_u *func,
711 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200712 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000713{
714 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000715 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000716
Bram Moolenaarded27a12018-08-01 19:06:03 +0200717 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718 return NULL;
719
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100720 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000721 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 return retval;
723}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000724
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000725/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100726 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100727 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000728 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000729 */
730 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100731call_func_retlist(
732 char_u *func,
733 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200734 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000735{
736 typval_T rettv;
737
Bram Moolenaarded27a12018-08-01 19:06:03 +0200738 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000739 return NULL;
740
741 if (rettv.v_type != VAR_LIST)
742 {
743 clear_tv(&rettv);
744 return NULL;
745 }
746
747 return rettv.vval.v_list;
748}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750#ifdef FEAT_FOLDING
751/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100752 * Evaluate "arg", which is 'foldexpr'.
753 * Note: caller must set "curwin" to match "arg".
754 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
755 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
757 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100758eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
Bram Moolenaar33570922005-01-25 22:26:29 +0000760 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200761 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000763 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
764 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000767 if (use_sandbox)
768 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200769 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200771 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 retval = 0;
773 else
774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100775 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000776 if (tv.v_type == VAR_NUMBER)
777 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000778 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 retval = 0;
780 else
781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100782 // If the result is a string, check if there is a non-digit before
783 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000784 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (!VIM_ISDIGIT(*s) && *s != '-')
786 *cp = *s++;
787 retval = atol((char *)s);
788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000789 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000792 if (use_sandbox)
793 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200794 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200795 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200797 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798}
799#endif
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000802 * Get an lval: variable, Dict item or List item that can be assigned a value
803 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
804 * "name.key", "name.key[expr]" etc.
805 * Indexing only works if "name" is an existing List or Dictionary.
806 * "name" points to the start of the name.
807 * If "rettv" is not NULL it points to the value to be assigned.
808 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
809 * wrong; must end in space or cmd separator.
810 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100811 * flags:
812 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100813 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100814 * GLV_NO_AUTOLOAD: do not use script autoloading
815 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000817 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000818 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000819 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200820 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100821get_lval(
822 char_u *name,
823 typval_T *rettv,
824 lval_T *lp,
825 int unlet,
826 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100827 int flags, // GLV_ values
828 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000830 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000831 char_u *expr_start, *expr_end;
832 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000833 dictitem_T *v;
834 typval_T var1;
835 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000837 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000838 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000839 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100840 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100841 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100842 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100844 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200845 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000846
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100847 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100849 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200851 lp->ll_name_end = find_name_end(name, NULL, NULL,
852 FNE_INCL_BR | fne_flags);
853 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000854 }
855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100856 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000857 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 if (expr_start != NULL)
860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100861 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100862 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000863 && *p != '[' && *p != '.')
864 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200865 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 return NULL;
867 }
868
869 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
870 if (lp->ll_exp_name == NULL)
871 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100872 // Report an invalid expression in braces, unless the
873 // expression evaluation has been cancelled due to an
874 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 if (!aborting() && !quiet)
876 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000877 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100878 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 return NULL;
880 }
881 }
882 lp->ll_name = lp->ll_exp_name;
883 }
884 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 lp->ll_name = name;
887
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200888 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200890 // "a: type" is declaring variable "a" with a type, not "a:".
891 if (p == name + 2 && p[-1] == ':')
892 {
893 --p;
894 lp->ll_name_end = p;
895 }
896 if (*p == ':')
897 {
898 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
899 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200901 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100902 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
903 if (lp->ll_type == NULL && !quiet)
904 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200905 lp->ll_name_end = tp;
906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100907 }
908 }
909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100910 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
912 return p;
913
914 cc = *p;
915 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100916 // When we would write to the variable pass &ht and prevent autoload.
917 writing = !(flags & GLV_READ_ONLY);
918 v = find_var(lp->ll_name, writing ? &ht : NULL,
919 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000920 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200921 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000923 if (v == NULL)
924 return NULL;
925
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100926 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
927 {
928 if (!quiet)
929 semsg(_(e_variable_already_declared), lp->ll_name);
930 return NULL;
931 }
932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 /*
934 * Loop until no more [idx] or .key is following.
935 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000936 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100937 var1.v_type = VAR_UNKNOWN;
938 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000939 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200942 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100943 && !(lp->ll_tv->v_type == VAR_BLOB
944 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000946 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100947 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000949 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000950 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100953 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000955 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000956
Bram Moolenaar10c65862020-10-08 21:16:42 +0200957 if (in_vim9script() && lp->ll_valtype == NULL
958 && lp->ll_tv == &v->di_tv
959 && ht != NULL && ht == get_script_local_ht())
960 {
961 svar_T *sv = find_typval_in_script(lp->ll_tv);
962
963 // Vim9 script local variable: get the type
964 if (sv != NULL)
965 lp->ll_valtype = sv->sv_type;
966 }
967
Bram Moolenaar8c711452005-01-14 21:53:12 +0000968 len = -1;
969 if (*p == '.')
970 {
971 key = p + 1;
972 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
973 ;
974 if (len == 0)
975 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100977 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000979 }
980 p = key + len;
981 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000982 else
983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100984 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000985 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000986 if (*p == ':')
987 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000988 else
989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200991 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100993 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000994 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100995 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000996 clear_tv(&var1);
997 return NULL;
998 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200999 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 }
1001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001002 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001003 if (*p == ':')
1004 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001008 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001009 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001011 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001012 if (rettv != NULL
1013 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001014 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001015 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001016 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001019 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001020 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001022 }
1023 p = skipwhite(p + 1);
1024 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001026 else
1027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001029 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001030 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001031 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001032 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001035 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001036 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001037 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001038 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001039 clear_tv(&var2);
1040 return NULL;
1041 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001042 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001043 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001044 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001047
Bram Moolenaar8c711452005-01-14 21:53:12 +00001048 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001049 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001051 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001052 clear_tv(&var1);
1053 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001055 }
1056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001057 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001058 ++p;
1059 }
1060
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 {
1063 if (len == -1)
1064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001065 // "[key]": get key from "var1"
1066 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001067 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001070 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 }
1072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001073 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001074
1075 // a NULL dict is equivalent with an empty dict
1076 if (lp->ll_tv->vval.v_dict == NULL)
1077 {
1078 lp->ll_tv->vval.v_dict = dict_alloc();
1079 if (lp->ll_tv->vval.v_dict == NULL)
1080 {
1081 clear_tv(&var1);
1082 return NULL;
1083 }
1084 ++lp->ll_tv->vval.v_dict->dv_refcount;
1085 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001086 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001087
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001088 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001090 // When assigning to a scope dictionary check that a function and
1091 // variable name is valid (only variable name unless it is l: or
1092 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001093 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001094 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001095 int prevval;
1096 int wrong;
1097
1098 if (len != -1)
1099 {
1100 prevval = key[len];
1101 key[len] = NUL;
1102 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001103 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001104 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001105 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1106 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001107 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001108 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001109 if (len != -1)
1110 key[len] = prevval;
1111 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001112 {
1113 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001114 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001115 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001116 }
1117
Bram Moolenaar10c65862020-10-08 21:16:42 +02001118 if (lp->ll_valtype != NULL)
1119 // use the type of the member
1120 lp->ll_valtype = lp->ll_valtype->tt_member;
1121
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001123 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001124 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001125 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001126 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001128 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001129 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001130 return NULL;
1131 }
1132
Bram Moolenaar31b81602019-02-10 22:14:27 +01001133 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001137 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001138 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001140 }
1141 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001145 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001147 p = NULL;
1148 break;
1149 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001150 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001151 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001152 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1153 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001154 {
1155 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001156 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001157 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001158
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001159 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001161 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001162 else if (lp->ll_tv->v_type == VAR_BLOB)
1163 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001164 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1165
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001166 /*
1167 * Get the number and item for the only or first index of the List.
1168 */
1169 if (empty1)
1170 lp->ll_n1 = 0;
1171 else
1172 // is number or string
1173 lp->ll_n1 = (long)tv_get_number(&var1);
1174 clear_tv(&var1);
1175
1176 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001177 || lp->ll_n1 > bloblen
1178 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 {
1180 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001181 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001182 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001183 return NULL;
1184 }
1185 if (lp->ll_range && !lp->ll_empty2)
1186 {
1187 lp->ll_n2 = (long)tv_get_number(&var2);
1188 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001189 if (lp->ll_n2 < 0
1190 || lp->ll_n2 >= bloblen
1191 || lp->ll_n2 < lp->ll_n1)
1192 {
1193 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001194 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001195 return NULL;
1196 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001197 }
1198 lp->ll_blob = lp->ll_tv->vval.v_blob;
1199 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001200 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001201 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 else
1203 {
1204 /*
1205 * Get the number and item for the only or first index of the List.
1206 */
1207 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001209 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001210 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001211 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001212 clear_tv(&var1);
1213
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001214 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 lp->ll_list = lp->ll_tv->vval.v_list;
1216 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1217 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001218 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001219 if (lp->ll_n1 < 0)
1220 {
1221 lp->ll_n1 = 0;
1222 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1223 }
1224 }
1225 if (lp->ll_li == NULL)
1226 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001227 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001228 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001229 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 }
1232
Bram Moolenaar10c65862020-10-08 21:16:42 +02001233 if (lp->ll_valtype != NULL)
1234 // use the type of the member
1235 lp->ll_valtype = lp->ll_valtype->tt_member;
1236
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 /*
1238 * May need to find the item or absolute index for the second
1239 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 * When no index given: "lp->ll_empty2" is TRUE.
1241 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001245 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001246 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001249 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001251 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001252 {
1253 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001254 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001256 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001258 }
1259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001260 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 if (lp->ll_n1 < 0)
1262 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1263 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001264 {
1265 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001266 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001268 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001269 }
1270
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001273 }
1274
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001275 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277 return p;
1278}
1279
1280/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001281 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001282 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001284clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001285{
1286 vim_free(lp->ll_exp_name);
1287 vim_free(lp->ll_newkey);
1288}
1289
1290/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001291 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001292 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001293 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1294 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001296 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001297set_var_lval(
1298 lval_T *lp,
1299 char_u *endp,
1300 typval_T *rettv,
1301 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001302 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1303 char_u *op,
1304 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305{
1306 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001307 listitem_T *ri;
1308 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001309
1310 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001312 cc = *endp;
1313 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001314 if (lp->ll_blob != NULL)
1315 {
1316 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001317
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001318 if (op != NULL && *op != '=')
1319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001320 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001321 return;
1322 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001323 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001324 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001325
1326 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1327 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001328 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001329
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001330 if (lp->ll_empty2)
1331 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1332
1333 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001335 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001336 return;
1337 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001338 if (lp->ll_empty2)
1339 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001340
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001341 ir = 0;
1342 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1343 blob_set(lp->ll_blob, il,
1344 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001345 }
1346 else
1347 {
1348 val = (int)tv_get_number_chk(rettv, &error);
1349 if (!error)
1350 {
1351 garray_T *gap = &lp->ll_blob->bv_ga;
1352
1353 // Allow for appending a byte. Setting a byte beyond
1354 // the end is an error otherwise.
1355 if (lp->ll_n1 < gap->ga_len
1356 || (lp->ll_n1 == gap->ga_len
1357 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1358 {
1359 blob_set(lp->ll_blob, lp->ll_n1, val);
1360 if (lp->ll_n1 == gap->ga_len)
1361 ++gap->ga_len;
1362 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001363 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001364 }
1365 }
1366 }
1367 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001369 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001370
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001371 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001372 {
1373 emsg(_(e_cannot_mod));
1374 *endp = cc;
1375 return;
1376 }
1377
Bram Moolenaarff697e62019-02-12 22:28:33 +01001378 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001379 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001380 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001381 &tv, &di, TRUE, FALSE) == OK)
1382 {
1383 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001384 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1385 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001386 && tv_op(&tv, rettv, op) == OK)
1387 set_var(lp->ll_name, &tv, FALSE);
1388 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001389 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001391 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001392 {
1393 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001394 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001395 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001396 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1397 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001398 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001399 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001400 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001401 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001402 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001403 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001404 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001405 else if (lp->ll_range)
1406 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001407 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001408 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001409
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001410 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001411 {
1412 emsg(_("E996: Cannot lock a range"));
1413 return;
1414 }
1415
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001416 /*
1417 * Check whether any of the list items is locked
1418 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001419 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001420 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001421 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001422 return;
1423 ri = ri->li_next;
1424 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1425 break;
1426 ll_li = ll_li->li_next;
1427 ++ll_n1;
1428 }
1429
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 /*
1431 * Assign the List values to the list items.
1432 */
1433 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001434 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001435 if (op != NULL && *op != '=')
1436 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1437 else
1438 {
1439 clear_tv(&lp->ll_li->li_tv);
1440 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1441 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001442 ri = ri->li_next;
1443 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1444 break;
1445 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001446 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001447 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001448 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001449 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001450 ri = NULL;
1451 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001452 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001453 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001454 lp->ll_li = lp->ll_li->li_next;
1455 ++lp->ll_n1;
1456 }
1457 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001458 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001459 else if (lp->ll_empty2
1460 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001462 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001463 }
1464 else
1465 {
1466 /*
1467 * Assign to a List or Dictionary item.
1468 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001469 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001470 {
1471 emsg(_("E996: Cannot lock a list or dict"));
1472 return;
1473 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001474
1475 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001476 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001477 return;
1478
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001479 if (lp->ll_newkey != NULL)
1480 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001481 if (op != NULL && *op != '=')
1482 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001483 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 return;
1485 }
1486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001487 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001488 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001489 if (di == NULL)
1490 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001491 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1492 {
1493 vim_free(di);
1494 return;
1495 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001496 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001497 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001498 else if (op != NULL && *op != '=')
1499 {
1500 tv_op(lp->ll_tv, rettv, op);
1501 return;
1502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001503 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001504 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001505
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001506 /*
1507 * Assign the value to the variable or list item.
1508 */
1509 if (copy)
1510 copy_tv(rettv, lp->ll_tv);
1511 else
1512 {
1513 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001514 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001515 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 }
1517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001518}
1519
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001520/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001521 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1522 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001523 * Returns OK or FAIL.
1524 */
1525 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001526tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001527{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001528 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 char_u numbuf[NUMBUFLEN];
1530 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001531 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001533 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001534 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001535 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001536 {
1537 switch (tv1->v_type)
1538 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001539 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001540 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001542 case VAR_DICT:
1543 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001544 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001545 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001546 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001547 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001548 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001549 break;
1550
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001551 case VAR_BLOB:
1552 if (*op != '+' || tv2->v_type != VAR_BLOB)
1553 break;
1554 // BLOB += BLOB
1555 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1556 {
1557 blob_T *b1 = tv1->vval.v_blob;
1558 blob_T *b2 = tv2->vval.v_blob;
1559 int i, len = blob_len(b2);
1560 for (i = 0; i < len; i++)
1561 ga_append(&b1->bv_ga, blob_get(b2, i));
1562 }
1563 return OK;
1564
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001565 case VAR_LIST:
1566 if (*op != '+' || tv2->v_type != VAR_LIST)
1567 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001568 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001569 if (tv2->vval.v_list != NULL)
1570 {
1571 if (tv1->vval.v_list == NULL)
1572 {
1573 tv1->vval.v_list = tv2->vval.v_list;
1574 ++tv1->vval.v_list->lv_refcount;
1575 }
1576 else
1577 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1578 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001579 return OK;
1580
1581 case VAR_NUMBER:
1582 case VAR_STRING:
1583 if (tv2->v_type == VAR_LIST)
1584 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001585 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001587 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001588 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589#ifdef FEAT_FLOAT
1590 if (tv2->v_type == VAR_FLOAT)
1591 {
1592 float_T f = n;
1593
Bram Moolenaarff697e62019-02-12 22:28:33 +01001594 if (*op == '%')
1595 break;
1596 switch (*op)
1597 {
1598 case '+': f += tv2->vval.v_float; break;
1599 case '-': f -= tv2->vval.v_float; break;
1600 case '*': f *= tv2->vval.v_float; break;
1601 case '/': f /= tv2->vval.v_float; break;
1602 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001603 clear_tv(tv1);
1604 tv1->v_type = VAR_FLOAT;
1605 tv1->vval.v_float = f;
1606 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001608#endif
1609 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001610 switch (*op)
1611 {
1612 case '+': n += tv_get_number(tv2); break;
1613 case '-': n -= tv_get_number(tv2); break;
1614 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001615 case '/': n = num_divide(n, tv_get_number(tv2),
1616 &failed); break;
1617 case '%': n = num_modulus(n, tv_get_number(tv2),
1618 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001619 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001620 clear_tv(tv1);
1621 tv1->v_type = VAR_NUMBER;
1622 tv1->vval.v_number = n;
1623 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001624 }
1625 else
1626 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001627 if (tv2->v_type == VAR_FLOAT)
1628 break;
1629
Bram Moolenaarff697e62019-02-12 22:28:33 +01001630 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001631 s = tv_get_string(tv1);
1632 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001633 clear_tv(tv1);
1634 tv1->v_type = VAR_STRING;
1635 tv1->vval.v_string = s;
1636 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001637 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001639 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001640#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001641 {
1642 float_T f;
1643
Bram Moolenaarff697e62019-02-12 22:28:33 +01001644 if (*op == '%' || *op == '.'
1645 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001646 && tv2->v_type != VAR_NUMBER
1647 && tv2->v_type != VAR_STRING))
1648 break;
1649 if (tv2->v_type == VAR_FLOAT)
1650 f = tv2->vval.v_float;
1651 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001652 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001653 switch (*op)
1654 {
1655 case '+': tv1->vval.v_float += f; break;
1656 case '-': tv1->vval.v_float -= f; break;
1657 case '*': tv1->vval.v_float *= f; break;
1658 case '/': tv1->vval.v_float /= f; break;
1659 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001660 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001661#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001662 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001663 }
1664 }
1665
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001666 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001667 return FAIL;
1668}
1669
1670/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 * Evaluate the expression used in a ":for var in expr" command.
1672 * "arg" points to "var".
1673 * Set "*errp" to TRUE for an error, FALSE otherwise;
1674 * Return a pointer that holds the info. Null when there is an error.
1675 */
1676 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001677eval_for_line(
1678 char_u *arg,
1679 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001680 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001681 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682{
Bram Moolenaar33570922005-01-25 22:26:29 +00001683 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001685 typval_T tv;
1686 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001687 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001689 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001691 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 if (fi == NULL)
1693 return NULL;
1694
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001695 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 if (expr == NULL)
1697 return fi;
1698
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001699 expr = skipwhite_and_linebreak(expr, evalarg);
1700 if (expr[0] != 'i' || expr[1] != 'n'
1701 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 return fi;
1705 }
1706
1707 if (skip)
1708 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001709 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1710 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 {
1712 *errp = FALSE;
1713 if (!skip)
1714 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001715 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001716 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001717 l = tv.vval.v_list;
1718 if (l == NULL)
1719 {
1720 // a null list is like an empty list: do nothing
1721 clear_tv(&tv);
1722 }
1723 else
1724 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001726 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001728 // No need to increment the refcount, it's already set for
1729 // the list being used in "tv".
1730 fi->fi_list = l;
1731 list_add_watch(l, &fi->fi_lw);
1732 fi->fi_lw.lw_item = l->lv_first;
1733 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001734 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001735 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001736 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001737 fi->fi_bi = 0;
1738 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001739 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001740 typval_T btv;
1741
1742 // Make a copy, so that the iteration still works when the
1743 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001745 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001746 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001747 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001748 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 else
1750 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001751 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001752 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753 }
1754 }
1755 }
1756 if (skip)
1757 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001758 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759
1760 return fi;
1761}
1762
1763/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001764 * Used when looping over a :for line, skip the "in expr" part.
1765 */
1766 void
1767skip_for_lines(void *fi_void, evalarg_T *evalarg)
1768{
1769 forinfo_T *fi = (forinfo_T *)fi_void;
1770 int i;
1771
1772 for (i = 0; i < fi->fi_break_count; ++i)
1773 eval_next_line(evalarg);
1774}
1775
1776/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 * Use the first item in a ":for" list. Advance to the next.
1778 * Assign the values to the variable (list). "arg" points to the first one.
1779 * Return TRUE when a valid item was found, FALSE when at end of list or
1780 * something wrong.
1781 */
1782 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001783next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001785 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786 int result;
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001787 int flag = in_vim9script() ? ASSIGN_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001788 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001790 if (fi->fi_blob != NULL)
1791 {
1792 typval_T tv;
1793
1794 if (fi->fi_bi >= blob_len(fi->fi_blob))
1795 return FALSE;
1796 tv.v_type = VAR_NUMBER;
1797 tv.v_lock = VAR_FIXED;
1798 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1799 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001800 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001801 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001802 }
1803
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 item = fi->fi_lw.lw_item;
1805 if (item == NULL)
1806 result = FALSE;
1807 else
1808 {
1809 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001810 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001811 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812 }
1813 return result;
1814}
1815
1816/*
1817 * Free the structure used to store info used by ":for".
1818 */
1819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001820free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821{
Bram Moolenaar33570922005-01-25 22:26:29 +00001822 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001824 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001825 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001826 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001827 list_unref(fi->fi_list);
1828 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001829 if (fi != NULL && fi->fi_blob != NULL)
1830 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831 vim_free(fi);
1832}
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001835set_context_for_expression(
1836 expand_T *xp,
1837 char_u *arg,
1838 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001840 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001844 if (cmdidx == CMD_let || cmdidx == CMD_var
1845 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 {
1847 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001848 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001850 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001851 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 {
1853 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001854 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001855 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 break;
1857 }
1858 return;
1859 }
1860 }
1861 else
1862 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1863 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 while ((xp->xp_pattern = vim_strpbrk(arg,
1865 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1866 {
1867 c = *xp->xp_pattern;
1868 if (c == '&')
1869 {
1870 c = xp->xp_pattern[1];
1871 if (c == '&')
1872 {
1873 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001874 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001877 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001879 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1880 xp->xp_pattern += 2;
1881
1882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 }
1884 else if (c == '$')
1885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001886 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 xp->xp_context = EXPAND_ENV_VARS;
1888 }
1889 else if (c == '=')
1890 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001891 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 xp->xp_context = EXPAND_EXPRESSION;
1893 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001894 else if (c == '#'
1895 && xp->xp_context == EXPAND_EXPRESSION)
1896 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001897 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001898 break;
1899 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001900 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 && xp->xp_context == EXPAND_FUNCTIONS
1902 && vim_strchr(xp->xp_pattern, '(') == NULL)
1903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001904 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 break;
1906 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001907 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001909 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 {
1911 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1912 if (c == '\\' && xp->xp_pattern[1] != NUL)
1913 ++xp->xp_pattern;
1914 xp->xp_context = EXPAND_NOTHING;
1915 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001916 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001918 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1920 /* skip */ ;
1921 xp->xp_context = EXPAND_NOTHING;
1922 }
1923 else if (c == '|')
1924 {
1925 if (xp->xp_pattern[1] == '|')
1926 {
1927 ++xp->xp_pattern;
1928 xp->xp_context = EXPAND_EXPRESSION;
1929 }
1930 else
1931 xp->xp_context = EXPAND_COMMANDS;
1932 }
1933 else
1934 xp->xp_context = EXPAND_EXPRESSION;
1935 }
1936 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001937 // Doesn't look like something valid, expand as an expression
1938 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 arg = xp->xp_pattern;
1941 if (*arg != NUL)
1942 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1943 /* skip */ ;
1944 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001945
1946 // ":exe one two" completes "two"
1947 if ((cmdidx == CMD_execute
1948 || cmdidx == CMD_echo
1949 || cmdidx == CMD_echon
1950 || cmdidx == CMD_echomsg)
1951 && xp->xp_context == EXPAND_EXPRESSION)
1952 {
1953 for (;;)
1954 {
1955 char_u *n = skiptowhite(arg);
1956
1957 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1958 break;
1959 arg = skipwhite(n);
1960 }
1961 }
1962
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 xp->xp_pattern = arg;
1964}
1965
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001967 * Return TRUE if "pat" matches "text".
1968 * Does not use 'cpo' and always uses 'magic'.
1969 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001970 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001971pattern_match(char_u *pat, char_u *text, int ic)
1972{
1973 int matches = FALSE;
1974 char_u *save_cpo;
1975 regmatch_T regmatch;
1976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001977 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001978 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001979 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001980 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1981 if (regmatch.regprog != NULL)
1982 {
1983 regmatch.rm_ic = ic;
1984 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1985 vim_regfree(regmatch.regprog);
1986 }
1987 p_cpo = save_cpo;
1988 return matches;
1989}
1990
1991/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001992 * Handle a name followed by "(". Both for just "name(arg)" and for
1993 * "expr->name(arg)".
1994 * Returns OK or FAIL.
1995 */
1996 static int
1997eval_func(
1998 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001999 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002000 char_u *name,
2001 int name_len,
2002 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002003 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002004 typval_T *basetv) // "expr" for "expr->name(arg)"
2005{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002006 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002007 char_u *s = name;
2008 int len = name_len;
2009 partial_T *partial;
2010 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002011 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002012
2013 if (!evaluate)
2014 check_vars(s, len);
2015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002016 // If "s" is the name of a variable of type VAR_FUNC
2017 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002018 s = deref_func_name(s, &len, &partial,
2019 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002020
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002021 // Need to make a copy, in case evaluating the arguments makes
2022 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002023 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002024 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002025 ret = FAIL;
2026 else
2027 {
2028 funcexe_T funcexe;
2029
2030 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002031 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002032 funcexe.firstline = curwin->w_cursor.lnum;
2033 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002034 funcexe.evaluate = evaluate;
2035 funcexe.partial = partial;
2036 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002037 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002038 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002039 }
2040 vim_free(s);
2041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002042 // If evaluate is FALSE rettv->v_type was not set in
2043 // get_func_tv, but it's needed in handle_subscript() to parse
2044 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002045 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2046 {
2047 rettv->vval.v_string = NULL;
2048 rettv->v_type = VAR_FUNC;
2049 }
2050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002051 // Stop the expression evaluation when immediately
2052 // aborting on error, or when an interrupt occurred or
2053 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002054 if (evaluate && aborting())
2055 {
2056 if (ret == OK)
2057 clear_tv(rettv);
2058 ret = FAIL;
2059 }
2060 return ret;
2061}
2062
2063/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002064 * Get the next line source line without advancing. But do skip over comment
2065 * lines.
2066 */
2067 static char_u *
2068getline_peek_skip_comments(evalarg_T *evalarg)
2069{
2070 for (;;)
2071 {
2072 char_u *next = getline_peek(evalarg->eval_getline,
2073 evalarg->eval_cookie);
2074 char_u *p;
2075
2076 if (next == NULL)
2077 break;
2078 p = skipwhite(next);
2079 if (*p != NUL && !vim9_comment_start(p))
2080 return next;
2081 (void)eval_next_line(evalarg);
2082 }
2083 return NULL;
2084}
2085
2086/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002087 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2088 * comment) and there is a next line, return the next line (skipping blanks)
2089 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2091 * "arg" must point somewhere inside a line, not at the start.
2092 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002093 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002094eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2095{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002096 char_u *p = skipwhite(arg);
2097
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002098 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002099 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002100 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002101 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002102 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002103 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002104 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002105
2106 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002107 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002108 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002109 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002110
Bram Moolenaar9d489562020-07-30 20:08:50 +02002111 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002112 {
2113 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002114 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002115 }
2116 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002117 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002118}
2119
2120/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002121 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002122 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002123 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002124eval_next_line(evalarg_T *evalarg)
2125{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002126 garray_T *gap = &evalarg->eval_ga;
2127 char_u *line;
2128
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002129 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002130 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2131 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002132 else
2133 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002134 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002135 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2136 {
2137 // Going to concatenate the lines after parsing.
2138 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2139 ++gap->ga_len;
2140 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002141 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002142 {
2143 vim_free(evalarg->eval_tofree);
2144 evalarg->eval_tofree = line;
2145 }
2146 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002147}
2148
Bram Moolenaare6b53242020-07-01 17:28:33 +02002149/*
2150 * Call eval_next_non_blank() and get the next line if needed.
2151 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002152 char_u *
2153skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2154{
2155 int getnext;
2156 char_u *p = skipwhite(arg);
2157
Bram Moolenaar962d7212020-07-04 14:15:00 +02002158 if (evalarg == NULL)
2159 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002160 eval_next_non_blank(p, evalarg, &getnext);
2161 if (getnext)
2162 return eval_next_line(evalarg);
2163 return p;
2164}
2165
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002166/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002167 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002168 */
2169 void
2170clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2171{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002172 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002173 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002174 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002175 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002176 if (eap != NULL)
2177 {
2178 // We may need to keep the original command line, e.g. for
2179 // ":let" it has the variable names. But we may also need the
2180 // new one, "nextcmd" points into it. Keep both.
2181 vim_free(eap->cmdline_tofree);
2182 eap->cmdline_tofree = *eap->cmdlinep;
2183 *eap->cmdlinep = evalarg->eval_tofree;
2184 }
2185 else
2186 vim_free(evalarg->eval_tofree);
2187 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002188 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002189
2190 vim_free(evalarg->eval_tofree_lambda);
2191 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002192 }
2193}
2194
2195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2199 */
2200
2201/*
2202 * Handle zero level expression.
2203 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002205 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002206 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 * Return OK or FAIL.
2208 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002209 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210eval0(
2211 char_u *arg,
2212 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002213 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002214 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216 int ret;
2217 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002218 int did_emsg_before = did_emsg;
2219 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002220 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
2222 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002223 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002224 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002225
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002226 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
2228 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 /*
2231 * Report the invalid expression unless the expression evaluation has
2232 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002233 * exception, or we already gave a more specific error.
2234 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002236 if (!aborting()
2237 && did_emsg == did_emsg_before
2238 && called_emsg == called_emsg_before
2239 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002240 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002241
2242 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002243 // a next command to avoid more errors, unless "|" is following, which
2244 // could only be a command separator.
2245 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2246 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002249
2250 if (eap != NULL)
2251 eap->nextcmd = check_nextcmd(p);
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 return ret;
2254}
2255
2256/*
2257 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002258 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002259 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 *
2261 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002262 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002264 * Note: "rettv.v_lock" is not set.
2265 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 * Return OK or FAIL.
2267 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002268 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002269eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002271 char_u *p;
2272 int getnext;
2273
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002274 CLEAR_POINTER(rettv);
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 /*
2277 * Get the first variable.
2278 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002279 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 return FAIL;
2281
Bram Moolenaar793648f2020-06-26 21:28:25 +02002282 p = eval_next_non_blank(*arg, evalarg, &getnext);
2283 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002285 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002286 int result;
2287 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002288 evalarg_T *evalarg_used = evalarg;
2289 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002290 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002291 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002292 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002293
2294 if (evalarg == NULL)
2295 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002296 CLEAR_FIELD(local_evalarg);
2297 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002298 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002299 orig_flags = evalarg_used->eval_flags;
2300 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002301
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002302 if (getnext)
2303 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002304 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002305 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002306 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002307 {
2308 error_white_both(p, 1);
2309 clear_tv(rettv);
2310 return FAIL;
2311 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002312 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002313 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002316 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002318 int error = FALSE;
2319
Bram Moolenaar13106602020-10-04 16:06:05 +02002320 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002321 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002322 else if (vim9script)
2323 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002324 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002326 if (error || !op_falsy || !result)
2327 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002328 if (error)
2329 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331
2332 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002333 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002335 if (op_falsy)
2336 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002337 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002338 {
2339 error_white_both(p, 1);
2340 clear_tv(rettv);
2341 return FAIL;
2342 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002343 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002344 evalarg_used->eval_flags = (op_falsy ? !result : result)
2345 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2346 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002347 {
2348 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002350 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002351 if (!op_falsy || !result)
2352 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002354 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002356 /*
2357 * Check for the ":".
2358 */
2359 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2360 if (*p != ':')
2361 {
2362 emsg(_(e_missing_colon));
2363 if (evaluate && result)
2364 clear_tv(rettv);
2365 evalarg_used->eval_flags = orig_flags;
2366 return FAIL;
2367 }
2368 if (getnext)
2369 *arg = eval_next_line(evalarg_used);
2370 else
2371 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002372 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002373 {
2374 error_white_both(p, 1);
2375 clear_tv(rettv);
2376 evalarg_used->eval_flags = orig_flags;
2377 return FAIL;
2378 }
2379 *arg = p;
2380 }
2381
2382 /*
2383 * Get the third variable. Recursive!
2384 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002385 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002386 {
2387 error_white_both(p, 1);
2388 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002389 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002390 return FAIL;
2391 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002392 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2393 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002394 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002395 if (eval1(arg, &var2, evalarg_used) == FAIL)
2396 {
2397 if (evaluate && result)
2398 clear_tv(rettv);
2399 evalarg_used->eval_flags = orig_flags;
2400 return FAIL;
2401 }
2402 if (evaluate && !result)
2403 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002405
2406 if (evalarg == NULL)
2407 clear_evalarg(&local_evalarg, NULL);
2408 else
2409 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
2411
2412 return OK;
2413}
2414
2415/*
2416 * Handle first level expression:
2417 * expr2 || expr2 || expr2 logical OR
2418 *
2419 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002420 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 *
2422 * Return OK or FAIL.
2423 */
2424 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002425eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002427 char_u *p;
2428 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430 /*
2431 * Get the first variable.
2432 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002433 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 return FAIL;
2435
2436 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002437 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002439 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002440 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002442 evalarg_T *evalarg_used = evalarg;
2443 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002444 int evaluate;
2445 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002446 long result = FALSE;
2447 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002448 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002449 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002450
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002451 if (evalarg == NULL)
2452 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002453 CLEAR_FIELD(local_evalarg);
2454 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002455 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002456 orig_flags = evalarg_used->eval_flags;
2457 evaluate = orig_flags & EVAL_EVALUATE;
2458 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002459 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002460 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002461 result = tv_get_bool_chk(rettv, &error);
2462 else if (tv_get_number_chk(rettv, &error) != 0)
2463 result = TRUE;
2464 clear_tv(rettv);
2465 if (error)
2466 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 }
2468
2469 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002470 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002472 while (p[0] == '|' && p[1] == '|')
2473 {
2474 if (getnext)
2475 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002476 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002477 {
2478 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2479 {
2480 error_white_both(p, 2);
2481 clear_tv(rettv);
2482 return FAIL;
2483 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002484 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002485 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002486
2487 /*
2488 * Get the second variable.
2489 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002490 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2491 {
2492 error_white_both(p, 2);
2493 clear_tv(rettv);
2494 return FAIL;
2495 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002496 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2497 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002498 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002499 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002500 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002501
2502 /*
2503 * Compute the result.
2504 */
2505 if (evaluate && !result)
2506 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002507 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002508 result = tv_get_bool_chk(&var2, &error);
2509 else if (tv_get_number_chk(&var2, &error) != 0)
2510 result = TRUE;
2511 clear_tv(&var2);
2512 if (error)
2513 return FAIL;
2514 }
2515 if (evaluate)
2516 {
2517 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002518 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002519 rettv->v_type = VAR_BOOL;
2520 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002521 }
2522 else
2523 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002524 rettv->v_type = VAR_NUMBER;
2525 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002526 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002527 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002528
2529 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002531
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002532 if (evalarg == NULL)
2533 clear_evalarg(&local_evalarg, NULL);
2534 else
2535 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
2537
2538 return OK;
2539}
2540
2541/*
2542 * Handle second level expression:
2543 * expr3 && expr3 && expr3 logical AND
2544 *
2545 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002546 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 *
2548 * Return OK or FAIL.
2549 */
2550 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002551eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002553 char_u *p;
2554 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
2556 /*
2557 * Get the first variable.
2558 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002559 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 return FAIL;
2561
2562 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002563 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002565 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002566 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002568 evalarg_T *evalarg_used = evalarg;
2569 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002570 int orig_flags;
2571 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002572 long result = TRUE;
2573 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002574 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002575 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002576
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002577 if (evalarg == NULL)
2578 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002579 CLEAR_FIELD(local_evalarg);
2580 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002581 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 orig_flags = evalarg_used->eval_flags;
2583 evaluate = orig_flags & EVAL_EVALUATE;
2584 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002585 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002586 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002587 result = tv_get_bool_chk(rettv, &error);
2588 else if (tv_get_number_chk(rettv, &error) == 0)
2589 result = FALSE;
2590 clear_tv(rettv);
2591 if (error)
2592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594
2595 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598 while (p[0] == '&' && p[1] == '&')
2599 {
2600 if (getnext)
2601 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002602 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002603 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002604 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002605 {
2606 error_white_both(p, 2);
2607 clear_tv(rettv);
2608 return FAIL;
2609 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002610 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002611 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002612
2613 /*
2614 * Get the second variable.
2615 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002616 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2617 {
2618 error_white_both(p, 2);
2619 clear_tv(rettv);
2620 return FAIL;
2621 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2623 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002624 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002625 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002626 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002627 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002628
2629 /*
2630 * Compute the result.
2631 */
2632 if (evaluate && result)
2633 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002634 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002635 result = tv_get_bool_chk(&var2, &error);
2636 else if (tv_get_number_chk(&var2, &error) == 0)
2637 result = FALSE;
2638 clear_tv(&var2);
2639 if (error)
2640 return FAIL;
2641 }
2642 if (evaluate)
2643 {
2644 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002645 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002646 rettv->v_type = VAR_BOOL;
2647 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002648 }
2649 else
2650 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002651 rettv->v_type = VAR_NUMBER;
2652 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002653 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002654 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655
2656 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002658
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002659 if (evalarg == NULL)
2660 clear_evalarg(&local_evalarg, NULL);
2661 else
2662 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
2664
2665 return OK;
2666}
2667
2668/*
2669 * Handle third level expression:
2670 * var1 == var2
2671 * var1 =~ var2
2672 * var1 != var2
2673 * var1 !~ var2
2674 * var1 > var2
2675 * var1 >= var2
2676 * var1 < var2
2677 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002678 * var1 is var2
2679 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 *
2681 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002682 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 *
2684 * Return OK or FAIL.
2685 */
2686 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002687eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002690 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002691 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002693 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
2695 /*
2696 * Get the first variable.
2697 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002698 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 return FAIL;
2700
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002701 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002702 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
2704 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002705 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002707 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002709 typval_T var2;
2710 int ic;
2711 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002712 int evaluate = evalarg == NULL
2713 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002714
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002715 if (getnext)
2716 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002717 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2718 {
2719 error_white_both(p, len);
2720 clear_tv(rettv);
2721 return FAIL;
2722 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002723
Bram Moolenaar696ba232020-07-29 21:20:41 +02002724 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2725 {
2726 semsg(_(e_invexpr2), p);
2727 clear_tv(rettv);
2728 return FAIL;
2729 }
2730
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002731 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 if (p[len] == '?')
2733 {
2734 ic = TRUE;
2735 ++len;
2736 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002737 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 else if (p[len] == '#')
2739 {
2740 ic = FALSE;
2741 ++len;
2742 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002743 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002745 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 /*
2748 * Get the second variable.
2749 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002750 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2751 {
2752 error_white_both(p, 1);
2753 clear_tv(rettv);
2754 return FAIL;
2755 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002756 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002757 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002759 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 return FAIL;
2761 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002762 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002763 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002764 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002765
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002766 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002767 {
2768 ret = FAIL;
2769 clear_tv(rettv);
2770 }
2771 else
2772 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002773 clear_tv(&var2);
2774 return ret;
2775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777
2778 return OK;
2779}
2780
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002781/*
2782 * Make a copy of blob "tv1" and append blob "tv2".
2783 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 void
2785eval_addblob(typval_T *tv1, typval_T *tv2)
2786{
2787 blob_T *b1 = tv1->vval.v_blob;
2788 blob_T *b2 = tv2->vval.v_blob;
2789 blob_T *b = blob_alloc();
2790 int i;
2791
2792 if (b != NULL)
2793 {
2794 for (i = 0; i < blob_len(b1); i++)
2795 ga_append(&b->bv_ga, blob_get(b1, i));
2796 for (i = 0; i < blob_len(b2); i++)
2797 ga_append(&b->bv_ga, blob_get(b2, i));
2798
2799 clear_tv(tv1);
2800 rettv_blob_set(tv1, b);
2801 }
2802}
2803
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002804/*
2805 * Make a copy of list "tv1" and append list "tv2".
2806 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 int
2808eval_addlist(typval_T *tv1, typval_T *tv2)
2809{
2810 typval_T var3;
2811
2812 // concatenate Lists
2813 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2814 {
2815 clear_tv(tv1);
2816 clear_tv(tv2);
2817 return FAIL;
2818 }
2819 clear_tv(tv1);
2820 *tv1 = var3;
2821 return OK;
2822}
2823
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824/*
2825 * Handle fourth level expression:
2826 * + number addition
2827 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002828 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002829 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 *
2831 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002832 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 *
2834 * Return OK or FAIL.
2835 */
2836 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002837eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 /*
2840 * Get the first variable.
2841 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002842 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 return FAIL;
2844
2845 /*
2846 * Repeat computing, until no '+', '-' or '.' is following.
2847 */
2848 for (;;)
2849 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002850 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002851 int getnext;
2852 char_u *p;
2853 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002854 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002855 int concat;
2856 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002857 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002858
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002859 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002860 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002861 p = eval_next_non_blank(*arg, evalarg, &getnext);
2862 op = *p;
2863 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002864 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002866
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002867 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002868 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002869 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002870 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002871 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002872 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002873 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002874 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002875 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002876 clear_tv(rettv);
2877 return FAIL;
2878 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002879 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002880 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002881 if ((op != '+' || (rettv->v_type != VAR_LIST
2882 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002883#ifdef FEAT_FLOAT
2884 && (op == '.' || rettv->v_type != VAR_FLOAT)
2885#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002886 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002887 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002888 int error = FALSE;
2889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002890 // For "list + ...", an illegal use of the first operand as
2891 // a number cannot be determined before evaluating the 2nd
2892 // operand: if this is also a list, all is ok.
2893 // For "something . ...", "something - ..." or "non-list + ...",
2894 // we know that the first operand needs to be a string or number
2895 // without evaluating the 2nd operand. So check before to avoid
2896 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002897 if (op != '.')
2898 tv_get_number_chk(rettv, &error);
2899 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002900 {
2901 clear_tv(rettv);
2902 return FAIL;
2903 }
2904 }
2905
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 /*
2907 * Get the second variable.
2908 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002909 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002910 {
2911 error_white_both(p, oplen);
2912 clear_tv(rettv);
2913 return FAIL;
2914 }
2915 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002916 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002918 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 return FAIL;
2920 }
2921
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002922 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 {
2924 /*
2925 * Compute the result.
2926 */
2927 if (op == '.')
2928 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002929 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2930 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002931 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002932
Bram Moolenaar2e086612020-08-25 22:37:48 +02002933 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002934 || var2.v_type == VAR_CHANNEL
2935 || var2.v_type == VAR_JOB))
2936 emsg(_(e_inval_string));
2937#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002938 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002939 {
2940 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2941 var2.vval.v_float);
2942 s2 = buf2;
2943 }
2944#endif
2945 else
2946 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002947 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002948 {
2949 clear_tv(rettv);
2950 clear_tv(&var2);
2951 return FAIL;
2952 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002954 clear_tv(rettv);
2955 rettv->v_type = VAR_STRING;
2956 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002958 else if (op == '+' && rettv->v_type == VAR_BLOB
2959 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002961 else if (op == '+' && rettv->v_type == VAR_LIST
2962 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002963 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002965 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 else
2968 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002969 int error = FALSE;
2970 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002971#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002972 float_T f1 = 0, f2 = 0;
2973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002974 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002976 f1 = rettv->vval.v_float;
2977 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002978 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002979 else
2980#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002981 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002982 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002983 if (error)
2984 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002985 // This can only happen for "list + non-list". For
2986 // "non-list + ..." or "something - ...", we returned
2987 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002988 clear_tv(rettv);
2989 return FAIL;
2990 }
2991#ifdef FEAT_FLOAT
2992 if (var2.v_type == VAR_FLOAT)
2993 f1 = n1;
2994#endif
2995 }
2996#ifdef FEAT_FLOAT
2997 if (var2.v_type == VAR_FLOAT)
2998 {
2999 f2 = var2.vval.v_float;
3000 n2 = 0;
3001 }
3002 else
3003#endif
3004 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003005 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003006 if (error)
3007 {
3008 clear_tv(rettv);
3009 clear_tv(&var2);
3010 return FAIL;
3011 }
3012#ifdef FEAT_FLOAT
3013 if (rettv->v_type == VAR_FLOAT)
3014 f2 = n2;
3015#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003016 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003018
3019#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003020 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003021 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3022 {
3023 if (op == '+')
3024 f1 = f1 + f2;
3025 else
3026 f1 = f1 - f2;
3027 rettv->v_type = VAR_FLOAT;
3028 rettv->vval.v_float = f1;
3029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003031#endif
3032 {
3033 if (op == '+')
3034 n1 = n1 + n2;
3035 else
3036 n1 = n1 - n2;
3037 rettv->v_type = VAR_NUMBER;
3038 rettv->vval.v_number = n1;
3039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043 }
3044 return OK;
3045}
3046
3047/*
3048 * Handle fifth level expression:
3049 * * number multiplication
3050 * / number division
3051 * % number modulo
3052 *
3053 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003054 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 *
3056 * Return OK or FAIL.
3057 */
3058 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003059eval6(
3060 char_u **arg,
3061 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003062 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003063 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003066 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069 /*
3070 * Get the first variable.
3071 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003072 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 return FAIL;
3074
3075 /*
3076 * Repeat computing, until no '*', '/' or '%' is following.
3077 */
3078 for (;;)
3079 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003080 int evaluate;
3081 int getnext;
3082 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003083 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003084 int op;
3085 varnumber_T n1, n2;
3086#ifdef FEAT_FLOAT
3087 float_T f1, f2;
3088#endif
3089 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003090
Bram Moolenaar9d489562020-07-30 20:08:50 +02003091 p = eval_next_non_blank(*arg, evalarg, &getnext);
3092 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003093 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003095
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003096 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003097 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003098 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003099 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003100 {
3101 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3102 {
3103 error_white_both(p, 1);
3104 clear_tv(rettv);
3105 return FAIL;
3106 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003107 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003110#ifdef FEAT_FLOAT
3111 f1 = 0;
3112 f2 = 0;
3113#endif
3114 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003115 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117#ifdef FEAT_FLOAT
3118 if (rettv->v_type == VAR_FLOAT)
3119 {
3120 f1 = rettv->vval.v_float;
3121 use_float = TRUE;
3122 n1 = 0;
3123 }
3124 else
3125#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003126 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003127 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003128 if (error)
3129 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 }
3131 else
3132 n1 = 0;
3133
3134 /*
3135 * Get the second variable.
3136 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003137 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3138 {
3139 error_white_both(p, 1);
3140 clear_tv(rettv);
3141 return FAIL;
3142 }
3143 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003144 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 return FAIL;
3146
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003147 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149#ifdef FEAT_FLOAT
3150 if (var2.v_type == VAR_FLOAT)
3151 {
3152 if (!use_float)
3153 {
3154 f1 = n1;
3155 use_float = TRUE;
3156 }
3157 f2 = var2.vval.v_float;
3158 n2 = 0;
3159 }
3160 else
3161#endif
3162 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003163 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164 clear_tv(&var2);
3165 if (error)
3166 return FAIL;
3167#ifdef FEAT_FLOAT
3168 if (use_float)
3169 f2 = n2;
3170#endif
3171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
3173 /*
3174 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003175 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177#ifdef FEAT_FLOAT
3178 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180 if (op == '*')
3181 f1 = f1 * f2;
3182 else if (op == '/')
3183 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003184# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003185 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003186 if (f2 == 0.0)
3187 {
3188 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003189 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003190 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003191 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003192 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003193 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003194 }
3195 else
3196 f1 = f1 / f2;
3197# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003198 // We rely on the floating point library to handle divide
3199 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003200 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003201# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003204 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003206 return FAIL;
3207 }
3208 rettv->v_type = VAR_FLOAT;
3209 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
3211 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003214 int failed = FALSE;
3215
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003216 if (op == '*')
3217 n1 = n1 * n2;
3218 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003219 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003221 n1 = num_modulus(n1, n2, &failed);
3222 if (failed)
3223 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003224
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003225 rettv->v_type = VAR_NUMBER;
3226 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229 }
3230
3231 return OK;
3232}
3233
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003234 int
3235eval_leader(char_u **arg, int vim9)
3236{
3237 char_u *s = *arg;
3238 char_u *p = *arg;
3239
3240 while (*p == '!' || *p == '-' || *p == '+')
3241 {
3242 char_u *n = skipwhite(p + 1);
3243
3244 // ++, --, -+ and +- are not accepted in Vim9 script
3245 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3246 {
3247 semsg(_(e_invexpr2), s);
3248 return FAIL;
3249 }
3250 p = n;
3251 }
3252 *arg = p;
3253 return OK;
3254}
3255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256/*
3257 * Handle sixth level expression:
3258 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003259 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003260 * "string" string constant
3261 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 * &option-name option value
3263 * @r register contents
3264 * identifier variable value
3265 * function() function call
3266 * $VAR environment variable
3267 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003268 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003270 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003271 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 *
3273 * Also handle:
3274 * ! in front logical NOT
3275 * - in front unary minus
3276 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003277 * trailing [] subscript in String or List
3278 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003279 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 *
3281 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003282 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 *
3284 * Return OK or FAIL.
3285 */
3286 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003287eval7(
3288 char_u **arg,
3289 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003290 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003293 int evaluate = evalarg != NULL
3294 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 int len;
3296 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 char_u *start_leader, *end_leader;
3298 int ret = OK;
3299 char_u *alias;
3300
3301 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003302 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003303 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003305 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
3307 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003308 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 */
3310 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003311 if (eval_leader(arg, in_vim9script()) == FAIL)
3312 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 end_leader = *arg;
3314
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003315 if (**arg == '.' && (!isdigit(*(*arg + 1))
3316#ifdef FEAT_FLOAT
3317 || current_sctx.sc_version < 2
3318#endif
3319 ))
3320 {
3321 semsg(_(e_invexpr2), *arg);
3322 ++*arg;
3323 return FAIL;
3324 }
3325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 switch (**arg)
3327 {
3328 /*
3329 * Number constant.
3330 */
3331 case '0':
3332 case '1':
3333 case '2':
3334 case '3':
3335 case '4':
3336 case '5':
3337 case '6':
3338 case '7':
3339 case '8':
3340 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003341 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003342
3343 // Apply prefixed "-" and "+" now. Matters especially when
3344 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003345 if (ret == OK && evaluate && end_leader > start_leader
3346 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003347 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 break;
3349
3350 /*
3351 * String constant: "string".
3352 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003353 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 break;
3355
3356 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003357 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003359 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003360 break;
3361
3362 /*
3363 * List: [expr, expr]
3364 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003365 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 break;
3367
3368 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003369 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003370 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003371 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003372 {
3373 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003374 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003375 }
3376 else
3377 ret = NOTDONE;
3378 break;
3379
3380 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003381 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003382 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003383 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003384 case '{': if (in_vim9script())
3385 ret = NOTDONE;
3386 else
3387 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003388 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003389 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003390 break;
3391
3392 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003393 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003395 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 break;
3397
3398 /*
3399 * Environment variable: $VAR.
3400 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003401 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 break;
3403
3404 /*
3405 * Register contents: @r.
3406 */
3407 case '@': ++*arg;
3408 if (evaluate)
3409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003410 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003411 rettv->vval.v_string = get_reg_contents(**arg,
3412 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
3414 if (**arg != NUL)
3415 ++*arg;
3416 break;
3417
3418 /*
3419 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003420 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003422 case '(': ret = NOTDONE;
3423 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003424 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003425 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003426 if (ret == OK && evaluate)
3427 {
3428 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3429
3430 // compile it here to get the return type
3431 compile_def_function(ufunc,
3432 TRUE, PROFILING(ufunc), NULL);
3433 }
3434 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003435 if (ret == NOTDONE)
3436 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003437 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003438 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003439
Bram Moolenaar9215f012020-06-27 21:18:00 +02003440 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003441 if (**arg == ')')
3442 ++*arg;
3443 else if (ret == OK)
3444 {
3445 emsg(_(e_missing_close));
3446 clear_tv(rettv);
3447 ret = FAIL;
3448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 }
3450 break;
3451
Bram Moolenaar8c711452005-01-14 21:53:12 +00003452 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 break;
3454 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003455
3456 if (ret == NOTDONE)
3457 {
3458 /*
3459 * Must be a variable or function name.
3460 * Can also be a curly-braces kind of name: {expr}.
3461 */
3462 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003463 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003464 if (alias != NULL)
3465 s = alias;
3466
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003467 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003468 ret = FAIL;
3469 else
3470 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003471 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3472
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003473 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3474 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003475 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003476 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003477 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003478 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003479 else if (flags & EVAL_CONSTANT)
3480 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003481 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003482 {
3483 // get the value of "true", "false" or a variable
3484 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3485 {
3486 rettv->v_type = VAR_BOOL;
3487 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003488 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003489 }
3490 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003491 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003492 {
3493 rettv->v_type = VAR_BOOL;
3494 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003495 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003496 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003497 else if (len == 4 && in_vim9script()
3498 && STRNCMP(s, "null", 4) == 0)
3499 {
3500 rettv->v_type = VAR_SPECIAL;
3501 rettv->vval.v_number = VVAL_NULL;
3502 ret = OK;
3503 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003504 else
3505 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3506 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003507 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003508 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003509 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003510 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003511 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003512 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003513 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003514 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003515 }
3516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003517 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3518 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003519 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003520 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
3522 /*
3523 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3524 */
3525 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003526 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003527 return ret;
3528}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003529
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003530/*
3531 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003532 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003533 * Adjusts "end_leaderp" until it is at "start_leader".
3534 */
3535 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003536eval7_leader(
3537 typval_T *rettv,
3538 int numeric_only,
3539 char_u *start_leader,
3540 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003541{
3542 char_u *end_leader = *end_leaderp;
3543 int ret = OK;
3544 int error = FALSE;
3545 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003546 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003547#ifdef FEAT_FLOAT
3548 float_T f = 0.0;
3549
3550 if (rettv->v_type == VAR_FLOAT)
3551 f = rettv->vval.v_float;
3552 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003553#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003554 {
3555 while (VIM_ISWHITE(end_leader[-1]))
3556 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003557 if (in_vim9script() && end_leader[-1] == '!')
3558 val = tv2bool(rettv);
3559 else
3560 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003561 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003562 if (error)
3563 {
3564 clear_tv(rettv);
3565 ret = FAIL;
3566 }
3567 else
3568 {
3569 while (end_leader > start_leader)
3570 {
3571 --end_leader;
3572 if (*end_leader == '!')
3573 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003574 if (numeric_only)
3575 {
3576 ++end_leader;
3577 break;
3578 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003579#ifdef FEAT_FLOAT
3580 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003581 {
3582 if (in_vim9script())
3583 {
3584 rettv->v_type = VAR_BOOL;
3585 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3586 }
3587 else
3588 f = !f;
3589 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003590 else
3591#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003592 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003593 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003594 type = VAR_BOOL;
3595 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003596 }
3597 else if (*end_leader == '-')
3598 {
3599#ifdef FEAT_FLOAT
3600 if (rettv->v_type == VAR_FLOAT)
3601 f = -f;
3602 else
3603#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003604 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003605 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003606 type = VAR_NUMBER;
3607 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003608 }
3609 }
3610#ifdef FEAT_FLOAT
3611 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003613 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003614 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003616 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003617#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003618 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003619 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003620 if (in_vim9script())
3621 rettv->v_type = type;
3622 else
3623 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003624 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003627 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 return ret;
3629}
3630
3631/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003632 * Call the function referred to in "rettv".
3633 */
3634 static int
3635call_func_rettv(
3636 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003637 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003638 typval_T *rettv,
3639 int evaluate,
3640 dict_T *selfdict,
3641 typval_T *basetv)
3642{
3643 partial_T *pt = NULL;
3644 funcexe_T funcexe;
3645 typval_T functv;
3646 char_u *s;
3647 int ret;
3648
3649 // need to copy the funcref so that we can clear rettv
3650 if (evaluate)
3651 {
3652 functv = *rettv;
3653 rettv->v_type = VAR_UNKNOWN;
3654
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003655 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003656 if (functv.v_type == VAR_PARTIAL)
3657 {
3658 pt = functv.vval.v_partial;
3659 s = partial_name(pt);
3660 }
3661 else
3662 s = functv.vval.v_string;
3663 }
3664 else
3665 s = (char_u *)"";
3666
Bram Moolenaara80faa82020-04-12 19:37:17 +02003667 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003668 funcexe.firstline = curwin->w_cursor.lnum;
3669 funcexe.lastline = curwin->w_cursor.lnum;
3670 funcexe.evaluate = evaluate;
3671 funcexe.partial = pt;
3672 funcexe.selfdict = selfdict;
3673 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003674 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003676 // Clear the funcref afterwards, so that deleting it while
3677 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003678 if (evaluate)
3679 clear_tv(&functv);
3680
3681 return ret;
3682}
3683
3684/*
3685 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003686 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003687 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3688 */
3689 static int
3690eval_lambda(
3691 char_u **arg,
3692 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003693 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003694 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003695{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003696 int evaluate = evalarg != NULL
3697 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003698 typval_T base = *rettv;
3699 int ret;
3700
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003701 rettv->v_type = VAR_UNKNOWN;
3702
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003703 if (**arg == '{')
3704 {
3705 // ->{lambda}()
3706 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3707 }
3708 else
3709 {
3710 // ->(lambda)()
3711 ++*arg;
3712 ret = eval1(arg, rettv, evalarg);
3713 *arg = skipwhite_and_linebreak(*arg, evalarg);
3714 if (**arg != ')')
3715 {
3716 emsg(_(e_missing_close));
3717 ret = FAIL;
3718 }
3719 ++*arg;
3720 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003721 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003722 return FAIL;
3723 else if (**arg != '(')
3724 {
3725 if (verbose)
3726 {
3727 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003728 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003729 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003731 }
3732 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003733 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003734 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003735 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003736 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003737
3738 // Clear the funcref afterwards, so that deleting it while
3739 // evaluating the arguments is possible (see test55).
3740 if (evaluate)
3741 clear_tv(&base);
3742
3743 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003744}
3745
3746/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003747 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003748 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003749 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3750 */
3751 static int
3752eval_method(
3753 char_u **arg,
3754 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003755 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003756 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003757{
3758 char_u *name;
3759 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003760 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003761 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003762 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003763 int evaluate = evalarg != NULL
3764 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003765
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003766 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003767
Bram Moolenaarac92e252019-08-03 21:58:38 +02003768 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003769 len = get_name_len(arg, &alias, evaluate, TRUE);
3770 if (alias != NULL)
3771 name = alias;
3772
3773 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003774 {
3775 if (verbose)
3776 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003777 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003778 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003779 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003780 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003781 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003782 if (**arg != '(')
3783 {
3784 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003786 ret = FAIL;
3787 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003788 else if (VIM_ISWHITE((*arg)[-1]))
3789 {
3790 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003791 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003792 ret = FAIL;
3793 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003794 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003795 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003796 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003797 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003798
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003799 // Clear the funcref afterwards, so that deleting it while
3800 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003801 if (evaluate)
3802 clear_tv(&base);
3803
Bram Moolenaarac92e252019-08-03 21:58:38 +02003804 return ret;
3805}
3806
3807/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003808 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3809 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003810 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3811 */
3812 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003813eval_index(
3814 char_u **arg,
3815 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003816 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003817 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003818{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003819 int evaluate = evalarg != NULL
3820 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003821 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003822 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003823 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003824 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003825 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003826 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003827
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003828 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3829 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003830
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003831 init_tv(&var1);
3832 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003833 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003834 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003835 /*
3836 * dict.name
3837 */
3838 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003839 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003840 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003841 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003842 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003843 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003844 }
3845 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003846 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003847 /*
3848 * something[idx]
3849 *
3850 * Get the (first) variable from inside the [].
3851 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003852 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003853 if (**arg == ':')
3854 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003855 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003856 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003857 else if (vim9 && **arg == ':')
3858 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003859 semsg(_(e_white_space_required_before_and_after_str_at_str),
3860 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003861 clear_tv(&var1);
3862 return FAIL;
3863 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003864 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003865 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003866#ifdef FEAT_FLOAT
3867 // allow for indexing with float
3868 if (vim9 && rettv->v_type == VAR_DICT
3869 && var1.v_type == VAR_FLOAT)
3870 {
3871 var1.vval.v_string = typval_tostring(&var1, TRUE);
3872 var1.v_type = VAR_STRING;
3873 }
3874#endif
3875 if (tv_get_string_chk(&var1) == NULL)
3876 {
3877 // not a number or string
3878 clear_tv(&var1);
3879 return FAIL;
3880 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003881 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003882
3883 /*
3884 * Get the second variable from inside the [:].
3885 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003886 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003887 if (**arg == ':')
3888 {
3889 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003890 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003891 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003892 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003893 semsg(_(e_white_space_required_before_and_after_str_at_str),
3894 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003895 if (!empty1)
3896 clear_tv(&var1);
3897 return FAIL;
3898 }
3899 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003900 if (**arg == ']')
3901 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003902 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003904 if (!empty1)
3905 clear_tv(&var1);
3906 return FAIL;
3907 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003908 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003910 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003911 if (!empty1)
3912 clear_tv(&var1);
3913 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003914 return FAIL;
3915 }
3916 }
3917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003918 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003919 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003920 if (**arg != ']')
3921 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003922 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003923 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003924 clear_tv(&var1);
3925 if (range)
3926 clear_tv(&var2);
3927 return FAIL;
3928 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003929 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003930 }
3931
3932 if (evaluate)
3933 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003934 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003935 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003936 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003937
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003938 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003939 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003940 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003941 clear_tv(&var2);
3942 return res;
3943 }
3944 return OK;
3945}
3946
3947/*
3948 * Check if "rettv" can have an [index] or [sli:ce]
3949 */
3950 int
3951check_can_index(typval_T *rettv, int evaluate, int verbose)
3952{
3953 switch (rettv->v_type)
3954 {
3955 case VAR_FUNC:
3956 case VAR_PARTIAL:
3957 if (verbose)
3958 emsg(_("E695: Cannot index a Funcref"));
3959 return FAIL;
3960 case VAR_FLOAT:
3961#ifdef FEAT_FLOAT
3962 if (verbose)
3963 emsg(_(e_float_as_string));
3964 return FAIL;
3965#endif
3966 case VAR_BOOL:
3967 case VAR_SPECIAL:
3968 case VAR_JOB:
3969 case VAR_CHANNEL:
3970 if (verbose)
3971 emsg(_(e_cannot_index_special_variable));
3972 return FAIL;
3973 case VAR_UNKNOWN:
3974 case VAR_ANY:
3975 case VAR_VOID:
3976 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003977 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003978 emsg(_(e_cannot_index_special_variable));
3979 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003980 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003981 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003982
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003983 case VAR_STRING:
3984 case VAR_LIST:
3985 case VAR_DICT:
3986 case VAR_BLOB:
3987 break;
3988 case VAR_NUMBER:
3989 if (in_vim9script())
3990 emsg(_(e_cannot_index_number));
3991 break;
3992 }
3993 return OK;
3994}
3995
3996/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01003997 * slice() function
3998 */
3999 void
4000f_slice(typval_T *argvars, typval_T *rettv)
4001{
4002 if (check_can_index(argvars, TRUE, FALSE) == OK)
4003 {
4004 copy_tv(argvars, rettv);
4005 eval_index_inner(rettv, TRUE, argvars + 1,
4006 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4007 TRUE, NULL, 0, FALSE);
4008 }
4009}
4010
4011/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004012 * Apply index or range to "rettv".
4013 * "var1" is the first index, NULL for [:expr].
4014 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004015 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4016 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004017 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4018 */
4019 int
4020eval_index_inner(
4021 typval_T *rettv,
4022 int is_range,
4023 typval_T *var1,
4024 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004025 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004026 char_u *key,
4027 int keylen,
4028 int verbose)
4029{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004030 varnumber_T n1, n2 = 0;
4031 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004032
4033 n1 = 0;
4034 if (var1 != NULL && rettv->v_type != VAR_DICT)
4035 n1 = tv_get_number(var1);
4036
4037 if (is_range)
4038 {
4039 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004040 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004041 if (verbose)
4042 emsg(_(e_cannot_slice_dictionary));
4043 return FAIL;
4044 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004045 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004046 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004047 else
4048 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004049 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004050
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004051 switch (rettv->v_type)
4052 {
4053 case VAR_UNKNOWN:
4054 case VAR_ANY:
4055 case VAR_VOID:
4056 case VAR_FUNC:
4057 case VAR_PARTIAL:
4058 case VAR_FLOAT:
4059 case VAR_BOOL:
4060 case VAR_SPECIAL:
4061 case VAR_JOB:
4062 case VAR_CHANNEL:
4063 break; // not evaluating, skipping over subscript
4064
4065 case VAR_NUMBER:
4066 case VAR_STRING:
4067 {
4068 char_u *s = tv_get_string(rettv);
4069
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004070 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004071 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004072 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004073 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004074 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004075 else
4076 s = char_from_string(s, n1);
4077 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004078 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004079 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004080 // The resulting variable is a substring. If the indexes
4081 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004082 if (n1 < 0)
4083 {
4084 n1 = len + n1;
4085 if (n1 < 0)
4086 n1 = 0;
4087 }
4088 if (n2 < 0)
4089 n2 = len + n2;
4090 else if (n2 >= len)
4091 n2 = len;
4092 if (n1 >= len || n2 < 0 || n1 > n2)
4093 s = NULL;
4094 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004095 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004096 }
4097 else
4098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004099 // The resulting variable is a string of a single
4100 // character. If the index is too big or negative the
4101 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004102 if (n1 >= len || n1 < 0)
4103 s = NULL;
4104 else
4105 s = vim_strnsave(s + n1, 1);
4106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004107 clear_tv(rettv);
4108 rettv->v_type = VAR_STRING;
4109 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004110 }
4111 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004112
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004113 case VAR_BLOB:
4114 len = blob_len(rettv->vval.v_blob);
4115 if (is_range)
4116 {
4117 // The resulting variable is a sub-blob. If the indexes
4118 // are out of range the result is empty.
4119 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004120 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004121 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004122 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004123 n1 = 0;
4124 }
4125 if (n2 < 0)
4126 n2 = len + n2;
4127 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004128 n2 = len - (exclusive ? 0 : 1);
4129 if (exclusive)
4130 --n2;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004131 if (n1 >= len || n2 < 0 || n1 > n2)
4132 {
4133 clear_tv(rettv);
4134 rettv->v_type = VAR_BLOB;
4135 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004136 }
4137 else
4138 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004139 blob_T *blob = blob_alloc();
4140 long i;
4141
4142 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004143 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004144 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004145 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004146 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004147 return FAIL;
4148 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004149 blob->bv_ga.ga_len = n2 - n1 + 1;
4150 for (i = n1; i <= n2; i++)
4151 blob_set(blob, i - n1,
4152 blob_get(rettv->vval.v_blob, i));
4153
4154 clear_tv(rettv);
4155 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004156 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004157 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004158 }
4159 else
4160 {
4161 // The resulting variable is a byte value.
4162 // If the index is too big or negative that is an error.
4163 if (n1 < 0)
4164 n1 = len + n1;
4165 if (n1 < len && n1 >= 0)
4166 {
4167 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004168
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004169 clear_tv(rettv);
4170 rettv->v_type = VAR_NUMBER;
4171 rettv->vval.v_number = v;
4172 }
4173 else
4174 semsg(_(e_blobidx), n1);
4175 }
4176 break;
4177
4178 case VAR_LIST:
4179 if (var1 == NULL)
4180 n1 = 0;
4181 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004182 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004183 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004184 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004185 return FAIL;
4186 break;
4187
4188 case VAR_DICT:
4189 {
4190 dictitem_T *item;
4191 typval_T tmp;
4192
4193 if (key == NULL)
4194 {
4195 key = tv_get_string_chk(var1);
4196 if (key == NULL)
4197 return FAIL;
4198 }
4199
4200 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4201
4202 if (item == NULL && verbose)
4203 semsg(_(e_dictkey), key);
4204 if (item == NULL)
4205 return FAIL;
4206
4207 copy_tv(&item->di_tv, &tmp);
4208 clear_tv(rettv);
4209 *rettv = tmp;
4210 }
4211 break;
4212 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004213 return OK;
4214}
4215
4216/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004217 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004218 */
4219 char_u *
4220partial_name(partial_T *pt)
4221{
4222 if (pt->pt_name != NULL)
4223 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004224 if (pt->pt_func != NULL)
4225 return pt->pt_func->uf_name;
4226 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004227}
4228
Bram Moolenaarddecc252016-04-06 22:59:37 +02004229 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004230partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004231{
4232 int i;
4233
4234 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004235 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004236 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004237 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004238 if (pt->pt_name != NULL)
4239 {
4240 func_unref(pt->pt_name);
4241 vim_free(pt->pt_name);
4242 }
4243 else
4244 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004245
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004246 // Decrease the reference count for the context of a closure. If down
4247 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004248 if (pt->pt_funcstack != NULL)
4249 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004250 --pt->pt_funcstack->fs_refcount;
4251 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004252 }
4253
Bram Moolenaarddecc252016-04-06 22:59:37 +02004254 vim_free(pt);
4255}
4256
4257/*
4258 * Unreference a closure: decrement the reference count and free it when it
4259 * becomes zero.
4260 */
4261 void
4262partial_unref(partial_T *pt)
4263{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004264 if (pt != NULL)
4265 {
4266 if (--pt->pt_refcount <= 0)
4267 partial_free(pt);
4268
4269 // If the reference count goes down to one, the funcstack may be the
4270 // only reference and can be freed if no other partials reference it.
4271 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4272 funcstack_check_refcount(pt->pt_funcstack);
4273 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004274}
4275
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004276/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004277 * Return the next (unique) copy ID.
4278 * Used for serializing nested structures.
4279 */
4280 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004281get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004282{
4283 current_copyID += COPYID_INC;
4284 return current_copyID;
4285}
4286
4287/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004288 * Garbage collection for lists and dictionaries.
4289 *
4290 * We use reference counts to be able to free most items right away when they
4291 * are no longer used. But for composite items it's possible that it becomes
4292 * unused while the reference count is > 0: When there is a recursive
4293 * reference. Example:
4294 * :let l = [1, 2, 3]
4295 * :let d = {9: l}
4296 * :let l[1] = d
4297 *
4298 * Since this is quite unusual we handle this with garbage collection: every
4299 * once in a while find out which lists and dicts are not referenced from any
4300 * variable.
4301 *
4302 * Here is a good reference text about garbage collection (refers to Python
4303 * but it applies to all reference-counting mechanisms):
4304 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004305 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004306
4307/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004308 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004309 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004310 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004311 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004312 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004313garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004314{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004315 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004316 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004317 buf_T *buf;
4318 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004319 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004320 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004321
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004322 if (!testing)
4323 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004324 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004325 want_garbage_collect = FALSE;
4326 may_garbage_collect = FALSE;
4327 garbage_collect_at_exit = FALSE;
4328 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004329
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004330 // The execution stack can grow big, limit the size.
4331 if (exestack.ga_maxlen - exestack.ga_len > 500)
4332 {
4333 size_t new_len;
4334 char_u *pp;
4335 int n;
4336
4337 // Keep 150% of the current size, with a minimum of the growth size.
4338 n = exestack.ga_len / 2;
4339 if (n < exestack.ga_growsize)
4340 n = exestack.ga_growsize;
4341
4342 // Don't make it bigger though.
4343 if (exestack.ga_len + n < exestack.ga_maxlen)
4344 {
4345 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4346 pp = vim_realloc(exestack.ga_data, new_len);
4347 if (pp == NULL)
4348 return FAIL;
4349 exestack.ga_maxlen = exestack.ga_len + n;
4350 exestack.ga_data = pp;
4351 }
4352 }
4353
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004354 // We advance by two because we add one for items referenced through
4355 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004356 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004357
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004358 /*
4359 * 1. Go through all accessible variables and mark all lists and dicts
4360 * with copyID.
4361 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004362
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004363 // Don't free variables in the previous_funccal list unless they are only
4364 // referenced through previous_funccal. This must be first, because if
4365 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004366 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004369 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004370
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004371 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004372 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004373 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4374 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004375
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004376 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004377 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004378 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4379 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004380 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004381 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4382 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004383#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004384 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004385 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4386 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004387 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004388 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004389 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4390 NULL, NULL);
4391#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004392
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004393 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004394 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004395 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4396 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004397 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004398 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004399
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004400 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004401 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004402
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004403 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004404 abort = abort || set_ref_in_functions(copyID);
4405
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004406 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004407 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004408
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004409 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004410 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004411
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004412 // callbacks in buffers
4413 abort = abort || set_ref_in_buffers(copyID);
4414
Bram Moolenaar1dced572012-04-05 16:54:08 +02004415#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004416 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004417#endif
4418
Bram Moolenaardb913952012-06-29 12:54:53 +02004419#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004420 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004421#endif
4422
4423#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004424 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004425#endif
4426
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004427#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004428 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004429 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004430#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004431#ifdef FEAT_NETBEANS_INTG
4432 abort = abort || set_ref_in_nb_channel(copyID);
4433#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004434
Bram Moolenaare3188e22016-05-31 21:13:04 +02004435#ifdef FEAT_TIMERS
4436 abort = abort || set_ref_in_timer(copyID);
4437#endif
4438
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004439#ifdef FEAT_QUICKFIX
4440 abort = abort || set_ref_in_quickfix(copyID);
4441#endif
4442
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004443#ifdef FEAT_TERMINAL
4444 abort = abort || set_ref_in_term(copyID);
4445#endif
4446
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004447#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004448 abort = abort || set_ref_in_popups(copyID);
4449#endif
4450
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004451 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004452 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004453 /*
4454 * 2. Free lists and dictionaries that are not referenced.
4455 */
4456 did_free = free_unref_items(copyID);
4457
4458 /*
4459 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004460 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004461 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004462 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004463 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004464 else if (p_verbose > 0)
4465 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004466 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004467 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004468
4469 return did_free;
4470}
4471
4472/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004473 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004474 */
4475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004476free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004477{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004478 int did_free = FALSE;
4479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004480 // Let all "free" functions know that we are here. This means no
4481 // dictionaries, lists, channels or jobs are to be freed, because we will
4482 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004483 in_free_unref_items = TRUE;
4484
4485 /*
4486 * PASS 1: free the contents of the items. We don't free the items
4487 * themselves yet, so that it is possible to decrement refcount counters
4488 */
4489
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004490 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004491 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004493 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004494 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004495
4496#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004497 // Go through the list of jobs and free items without the copyID. This
4498 // must happen before doing channels, because jobs refer to channels, but
4499 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004500 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4501
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004502 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004503 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4504#endif
4505
4506 /*
4507 * PASS 2: free the items themselves.
4508 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004509 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004510 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004511
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004512#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004513 // Go through the list of jobs and free items without the copyID. This
4514 // must happen before doing channels, because jobs refer to channels, but
4515 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004516 free_unused_jobs(copyID, COPYID_MASK);
4517
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004518 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004519 free_unused_channels(copyID, COPYID_MASK);
4520#endif
4521
4522 in_free_unref_items = FALSE;
4523
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004524 return did_free;
4525}
4526
4527/*
4528 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004529 * "list_stack" is used to add lists to be marked. Can be NULL.
4530 *
4531 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004532 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004534set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004535{
4536 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004537 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004538 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004539 hashtab_T *cur_ht;
4540 ht_stack_T *ht_stack = NULL;
4541 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004542
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004543 cur_ht = ht;
4544 for (;;)
4545 {
4546 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004547 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004548 // Mark each item in the hashtab. If the item contains a hashtab
4549 // it is added to ht_stack, if it contains a list it is added to
4550 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004551 todo = (int)cur_ht->ht_used;
4552 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4553 if (!HASHITEM_EMPTY(hi))
4554 {
4555 --todo;
4556 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4557 &ht_stack, list_stack);
4558 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004559 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004560
4561 if (ht_stack == NULL)
4562 break;
4563
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004564 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004565 cur_ht = ht_stack->ht;
4566 tempitem = ht_stack;
4567 ht_stack = ht_stack->prev;
4568 free(tempitem);
4569 }
4570
4571 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004572}
4573
4574/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004575 * Mark a dict and its items with "copyID".
4576 * Returns TRUE if setting references failed somehow.
4577 */
4578 int
4579set_ref_in_dict(dict_T *d, int copyID)
4580{
4581 if (d != NULL && d->dv_copyID != copyID)
4582 {
4583 d->dv_copyID = copyID;
4584 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4585 }
4586 return FALSE;
4587}
4588
4589/*
4590 * Mark a list and its items with "copyID".
4591 * Returns TRUE if setting references failed somehow.
4592 */
4593 int
4594set_ref_in_list(list_T *ll, int copyID)
4595{
4596 if (ll != NULL && ll->lv_copyID != copyID)
4597 {
4598 ll->lv_copyID = copyID;
4599 return set_ref_in_list_items(ll, copyID, NULL);
4600 }
4601 return FALSE;
4602}
4603
4604/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004605 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004606 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4607 *
4608 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004609 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004610 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004611set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004612{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004613 listitem_T *li;
4614 int abort = FALSE;
4615 list_T *cur_l;
4616 list_stack_T *list_stack = NULL;
4617 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004618
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004619 cur_l = l;
4620 for (;;)
4621 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004622 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004623 // Mark each item in the list. If the item contains a hashtab
4624 // it is added to ht_stack, if it contains a list it is added to
4625 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004626 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4627 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4628 ht_stack, &list_stack);
4629 if (list_stack == NULL)
4630 break;
4631
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004632 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004633 cur_l = list_stack->list;
4634 tempitem = list_stack;
4635 list_stack = list_stack->prev;
4636 free(tempitem);
4637 }
4638
4639 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004640}
4641
4642/*
4643 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004644 * "list_stack" is used to add lists to be marked. Can be NULL.
4645 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4646 *
4647 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004648 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004649 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004650set_ref_in_item(
4651 typval_T *tv,
4652 int copyID,
4653 ht_stack_T **ht_stack,
4654 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004655{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004656 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004657
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004658 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004659 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004660 dict_T *dd = tv->vval.v_dict;
4661
Bram Moolenaara03f2332016-02-06 18:09:59 +01004662 if (dd != NULL && dd->dv_copyID != copyID)
4663 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004664 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004665 dd->dv_copyID = copyID;
4666 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004667 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004668 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4669 }
4670 else
4671 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004672 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4673
Bram Moolenaara03f2332016-02-06 18:09:59 +01004674 if (newitem == NULL)
4675 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004676 else
4677 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004678 newitem->ht = &dd->dv_hashtab;
4679 newitem->prev = *ht_stack;
4680 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004681 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004682 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004683 }
4684 }
4685 else if (tv->v_type == VAR_LIST)
4686 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004687 list_T *ll = tv->vval.v_list;
4688
Bram Moolenaara03f2332016-02-06 18:09:59 +01004689 if (ll != NULL && ll->lv_copyID != copyID)
4690 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004691 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004692 ll->lv_copyID = copyID;
4693 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004694 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004695 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004696 }
4697 else
4698 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004699 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4700
Bram Moolenaara03f2332016-02-06 18:09:59 +01004701 if (newitem == NULL)
4702 abort = TRUE;
4703 else
4704 {
4705 newitem->list = ll;
4706 newitem->prev = *list_stack;
4707 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004708 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004709 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004710 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004711 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004712 else if (tv->v_type == VAR_FUNC)
4713 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004714 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004715 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004716 else if (tv->v_type == VAR_PARTIAL)
4717 {
4718 partial_T *pt = tv->vval.v_partial;
4719 int i;
4720
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004721 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004722 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004723 // Didn't see this partial yet.
4724 pt->pt_copyID = copyID;
4725
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004726 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004727
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004728 if (pt->pt_dict != NULL)
4729 {
4730 typval_T dtv;
4731
4732 dtv.v_type = VAR_DICT;
4733 dtv.vval.v_dict = pt->pt_dict;
4734 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4735 }
4736
4737 for (i = 0; i < pt->pt_argc; ++i)
4738 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4739 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004740 if (pt->pt_funcstack != NULL)
4741 {
4742 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4743
4744 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4745 abort = abort || set_ref_in_item(stack + i, copyID,
4746 ht_stack, list_stack);
4747 }
4748
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004749 }
4750 }
4751#ifdef FEAT_JOB_CHANNEL
4752 else if (tv->v_type == VAR_JOB)
4753 {
4754 job_T *job = tv->vval.v_job;
4755 typval_T dtv;
4756
4757 if (job != NULL && job->jv_copyID != copyID)
4758 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004759 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004760 if (job->jv_channel != NULL)
4761 {
4762 dtv.v_type = VAR_CHANNEL;
4763 dtv.vval.v_channel = job->jv_channel;
4764 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4765 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004766 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004767 {
4768 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004769 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004770 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4771 }
4772 }
4773 }
4774 else if (tv->v_type == VAR_CHANNEL)
4775 {
4776 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004777 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004778 typval_T dtv;
4779 jsonq_T *jq;
4780 cbq_T *cq;
4781
4782 if (ch != NULL && ch->ch_copyID != copyID)
4783 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004784 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004785 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004786 {
4787 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4788 jq = jq->jq_next)
4789 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4790 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4791 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004792 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004793 {
4794 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004795 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004796 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4797 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004798 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004799 {
4800 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004801 dtv.vval.v_partial =
4802 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004803 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4804 }
4805 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004806 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004807 {
4808 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004809 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004810 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4811 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004812 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004813 {
4814 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004815 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004816 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4817 }
4818 }
4819 }
4820#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004821 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004822}
4823
Bram Moolenaar8c711452005-01-14 21:53:12 +00004824/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004825 * Return a string with the string representation of a variable.
4826 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004827 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004828 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004829 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004830 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004831 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004832 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4833 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004834 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004835 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004836 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004837echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004838 typval_T *tv,
4839 char_u **tofree,
4840 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004841 int copyID,
4842 int echo_style,
4843 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004844 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004845{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004846 static int recurse = 0;
4847 char_u *r = NULL;
4848
Bram Moolenaar33570922005-01-25 22:26:29 +00004849 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004850 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004851 if (!did_echo_string_emsg)
4852 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004853 // Only give this message once for a recursive call to avoid
4854 // flooding the user with errors. And stop iterating over lists
4855 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004856 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004857 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004859 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004860 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004861 }
4862 ++recurse;
4863
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004864 switch (tv->v_type)
4865 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004866 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004867 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004868 {
4869 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004870 r = tv->vval.v_string;
4871 if (r == NULL)
4872 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004873 }
4874 else
4875 {
4876 *tofree = string_quote(tv->vval.v_string, FALSE);
4877 r = *tofree;
4878 }
4879 break;
4880
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004881 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004882 if (echo_style)
4883 {
4884 *tofree = NULL;
4885 r = tv->vval.v_string;
4886 }
4887 else
4888 {
4889 *tofree = string_quote(tv->vval.v_string, TRUE);
4890 r = *tofree;
4891 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004892 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004893
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004894 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004895 {
4896 partial_T *pt = tv->vval.v_partial;
4897 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004898 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004899 garray_T ga;
4900 int i;
4901 char_u *tf;
4902
4903 ga_init2(&ga, 1, 100);
4904 ga_concat(&ga, (char_u *)"function(");
4905 if (fname != NULL)
4906 {
4907 ga_concat(&ga, fname);
4908 vim_free(fname);
4909 }
4910 if (pt != NULL && pt->pt_argc > 0)
4911 {
4912 ga_concat(&ga, (char_u *)", [");
4913 for (i = 0; i < pt->pt_argc; ++i)
4914 {
4915 if (i > 0)
4916 ga_concat(&ga, (char_u *)", ");
4917 ga_concat(&ga,
4918 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4919 vim_free(tf);
4920 }
4921 ga_concat(&ga, (char_u *)"]");
4922 }
4923 if (pt != NULL && pt->pt_dict != NULL)
4924 {
4925 typval_T dtv;
4926
4927 ga_concat(&ga, (char_u *)", ");
4928 dtv.v_type = VAR_DICT;
4929 dtv.vval.v_dict = pt->pt_dict;
4930 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4931 vim_free(tf);
4932 }
4933 ga_concat(&ga, (char_u *)")");
4934
4935 *tofree = ga.ga_data;
4936 r = *tofree;
4937 break;
4938 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004939
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004940 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004941 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004942 break;
4943
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004944 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004945 if (tv->vval.v_list == NULL)
4946 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004947 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004948 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004949 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004950 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004951 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4952 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004953 {
4954 *tofree = NULL;
4955 r = (char_u *)"[...]";
4956 }
4957 else
4958 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004959 int old_copyID = tv->vval.v_list->lv_copyID;
4960
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004961 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004962 *tofree = list2string(tv, copyID, restore_copyID);
4963 if (restore_copyID)
4964 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004965 r = *tofree;
4966 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004967 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004968
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004970 if (tv->vval.v_dict == NULL)
4971 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004972 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004973 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004974 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004975 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004976 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4977 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004978 {
4979 *tofree = NULL;
4980 r = (char_u *)"{...}";
4981 }
4982 else
4983 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004984 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004985
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004986 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004987 *tofree = dict2string(tv, copyID, restore_copyID);
4988 if (restore_copyID)
4989 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004990 r = *tofree;
4991 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004992 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004993
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004995 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004996 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004998 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004999 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005000 break;
5001
Bram Moolenaar835dc632016-02-07 14:27:38 +01005002 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005003 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005004 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005005 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005006 if (composite_val)
5007 {
5008 *tofree = string_quote(r, FALSE);
5009 r = *tofree;
5010 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005011 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005012
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005013 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005014#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005015 *tofree = NULL;
5016 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5017 r = numbuf;
5018 break;
5019#endif
5020
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005021 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005022 case VAR_SPECIAL:
5023 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005024 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005025 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005026 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005027
Bram Moolenaar8502c702014-06-17 12:51:16 +02005028 if (--recurse == 0)
5029 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005030 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005031}
5032
5033/*
5034 * Return a string with the string representation of a variable.
5035 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5036 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005037 * Does not put quotes around strings, as ":echo" displays values.
5038 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5039 * May return NULL.
5040 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005041 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005042echo_string(
5043 typval_T *tv,
5044 char_u **tofree,
5045 char_u *numbuf,
5046 int copyID)
5047{
5048 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5049}
5050
5051/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005052 * Return string "str" in ' quotes, doubling ' characters.
5053 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005055 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005056 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005057string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058{
Bram Moolenaar33570922005-01-25 22:26:29 +00005059 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005060 char_u *p, *r, *s;
5061
Bram Moolenaar33570922005-01-25 22:26:29 +00005062 len = (function ? 13 : 3);
5063 if (str != NULL)
5064 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005065 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005066 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005067 if (*p == '\'')
5068 ++len;
5069 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005070 s = r = alloc(len);
5071 if (r != NULL)
5072 {
5073 if (function)
5074 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076 r += 10;
5077 }
5078 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005079 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005080 if (str != NULL)
5081 for (p = str; *p != NUL; )
5082 {
5083 if (*p == '\'')
5084 *r++ = '\'';
5085 MB_COPY_CHAR(p, r);
5086 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005088 if (function)
5089 *r++ = ')';
5090 *r++ = NUL;
5091 }
5092 return s;
5093}
5094
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005095#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096/*
5097 * Convert the string "text" to a floating point number.
5098 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5099 * this always uses a decimal point.
5100 * Returns the length of the text that was consumed.
5101 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005103string2float(
5104 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005105 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005106{
5107 char *s = (char *)text;
5108 float_T f;
5109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005110 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005111 if (STRNICMP(text, "inf", 3) == 0)
5112 {
5113 *value = INFINITY;
5114 return 3;
5115 }
5116 if (STRNICMP(text, "-inf", 3) == 0)
5117 {
5118 *value = -INFINITY;
5119 return 4;
5120 }
5121 if (STRNICMP(text, "nan", 3) == 0)
5122 {
5123 *value = NAN;
5124 return 3;
5125 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005126 f = strtod(s, &s);
5127 *value = f;
5128 return (int)((char_u *)s - text);
5129}
5130#endif
5131
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005132/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005133 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5134 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005135 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005136 */
5137 int
5138buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5139{
5140 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005141 char_u *t;
5142 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005143
5144 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5145 return -1;
5146
5147 if (lnum > buf->b_ml.ml_line_count)
5148 lnum = buf->b_ml.ml_line_count;
5149
5150 str = ml_get_buf(buf, lnum, FALSE);
5151 if (str == NULL)
5152 return -1;
5153
5154 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005155 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005156
Bram Moolenaar91458462021-01-13 20:08:38 +01005157 // count the number of characters
5158 t = str;
5159 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5160 t += mb_ptr2len(t);
5161
5162 // In insert mode, when the cursor is at the end of a non-empty line,
5163 // byteidx points to the NUL character immediately past the end of the
5164 // string. In this case, add one to the character count.
5165 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5166 count++;
5167
5168 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005169}
5170
5171/*
5172 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005173 * byte index. Works only for loaded buffers. Returns -1 on failure.
5174 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005175 */
5176 int
5177buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5178{
5179 char_u *str;
5180 char_u *t;
5181
5182 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5183 return -1;
5184
5185 if (lnum > buf->b_ml.ml_line_count)
5186 lnum = buf->b_ml.ml_line_count;
5187
5188 str = ml_get_buf(buf, lnum, FALSE);
5189 if (str == NULL)
5190 return -1;
5191
5192 // Convert the character offset to a byte offset
5193 t = str;
5194 while (*t != NUL && --charidx > 0)
5195 t += mb_ptr2len(t);
5196
Bram Moolenaar91458462021-01-13 20:08:38 +01005197 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005198}
5199
5200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005202 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005204 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005205var2fpos(
5206 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005207 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005208 int *fnum, // set to fnum for '0, 'A, etc.
5209 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005211 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005213 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005215 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005216 if (varp->v_type == VAR_LIST)
5217 {
5218 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005219 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005220 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005221 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005222
5223 l = varp->vval.v_list;
5224 if (l == NULL)
5225 return NULL;
5226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005227 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005228 pos.lnum = list_find_nr(l, 0L, &error);
5229 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005230 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005231 if (charcol)
5232 len = (long)mb_charlen(ml_get(pos.lnum));
5233 else
5234 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005235
Bram Moolenaarec65d772020-08-20 22:29:12 +02005236 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005237 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005238 li = list_find(l, 1L);
5239 if (li != NULL && li->li_tv.v_type == VAR_STRING
5240 && li->li_tv.vval.v_string != NULL
5241 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005242 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005243 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005244 }
5245 else
5246 {
5247 pos.col = list_find_nr(l, 1L, &error);
5248 if (error)
5249 return NULL;
5250 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005252 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005253 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005254 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005255 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005256
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005257 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005258 pos.coladd = list_find_nr(l, 2L, &error);
5259 if (error)
5260 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005261
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005262 return &pos;
5263 }
5264
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005265 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005266 if (name == NULL)
5267 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005268 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005269 {
5270 pos = curwin->w_cursor;
5271 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005272 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005273 return &pos;
5274 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005275 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005276 {
5277 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005278 pos = VIsual;
5279 else
5280 pos = curwin->w_cursor;
5281 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005282 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005283 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005284 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005285 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005287 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5289 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005290 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005291 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 return pp;
5293 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005294
Bram Moolenaara5525202006-03-02 22:52:09 +00005295 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005296
Bram Moolenaar477933c2007-07-17 14:32:23 +00005297 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005298 {
5299 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005300 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005301 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005302 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005303 // In silent Ex mode topline is zero, but that's not a valid line
5304 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005305 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005306 return &pos;
5307 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005308 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005309 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005310 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005311 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005312 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005313 return &pos;
5314 }
5315 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005316 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005318 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
5320 pos.lnum = curbuf->b_ml.ml_line_count;
5321 pos.col = 0;
5322 }
5323 else
5324 {
5325 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005326 if (charcol)
5327 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5328 else
5329 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331 return &pos;
5332 }
5333 return NULL;
5334}
5335
5336/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005337 * Convert list in "arg" into a position and optional file number.
5338 * When "fnump" is NULL there is no file number, only 3 items.
5339 * Note that the column is passed on as-is, the caller may want to decrement
5340 * it to use 1 for the first column.
5341 * Return FAIL when conversion is not possible, doesn't check the position for
5342 * validity.
5343 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005344 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005345list2fpos(
5346 typval_T *arg,
5347 pos_T *posp,
5348 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005349 colnr_T *curswantp,
5350 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005351{
5352 list_T *l = arg->vval.v_list;
5353 long i = 0;
5354 long n;
5355
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005356 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5357 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005358 if (arg->v_type != VAR_LIST
5359 || l == NULL
5360 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005361 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005362 return FAIL;
5363
5364 if (fnump != NULL)
5365 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005366 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005367 if (n < 0)
5368 return FAIL;
5369 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005370 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005371 *fnump = n;
5372 }
5373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005374 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005375 if (n < 0)
5376 return FAIL;
5377 posp->lnum = n;
5378
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005379 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005380 if (n < 0)
5381 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005382 // If character position is specified, then convert to byte position
5383 if (charcol)
5384 {
5385 buf_T *buf;
5386
5387 // Get the text for the specified line in a loaded buffer
5388 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5389 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5390 return FAIL;
5391
Bram Moolenaar91458462021-01-13 20:08:38 +01005392 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005393 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005394 posp->col = n;
5395
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005396 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005397 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005398 posp->coladd = 0;
5399 else
5400 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005401
Bram Moolenaar493c1782014-05-28 14:34:46 +02005402 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005403 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005404
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005405 return OK;
5406}
5407
5408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 * Get the length of an environment variable name.
5410 * Advance "arg" to the first character after the name.
5411 * Return 0 for error.
5412 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005414get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 char_u *p;
5417 int len;
5418
5419 for (p = *arg; vim_isIDc(*p); ++p)
5420 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005421 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 return 0;
5423
5424 len = (int)(p - *arg);
5425 *arg = p;
5426 return len;
5427}
5428
5429/*
5430 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005431 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 * Return 0 if something is wrong.
5433 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005435get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
5437 char_u *p;
5438 int len;
5439
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005440 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005442 {
5443 if (*p == ':')
5444 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005445 // "s:" is start of "s:var", but "n:" is not and can be used in
5446 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005447 len = (int)(p - *arg);
5448 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5449 || len > 1)
5450 break;
5451 }
5452 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 return 0;
5455
5456 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005457 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458
5459 return len;
5460}
5461
5462/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005463 * Get the length of the name of a variable or function.
5464 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005466 * Return -1 if curly braces expansion failed.
5467 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 * If the name contains 'magic' {}'s, expand them and return the
5469 * expanded name in an allocated string via 'alias' - caller must free.
5470 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005471 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005472get_name_len(
5473 char_u **arg,
5474 char_u **alias,
5475 int evaluate,
5476 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 char_u *p;
5480 char_u *expr_start;
5481 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005483 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
5485 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5486 && (*arg)[2] == (int)KE_SNR)
5487 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005488 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 *arg += 3;
5490 return get_id_len(arg) + 3;
5491 }
5492 len = eval_fname_script(*arg);
5493 if (len > 0)
5494 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005495 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 *arg += len;
5497 }
5498
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005502 p = find_name_end(*arg, &expr_start, &expr_end,
5503 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 if (expr_start != NULL)
5505 {
5506 char_u *temp_string;
5507
5508 if (!evaluate)
5509 {
5510 len += (int)(p - *arg);
5511 *arg = skipwhite(p);
5512 return len;
5513 }
5514
5515 /*
5516 * Include any <SID> etc in the expanded string:
5517 * Thus the -len here.
5518 */
5519 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5520 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005521 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 *alias = temp_string;
5523 *arg = skipwhite(p);
5524 return (int)STRLEN(temp_string);
5525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
5527 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005528 // Only give an error when there is something, otherwise it will be
5529 // reported at a higher level.
5530 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005531 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
5533 return len;
5534}
5535
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005536/*
5537 * Find the end of a variable or function name, taking care of magic braces.
5538 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5539 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005540 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 * Return a pointer to just after the name. Equal to "arg" if there is no
5542 * valid name.
5543 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005544 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005545find_name_end(
5546 char_u *arg,
5547 char_u **expr_start,
5548 char_u **expr_end,
5549 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 int mb_nest = 0;
5552 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005554 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005555 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 *expr_start = NULL;
5560 *expr_end = NULL;
5561 }
5562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005563 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005564 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5565 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005566 return arg;
5567
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 for (p = arg; *p != NUL
5569 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005570 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005571 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005572 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005574 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005576 if (*p == '\'')
5577 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005578 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005579 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005580 ;
5581 if (*p == NUL)
5582 break;
5583 }
5584 else if (*p == '"')
5585 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005586 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005587 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005588 if (*p == '\\' && p[1] != NUL)
5589 ++p;
5590 if (*p == NUL)
5591 break;
5592 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005593 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5594 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005595 // "s:" is start of "s:var", but "n:" is not and can be used in
5596 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005597 len = (int)(p - arg);
5598 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005599 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005600 break;
5601 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005602
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005603 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005605 if (*p == '[')
5606 ++br_nest;
5607 else if (*p == ']')
5608 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005610
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005611 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005613 if (*p == '{')
5614 {
5615 mb_nest++;
5616 if (expr_start != NULL && *expr_start == NULL)
5617 *expr_start = p;
5618 }
5619 else if (*p == '}')
5620 {
5621 mb_nest--;
5622 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5623 *expr_end = p;
5624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627
5628 return p;
5629}
5630
5631/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005632 * Expands out the 'magic' {}'s in a variable/function name.
5633 * Note that this can call itself recursively, to deal with
5634 * constructs like foo{bar}{baz}{bam}
5635 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5636 * "in_start" ^
5637 * "expr_start" ^
5638 * "expr_end" ^
5639 * "in_end" ^
5640 *
5641 * Returns a new allocated string, which the caller must free.
5642 * Returns NULL for failure.
5643 */
5644 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005645make_expanded_name(
5646 char_u *in_start,
5647 char_u *expr_start,
5648 char_u *expr_end,
5649 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005650{
5651 char_u c1;
5652 char_u *retval = NULL;
5653 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005654
5655 if (expr_end == NULL || in_end == NULL)
5656 return NULL;
5657 *expr_start = NUL;
5658 *expr_end = NUL;
5659 c1 = *in_end;
5660 *in_end = NUL;
5661
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005662 temp_result = eval_to_string(expr_start + 1, FALSE);
5663 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005664 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005665 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5666 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005667 if (retval != NULL)
5668 {
5669 STRCPY(retval, in_start);
5670 STRCAT(retval, temp_result);
5671 STRCAT(retval, expr_end + 1);
5672 }
5673 }
5674 vim_free(temp_result);
5675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005676 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005677 *expr_start = '{';
5678 *expr_end = '}';
5679
5680 if (retval != NULL)
5681 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005682 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005683 if (expr_start != NULL)
5684 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005685 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005686 temp_result = make_expanded_name(retval, expr_start,
5687 expr_end, temp_result);
5688 vim_free(retval);
5689 retval = temp_result;
5690 }
5691 }
5692
5693 return retval;
5694}
5695
5696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005698 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005701eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005703 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005704}
5705
5706/*
5707 * Return TRUE if character "c" can be used as the first character in a
5708 * variable or function name (excluding '{' and '}').
5709 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005711eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005712{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005713 return ASCII_ISALPHA(c) || c == '_';
5714}
5715
5716/*
5717 * Return TRUE if character "c" can be used as the first character of a
5718 * dictionary key.
5719 */
5720 int
5721eval_isdictc(int c)
5722{
5723 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724}
5725
5726/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005727 * Handle:
5728 * - expr[expr], expr[expr:expr] subscript
5729 * - ".name" lookup
5730 * - function call with Funcref variable: func(expr)
5731 * - method call: var->method()
5732 *
5733 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005734 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005735 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005736handle_subscript(
5737 char_u **arg,
5738 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005739 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005740 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005741{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005742 int evaluate = evalarg != NULL
5743 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005744 int ret = OK;
5745 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005746 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005747 int getnext;
5748 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005749
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005750 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005751 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005752 // When at the end of the line and ".name" or "->{" or "->X" follows in
5753 // the next line then consume the line break.
5754 p = eval_next_non_blank(*arg, evalarg, &getnext);
5755 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005756 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005757 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005758 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005759 {
5760 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005761 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005762 check_white = FALSE;
5763 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005764
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005765 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5766 || rettv->v_type == VAR_PARTIAL))
5767 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005768 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005769 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5770 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005771
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005772 // Stop the expression evaluation when immediately aborting on
5773 // error, or when an interrupt occurred or an exception was thrown
5774 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005775 if (aborting())
5776 {
5777 if (ret == OK)
5778 clear_tv(rettv);
5779 ret = FAIL;
5780 }
5781 dict_unref(selfdict);
5782 selfdict = NULL;
5783 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005784 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005785 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005786 *arg = skipwhite(p + 2);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005787 if (ret == OK)
5788 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005789 if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005790 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005791 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005792 else
5793 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005794 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005795 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005796 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005797 // "." is ".name" lookup when we found a dict or when evaluating and
5798 // scriptversion is at least 2, where string concatenation is "..".
5799 else if (**arg == '['
5800 || (**arg == '.' && (rettv->v_type == VAR_DICT
5801 || (!evaluate
5802 && (*arg)[1] != '.'
5803 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005804 {
5805 dict_unref(selfdict);
5806 if (rettv->v_type == VAR_DICT)
5807 {
5808 selfdict = rettv->vval.v_dict;
5809 if (selfdict != NULL)
5810 ++selfdict->dv_refcount;
5811 }
5812 else
5813 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005814 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005815 {
5816 clear_tv(rettv);
5817 ret = FAIL;
5818 }
5819 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005820 else
5821 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005822 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005823
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005824 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5825 // Don't do this when "Func" is already a partial that was bound
5826 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005827 if (selfdict != NULL
5828 && (rettv->v_type == VAR_FUNC
5829 || (rettv->v_type == VAR_PARTIAL
5830 && (rettv->vval.v_partial->pt_auto
5831 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005832 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005833
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005834 dict_unref(selfdict);
5835 return ret;
5836}
5837
5838/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005839 * Make a copy of an item.
5840 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005841 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5842 * reference to an already copied list/dict can be used.
5843 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005844 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005845 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005846item_copy(
5847 typval_T *from,
5848 typval_T *to,
5849 int deep,
5850 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005851{
5852 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005853 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005854
Bram Moolenaar33570922005-01-25 22:26:29 +00005855 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005857 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005858 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005859 }
5860 ++recurse;
5861
5862 switch (from->v_type)
5863 {
5864 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005865 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005866 case VAR_STRING:
5867 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005868 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005869 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005870 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005871 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005872 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005873 copy_tv(from, to);
5874 break;
5875 case VAR_LIST:
5876 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005877 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005878 if (from->vval.v_list == NULL)
5879 to->vval.v_list = NULL;
5880 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5881 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005882 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005883 to->vval.v_list = from->vval.v_list->lv_copylist;
5884 ++to->vval.v_list->lv_refcount;
5885 }
5886 else
5887 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5888 if (to->vval.v_list == NULL)
5889 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005890 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005891 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005892 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005893 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005894 case VAR_DICT:
5895 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005896 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005897 if (from->vval.v_dict == NULL)
5898 to->vval.v_dict = NULL;
5899 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5900 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005901 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005902 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5903 ++to->vval.v_dict->dv_refcount;
5904 }
5905 else
5906 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5907 if (to->vval.v_dict == NULL)
5908 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005909 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005910 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005911 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005912 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005913 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005914 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005915 }
5916 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005917 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005918}
5919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005920 void
5921echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5922{
5923 char_u *tofree;
5924 char_u numbuf[NUMBUFLEN];
5925 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5926
5927 if (*atstart)
5928 {
5929 *atstart = FALSE;
5930 // Call msg_start() after eval1(), evaluating the expression
5931 // may cause a message to appear.
5932 if (with_space)
5933 {
5934 // Mark the saved text as finishing the line, so that what
5935 // follows is displayed on a new line when scrolling back
5936 // at the more prompt.
5937 msg_sb_eol();
5938 msg_start();
5939 }
5940 }
5941 else if (with_space)
5942 msg_puts_attr(" ", echo_attr);
5943
5944 if (p != NULL)
5945 for ( ; *p != NUL && !got_int; ++p)
5946 {
5947 if (*p == '\n' || *p == '\r' || *p == TAB)
5948 {
5949 if (*p != TAB && *needclr)
5950 {
5951 // remove any text still there from the command
5952 msg_clr_eos();
5953 *needclr = FALSE;
5954 }
5955 msg_putchar_attr(*p, echo_attr);
5956 }
5957 else
5958 {
5959 if (has_mbyte)
5960 {
5961 int i = (*mb_ptr2len)(p);
5962
5963 (void)msg_outtrans_len_attr(p, i, echo_attr);
5964 p += i - 1;
5965 }
5966 else
5967 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5968 }
5969 }
5970 vim_free(tofree);
5971}
5972
Bram Moolenaare9a41262005-01-15 22:18:47 +00005973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 * ":echo expr1 ..." print each argument separated with a space, add a
5975 * newline at the end.
5976 * ":echon expr1 ..." print each argument plain.
5977 */
5978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005979ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
5981 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005982 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 char_u *p;
5984 int needclr = TRUE;
5985 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005986 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005987 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005988 evalarg_T evalarg;
5989
Bram Moolenaare707c882020-07-01 13:07:37 +02005990 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991
5992 if (eap->skip)
5993 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005994 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005996 // If eval1() causes an error message the text from the command may
5997 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005998 need_clr_eos = needclr;
5999
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006001 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 {
6003 /*
6004 * Report the invalid expression unless the expression evaluation
6005 * has been cancelled due to an aborting error, an interrupt, or an
6006 * exception.
6007 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006008 if (!aborting() && did_emsg == did_emsg_before
6009 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006010 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006011 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 break;
6013 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006014 need_clr_eos = FALSE;
6015
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006017 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006018
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006019 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 arg = skipwhite(arg);
6021 }
6022 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006023 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024
6025 if (eap->skip)
6026 --emsg_skip;
6027 else
6028 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006029 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 if (needclr)
6031 msg_clr_eos();
6032 if (eap->cmdidx == CMD_echo)
6033 msg_end();
6034 }
6035}
6036
6037/*
6038 * ":echohl {name}".
6039 */
6040 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006041ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006043 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044}
6045
6046/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006047 * Returns the :echo attribute
6048 */
6049 int
6050get_echo_attr(void)
6051{
6052 return echo_attr;
6053}
6054
6055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 * ":execute expr1 ..." execute the result of an expression.
6057 * ":echomsg expr1 ..." Print a message
6058 * ":echoerr expr1 ..." Print an error
6059 * Each gets spaces around each argument and a newline at the end for
6060 * echo commands
6061 */
6062 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006063ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 int ret = OK;
6068 char_u *p;
6069 garray_T ga;
6070 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
6072 ga_init2(&ga, 1, 80);
6073
6074 if (eap->skip)
6075 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006076 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006078 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006079 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081
6082 if (!eap->skip)
6083 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006084 char_u buf[NUMBUFLEN];
6085
6086 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006087 {
6088 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6089 {
6090 emsg(_(e_inval_string));
6091 p = NULL;
6092 }
6093 else
6094 p = tv_get_string_buf(&rettv, buf);
6095 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006096 else
6097 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006098 if (p == NULL)
6099 {
6100 clear_tv(&rettv);
6101 ret = FAIL;
6102 break;
6103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 len = (int)STRLEN(p);
6105 if (ga_grow(&ga, len + 2) == FAIL)
6106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006107 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 ret = FAIL;
6109 break;
6110 }
6111 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 ga.ga_len += len;
6115 }
6116
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006117 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 arg = skipwhite(arg);
6119 }
6120
6121 if (ret != FAIL && ga.ga_data != NULL)
6122 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006123 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006125 // Mark the already saved text as finishing the line, so that what
6126 // follows is displayed on a new line when scrolling back at the
6127 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006128 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006129 }
6130
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006132 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006133 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006134 out_flush();
6135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 else if (eap->cmdidx == CMD_echoerr)
6137 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006138 int save_did_emsg = did_emsg;
6139
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006140 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006141 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (!force_abort)
6143 did_emsg = save_did_emsg;
6144 }
6145 else if (eap->cmdidx == CMD_execute)
6146 do_cmdline((char_u *)ga.ga_data,
6147 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6148 }
6149
6150 ga_clear(&ga);
6151
6152 if (eap->skip)
6153 --emsg_skip;
6154
6155 eap->nextcmd = check_nextcmd(arg);
6156}
6157
6158/*
6159 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6160 * "arg" points to the "&" or '+' when called, to "option" when returning.
6161 * Returns NULL when no option name found. Otherwise pointer to the char
6162 * after the option name.
6163 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006164 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006165find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166{
6167 char_u *p = *arg;
6168
6169 ++p;
6170 if (*p == 'g' && p[1] == ':')
6171 {
6172 *opt_flags = OPT_GLOBAL;
6173 p += 2;
6174 }
6175 else if (*p == 'l' && p[1] == ':')
6176 {
6177 *opt_flags = OPT_LOCAL;
6178 p += 2;
6179 }
6180 else
6181 *opt_flags = 0;
6182
6183 if (!ASCII_ISALPHA(*p))
6184 return NULL;
6185 *arg = p;
6186
6187 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006188 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 else
6190 while (ASCII_ISALPHA(*p))
6191 ++p;
6192 return p;
6193}
6194
6195/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006196 * Display script name where an item was last set.
6197 * Should only be invoked when 'verbose' is non-zero.
6198 */
6199 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006200last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006201{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006202 char_u *p;
6203
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006204 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006205 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006206 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006207 if (p != NULL)
6208 {
6209 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006210 msg_puts(_("\n\tLast set from "));
6211 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006212 if (script_ctx.sc_lnum > 0)
6213 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006214 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006215 msg_outnum((long)script_ctx.sc_lnum);
6216 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006217 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006218 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006219 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006220 }
6221}
6222
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006223#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224
6225/*
6226 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006227 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 * "flags" can be "g" to do a global substitute.
6229 * Returns an allocated string, NULL for error.
6230 */
6231 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006232do_string_sub(
6233 char_u *str,
6234 char_u *pat,
6235 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006236 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006237 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
6239 int sublen;
6240 regmatch_T regmatch;
6241 int i;
6242 int do_all;
6243 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006244 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 garray_T ga;
6246 char_u *ret;
6247 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006248 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006250 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006252 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253
6254 ga_init2(&ga, 1, 200);
6255
6256 do_all = (flags[0] == 'g');
6257
6258 regmatch.rm_ic = p_ic;
6259 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6260 if (regmatch.regprog != NULL)
6261 {
6262 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006263 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6265 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006266 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006267 if (regmatch.startp[0] == regmatch.endp[0])
6268 {
6269 if (zero_width == regmatch.startp[0])
6270 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006271 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006272 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006273 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6274 (size_t)i);
6275 ga.ga_len += i;
6276 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006277 continue;
6278 }
6279 zero_width = regmatch.startp[0];
6280 }
6281
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 /*
6283 * Get some space for a temporary buffer to do the substitution
6284 * into. It will contain:
6285 * - The text up to where the match is.
6286 * - The substituted text.
6287 * - The text after the match.
6288 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006289 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006290 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6292 {
6293 ga_clear(&ga);
6294 break;
6295 }
6296
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006297 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 i = (int)(regmatch.startp[0] - tail);
6299 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006300 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006301 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 + ga.ga_len + i, TRUE, TRUE, FALSE);
6303 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006304 tail = regmatch.endp[0];
6305 if (*tail == NUL)
6306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 if (!do_all)
6308 break;
6309 }
6310
6311 if (ga.ga_data != NULL)
6312 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6313
Bram Moolenaar473de612013-06-08 18:19:48 +02006314 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
6316
6317 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6318 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006319 if (p_cpo == empty_option)
6320 p_cpo = save_cpo;
6321 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006322 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006323 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006324 // If it's still empty it was changed and restored, need to restore in
6325 // the complicated way.
6326 if (*p_cpo == NUL)
6327 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006328 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331 return ret;
6332}