blob: f8e922f911d30d5e11992188fb0a493e7cfddba1 [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 Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
54static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static 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 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010062 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010063 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020064 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010065num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010066{
67 varnumber_T result;
68
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 if (failed != NULL)
75 *failed = TRUE;
76 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010077 if (n1 == 0)
78 result = VARNUM_MIN; // similar to NaN
79 else if (n1 < 0)
80 result = -VARNUM_MAX;
81 else
82 result = VARNUM_MAX;
83 }
84 else
85 result = n1 / n2;
86
87 return result;
88}
89
90/*
91 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010092 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010093 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020094 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010095num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010096{
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 if (failed != NULL)
101 *failed = TRUE;
102 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100103 return (n2 == 0) ? 0 : (n1 % n2);
104}
105
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100106#if defined(EBCDIC) || defined(PROTO)
107/*
108 * Compare struct fst by function name.
109 */
110 static int
111compare_func_name(const void *s1, const void *s2)
112{
113 struct fst *p1 = (struct fst *)s1;
114 struct fst *p2 = (struct fst *)s2;
115
116 return STRCMP(p1->f_name, p2->f_name);
117}
118
119/*
120 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100121 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100122 * On machines using EBCDIC we have to sort it.
123 */
124 static void
125sortFunctions(void)
126{
127 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
128
129 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
130}
131#endif
132
Bram Moolenaar33570922005-01-25 22:26:29 +0000133/*
134 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000135 */
136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000138{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200139 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200140 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000141
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200142#ifdef EBCDIC
143 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100144 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200145 */
146 sortFunctions();
147#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000148}
149
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000150#if defined(EXITFREE) || defined(PROTO)
151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100152eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000153{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200154 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100155 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200156 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000157
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200158 // autoloaded script names
159 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000160
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200161 // unreferenced lists and dicts
162 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200163
164 // functions not garbage collected
165 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000166}
167#endif
168
Bram Moolenaare6b53242020-07-01 17:28:33 +0200169 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200170fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
171{
172 CLEAR_FIELD(*evalarg);
173 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
174 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
175 {
176 evalarg->eval_getline = eap->getline;
177 evalarg->eval_cookie = eap->cookie;
178 }
179}
180
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181/*
182 * Top level evaluation function, returning a boolean.
183 * Sets "error" to TRUE if there was an error.
184 * Return TRUE or FALSE.
185 */
186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100187eval_to_bool(
188 char_u *arg,
189 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200190 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100191 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192{
Bram Moolenaar33570922005-01-25 22:26:29 +0000193 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200194 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200195 evalarg_T evalarg;
196
Bram Moolenaar37c83712020-06-30 21:18:36 +0200197 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199 if (skip)
200 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200201 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 else
204 {
205 *error = FALSE;
206 if (!skip)
207 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200208 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200209 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200210 else
211 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000212 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 }
214 }
215 if (skip)
216 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200217 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200219 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220}
221
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222/*
223 * Call eval1() and give an error message if not done at a lower level.
224 */
225 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200226eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100227{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100228 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100229 int ret;
230 int did_emsg_before = did_emsg;
231 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100233
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200234 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
235
236 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100237 if (ret == FAIL)
238 {
239 // Report the invalid expression unless the expression evaluation has
240 // been cancelled due to an aborting error, an interrupt, or an
241 // exception, or we already gave a more specific error.
242 // Also check called_emsg for when using assert_fails().
243 if (!aborting() && did_emsg == did_emsg_before
244 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100245 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100246 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200247 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100248 return ret;
249}
250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200252 * Return whether a typval is a valid expression to pass to eval_expr_typval()
253 * or eval_expr_to_bool(). An empty string returns FALSE;
254 */
255 int
256eval_expr_valid_arg(typval_T *tv)
257{
258 return tv->v_type != VAR_UNKNOWN
259 && (tv->v_type != VAR_STRING
260 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
261}
262
263/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 * Evaluate an expression, which can be a function, partial or string.
265 * Pass arguments "argv[argc]".
266 * Return the result in "rettv" and OK or FAIL.
267 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200268 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100269eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
270{
271 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100272 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200273 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100274
275 if (expr->v_type == VAR_FUNC)
276 {
277 s = expr->vval.v_string;
278 if (s == NULL || *s == NUL)
279 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200280 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200281 funcexe.evaluate = TRUE;
282 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100283 return FAIL;
284 }
285 else if (expr->v_type == VAR_PARTIAL)
286 {
287 partial_T *partial = expr->vval.v_partial;
288
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200289 if (partial == NULL)
290 return FAIL;
291
Bram Moolenaar822ba242020-05-24 23:00:18 +0200292 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200293 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200295 if (call_def_function(partial->pt_func, argc, argv,
296 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 return FAIL;
298 }
299 else
300 {
301 s = partial_name(partial);
302 if (s == NULL || *s == NUL)
303 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200304 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 funcexe.evaluate = TRUE;
306 funcexe.partial = partial;
307 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
308 return FAIL;
309 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100310 }
311 else
312 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100313 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100314 if (s == NULL)
315 return FAIL;
316 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200317 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100318 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200319 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100320 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200321 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100322 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100323 return FAIL;
324 }
325 }
326 return OK;
327}
328
329/*
330 * Like eval_to_bool() but using a typval_T instead of a string.
331 * Works for string, funcref and partial.
332 */
333 int
334eval_expr_to_bool(typval_T *expr, int *error)
335{
336 typval_T rettv;
337 int res;
338
339 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
340 {
341 *error = TRUE;
342 return FALSE;
343 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200344 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100345 clear_tv(&rettv);
346 return res;
347}
348
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349/*
350 * Top level evaluation function, returning a string. If "skip" is TRUE,
351 * only parsing to "nextcmd" is done, without reporting errors. Return
352 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
353 */
354 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100355eval_to_string_skip(
356 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200357 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100358 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
Bram Moolenaar33570922005-01-25 22:26:29 +0000360 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200362 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
Bram Moolenaar37c83712020-06-30 21:18:36 +0200364 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 if (skip)
366 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200367 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 retval = NULL;
369 else
370 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100371 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000372 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 }
374 if (skip)
375 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
378 return retval;
379}
380
381/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000382 * Skip over an expression at "*pp".
383 * Return FAIL for an error, OK otherwise.
384 */
385 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200386skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000387{
Bram Moolenaar33570922005-01-25 22:26:29 +0000388 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000389
390 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200391 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000392}
393
394/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200395 * Skip over an expression at "*pp".
396 * If in Vim9 script and line breaks are encountered, the lines are
397 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200398 * "arg" is advanced to just after the expression.
399 * "start" is set to the start of the expression, "end" to just after the end.
400 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200401 * Return FAIL for an error, OK otherwise.
402 */
403 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200404skip_expr_concatenate(
405 char_u **arg,
406 char_u **start,
407 char_u **end,
408 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409{
410 typval_T rettv;
411 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200412 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200413 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200414 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200415 int evaluate = evalarg == NULL
416 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200417
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200418 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200420 {
421 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200422 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200423 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 ++gap->ga_len;
425 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200426 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200427
428 // Don't evaluate the expression.
429 if (evalarg != NULL)
430 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200431 *arg = skipwhite(*arg);
432 res = eval1(arg, &rettv, evalarg);
433 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200434 if (evalarg != NULL)
435 evalarg->eval_flags = save_flags;
436
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200437 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200438 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200439 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200440 if (evalarg->eval_ga.ga_len == 1)
441 {
442 // just one line, no need to concatenate
443 ga_clear(gap);
444 gap->ga_itemsize = 0;
445 }
446 else
447 {
448 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200449 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200450
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 // Line breaks encountered, concatenate all the lines.
452 *((char_u **)gap->ga_data) = *start;
453 p = ga_concat_strings(gap, "");
454
455 // free the lines only when using getsourceline()
456 if (evalarg->eval_cookie != NULL)
457 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200458 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200460 // Do not free the last line, "arg" points into it, free it
461 // later.
462 vim_free(evalarg->eval_tofree);
463 evalarg->eval_tofree =
464 ((char_u **)gap->ga_data)[gap->ga_len - 1];
465 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200466 ga_clear_strings(gap);
467 }
468 else
469 ga_clear(gap);
470 gap->ga_itemsize = 0;
471 if (p == NULL)
472 return FAIL;
473 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200474 vim_free(evalarg->eval_tofree_lambda);
475 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200476 // Compute "end" relative to the end.
477 *end = *start + STRLEN(*start) - endoff;
478 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 }
480
481 return res;
482}
483
484/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100485 * Convert "tv" to a string.
486 * When "convert" is TRUE convert a List into a sequence of lines and convert
487 * a Float to a String.
488 * Returns an allocated string (NULL when out of memory).
489 */
490 char_u *
491typval2string(typval_T *tv, int convert)
492{
493 garray_T ga;
494 char_u *retval;
495#ifdef FEAT_FLOAT
496 char_u numbuf[NUMBUFLEN];
497#endif
498
499 if (convert && tv->v_type == VAR_LIST)
500 {
501 ga_init2(&ga, (int)sizeof(char), 80);
502 if (tv->vval.v_list != NULL)
503 {
504 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
505 if (tv->vval.v_list->lv_len > 0)
506 ga_append(&ga, NL);
507 }
508 ga_append(&ga, NUL);
509 retval = (char_u *)ga.ga_data;
510 }
511#ifdef FEAT_FLOAT
512 else if (convert && tv->v_type == VAR_FLOAT)
513 {
514 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
515 retval = vim_strsave(numbuf);
516 }
517#endif
518 else
519 retval = vim_strsave(tv_get_string(tv));
520 return retval;
521}
522
523/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200524 * Top level evaluation function, returning a string. Does not handle line
525 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000526 * When "convert" is TRUE convert a List into a sequence of lines and convert
527 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * Return pointer to allocated memory, or NULL for failure.
529 */
530 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100531eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100532 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100533 int convert,
534 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaar33570922005-01-25 22:26:29 +0000536 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100538 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100540 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
541 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 retval = NULL;
543 else
544 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100545 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000546 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100548 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
550 return retval;
551}
552
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100553 char_u *
554eval_to_string(
555 char_u *arg,
556 int convert)
557{
558 return eval_to_string_eap(arg, convert, NULL);
559}
560
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000562 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200563 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200564 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 */
566 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100567eval_to_string_safe(
568 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100569 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
571 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200572 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200573 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200575 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200576 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000577 if (use_sandbox)
578 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200579 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200580 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000581 if (use_sandbox)
582 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200583 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200584 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200585 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 return retval;
587}
588
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589/*
590 * Top level evaluation function, returning a number.
591 * Evaluates "expr" silently.
592 * Returns -1 for an error.
593 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200594 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100595eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596{
Bram Moolenaar33570922005-01-25 22:26:29 +0000597 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200598 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000599 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
601 ++emsg_off;
602
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200603 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 retval = -1;
605 else
606 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100607 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000608 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610 --emsg_off;
611
612 return retval;
613}
614
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000615/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000616 * Top level evaluation function.
617 * Returns an allocated typval_T with the result.
618 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619 */
620 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200621eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000622{
623 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200624 evalarg_T evalarg;
625
626 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000627
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200628 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200629 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100630 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000631
Bram Moolenaar37c83712020-06-30 21:18:36 +0200632 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000633 return tv;
634}
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100637 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200638 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
639 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000640 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100643call_vim_function(
644 char_u *func,
645 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200646 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200647 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000649 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200650 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100652 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200653 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200654 funcexe.firstline = curwin->w_cursor.lnum;
655 funcexe.lastline = curwin->w_cursor.lnum;
656 funcexe.evaluate = TRUE;
657 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000658 if (ret == FAIL)
659 clear_tv(rettv);
660
661 return ret;
662}
663
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100664/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100665 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100666 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200667 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
668 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100669 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200670 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671call_func_retnr(
672 char_u *func,
673 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200674 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100675{
676 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200677 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100678
Bram Moolenaarded27a12018-08-01 19:06:03 +0200679 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100680 return -1;
681
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100682 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100683 clear_tv(&rettv);
684 return retval;
685}
686
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000687/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100688 * Call Vim script function like call_func_retnr() and drop the result.
689 * Returns FAIL when calling the function fails.
690 */
691 int
692call_func_noret(
693 char_u *func,
694 int argc,
695 typval_T *argv)
696{
697 typval_T rettv;
698
699 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
700 return FAIL;
701 clear_tv(&rettv);
702 return OK;
703}
704
705/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100706 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100707 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000708 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000709 */
710 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100711call_func_retstr(
712 char_u *func,
713 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200714 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000715{
716 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000717 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718
Bram Moolenaarded27a12018-08-01 19:06:03 +0200719 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000720 return NULL;
721
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100722 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000723 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 return retval;
725}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000726
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000727/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100728 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100729 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000730 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000731 */
732 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100733call_func_retlist(
734 char_u *func,
735 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200736 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000737{
738 typval_T rettv;
739
Bram Moolenaarded27a12018-08-01 19:06:03 +0200740 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000741 return NULL;
742
743 if (rettv.v_type != VAR_LIST)
744 {
745 clear_tv(&rettv);
746 return NULL;
747 }
748
749 return rettv.vval.v_list;
750}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752#ifdef FEAT_FOLDING
753/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100754 * Evaluate "arg", which is 'foldexpr'.
755 * Note: caller must set "curwin" to match "arg".
756 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
757 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 */
759 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100760eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761{
Bram Moolenaar33570922005-01-25 22:26:29 +0000762 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200763 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000765 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
766 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
768 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000769 if (use_sandbox)
770 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200771 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200773 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 retval = 0;
775 else
776 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100777 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000778 if (tv.v_type == VAR_NUMBER)
779 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000780 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 retval = 0;
782 else
783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100784 // If the result is a string, check if there is a non-digit before
785 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000786 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (!VIM_ISDIGIT(*s) && *s != '-')
788 *cp = *s++;
789 retval = atol((char *)s);
790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000791 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000794 if (use_sandbox)
795 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200796 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200797 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200799 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801#endif
802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000804 * Get an lval: variable, Dict item or List item that can be assigned a value
805 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
806 * "name.key", "name.key[expr]" etc.
807 * Indexing only works if "name" is an existing List or Dictionary.
808 * "name" points to the start of the name.
809 * If "rettv" is not NULL it points to the value to be assigned.
810 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
811 * wrong; must end in space or cmd separator.
812 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100813 * flags:
814 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100815 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100816 * GLV_NO_AUTOLOAD: do not use script autoloading
817 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000818 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000819 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000820 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200822 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100823get_lval(
824 char_u *name,
825 typval_T *rettv,
826 lval_T *lp,
827 int unlet,
828 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100829 int flags, // GLV_ values
830 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000831{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000832 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000833 char_u *expr_start, *expr_end;
834 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000835 dictitem_T *v;
836 typval_T var1;
837 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000838 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000840 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000841 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100842 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100843 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100844 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000845
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100846 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200847 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100849 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100851 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000852 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200853 lp->ll_name_end = find_name_end(name, NULL, NULL,
854 FNE_INCL_BR | fne_flags);
855 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 }
857
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100858 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000859 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000861 if (expr_start != NULL)
862 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100863 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100864 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 && *p != '[' && *p != '.')
866 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200867 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 return NULL;
869 }
870
871 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
872 if (lp->ll_exp_name == NULL)
873 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100874 // Report an invalid expression in braces, unless the
875 // expression evaluation has been cancelled due to an
876 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 if (!aborting() && !quiet)
878 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000879 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100880 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 return NULL;
882 }
883 }
884 lp->ll_name = lp->ll_exp_name;
885 }
886 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000888 lp->ll_name = name;
889
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200890 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200892 // "a: type" is declaring variable "a" with a type, not "a:".
893 if (p == name + 2 && p[-1] == ':')
894 {
895 --p;
896 lp->ll_name_end = p;
897 }
898 if (*p == ':')
899 {
900 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
901 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200903 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100904 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
905 if (lp->ll_type == NULL && !quiet)
906 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200907 lp->ll_name_end = tp;
908 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909 }
910 }
911
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100912 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
914 return p;
915
916 cc = *p;
917 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100918 // When we would write to the variable pass &ht and prevent autoload.
919 writing = !(flags & GLV_READ_ONLY);
920 v = find_var(lp->ll_name, writing ? &ht : NULL,
921 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200923 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000924 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000925 if (v == NULL)
926 return NULL;
927
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100928 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
929 {
930 if (!quiet)
931 semsg(_(e_variable_already_declared), lp->ll_name);
932 return NULL;
933 }
934
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 /*
936 * Loop until no more [idx] or .key is following.
937 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100939 var1.v_type = VAR_UNKNOWN;
940 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000942 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000943 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200944 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100945 && !(lp->ll_tv->v_type == VAR_BLOB
946 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100949 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000950 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000951 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000953 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100955 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000957 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000958
Bram Moolenaar10c65862020-10-08 21:16:42 +0200959 if (in_vim9script() && lp->ll_valtype == NULL
960 && lp->ll_tv == &v->di_tv
961 && ht != NULL && ht == get_script_local_ht())
962 {
963 svar_T *sv = find_typval_in_script(lp->ll_tv);
964
965 // Vim9 script local variable: get the type
966 if (sv != NULL)
967 lp->ll_valtype = sv->sv_type;
968 }
969
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 len = -1;
971 if (*p == '.')
972 {
973 key = p + 1;
974 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
975 ;
976 if (len == 0)
977 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100979 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 }
982 p = key + len;
983 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000984 else
985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100986 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000987 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 if (*p == ':')
989 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000990 else
991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200993 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100995 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100997 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000998 clear_tv(&var1);
999 return NULL;
1000 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001001 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 }
1003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001004 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001005 if (*p == ':')
1006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001010 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001011 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001013 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001014 if (rettv != NULL
1015 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001016 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001017 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001018 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001021 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001022 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 }
1025 p = skipwhite(p + 1);
1026 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 else
1029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001031 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001032 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001033 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001034 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001036 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001037 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001039 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001040 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001041 clear_tv(&var2);
1042 return NULL;
1043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001044 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001047 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001049
Bram Moolenaar8c711452005-01-14 21:53:12 +00001050 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001053 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001054 clear_tv(&var1);
1055 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001057 }
1058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001059 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001060 ++p;
1061 }
1062
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001063 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001064 {
1065 if (len == -1)
1066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001067 // "[key]": get key from "var1"
1068 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001069 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001072 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 }
1074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001075 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001076
1077 // a NULL dict is equivalent with an empty dict
1078 if (lp->ll_tv->vval.v_dict == NULL)
1079 {
1080 lp->ll_tv->vval.v_dict = dict_alloc();
1081 if (lp->ll_tv->vval.v_dict == NULL)
1082 {
1083 clear_tv(&var1);
1084 return NULL;
1085 }
1086 ++lp->ll_tv->vval.v_dict->dv_refcount;
1087 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001088 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001089
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001090 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 // When assigning to a scope dictionary check that a function and
1093 // variable name is valid (only variable name unless it is l: or
1094 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001095 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001096 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001097 int prevval;
1098 int wrong;
1099
1100 if (len != -1)
1101 {
1102 prevval = key[len];
1103 key[len] = NUL;
1104 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001105 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001106 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001107 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1108 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001109 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001110 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001111 if (len != -1)
1112 key[len] = prevval;
1113 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001114 {
1115 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001116 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001117 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001118 }
1119
Bram Moolenaar10c65862020-10-08 21:16:42 +02001120 if (lp->ll_valtype != NULL)
1121 // use the type of the member
1122 lp->ll_valtype = lp->ll_valtype->tt_member;
1123
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001125 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001126 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001127 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001128 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001130 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001131 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001132 return NULL;
1133 }
1134
Bram Moolenaar31b81602019-02-10 22:14:27 +01001135 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001139 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001140 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 }
1143 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001145 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001147 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 p = NULL;
1150 break;
1151 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001152 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001153 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001154 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1155 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001156 {
1157 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001158 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001159 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001160
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001161 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001164 else if (lp->ll_tv->v_type == VAR_BLOB)
1165 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001166 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1167
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001168 /*
1169 * Get the number and item for the only or first index of the List.
1170 */
1171 if (empty1)
1172 lp->ll_n1 = 0;
1173 else
1174 // is number or string
1175 lp->ll_n1 = (long)tv_get_number(&var1);
1176 clear_tv(&var1);
1177
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001178 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001180 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001181 return NULL;
1182 }
1183 if (lp->ll_range && !lp->ll_empty2)
1184 {
1185 lp->ll_n2 = (long)tv_get_number(&var2);
1186 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001187 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1188 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001189 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001190 }
1191 lp->ll_blob = lp->ll_tv->vval.v_blob;
1192 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001193 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001194 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001195 else
1196 {
1197 /*
1198 * Get the number and item for the only or first index of the List.
1199 */
1200 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001201 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001203 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001204 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001205 clear_tv(&var1);
1206
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001207 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001209 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001210 if (lp->ll_li == NULL)
1211 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001212 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001213 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001214 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 }
1217
Bram Moolenaar10c65862020-10-08 21:16:42 +02001218 if (lp->ll_valtype != NULL)
1219 // use the type of the member
1220 lp->ll_valtype = lp->ll_valtype->tt_member;
1221
Bram Moolenaar8c711452005-01-14 21:53:12 +00001222 /*
1223 * May need to find the item or absolute index for the second
1224 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 * When no index given: "lp->ll_empty2" is TRUE.
1226 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001227 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001229 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001230 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001231 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001237 {
1238 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001239 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001241 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 }
1244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001245 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 if (lp->ll_n1 < 0)
1247 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1248 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001249 {
1250 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001251 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001253 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001254 }
1255
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001257 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001258 }
1259
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001260 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 return p;
1263}
1264
1265/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001266 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001268 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001269clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001270{
1271 vim_free(lp->ll_exp_name);
1272 vim_free(lp->ll_newkey);
1273}
1274
1275/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001276 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001278 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1279 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001282set_var_lval(
1283 lval_T *lp,
1284 char_u *endp,
1285 typval_T *rettv,
1286 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001287 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1288 char_u *op,
1289 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290{
1291 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001292 listitem_T *ri;
1293 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294
1295 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001296 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001297 cc = *endp;
1298 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001299 if (lp->ll_blob != NULL)
1300 {
1301 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001302
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303 if (op != NULL && *op != '=')
1304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001305 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001306 return;
1307 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001308 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001309 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001310
1311 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1312 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001313 if (lp->ll_empty2)
1314 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1315
Bram Moolenaar68452172021-04-12 21:21:02 +02001316 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1317 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001318 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319 }
1320 else
1321 {
1322 val = (int)tv_get_number_chk(rettv, &error);
1323 if (!error)
1324 {
1325 garray_T *gap = &lp->ll_blob->bv_ga;
1326
1327 // Allow for appending a byte. Setting a byte beyond
1328 // the end is an error otherwise.
1329 if (lp->ll_n1 < gap->ga_len
1330 || (lp->ll_n1 == gap->ga_len
1331 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1332 {
1333 blob_set(lp->ll_blob, lp->ll_n1, val);
1334 if (lp->ll_n1 == gap->ga_len)
1335 ++gap->ga_len;
1336 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001337 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001338 }
1339 }
1340 }
1341 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001342 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001343 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001344
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001345 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1346 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001347 {
1348 emsg(_(e_cannot_mod));
1349 *endp = cc;
1350 return;
1351 }
1352
Bram Moolenaarff697e62019-02-12 22:28:33 +01001353 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001354 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001355 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001356 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001357 {
1358 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001359 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1360 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001361 && tv_op(&tv, rettv, op) == OK)
1362 set_var(lp->ll_name, &tv, FALSE);
1363 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001365 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001366 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001367 {
1368 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001369 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001370 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001371 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1372 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001373 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001374 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001375 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001376 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001377 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001378 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001379 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001380 else if (lp->ll_range)
1381 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001382 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001383 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001384
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001385 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1386 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001387 {
1388 emsg(_("E996: Cannot lock a range"));
1389 return;
1390 }
1391
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001392 /*
1393 * Check whether any of the list items is locked
1394 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001395 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001396 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001397 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001398 return;
1399 ri = ri->li_next;
1400 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1401 break;
1402 ll_li = ll_li->li_next;
1403 ++ll_n1;
1404 }
1405
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001406 /*
1407 * Assign the List values to the list items.
1408 */
1409 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001410 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001411 if (op != NULL && *op != '=')
1412 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1413 else
1414 {
1415 clear_tv(&lp->ll_li->li_tv);
1416 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1417 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 ri = ri->li_next;
1419 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1420 break;
1421 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001422 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001423 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001424 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001425 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001426 ri = NULL;
1427 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001428 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001429 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 lp->ll_li = lp->ll_li->li_next;
1431 ++lp->ll_n1;
1432 }
1433 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001434 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001435 else if (lp->ll_empty2
1436 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001437 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001438 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001439 }
1440 else
1441 {
1442 /*
1443 * Assign to a List or Dictionary item.
1444 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001445 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1446 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001447 {
1448 emsg(_("E996: Cannot lock a list or dict"));
1449 return;
1450 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001451
1452 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001453 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001454 return;
1455
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001456 if (lp->ll_newkey != NULL)
1457 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458 if (op != NULL && *op != '=')
1459 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001460 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001461 return;
1462 }
1463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001464 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001465 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001466 if (di == NULL)
1467 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001468 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1469 {
1470 vim_free(di);
1471 return;
1472 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001473 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001474 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001475 else if (op != NULL && *op != '=')
1476 {
1477 tv_op(lp->ll_tv, rettv, op);
1478 return;
1479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001480 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001481 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001482
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001483 /*
1484 * Assign the value to the variable or list item.
1485 */
1486 if (copy)
1487 copy_tv(rettv, lp->ll_tv);
1488 else
1489 {
1490 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001491 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001492 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001493 }
1494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495}
1496
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001497/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001498 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1499 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001500 * Returns OK or FAIL.
1501 */
1502 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001503tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001504{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001505 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 char_u numbuf[NUMBUFLEN];
1507 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001508 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001510 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001511 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001512 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001513 {
1514 switch (tv1->v_type)
1515 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001516 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001517 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001519 case VAR_DICT:
1520 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001521 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001522 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001523 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001524 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001525 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001526 break;
1527
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001528 case VAR_BLOB:
1529 if (*op != '+' || tv2->v_type != VAR_BLOB)
1530 break;
1531 // BLOB += BLOB
1532 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1533 {
1534 blob_T *b1 = tv1->vval.v_blob;
1535 blob_T *b2 = tv2->vval.v_blob;
1536 int i, len = blob_len(b2);
1537 for (i = 0; i < len; i++)
1538 ga_append(&b1->bv_ga, blob_get(b2, i));
1539 }
1540 return OK;
1541
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001542 case VAR_LIST:
1543 if (*op != '+' || tv2->v_type != VAR_LIST)
1544 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001545 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001546 if (tv2->vval.v_list != NULL)
1547 {
1548 if (tv1->vval.v_list == NULL)
1549 {
1550 tv1->vval.v_list = tv2->vval.v_list;
1551 ++tv1->vval.v_list->lv_refcount;
1552 }
1553 else
1554 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1555 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001556 return OK;
1557
1558 case VAR_NUMBER:
1559 case VAR_STRING:
1560 if (tv2->v_type == VAR_LIST)
1561 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001562 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001563 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001564 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001565 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001566#ifdef FEAT_FLOAT
1567 if (tv2->v_type == VAR_FLOAT)
1568 {
1569 float_T f = n;
1570
Bram Moolenaarff697e62019-02-12 22:28:33 +01001571 if (*op == '%')
1572 break;
1573 switch (*op)
1574 {
1575 case '+': f += tv2->vval.v_float; break;
1576 case '-': f -= tv2->vval.v_float; break;
1577 case '*': f *= tv2->vval.v_float; break;
1578 case '/': f /= tv2->vval.v_float; break;
1579 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001580 clear_tv(tv1);
1581 tv1->v_type = VAR_FLOAT;
1582 tv1->vval.v_float = f;
1583 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001584 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001585#endif
1586 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001587 switch (*op)
1588 {
1589 case '+': n += tv_get_number(tv2); break;
1590 case '-': n -= tv_get_number(tv2); break;
1591 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001592 case '/': n = num_divide(n, tv_get_number(tv2),
1593 &failed); break;
1594 case '%': n = num_modulus(n, tv_get_number(tv2),
1595 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001596 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001597 clear_tv(tv1);
1598 tv1->v_type = VAR_NUMBER;
1599 tv1->vval.v_number = n;
1600 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001601 }
1602 else
1603 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001604 if (tv2->v_type == VAR_FLOAT)
1605 break;
1606
Bram Moolenaarff697e62019-02-12 22:28:33 +01001607 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001608 s = tv_get_string(tv1);
1609 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001610 clear_tv(tv1);
1611 tv1->v_type = VAR_STRING;
1612 tv1->vval.v_string = s;
1613 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001614 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001615
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001616 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001617#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618 {
1619 float_T f;
1620
Bram Moolenaarff697e62019-02-12 22:28:33 +01001621 if (*op == '%' || *op == '.'
1622 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623 && tv2->v_type != VAR_NUMBER
1624 && tv2->v_type != VAR_STRING))
1625 break;
1626 if (tv2->v_type == VAR_FLOAT)
1627 f = tv2->vval.v_float;
1628 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001629 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001630 switch (*op)
1631 {
1632 case '+': tv1->vval.v_float += f; break;
1633 case '-': tv1->vval.v_float -= f; break;
1634 case '*': tv1->vval.v_float *= f; break;
1635 case '/': tv1->vval.v_float /= f; break;
1636 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001637 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001638#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001639 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001640 }
1641 }
1642
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001643 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 return FAIL;
1645}
1646
1647/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648 * Evaluate the expression used in a ":for var in expr" command.
1649 * "arg" points to "var".
1650 * Set "*errp" to TRUE for an error, FALSE otherwise;
1651 * Return a pointer that holds the info. Null when there is an error.
1652 */
1653 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001654eval_for_line(
1655 char_u *arg,
1656 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001657 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001658 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659{
Bram Moolenaar33570922005-01-25 22:26:29 +00001660 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001662 typval_T tv;
1663 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001664 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001666 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001668 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 if (fi == NULL)
1670 return NULL;
1671
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001672 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 if (expr == NULL)
1674 return fi;
1675
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001676 expr = skipwhite_and_linebreak(expr, evalarg);
1677 if (expr[0] != 'i' || expr[1] != 'n'
1678 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 return fi;
1682 }
1683
1684 if (skip)
1685 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001686 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1687 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 {
1689 *errp = FALSE;
1690 if (!skip)
1691 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001692 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001693 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001694 l = tv.vval.v_list;
1695 if (l == NULL)
1696 {
1697 // a null list is like an empty list: do nothing
1698 clear_tv(&tv);
1699 }
1700 else
1701 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001703 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001705 // No need to increment the refcount, it's already set for
1706 // the list being used in "tv".
1707 fi->fi_list = l;
1708 list_add_watch(l, &fi->fi_lw);
1709 fi->fi_lw.lw_item = l->lv_first;
1710 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001711 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001712 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001713 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001714 fi->fi_bi = 0;
1715 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001716 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001717 typval_T btv;
1718
1719 // Make a copy, so that the iteration still works when the
1720 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001722 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001723 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001724 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001725 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001726 else if (tv.v_type == VAR_STRING)
1727 {
1728 fi->fi_byte_idx = 0;
1729 fi->fi_string = tv.vval.v_string;
1730 tv.vval.v_string = NULL;
1731 if (fi->fi_string == NULL)
1732 fi->fi_string = vim_strsave((char_u *)"");
1733 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001734 else
1735 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001736 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001737 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001738 }
1739 }
1740 }
1741 if (skip)
1742 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001743 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744
1745 return fi;
1746}
1747
1748/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001749 * Used when looping over a :for line, skip the "in expr" part.
1750 */
1751 void
1752skip_for_lines(void *fi_void, evalarg_T *evalarg)
1753{
1754 forinfo_T *fi = (forinfo_T *)fi_void;
1755 int i;
1756
1757 for (i = 0; i < fi->fi_break_count; ++i)
1758 eval_next_line(evalarg);
1759}
1760
1761/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762 * Use the first item in a ":for" list. Advance to the next.
1763 * Assign the values to the variable (list). "arg" points to the first one.
1764 * Return TRUE when a valid item was found, FALSE when at end of list or
1765 * something wrong.
1766 */
1767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001768next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001770 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001772 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1773 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1774 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001775 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001777 if (fi->fi_blob != NULL)
1778 {
1779 typval_T tv;
1780
1781 if (fi->fi_bi >= blob_len(fi->fi_blob))
1782 return FALSE;
1783 tv.v_type = VAR_NUMBER;
1784 tv.v_lock = VAR_FIXED;
1785 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1786 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001787 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001788 fi->fi_varcount, flag, NULL) == OK;
1789 }
1790
1791 if (fi->fi_string != NULL)
1792 {
1793 typval_T tv;
1794 int len;
1795
1796 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1797 if (len == 0)
1798 return FALSE;
1799 tv.v_type = VAR_STRING;
1800 tv.v_lock = VAR_FIXED;
1801 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1802 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001803 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001804 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001805 vim_free(tv.vval.v_string);
1806 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001807 }
1808
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 item = fi->fi_lw.lw_item;
1810 if (item == NULL)
1811 result = FALSE;
1812 else
1813 {
1814 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001815 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001816 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001817 }
1818 return result;
1819}
1820
1821/*
1822 * Free the structure used to store info used by ":for".
1823 */
1824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001825free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001826{
Bram Moolenaar33570922005-01-25 22:26:29 +00001827 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001829 if (fi == NULL)
1830 return;
1831 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001832 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001833 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001834 list_unref(fi->fi_list);
1835 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001836 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001837 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001838 else
1839 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 vim_free(fi);
1841}
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001844set_context_for_expression(
1845 expand_T *xp,
1846 char_u *arg,
1847 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001849 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001853 if (cmdidx == CMD_let || cmdidx == CMD_var
1854 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 {
1856 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001857 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001859 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001860 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 {
1862 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001863 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001864 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 break;
1866 }
1867 return;
1868 }
1869 }
1870 else
1871 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1872 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 while ((xp->xp_pattern = vim_strpbrk(arg,
1874 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1875 {
1876 c = *xp->xp_pattern;
1877 if (c == '&')
1878 {
1879 c = xp->xp_pattern[1];
1880 if (c == '&')
1881 {
1882 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001883 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 }
1885 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001888 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1889 xp->xp_pattern += 2;
1890
1891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 }
1893 else if (c == '$')
1894 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001895 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 xp->xp_context = EXPAND_ENV_VARS;
1897 }
1898 else if (c == '=')
1899 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001900 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 xp->xp_context = EXPAND_EXPRESSION;
1902 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001903 else if (c == '#'
1904 && xp->xp_context == EXPAND_EXPRESSION)
1905 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001906 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001907 break;
1908 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001909 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 && xp->xp_context == EXPAND_FUNCTIONS
1911 && vim_strchr(xp->xp_pattern, '(') == NULL)
1912 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001913 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 break;
1915 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001916 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001918 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
1920 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1921 if (c == '\\' && xp->xp_pattern[1] != NUL)
1922 ++xp->xp_pattern;
1923 xp->xp_context = EXPAND_NOTHING;
1924 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001925 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001927 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1929 /* skip */ ;
1930 xp->xp_context = EXPAND_NOTHING;
1931 }
1932 else if (c == '|')
1933 {
1934 if (xp->xp_pattern[1] == '|')
1935 {
1936 ++xp->xp_pattern;
1937 xp->xp_context = EXPAND_EXPRESSION;
1938 }
1939 else
1940 xp->xp_context = EXPAND_COMMANDS;
1941 }
1942 else
1943 xp->xp_context = EXPAND_EXPRESSION;
1944 }
1945 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001946 // Doesn't look like something valid, expand as an expression
1947 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 arg = xp->xp_pattern;
1950 if (*arg != NUL)
1951 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1952 /* skip */ ;
1953 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001954
1955 // ":exe one two" completes "two"
1956 if ((cmdidx == CMD_execute
1957 || cmdidx == CMD_echo
1958 || cmdidx == CMD_echon
1959 || cmdidx == CMD_echomsg)
1960 && xp->xp_context == EXPAND_EXPRESSION)
1961 {
1962 for (;;)
1963 {
1964 char_u *n = skiptowhite(arg);
1965
1966 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1967 break;
1968 arg = skipwhite(n);
1969 }
1970 }
1971
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 xp->xp_pattern = arg;
1973}
1974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001976 * Return TRUE if "pat" matches "text".
1977 * Does not use 'cpo' and always uses 'magic'.
1978 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001979 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001980pattern_match(char_u *pat, char_u *text, int ic)
1981{
1982 int matches = FALSE;
1983 char_u *save_cpo;
1984 regmatch_T regmatch;
1985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001986 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001987 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001988 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001989 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1990 if (regmatch.regprog != NULL)
1991 {
1992 regmatch.rm_ic = ic;
1993 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1994 vim_regfree(regmatch.regprog);
1995 }
1996 p_cpo = save_cpo;
1997 return matches;
1998}
1999
2000/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002001 * Handle a name followed by "(". Both for just "name(arg)" and for
2002 * "expr->name(arg)".
2003 * Returns OK or FAIL.
2004 */
2005 static int
2006eval_func(
2007 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002008 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002009 char_u *name,
2010 int name_len,
2011 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002012 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002013 typval_T *basetv) // "expr" for "expr->name(arg)"
2014{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002015 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002016 char_u *s = name;
2017 int len = name_len;
2018 partial_T *partial;
2019 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002020 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002021
2022 if (!evaluate)
2023 check_vars(s, len);
2024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002025 // If "s" is the name of a variable of type VAR_FUNC
2026 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002027 s = deref_func_name(s, &len, &partial,
2028 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002029
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002030 // Need to make a copy, in case evaluating the arguments makes
2031 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002032 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002033 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002034 ret = FAIL;
2035 else
2036 {
2037 funcexe_T funcexe;
2038
2039 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002040 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002041 funcexe.firstline = curwin->w_cursor.lnum;
2042 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002043 funcexe.evaluate = evaluate;
2044 funcexe.partial = partial;
2045 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002046 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002047 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002048 }
2049 vim_free(s);
2050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002051 // If evaluate is FALSE rettv->v_type was not set in
2052 // get_func_tv, but it's needed in handle_subscript() to parse
2053 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002054 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2055 {
2056 rettv->vval.v_string = NULL;
2057 rettv->v_type = VAR_FUNC;
2058 }
2059
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002060 // Stop the expression evaluation when immediately
2061 // aborting on error, or when an interrupt occurred or
2062 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002063 if (evaluate && aborting())
2064 {
2065 if (ret == OK)
2066 clear_tv(rettv);
2067 ret = FAIL;
2068 }
2069 return ret;
2070}
2071
2072/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002073 * Get the next line source line without advancing. But do skip over comment
2074 * lines.
2075 */
2076 static char_u *
2077getline_peek_skip_comments(evalarg_T *evalarg)
2078{
2079 for (;;)
2080 {
2081 char_u *next = getline_peek(evalarg->eval_getline,
2082 evalarg->eval_cookie);
2083 char_u *p;
2084
2085 if (next == NULL)
2086 break;
2087 p = skipwhite(next);
2088 if (*p != NUL && !vim9_comment_start(p))
2089 return next;
2090 (void)eval_next_line(evalarg);
2091 }
2092 return NULL;
2093}
2094
2095/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002096 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2097 * comment) and there is a next line, return the next line (skipping blanks)
2098 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002099 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2100 * "arg" must point somewhere inside a line, not at the start.
2101 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002102 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002103eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2104{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002105 char_u *p = skipwhite(arg);
2106
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002107 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002108 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002109 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002110 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002111 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002112 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002113 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002114
2115 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002116 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002117 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002118 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002119
Bram Moolenaar9d489562020-07-30 20:08:50 +02002120 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002121 {
2122 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002123 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002124 }
2125 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002126 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002127}
2128
2129/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002130 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002131 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002132 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002133eval_next_line(evalarg_T *evalarg)
2134{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002135 garray_T *gap = &evalarg->eval_ga;
2136 char_u *line;
2137
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002138 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002139 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2140 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002141 else
2142 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002143 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002144 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2145 {
2146 // Going to concatenate the lines after parsing.
2147 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2148 ++gap->ga_len;
2149 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002150 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002151 {
2152 vim_free(evalarg->eval_tofree);
2153 evalarg->eval_tofree = line;
2154 }
2155 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002156}
2157
Bram Moolenaare6b53242020-07-01 17:28:33 +02002158/*
2159 * Call eval_next_non_blank() and get the next line if needed.
2160 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002161 char_u *
2162skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2163{
2164 int getnext;
2165 char_u *p = skipwhite(arg);
2166
Bram Moolenaar962d7212020-07-04 14:15:00 +02002167 if (evalarg == NULL)
2168 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002169 eval_next_non_blank(p, evalarg, &getnext);
2170 if (getnext)
2171 return eval_next_line(evalarg);
2172 return p;
2173}
2174
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002175/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002176 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002177 */
2178 void
2179clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2180{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002181 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002182 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002183 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002184 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002185 if (eap != NULL)
2186 {
2187 // We may need to keep the original command line, e.g. for
2188 // ":let" it has the variable names. But we may also need the
2189 // new one, "nextcmd" points into it. Keep both.
2190 vim_free(eap->cmdline_tofree);
2191 eap->cmdline_tofree = *eap->cmdlinep;
2192 *eap->cmdlinep = evalarg->eval_tofree;
2193 }
2194 else
2195 vim_free(evalarg->eval_tofree);
2196 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002197 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002198
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002199 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2200 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002201 }
2202}
2203
2204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2208 */
2209
2210/*
2211 * Handle zero level expression.
2212 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002214 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002215 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 * Return OK or FAIL.
2217 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002218 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002219eval0(
2220 char_u *arg,
2221 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002222 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002223 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224{
2225 int ret;
2226 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002227 int did_emsg_before = did_emsg;
2228 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002229 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230
2231 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002232 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002233 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002234
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002235 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 /*
2240 * Report the invalid expression unless the expression evaluation has
2241 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002242 * exception, or we already gave a more specific error.
2243 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002245 if (!aborting()
2246 && did_emsg == did_emsg_before
2247 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002248 && (flags & EVAL_CONSTANT) == 0
2249 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002250 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002251
2252 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002253 // a next command to avoid more errors, unless "|" is following, which
2254 // could only be a command separator.
2255 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2256 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002257 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002259
2260 if (eap != NULL)
2261 eap->nextcmd = check_nextcmd(p);
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 return ret;
2264}
2265
2266/*
2267 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002268 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002269 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 *
2271 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002272 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002274 * Note: "rettv.v_lock" is not set.
2275 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 * Return OK or FAIL.
2277 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002278 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002279eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002281 char_u *p;
2282 int getnext;
2283
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002284 CLEAR_POINTER(rettv);
2285
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 /*
2287 * Get the first variable.
2288 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002289 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 return FAIL;
2291
Bram Moolenaar793648f2020-06-26 21:28:25 +02002292 p = eval_next_non_blank(*arg, evalarg, &getnext);
2293 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002295 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002296 int result;
2297 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002298 evalarg_T *evalarg_used = evalarg;
2299 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002300 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002301 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002302 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002303
2304 if (evalarg == NULL)
2305 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002306 CLEAR_FIELD(local_evalarg);
2307 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002308 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002309 orig_flags = evalarg_used->eval_flags;
2310 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002311
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002312 if (getnext)
2313 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002314 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002315 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002316 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002317 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002318 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002319 clear_tv(rettv);
2320 return FAIL;
2321 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002322 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002323 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002324
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002326 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002328 int error = FALSE;
2329
Bram Moolenaar13106602020-10-04 16:06:05 +02002330 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002331 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002332 else if (vim9script)
2333 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002334 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002336 if (error || !op_falsy || !result)
2337 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 if (error)
2339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341
2342 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002343 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002345 if (op_falsy)
2346 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002347 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002348 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002349 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002350 clear_tv(rettv);
2351 return FAIL;
2352 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002353 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002354 evalarg_used->eval_flags = (op_falsy ? !result : result)
2355 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2356 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002357 {
2358 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002360 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002361 if (!op_falsy || !result)
2362 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002364 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002366 /*
2367 * Check for the ":".
2368 */
2369 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2370 if (*p != ':')
2371 {
2372 emsg(_(e_missing_colon));
2373 if (evaluate && result)
2374 clear_tv(rettv);
2375 evalarg_used->eval_flags = orig_flags;
2376 return FAIL;
2377 }
2378 if (getnext)
2379 *arg = eval_next_line(evalarg_used);
2380 else
2381 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002382 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002383 {
2384 error_white_both(p, 1);
2385 clear_tv(rettv);
2386 evalarg_used->eval_flags = orig_flags;
2387 return FAIL;
2388 }
2389 *arg = p;
2390 }
2391
2392 /*
2393 * Get the third variable. Recursive!
2394 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002395 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002396 {
2397 error_white_both(p, 1);
2398 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002399 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002400 return FAIL;
2401 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002402 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2403 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002404 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002405 if (eval1(arg, &var2, evalarg_used) == FAIL)
2406 {
2407 if (evaluate && result)
2408 clear_tv(rettv);
2409 evalarg_used->eval_flags = orig_flags;
2410 return FAIL;
2411 }
2412 if (evaluate && !result)
2413 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002415
2416 if (evalarg == NULL)
2417 clear_evalarg(&local_evalarg, NULL);
2418 else
2419 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 }
2421
2422 return OK;
2423}
2424
2425/*
2426 * Handle first level expression:
2427 * expr2 || expr2 || expr2 logical OR
2428 *
2429 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002430 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 *
2432 * Return OK or FAIL.
2433 */
2434 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002435eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002437 char_u *p;
2438 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440 /*
2441 * Get the first variable.
2442 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002443 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 return FAIL;
2445
2446 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002447 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002449 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002450 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002452 evalarg_T *evalarg_used = evalarg;
2453 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002454 int evaluate;
2455 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002456 long result = FALSE;
2457 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002458 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002459 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002460
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002461 if (evalarg == NULL)
2462 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002463 CLEAR_FIELD(local_evalarg);
2464 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002466 orig_flags = evalarg_used->eval_flags;
2467 evaluate = orig_flags & EVAL_EVALUATE;
2468 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002470 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002471 result = tv_get_bool_chk(rettv, &error);
2472 else if (tv_get_number_chk(rettv, &error) != 0)
2473 result = TRUE;
2474 clear_tv(rettv);
2475 if (error)
2476 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 }
2478
2479 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002480 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002482 while (p[0] == '|' && p[1] == '|')
2483 {
2484 if (getnext)
2485 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002486 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002487 {
2488 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2489 {
2490 error_white_both(p, 2);
2491 clear_tv(rettv);
2492 return FAIL;
2493 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002494 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002495 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002496
2497 /*
2498 * Get the second variable.
2499 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002500 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2501 {
2502 error_white_both(p, 2);
2503 clear_tv(rettv);
2504 return FAIL;
2505 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002506 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2507 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002508 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002509 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002510 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002511
2512 /*
2513 * Compute the result.
2514 */
2515 if (evaluate && !result)
2516 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002517 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002518 result = tv_get_bool_chk(&var2, &error);
2519 else if (tv_get_number_chk(&var2, &error) != 0)
2520 result = TRUE;
2521 clear_tv(&var2);
2522 if (error)
2523 return FAIL;
2524 }
2525 if (evaluate)
2526 {
2527 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002528 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002529 rettv->v_type = VAR_BOOL;
2530 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002531 }
2532 else
2533 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002534 rettv->v_type = VAR_NUMBER;
2535 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002536 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002537 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002538
2539 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002541
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002542 if (evalarg == NULL)
2543 clear_evalarg(&local_evalarg, NULL);
2544 else
2545 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
2547
2548 return OK;
2549}
2550
2551/*
2552 * Handle second level expression:
2553 * expr3 && expr3 && expr3 logical AND
2554 *
2555 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002556 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 *
2558 * Return OK or FAIL.
2559 */
2560 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002561eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002563 char_u *p;
2564 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
2566 /*
2567 * Get the first variable.
2568 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002569 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 return FAIL;
2571
2572 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002573 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002575 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002576 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002578 evalarg_T *evalarg_used = evalarg;
2579 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002580 int orig_flags;
2581 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 long result = TRUE;
2583 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002584 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002585 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002586
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002587 if (evalarg == NULL)
2588 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002589 CLEAR_FIELD(local_evalarg);
2590 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002591 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002592 orig_flags = evalarg_used->eval_flags;
2593 evaluate = orig_flags & EVAL_EVALUATE;
2594 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002595 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002596 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002597 result = tv_get_bool_chk(rettv, &error);
2598 else if (tv_get_number_chk(rettv, &error) == 0)
2599 result = FALSE;
2600 clear_tv(rettv);
2601 if (error)
2602 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
2604
2605 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002606 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002608 while (p[0] == '&' && p[1] == '&')
2609 {
2610 if (getnext)
2611 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002612 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002613 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002614 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002615 {
2616 error_white_both(p, 2);
2617 clear_tv(rettv);
2618 return FAIL;
2619 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002620 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002621 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622
2623 /*
2624 * Get the second variable.
2625 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002626 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2627 {
2628 error_white_both(p, 2);
2629 clear_tv(rettv);
2630 return FAIL;
2631 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002632 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2633 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002634 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002635 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002636 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002637 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002638
2639 /*
2640 * Compute the result.
2641 */
2642 if (evaluate && result)
2643 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002644 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002645 result = tv_get_bool_chk(&var2, &error);
2646 else if (tv_get_number_chk(&var2, &error) == 0)
2647 result = FALSE;
2648 clear_tv(&var2);
2649 if (error)
2650 return FAIL;
2651 }
2652 if (evaluate)
2653 {
2654 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002655 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002656 rettv->v_type = VAR_BOOL;
2657 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002658 }
2659 else
2660 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002661 rettv->v_type = VAR_NUMBER;
2662 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002663 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002664 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002665
2666 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002668
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002669 if (evalarg == NULL)
2670 clear_evalarg(&local_evalarg, NULL);
2671 else
2672 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 }
2674
2675 return OK;
2676}
2677
2678/*
2679 * Handle third level expression:
2680 * var1 == var2
2681 * var1 =~ var2
2682 * var1 != var2
2683 * var1 !~ var2
2684 * var1 > var2
2685 * var1 >= var2
2686 * var1 < var2
2687 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002688 * var1 is var2
2689 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 *
2691 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002692 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 *
2694 * Return OK or FAIL.
2695 */
2696 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002697eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002700 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002701 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002703 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 /*
2706 * Get the first variable.
2707 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002708 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 return FAIL;
2710
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002711 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002712 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
2714 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002715 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002717 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002719 typval_T var2;
2720 int ic;
2721 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002722 int evaluate = evalarg == NULL
2723 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002724
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002725 if (getnext)
2726 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002727 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2728 {
2729 error_white_both(p, len);
2730 clear_tv(rettv);
2731 return FAIL;
2732 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002733
Bram Moolenaar696ba232020-07-29 21:20:41 +02002734 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2735 {
2736 semsg(_(e_invexpr2), p);
2737 clear_tv(rettv);
2738 return FAIL;
2739 }
2740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002741 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if (p[len] == '?')
2743 {
2744 ic = TRUE;
2745 ++len;
2746 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002747 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 else if (p[len] == '#')
2749 {
2750 ic = FALSE;
2751 ++len;
2752 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002753 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002755 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757 /*
2758 * Get the second variable.
2759 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002760 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2761 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002762 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002763 clear_tv(rettv);
2764 return FAIL;
2765 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002766 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002767 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002769 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 return FAIL;
2771 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002772 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002773 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002774 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002775
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002776 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002777 {
2778 ret = FAIL;
2779 clear_tv(rettv);
2780 }
2781 else
2782 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002783 clear_tv(&var2);
2784 return ret;
2785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
2787
2788 return OK;
2789}
2790
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002791/*
2792 * Make a copy of blob "tv1" and append blob "tv2".
2793 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 void
2795eval_addblob(typval_T *tv1, typval_T *tv2)
2796{
2797 blob_T *b1 = tv1->vval.v_blob;
2798 blob_T *b2 = tv2->vval.v_blob;
2799 blob_T *b = blob_alloc();
2800 int i;
2801
2802 if (b != NULL)
2803 {
2804 for (i = 0; i < blob_len(b1); i++)
2805 ga_append(&b->bv_ga, blob_get(b1, i));
2806 for (i = 0; i < blob_len(b2); i++)
2807 ga_append(&b->bv_ga, blob_get(b2, i));
2808
2809 clear_tv(tv1);
2810 rettv_blob_set(tv1, b);
2811 }
2812}
2813
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002814/*
2815 * Make a copy of list "tv1" and append list "tv2".
2816 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 int
2818eval_addlist(typval_T *tv1, typval_T *tv2)
2819{
2820 typval_T var3;
2821
2822 // concatenate Lists
2823 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2824 {
2825 clear_tv(tv1);
2826 clear_tv(tv2);
2827 return FAIL;
2828 }
2829 clear_tv(tv1);
2830 *tv1 = var3;
2831 return OK;
2832}
2833
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834/*
2835 * Handle fourth level expression:
2836 * + number addition
2837 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002838 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002839 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 *
2841 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002842 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 *
2844 * Return OK or FAIL.
2845 */
2846 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002847eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 /*
2850 * Get the first variable.
2851 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002852 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 return FAIL;
2854
2855 /*
2856 * Repeat computing, until no '+', '-' or '.' is following.
2857 */
2858 for (;;)
2859 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002860 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002861 int getnext;
2862 char_u *p;
2863 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002864 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002865 int concat;
2866 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002867 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002868
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002869 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002870 // "+=", "-=" and "..=" are assignments
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002871 p = eval_next_non_blank(*arg, evalarg, &getnext);
2872 op = *p;
2873 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002874 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2875 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002877
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002878 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002879 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002880 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002881 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002882 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002883 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002884 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002885 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002886 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002887 clear_tv(rettv);
2888 return FAIL;
2889 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002890 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002891 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002892 if ((op != '+' || (rettv->v_type != VAR_LIST
2893 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002894#ifdef FEAT_FLOAT
2895 && (op == '.' || rettv->v_type != VAR_FLOAT)
2896#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002897 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002898 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002899 int error = FALSE;
2900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002901 // For "list + ...", an illegal use of the first operand as
2902 // a number cannot be determined before evaluating the 2nd
2903 // operand: if this is also a list, all is ok.
2904 // For "something . ...", "something - ..." or "non-list + ...",
2905 // we know that the first operand needs to be a string or number
2906 // without evaluating the 2nd operand. So check before to avoid
2907 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002908 if (op != '.')
2909 tv_get_number_chk(rettv, &error);
2910 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002911 {
2912 clear_tv(rettv);
2913 return FAIL;
2914 }
2915 }
2916
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 /*
2918 * Get the second variable.
2919 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002920 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002921 {
2922 error_white_both(p, oplen);
2923 clear_tv(rettv);
2924 return FAIL;
2925 }
2926 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002927 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 return FAIL;
2931 }
2932
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002933 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
2935 /*
2936 * Compute the result.
2937 */
2938 if (op == '.')
2939 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002940 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2941 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002942 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002943
Bram Moolenaar2e086612020-08-25 22:37:48 +02002944 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002945 || var2.v_type == VAR_CHANNEL
2946 || var2.v_type == VAR_JOB))
2947 emsg(_(e_inval_string));
2948#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002949 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002950 {
2951 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2952 var2.vval.v_float);
2953 s2 = buf2;
2954 }
2955#endif
2956 else
2957 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002958 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002959 {
2960 clear_tv(rettv);
2961 clear_tv(&var2);
2962 return FAIL;
2963 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002964 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002965 clear_tv(rettv);
2966 rettv->v_type = VAR_STRING;
2967 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002969 else if (op == '+' && rettv->v_type == VAR_BLOB
2970 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002972 else if (op == '+' && rettv->v_type == VAR_LIST
2973 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002974 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002976 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 else
2979 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002980 int error = FALSE;
2981 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002982#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002983 float_T f1 = 0, f2 = 0;
2984
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002986 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002987 f1 = rettv->vval.v_float;
2988 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002989 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002990 else
2991#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002992 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002993 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002994 if (error)
2995 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002996 // This can only happen for "list + non-list" or
2997 // "blob + non-blob". For "non-list + ..." or
2998 // "something - ...", we returned before evaluating the
2999 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003000 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003001 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003002 return FAIL;
3003 }
3004#ifdef FEAT_FLOAT
3005 if (var2.v_type == VAR_FLOAT)
3006 f1 = n1;
3007#endif
3008 }
3009#ifdef FEAT_FLOAT
3010 if (var2.v_type == VAR_FLOAT)
3011 {
3012 f2 = var2.vval.v_float;
3013 n2 = 0;
3014 }
3015 else
3016#endif
3017 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003018 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019 if (error)
3020 {
3021 clear_tv(rettv);
3022 clear_tv(&var2);
3023 return FAIL;
3024 }
3025#ifdef FEAT_FLOAT
3026 if (rettv->v_type == VAR_FLOAT)
3027 f2 = n2;
3028#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003029 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003030 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003031
3032#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003033 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003034 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3035 {
3036 if (op == '+')
3037 f1 = f1 + f2;
3038 else
3039 f1 = f1 - f2;
3040 rettv->v_type = VAR_FLOAT;
3041 rettv->vval.v_float = f1;
3042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003044#endif
3045 {
3046 if (op == '+')
3047 n1 = n1 + n2;
3048 else
3049 n1 = n1 - n2;
3050 rettv->v_type = VAR_NUMBER;
3051 rettv->vval.v_number = n1;
3052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 }
3056 }
3057 return OK;
3058}
3059
3060/*
3061 * Handle fifth level expression:
3062 * * number multiplication
3063 * / number division
3064 * % number modulo
3065 *
3066 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003067 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 *
3069 * Return OK or FAIL.
3070 */
3071 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003072eval6(
3073 char_u **arg,
3074 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003075 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003076 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003079 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081
3082 /*
3083 * Get the first variable.
3084 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003085 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 return FAIL;
3087
3088 /*
3089 * Repeat computing, until no '*', '/' or '%' is following.
3090 */
3091 for (;;)
3092 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003093 int evaluate;
3094 int getnext;
3095 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003096 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003097 int op;
3098 varnumber_T n1, n2;
3099#ifdef FEAT_FLOAT
3100 float_T f1, f2;
3101#endif
3102 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003103
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003104 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003105 p = eval_next_non_blank(*arg, evalarg, &getnext);
3106 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003107 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003109
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003110 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003111 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003112 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003113 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003114 {
3115 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3116 {
3117 error_white_both(p, 1);
3118 clear_tv(rettv);
3119 return FAIL;
3120 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003121 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003124#ifdef FEAT_FLOAT
3125 f1 = 0;
3126 f2 = 0;
3127#endif
3128 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003129 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003131#ifdef FEAT_FLOAT
3132 if (rettv->v_type == VAR_FLOAT)
3133 {
3134 f1 = rettv->vval.v_float;
3135 use_float = TRUE;
3136 n1 = 0;
3137 }
3138 else
3139#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003140 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003141 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003142 if (error)
3143 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 }
3145 else
3146 n1 = 0;
3147
3148 /*
3149 * Get the second variable.
3150 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003151 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3152 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003153 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003154 clear_tv(rettv);
3155 return FAIL;
3156 }
3157 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003158 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 return FAIL;
3160
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003161 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003163#ifdef FEAT_FLOAT
3164 if (var2.v_type == VAR_FLOAT)
3165 {
3166 if (!use_float)
3167 {
3168 f1 = n1;
3169 use_float = TRUE;
3170 }
3171 f2 = var2.vval.v_float;
3172 n2 = 0;
3173 }
3174 else
3175#endif
3176 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003177 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003178 clear_tv(&var2);
3179 if (error)
3180 return FAIL;
3181#ifdef FEAT_FLOAT
3182 if (use_float)
3183 f2 = n2;
3184#endif
3185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187 /*
3188 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003191#ifdef FEAT_FLOAT
3192 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003194 if (op == '*')
3195 f1 = f1 * f2;
3196 else if (op == '/')
3197 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003198# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003199 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003200 if (f2 == 0.0)
3201 {
3202 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003203 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003204 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003205 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003206 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003207 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003208 }
3209 else
3210 f1 = f1 / f2;
3211# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003212 // We rely on the floating point library to handle divide
3213 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003214 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003215# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003218 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003220 return FAIL;
3221 }
3222 rettv->v_type = VAR_FLOAT;
3223 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 }
3225 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003228 int failed = FALSE;
3229
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003230 if (op == '*')
3231 n1 = n1 * n2;
3232 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003233 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003235 n1 = num_modulus(n1, n2, &failed);
3236 if (failed)
3237 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003238
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003239 rettv->v_type = VAR_NUMBER;
3240 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 }
3243 }
3244
3245 return OK;
3246}
3247
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003248 int
3249eval_leader(char_u **arg, int vim9)
3250{
3251 char_u *s = *arg;
3252 char_u *p = *arg;
3253
3254 while (*p == '!' || *p == '-' || *p == '+')
3255 {
3256 char_u *n = skipwhite(p + 1);
3257
3258 // ++, --, -+ and +- are not accepted in Vim9 script
3259 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3260 {
3261 semsg(_(e_invexpr2), s);
3262 return FAIL;
3263 }
3264 p = n;
3265 }
3266 *arg = p;
3267 return OK;
3268}
3269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270/*
3271 * Handle sixth level expression:
3272 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003273 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003274 * "string" string constant
3275 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 * &option-name option value
3277 * @r register contents
3278 * identifier variable value
3279 * function() function call
3280 * $VAR environment variable
3281 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003282 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003284 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003285 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 *
3287 * Also handle:
3288 * ! in front logical NOT
3289 * - in front unary minus
3290 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003291 * trailing [] subscript in String or List
3292 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003293 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 *
3295 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003296 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 *
3298 * Return OK or FAIL.
3299 */
3300 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003301eval7(
3302 char_u **arg,
3303 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003304 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003307 int evaluate = evalarg != NULL
3308 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 int len;
3310 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 char_u *start_leader, *end_leader;
3312 int ret = OK;
3313 char_u *alias;
3314
3315 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003316 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003317 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003319 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003322 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 */
3324 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003325 if (eval_leader(arg, in_vim9script()) == FAIL)
3326 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 end_leader = *arg;
3328
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003329 if (**arg == '.' && (!isdigit(*(*arg + 1))
3330#ifdef FEAT_FLOAT
3331 || current_sctx.sc_version < 2
3332#endif
3333 ))
3334 {
3335 semsg(_(e_invexpr2), *arg);
3336 ++*arg;
3337 return FAIL;
3338 }
3339
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 switch (**arg)
3341 {
3342 /*
3343 * Number constant.
3344 */
3345 case '0':
3346 case '1':
3347 case '2':
3348 case '3':
3349 case '4':
3350 case '5':
3351 case '6':
3352 case '7':
3353 case '8':
3354 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003355 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003356
3357 // Apply prefixed "-" and "+" now. Matters especially when
3358 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003359 if (ret == OK && evaluate && end_leader > start_leader
3360 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003361 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 break;
3363
3364 /*
3365 * String constant: "string".
3366 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003367 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 break;
3369
3370 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003371 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003373 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003374 break;
3375
3376 /*
3377 * List: [expr, expr]
3378 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003379 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 break;
3381
3382 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003383 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003384 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003385 case '#': if (in_vim9script())
3386 {
3387 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3388 }
3389 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003390 {
3391 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003392 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003393 }
3394 else
3395 ret = NOTDONE;
3396 break;
3397
3398 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003399 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003400 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003401 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003402 case '{': if (in_vim9script())
3403 ret = NOTDONE;
3404 else
3405 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003406 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003407 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003408 break;
3409
3410 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003411 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003413 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 break;
3415
3416 /*
3417 * Environment variable: $VAR.
3418 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003419 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 break;
3421
3422 /*
3423 * Register contents: @r.
3424 */
3425 case '@': ++*arg;
3426 if (evaluate)
3427 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003428 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3429 semsg(_(e_syntax_error_at_str), *arg);
3430 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3431 emsg_invreg(**arg);
3432 else
3433 {
3434 rettv->v_type = VAR_STRING;
3435 rettv->vval.v_string = get_reg_contents(**arg,
3436 GREG_EXPR_SRC);
3437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439 if (**arg != NUL)
3440 ++*arg;
3441 break;
3442
3443 /*
3444 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003445 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003447 case '(': ret = NOTDONE;
3448 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003449 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003450 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003451 if (ret == OK && evaluate)
3452 {
3453 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3454
3455 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003456 if (compile_def_function(ufunc,
3457 TRUE, PROFILING(ufunc), NULL) == FAIL)
3458 {
3459 clear_tv(rettv);
3460 ret = FAIL;
3461 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003462 }
3463 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003464 if (ret == NOTDONE)
3465 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003466 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003467 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003468
Bram Moolenaar9215f012020-06-27 21:18:00 +02003469 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003470 if (**arg == ')')
3471 ++*arg;
3472 else if (ret == OK)
3473 {
3474 emsg(_(e_missing_close));
3475 clear_tv(rettv);
3476 ret = FAIL;
3477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
3479 break;
3480
Bram Moolenaar8c711452005-01-14 21:53:12 +00003481 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 break;
3483 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003484
3485 if (ret == NOTDONE)
3486 {
3487 /*
3488 * Must be a variable or function name.
3489 * Can also be a curly-braces kind of name: {expr}.
3490 */
3491 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003492 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003493 if (alias != NULL)
3494 s = alias;
3495
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003496 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003497 ret = FAIL;
3498 else
3499 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003500 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3501
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003502 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003503 {
3504 emsg(_(e_cannot_use_underscore_here));
3505 ret = FAIL;
3506 }
3507 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003508 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003509 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003510 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003511 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003512 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003513 else if (flags & EVAL_CONSTANT)
3514 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003515 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003516 {
3517 // get the value of "true", "false" or a variable
3518 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3519 {
3520 rettv->v_type = VAR_BOOL;
3521 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003522 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003523 }
3524 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003525 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003526 {
3527 rettv->v_type = VAR_BOOL;
3528 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003529 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003530 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003531 else if (len == 4 && in_vim9script()
3532 && STRNCMP(s, "null", 4) == 0)
3533 {
3534 rettv->v_type = VAR_SPECIAL;
3535 rettv->vval.v_number = VVAL_NULL;
3536 ret = OK;
3537 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003538 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003539 ret = eval_variable(s, len, rettv, NULL,
3540 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003541 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003542 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003543 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003544 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003545 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003546 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003547 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003548 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003549 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003550 }
3551
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003552 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3553 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003554 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003555 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
3557 /*
3558 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3559 */
3560 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003561 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003562 return ret;
3563}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003564
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003565/*
3566 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003567 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003568 * Adjusts "end_leaderp" until it is at "start_leader".
3569 */
3570 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003571eval7_leader(
3572 typval_T *rettv,
3573 int numeric_only,
3574 char_u *start_leader,
3575 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003576{
3577 char_u *end_leader = *end_leaderp;
3578 int ret = OK;
3579 int error = FALSE;
3580 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003581 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003582#ifdef FEAT_FLOAT
3583 float_T f = 0.0;
3584
3585 if (rettv->v_type == VAR_FLOAT)
3586 f = rettv->vval.v_float;
3587 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003588#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003589 {
3590 while (VIM_ISWHITE(end_leader[-1]))
3591 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003592 if (in_vim9script() && end_leader[-1] == '!')
3593 val = tv2bool(rettv);
3594 else
3595 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003596 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003597 if (error)
3598 {
3599 clear_tv(rettv);
3600 ret = FAIL;
3601 }
3602 else
3603 {
3604 while (end_leader > start_leader)
3605 {
3606 --end_leader;
3607 if (*end_leader == '!')
3608 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003609 if (numeric_only)
3610 {
3611 ++end_leader;
3612 break;
3613 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003614#ifdef FEAT_FLOAT
3615 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003616 {
3617 if (in_vim9script())
3618 {
3619 rettv->v_type = VAR_BOOL;
3620 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3621 }
3622 else
3623 f = !f;
3624 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003625 else
3626#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003627 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003628 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003629 type = VAR_BOOL;
3630 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003631 }
3632 else if (*end_leader == '-')
3633 {
3634#ifdef FEAT_FLOAT
3635 if (rettv->v_type == VAR_FLOAT)
3636 f = -f;
3637 else
3638#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003639 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003640 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003641 type = VAR_NUMBER;
3642 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003643 }
3644 }
3645#ifdef FEAT_FLOAT
3646 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003648 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003649 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003651 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003652#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003653 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003654 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003655 if (in_vim9script())
3656 rettv->v_type = type;
3657 else
3658 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003659 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003662 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 return ret;
3664}
3665
3666/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003667 * Call the function referred to in "rettv".
3668 */
3669 static int
3670call_func_rettv(
3671 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003672 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003673 typval_T *rettv,
3674 int evaluate,
3675 dict_T *selfdict,
3676 typval_T *basetv)
3677{
3678 partial_T *pt = NULL;
3679 funcexe_T funcexe;
3680 typval_T functv;
3681 char_u *s;
3682 int ret;
3683
3684 // need to copy the funcref so that we can clear rettv
3685 if (evaluate)
3686 {
3687 functv = *rettv;
3688 rettv->v_type = VAR_UNKNOWN;
3689
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003690 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003691 if (functv.v_type == VAR_PARTIAL)
3692 {
3693 pt = functv.vval.v_partial;
3694 s = partial_name(pt);
3695 }
3696 else
3697 s = functv.vval.v_string;
3698 }
3699 else
3700 s = (char_u *)"";
3701
Bram Moolenaara80faa82020-04-12 19:37:17 +02003702 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003703 funcexe.firstline = curwin->w_cursor.lnum;
3704 funcexe.lastline = curwin->w_cursor.lnum;
3705 funcexe.evaluate = evaluate;
3706 funcexe.partial = pt;
3707 funcexe.selfdict = selfdict;
3708 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003709 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003711 // Clear the funcref afterwards, so that deleting it while
3712 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003713 if (evaluate)
3714 clear_tv(&functv);
3715
3716 return ret;
3717}
3718
3719/*
3720 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003721 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003722 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3723 */
3724 static int
3725eval_lambda(
3726 char_u **arg,
3727 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003728 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003729 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003730{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003731 int evaluate = evalarg != NULL
3732 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003733 typval_T base = *rettv;
3734 int ret;
3735
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003736 rettv->v_type = VAR_UNKNOWN;
3737
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003738 if (**arg == '{')
3739 {
3740 // ->{lambda}()
3741 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3742 }
3743 else
3744 {
3745 // ->(lambda)()
3746 ++*arg;
3747 ret = eval1(arg, rettv, evalarg);
3748 *arg = skipwhite_and_linebreak(*arg, evalarg);
3749 if (**arg != ')')
3750 {
3751 emsg(_(e_missing_close));
3752 ret = FAIL;
3753 }
3754 ++*arg;
3755 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003756 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003757 return FAIL;
3758 else if (**arg != '(')
3759 {
3760 if (verbose)
3761 {
3762 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003763 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003764 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003766 }
3767 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003768 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003769 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003770 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003771 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003772
3773 // Clear the funcref afterwards, so that deleting it while
3774 // evaluating the arguments is possible (see test55).
3775 if (evaluate)
3776 clear_tv(&base);
3777
3778 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003779}
3780
3781/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003782 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003783 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003784 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3785 */
3786 static int
3787eval_method(
3788 char_u **arg,
3789 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003790 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003791 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003792{
3793 char_u *name;
3794 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003795 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003796 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003797 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003798 int evaluate = evalarg != NULL
3799 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003800
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003801 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003802
Bram Moolenaarac92e252019-08-03 21:58:38 +02003803 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003804 len = get_name_len(arg, &alias, evaluate, TRUE);
3805 if (alias != NULL)
3806 name = alias;
3807
3808 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003809 {
3810 if (verbose)
3811 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003812 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003813 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003814 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003815 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003816 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003817 if (**arg != '(')
3818 {
3819 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003821 ret = FAIL;
3822 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003823 else if (VIM_ISWHITE((*arg)[-1]))
3824 {
3825 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003826 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003827 ret = FAIL;
3828 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003829 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003830 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003831 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003832 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003833
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003834 // Clear the funcref afterwards, so that deleting it while
3835 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003836 if (evaluate)
3837 clear_tv(&base);
3838
Bram Moolenaarac92e252019-08-03 21:58:38 +02003839 return ret;
3840}
3841
3842/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003843 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3844 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003845 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3846 */
3847 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003848eval_index(
3849 char_u **arg,
3850 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003851 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003852 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003853{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003854 int evaluate = evalarg != NULL
3855 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003856 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003857 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003858 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003859 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003860 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003861 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003862
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003863 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3864 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003865
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003866 init_tv(&var1);
3867 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003868 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003869 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003870 /*
3871 * dict.name
3872 */
3873 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003874 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003875 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003876 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003877 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003878 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003879 }
3880 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003881 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003882 /*
3883 * something[idx]
3884 *
3885 * Get the (first) variable from inside the [].
3886 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003887 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003888 if (**arg == ':')
3889 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003890 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003891 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003892 else if (vim9 && **arg == ':')
3893 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003894 semsg(_(e_white_space_required_before_and_after_str_at_str),
3895 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003896 clear_tv(&var1);
3897 return FAIL;
3898 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003899 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003900 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003901#ifdef FEAT_FLOAT
3902 // allow for indexing with float
3903 if (vim9 && rettv->v_type == VAR_DICT
3904 && var1.v_type == VAR_FLOAT)
3905 {
3906 var1.vval.v_string = typval_tostring(&var1, TRUE);
3907 var1.v_type = VAR_STRING;
3908 }
3909#endif
3910 if (tv_get_string_chk(&var1) == NULL)
3911 {
3912 // not a number or string
3913 clear_tv(&var1);
3914 return FAIL;
3915 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003916 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003917
3918 /*
3919 * Get the second variable from inside the [:].
3920 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003921 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003922 if (**arg == ':')
3923 {
3924 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003925 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003926 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003927 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003928 semsg(_(e_white_space_required_before_and_after_str_at_str),
3929 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003930 if (!empty1)
3931 clear_tv(&var1);
3932 return FAIL;
3933 }
3934 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003935 if (**arg == ']')
3936 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003937 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003939 if (!empty1)
3940 clear_tv(&var1);
3941 return FAIL;
3942 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003943 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003944 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003945 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003946 if (!empty1)
3947 clear_tv(&var1);
3948 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003949 return FAIL;
3950 }
3951 }
3952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003953 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003954 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003955 if (**arg != ']')
3956 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003957 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003958 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003959 clear_tv(&var1);
3960 if (range)
3961 clear_tv(&var2);
3962 return FAIL;
3963 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003964 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003965 }
3966
3967 if (evaluate)
3968 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003969 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003970 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003971 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003972
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003973 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003974 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003975 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003976 clear_tv(&var2);
3977 return res;
3978 }
3979 return OK;
3980}
3981
3982/*
3983 * Check if "rettv" can have an [index] or [sli:ce]
3984 */
3985 int
3986check_can_index(typval_T *rettv, int evaluate, int verbose)
3987{
3988 switch (rettv->v_type)
3989 {
3990 case VAR_FUNC:
3991 case VAR_PARTIAL:
3992 if (verbose)
3993 emsg(_("E695: Cannot index a Funcref"));
3994 return FAIL;
3995 case VAR_FLOAT:
3996#ifdef FEAT_FLOAT
3997 if (verbose)
3998 emsg(_(e_float_as_string));
3999 return FAIL;
4000#endif
4001 case VAR_BOOL:
4002 case VAR_SPECIAL:
4003 case VAR_JOB:
4004 case VAR_CHANNEL:
4005 if (verbose)
4006 emsg(_(e_cannot_index_special_variable));
4007 return FAIL;
4008 case VAR_UNKNOWN:
4009 case VAR_ANY:
4010 case VAR_VOID:
4011 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004012 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004013 emsg(_(e_cannot_index_special_variable));
4014 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004015 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004016 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004017
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004018 case VAR_STRING:
4019 case VAR_LIST:
4020 case VAR_DICT:
4021 case VAR_BLOB:
4022 break;
4023 case VAR_NUMBER:
4024 if (in_vim9script())
4025 emsg(_(e_cannot_index_number));
4026 break;
4027 }
4028 return OK;
4029}
4030
4031/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004032 * slice() function
4033 */
4034 void
4035f_slice(typval_T *argvars, typval_T *rettv)
4036{
4037 if (check_can_index(argvars, TRUE, FALSE) == OK)
4038 {
4039 copy_tv(argvars, rettv);
4040 eval_index_inner(rettv, TRUE, argvars + 1,
4041 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4042 TRUE, NULL, 0, FALSE);
4043 }
4044}
4045
4046/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004047 * Apply index or range to "rettv".
4048 * "var1" is the first index, NULL for [:expr].
4049 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004050 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4051 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004052 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4053 */
4054 int
4055eval_index_inner(
4056 typval_T *rettv,
4057 int is_range,
4058 typval_T *var1,
4059 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004060 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004061 char_u *key,
4062 int keylen,
4063 int verbose)
4064{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004065 varnumber_T n1, n2 = 0;
4066 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004067
4068 n1 = 0;
4069 if (var1 != NULL && rettv->v_type != VAR_DICT)
4070 n1 = tv_get_number(var1);
4071
4072 if (is_range)
4073 {
4074 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004075 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004076 if (verbose)
4077 emsg(_(e_cannot_slice_dictionary));
4078 return FAIL;
4079 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004080 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004081 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004082 else
4083 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004084 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004085
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004086 switch (rettv->v_type)
4087 {
4088 case VAR_UNKNOWN:
4089 case VAR_ANY:
4090 case VAR_VOID:
4091 case VAR_FUNC:
4092 case VAR_PARTIAL:
4093 case VAR_FLOAT:
4094 case VAR_BOOL:
4095 case VAR_SPECIAL:
4096 case VAR_JOB:
4097 case VAR_CHANNEL:
4098 break; // not evaluating, skipping over subscript
4099
4100 case VAR_NUMBER:
4101 case VAR_STRING:
4102 {
4103 char_u *s = tv_get_string(rettv);
4104
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004105 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004106 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004107 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004108 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004109 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004110 else
4111 s = char_from_string(s, n1);
4112 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004113 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004115 // The resulting variable is a substring. If the indexes
4116 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004117 if (n1 < 0)
4118 {
4119 n1 = len + n1;
4120 if (n1 < 0)
4121 n1 = 0;
4122 }
4123 if (n2 < 0)
4124 n2 = len + n2;
4125 else if (n2 >= len)
4126 n2 = len;
4127 if (n1 >= len || n2 < 0 || n1 > n2)
4128 s = NULL;
4129 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004130 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004131 }
4132 else
4133 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004134 // The resulting variable is a string of a single
4135 // character. If the index is too big or negative the
4136 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004137 if (n1 >= len || n1 < 0)
4138 s = NULL;
4139 else
4140 s = vim_strnsave(s + n1, 1);
4141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142 clear_tv(rettv);
4143 rettv->v_type = VAR_STRING;
4144 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004145 }
4146 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004147
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004148 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004149 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4150 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004151 break;
4152
4153 case VAR_LIST:
4154 if (var1 == NULL)
4155 n1 = 0;
4156 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004157 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004158 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004159 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004160 return FAIL;
4161 break;
4162
4163 case VAR_DICT:
4164 {
4165 dictitem_T *item;
4166 typval_T tmp;
4167
4168 if (key == NULL)
4169 {
4170 key = tv_get_string_chk(var1);
4171 if (key == NULL)
4172 return FAIL;
4173 }
4174
4175 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4176
4177 if (item == NULL && verbose)
4178 semsg(_(e_dictkey), key);
4179 if (item == NULL)
4180 return FAIL;
4181
4182 copy_tv(&item->di_tv, &tmp);
4183 clear_tv(rettv);
4184 *rettv = tmp;
4185 }
4186 break;
4187 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004188 return OK;
4189}
4190
4191/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004192 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004193 */
4194 char_u *
4195partial_name(partial_T *pt)
4196{
4197 if (pt->pt_name != NULL)
4198 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004199 if (pt->pt_func != NULL)
4200 return pt->pt_func->uf_name;
4201 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004202}
4203
Bram Moolenaarddecc252016-04-06 22:59:37 +02004204 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004205partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004206{
4207 int i;
4208
4209 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004210 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004211 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004212 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004213 if (pt->pt_name != NULL)
4214 {
4215 func_unref(pt->pt_name);
4216 vim_free(pt->pt_name);
4217 }
4218 else
4219 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004220
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004221 // Decrease the reference count for the context of a closure. If down
4222 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004223 if (pt->pt_funcstack != NULL)
4224 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004225 --pt->pt_funcstack->fs_refcount;
4226 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004227 }
4228
Bram Moolenaarddecc252016-04-06 22:59:37 +02004229 vim_free(pt);
4230}
4231
4232/*
4233 * Unreference a closure: decrement the reference count and free it when it
4234 * becomes zero.
4235 */
4236 void
4237partial_unref(partial_T *pt)
4238{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004239 if (pt != NULL)
4240 {
4241 if (--pt->pt_refcount <= 0)
4242 partial_free(pt);
4243
4244 // If the reference count goes down to one, the funcstack may be the
4245 // only reference and can be freed if no other partials reference it.
4246 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4247 funcstack_check_refcount(pt->pt_funcstack);
4248 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004249}
4250
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004251/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004252 * Return the next (unique) copy ID.
4253 * Used for serializing nested structures.
4254 */
4255 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004256get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004257{
4258 current_copyID += COPYID_INC;
4259 return current_copyID;
4260}
4261
4262/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004263 * Garbage collection for lists and dictionaries.
4264 *
4265 * We use reference counts to be able to free most items right away when they
4266 * are no longer used. But for composite items it's possible that it becomes
4267 * unused while the reference count is > 0: When there is a recursive
4268 * reference. Example:
4269 * :let l = [1, 2, 3]
4270 * :let d = {9: l}
4271 * :let l[1] = d
4272 *
4273 * Since this is quite unusual we handle this with garbage collection: every
4274 * once in a while find out which lists and dicts are not referenced from any
4275 * variable.
4276 *
4277 * Here is a good reference text about garbage collection (refers to Python
4278 * but it applies to all reference-counting mechanisms):
4279 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004280 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004281
4282/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004283 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004284 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004285 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004286 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004287 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004288garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004289{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004290 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004291 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004292 buf_T *buf;
4293 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004294 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004295 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004296
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004297 if (!testing)
4298 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004299 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004300 want_garbage_collect = FALSE;
4301 may_garbage_collect = FALSE;
4302 garbage_collect_at_exit = FALSE;
4303 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004304
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004305 // The execution stack can grow big, limit the size.
4306 if (exestack.ga_maxlen - exestack.ga_len > 500)
4307 {
4308 size_t new_len;
4309 char_u *pp;
4310 int n;
4311
4312 // Keep 150% of the current size, with a minimum of the growth size.
4313 n = exestack.ga_len / 2;
4314 if (n < exestack.ga_growsize)
4315 n = exestack.ga_growsize;
4316
4317 // Don't make it bigger though.
4318 if (exestack.ga_len + n < exestack.ga_maxlen)
4319 {
4320 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4321 pp = vim_realloc(exestack.ga_data, new_len);
4322 if (pp == NULL)
4323 return FAIL;
4324 exestack.ga_maxlen = exestack.ga_len + n;
4325 exestack.ga_data = pp;
4326 }
4327 }
4328
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004329 // We advance by two because we add one for items referenced through
4330 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004331 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004332
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004333 /*
4334 * 1. Go through all accessible variables and mark all lists and dicts
4335 * with copyID.
4336 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004338 // Don't free variables in the previous_funccal list unless they are only
4339 // referenced through previous_funccal. This must be first, because if
4340 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004341 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004342
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004343 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004344 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004345
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004346 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004347 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004348 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4349 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004351 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004352 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004353 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4354 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004355 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004356 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4357 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004358#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004359 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004360 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4361 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004362 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004363 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004364 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4365 NULL, NULL);
4366#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004369 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004370 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4371 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004372 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004373 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004375 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004376 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004378 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004379 abort = abort || set_ref_in_functions(copyID);
4380
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004381 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004382 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004383
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004384 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004385 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004386
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004387 // callbacks in buffers
4388 abort = abort || set_ref_in_buffers(copyID);
4389
Bram Moolenaar1dced572012-04-05 16:54:08 +02004390#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004391 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004392#endif
4393
Bram Moolenaardb913952012-06-29 12:54:53 +02004394#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004395 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004396#endif
4397
4398#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004399 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004400#endif
4401
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004402#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004403 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004404 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004405#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004406#ifdef FEAT_NETBEANS_INTG
4407 abort = abort || set_ref_in_nb_channel(copyID);
4408#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004409
Bram Moolenaare3188e22016-05-31 21:13:04 +02004410#ifdef FEAT_TIMERS
4411 abort = abort || set_ref_in_timer(copyID);
4412#endif
4413
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004414#ifdef FEAT_QUICKFIX
4415 abort = abort || set_ref_in_quickfix(copyID);
4416#endif
4417
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004418#ifdef FEAT_TERMINAL
4419 abort = abort || set_ref_in_term(copyID);
4420#endif
4421
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004422#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004423 abort = abort || set_ref_in_popups(copyID);
4424#endif
4425
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004426 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004427 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004428 /*
4429 * 2. Free lists and dictionaries that are not referenced.
4430 */
4431 did_free = free_unref_items(copyID);
4432
4433 /*
4434 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004435 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004436 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004437 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004438 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004439 else if (p_verbose > 0)
4440 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004441 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004442 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004443
4444 return did_free;
4445}
4446
4447/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004448 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004449 */
4450 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004451free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004452{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004453 int did_free = FALSE;
4454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004455 // Let all "free" functions know that we are here. This means no
4456 // dictionaries, lists, channels or jobs are to be freed, because we will
4457 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004458 in_free_unref_items = TRUE;
4459
4460 /*
4461 * PASS 1: free the contents of the items. We don't free the items
4462 * themselves yet, so that it is possible to decrement refcount counters
4463 */
4464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004465 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004466 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004468 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004469 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004470
4471#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004472 // Go through the list of jobs and free items without the copyID. This
4473 // must happen before doing channels, because jobs refer to channels, but
4474 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004475 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4476
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004477 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004478 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4479#endif
4480
4481 /*
4482 * PASS 2: free the items themselves.
4483 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004484 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004485 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004486
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004487#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004488 // Go through the list of jobs and free items without the copyID. This
4489 // must happen before doing channels, because jobs refer to channels, but
4490 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004491 free_unused_jobs(copyID, COPYID_MASK);
4492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004493 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004494 free_unused_channels(copyID, COPYID_MASK);
4495#endif
4496
4497 in_free_unref_items = FALSE;
4498
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004499 return did_free;
4500}
4501
4502/*
4503 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004504 * "list_stack" is used to add lists to be marked. Can be NULL.
4505 *
4506 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004507 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004509set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004510{
4511 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004512 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004513 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004514 hashtab_T *cur_ht;
4515 ht_stack_T *ht_stack = NULL;
4516 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004517
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004518 cur_ht = ht;
4519 for (;;)
4520 {
4521 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004522 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004523 // Mark each item in the hashtab. If the item contains a hashtab
4524 // it is added to ht_stack, if it contains a list it is added to
4525 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004526 todo = (int)cur_ht->ht_used;
4527 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4528 if (!HASHITEM_EMPTY(hi))
4529 {
4530 --todo;
4531 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4532 &ht_stack, list_stack);
4533 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004534 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004535
4536 if (ht_stack == NULL)
4537 break;
4538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004539 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004540 cur_ht = ht_stack->ht;
4541 tempitem = ht_stack;
4542 ht_stack = ht_stack->prev;
4543 free(tempitem);
4544 }
4545
4546 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004547}
4548
4549/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004550 * Mark a dict and its items with "copyID".
4551 * Returns TRUE if setting references failed somehow.
4552 */
4553 int
4554set_ref_in_dict(dict_T *d, int copyID)
4555{
4556 if (d != NULL && d->dv_copyID != copyID)
4557 {
4558 d->dv_copyID = copyID;
4559 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4560 }
4561 return FALSE;
4562}
4563
4564/*
4565 * Mark a list and its items with "copyID".
4566 * Returns TRUE if setting references failed somehow.
4567 */
4568 int
4569set_ref_in_list(list_T *ll, int copyID)
4570{
4571 if (ll != NULL && ll->lv_copyID != copyID)
4572 {
4573 ll->lv_copyID = copyID;
4574 return set_ref_in_list_items(ll, copyID, NULL);
4575 }
4576 return FALSE;
4577}
4578
4579/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004580 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004581 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4582 *
4583 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004584 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004585 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004586set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004587{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004588 listitem_T *li;
4589 int abort = FALSE;
4590 list_T *cur_l;
4591 list_stack_T *list_stack = NULL;
4592 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004593
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004594 cur_l = l;
4595 for (;;)
4596 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004597 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004598 // Mark each item in the list. If the item contains a hashtab
4599 // it is added to ht_stack, if it contains a list it is added to
4600 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004601 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4602 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4603 ht_stack, &list_stack);
4604 if (list_stack == NULL)
4605 break;
4606
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004607 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004608 cur_l = list_stack->list;
4609 tempitem = list_stack;
4610 list_stack = list_stack->prev;
4611 free(tempitem);
4612 }
4613
4614 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004615}
4616
4617/*
4618 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004619 * "list_stack" is used to add lists to be marked. Can be NULL.
4620 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4621 *
4622 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004623 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004624 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004625set_ref_in_item(
4626 typval_T *tv,
4627 int copyID,
4628 ht_stack_T **ht_stack,
4629 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004630{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004631 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004632
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004633 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004634 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004635 dict_T *dd = tv->vval.v_dict;
4636
Bram Moolenaara03f2332016-02-06 18:09:59 +01004637 if (dd != NULL && dd->dv_copyID != copyID)
4638 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004639 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004640 dd->dv_copyID = copyID;
4641 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004642 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004643 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4644 }
4645 else
4646 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004647 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4648
Bram Moolenaara03f2332016-02-06 18:09:59 +01004649 if (newitem == NULL)
4650 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004651 else
4652 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004653 newitem->ht = &dd->dv_hashtab;
4654 newitem->prev = *ht_stack;
4655 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004656 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004657 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004658 }
4659 }
4660 else if (tv->v_type == VAR_LIST)
4661 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004662 list_T *ll = tv->vval.v_list;
4663
Bram Moolenaara03f2332016-02-06 18:09:59 +01004664 if (ll != NULL && ll->lv_copyID != copyID)
4665 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004666 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004667 ll->lv_copyID = copyID;
4668 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004669 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004670 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004671 }
4672 else
4673 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004674 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4675
Bram Moolenaara03f2332016-02-06 18:09:59 +01004676 if (newitem == NULL)
4677 abort = TRUE;
4678 else
4679 {
4680 newitem->list = ll;
4681 newitem->prev = *list_stack;
4682 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004683 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004684 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004685 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004686 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004687 else if (tv->v_type == VAR_FUNC)
4688 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004689 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004690 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004691 else if (tv->v_type == VAR_PARTIAL)
4692 {
4693 partial_T *pt = tv->vval.v_partial;
4694 int i;
4695
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004696 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004697 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004698 // Didn't see this partial yet.
4699 pt->pt_copyID = copyID;
4700
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004701 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004702
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004703 if (pt->pt_dict != NULL)
4704 {
4705 typval_T dtv;
4706
4707 dtv.v_type = VAR_DICT;
4708 dtv.vval.v_dict = pt->pt_dict;
4709 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4710 }
4711
4712 for (i = 0; i < pt->pt_argc; ++i)
4713 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4714 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004715 if (pt->pt_funcstack != NULL)
4716 {
4717 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4718
4719 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4720 abort = abort || set_ref_in_item(stack + i, copyID,
4721 ht_stack, list_stack);
4722 }
4723
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004724 }
4725 }
4726#ifdef FEAT_JOB_CHANNEL
4727 else if (tv->v_type == VAR_JOB)
4728 {
4729 job_T *job = tv->vval.v_job;
4730 typval_T dtv;
4731
4732 if (job != NULL && job->jv_copyID != copyID)
4733 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004734 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004735 if (job->jv_channel != NULL)
4736 {
4737 dtv.v_type = VAR_CHANNEL;
4738 dtv.vval.v_channel = job->jv_channel;
4739 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4740 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004741 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004742 {
4743 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004744 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004745 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4746 }
4747 }
4748 }
4749 else if (tv->v_type == VAR_CHANNEL)
4750 {
4751 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004752 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004753 typval_T dtv;
4754 jsonq_T *jq;
4755 cbq_T *cq;
4756
4757 if (ch != NULL && ch->ch_copyID != copyID)
4758 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004759 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004760 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004761 {
4762 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4763 jq = jq->jq_next)
4764 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4765 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4766 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004767 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004768 {
4769 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004770 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004771 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4772 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004773 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004774 {
4775 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004776 dtv.vval.v_partial =
4777 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004778 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4779 }
4780 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004781 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004782 {
4783 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004784 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004785 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4786 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004787 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004788 {
4789 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004790 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004791 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4792 }
4793 }
4794 }
4795#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004796 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004797}
4798
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004800 * Return a string with the string representation of a variable.
4801 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004802 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004803 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004804 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004805 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004806 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004807 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4808 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004809 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004811 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004812echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004813 typval_T *tv,
4814 char_u **tofree,
4815 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004816 int copyID,
4817 int echo_style,
4818 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004819 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004820{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004821 static int recurse = 0;
4822 char_u *r = NULL;
4823
Bram Moolenaar33570922005-01-25 22:26:29 +00004824 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004825 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004826 if (!did_echo_string_emsg)
4827 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004828 // Only give this message once for a recursive call to avoid
4829 // flooding the user with errors. And stop iterating over lists
4830 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004831 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004832 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004833 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004834 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004835 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004836 }
4837 ++recurse;
4838
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004839 switch (tv->v_type)
4840 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004841 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004842 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004843 {
4844 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004845 r = tv->vval.v_string;
4846 if (r == NULL)
4847 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004848 }
4849 else
4850 {
4851 *tofree = string_quote(tv->vval.v_string, FALSE);
4852 r = *tofree;
4853 }
4854 break;
4855
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004856 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004857 if (echo_style)
4858 {
4859 *tofree = NULL;
4860 r = tv->vval.v_string;
4861 }
4862 else
4863 {
4864 *tofree = string_quote(tv->vval.v_string, TRUE);
4865 r = *tofree;
4866 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004867 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004868
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004869 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004870 {
4871 partial_T *pt = tv->vval.v_partial;
4872 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004873 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004874 garray_T ga;
4875 int i;
4876 char_u *tf;
4877
4878 ga_init2(&ga, 1, 100);
4879 ga_concat(&ga, (char_u *)"function(");
4880 if (fname != NULL)
4881 {
4882 ga_concat(&ga, fname);
4883 vim_free(fname);
4884 }
4885 if (pt != NULL && pt->pt_argc > 0)
4886 {
4887 ga_concat(&ga, (char_u *)", [");
4888 for (i = 0; i < pt->pt_argc; ++i)
4889 {
4890 if (i > 0)
4891 ga_concat(&ga, (char_u *)", ");
4892 ga_concat(&ga,
4893 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4894 vim_free(tf);
4895 }
4896 ga_concat(&ga, (char_u *)"]");
4897 }
4898 if (pt != NULL && pt->pt_dict != NULL)
4899 {
4900 typval_T dtv;
4901
4902 ga_concat(&ga, (char_u *)", ");
4903 dtv.v_type = VAR_DICT;
4904 dtv.vval.v_dict = pt->pt_dict;
4905 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4906 vim_free(tf);
4907 }
4908 ga_concat(&ga, (char_u *)")");
4909
4910 *tofree = ga.ga_data;
4911 r = *tofree;
4912 break;
4913 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004914
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004915 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004916 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004917 break;
4918
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004919 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004920 if (tv->vval.v_list == NULL)
4921 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004922 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004923 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004924 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004925 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004926 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4927 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004928 {
4929 *tofree = NULL;
4930 r = (char_u *)"[...]";
4931 }
4932 else
4933 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004934 int old_copyID = tv->vval.v_list->lv_copyID;
4935
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004936 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004937 *tofree = list2string(tv, copyID, restore_copyID);
4938 if (restore_copyID)
4939 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004940 r = *tofree;
4941 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004942 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004943
Bram Moolenaar8c711452005-01-14 21:53:12 +00004944 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004945 if (tv->vval.v_dict == NULL)
4946 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004947 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004948 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +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_dict->dv_copyID == copyID
4952 && tv->vval.v_dict->dv_hashtab.ht_used != 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_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004960
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004961 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004962 *tofree = dict2string(tv, copyID, restore_copyID);
4963 if (restore_copyID)
4964 tv->vval.v_dict->dv_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 Moolenaarc70646c2005-01-04 21:52:38 +00004969 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004970 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004971 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004973 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004974 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004975 break;
4976
Bram Moolenaar835dc632016-02-07 14:27:38 +01004977 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004978 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004979 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004980 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004981 if (composite_val)
4982 {
4983 *tofree = string_quote(r, FALSE);
4984 r = *tofree;
4985 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004986 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004987
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004989#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990 *tofree = NULL;
4991 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4992 r = numbuf;
4993 break;
4994#endif
4995
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004996 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004997 case VAR_SPECIAL:
4998 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004999 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005000 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005001 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005002
Bram Moolenaar8502c702014-06-17 12:51:16 +02005003 if (--recurse == 0)
5004 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005005 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005006}
5007
5008/*
5009 * Return a string with the string representation of a variable.
5010 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5011 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005012 * Does not put quotes around strings, as ":echo" displays values.
5013 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5014 * May return NULL.
5015 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005016 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005017echo_string(
5018 typval_T *tv,
5019 char_u **tofree,
5020 char_u *numbuf,
5021 int copyID)
5022{
5023 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5024}
5025
5026/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005027 * Return string "str" in ' quotes, doubling ' characters.
5028 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005029 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005030 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005031 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005032string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005033{
Bram Moolenaar33570922005-01-25 22:26:29 +00005034 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005035 char_u *p, *r, *s;
5036
Bram Moolenaar33570922005-01-25 22:26:29 +00005037 len = (function ? 13 : 3);
5038 if (str != NULL)
5039 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005040 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005041 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005042 if (*p == '\'')
5043 ++len;
5044 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005045 s = r = alloc(len);
5046 if (r != NULL)
5047 {
5048 if (function)
5049 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005050 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005051 r += 10;
5052 }
5053 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005055 if (str != NULL)
5056 for (p = str; *p != NUL; )
5057 {
5058 if (*p == '\'')
5059 *r++ = '\'';
5060 MB_COPY_CHAR(p, r);
5061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005063 if (function)
5064 *r++ = ')';
5065 *r++ = NUL;
5066 }
5067 return s;
5068}
5069
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005070#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005071/*
5072 * Convert the string "text" to a floating point number.
5073 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5074 * this always uses a decimal point.
5075 * Returns the length of the text that was consumed.
5076 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005077 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005078string2float(
5079 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005080 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005081{
5082 char *s = (char *)text;
5083 float_T f;
5084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005085 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005086 if (STRNICMP(text, "inf", 3) == 0)
5087 {
5088 *value = INFINITY;
5089 return 3;
5090 }
5091 if (STRNICMP(text, "-inf", 3) == 0)
5092 {
5093 *value = -INFINITY;
5094 return 4;
5095 }
5096 if (STRNICMP(text, "nan", 3) == 0)
5097 {
5098 *value = NAN;
5099 return 3;
5100 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005101 f = strtod(s, &s);
5102 *value = f;
5103 return (int)((char_u *)s - text);
5104}
5105#endif
5106
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005108 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5109 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005110 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005111 */
5112 int
5113buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5114{
5115 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005116 char_u *t;
5117 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005118
5119 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5120 return -1;
5121
5122 if (lnum > buf->b_ml.ml_line_count)
5123 lnum = buf->b_ml.ml_line_count;
5124
5125 str = ml_get_buf(buf, lnum, FALSE);
5126 if (str == NULL)
5127 return -1;
5128
5129 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005130 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005131
Bram Moolenaar91458462021-01-13 20:08:38 +01005132 // count the number of characters
5133 t = str;
5134 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5135 t += mb_ptr2len(t);
5136
5137 // In insert mode, when the cursor is at the end of a non-empty line,
5138 // byteidx points to the NUL character immediately past the end of the
5139 // string. In this case, add one to the character count.
5140 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5141 count++;
5142
5143 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005144}
5145
5146/*
5147 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005148 * byte index. Works only for loaded buffers. Returns -1 on failure.
5149 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005150 */
5151 int
5152buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5153{
5154 char_u *str;
5155 char_u *t;
5156
5157 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5158 return -1;
5159
5160 if (lnum > buf->b_ml.ml_line_count)
5161 lnum = buf->b_ml.ml_line_count;
5162
5163 str = ml_get_buf(buf, lnum, FALSE);
5164 if (str == NULL)
5165 return -1;
5166
5167 // Convert the character offset to a byte offset
5168 t = str;
5169 while (*t != NUL && --charidx > 0)
5170 t += mb_ptr2len(t);
5171
Bram Moolenaar91458462021-01-13 20:08:38 +01005172 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005173}
5174
5175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005177 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005179 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005180var2fpos(
5181 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005182 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005183 int *fnum, // set to fnum for '0, 'A, etc.
5184 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005186 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005188 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005190 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005191 if (varp->v_type == VAR_LIST)
5192 {
5193 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005194 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005195 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005196 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005197
5198 l = varp->vval.v_list;
5199 if (l == NULL)
5200 return NULL;
5201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005202 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005203 pos.lnum = list_find_nr(l, 0L, &error);
5204 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005205 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005206 if (charcol)
5207 len = (long)mb_charlen(ml_get(pos.lnum));
5208 else
5209 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005210
Bram Moolenaarec65d772020-08-20 22:29:12 +02005211 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005212 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005213 li = list_find(l, 1L);
5214 if (li != NULL && li->li_tv.v_type == VAR_STRING
5215 && li->li_tv.vval.v_string != NULL
5216 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005217 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005218 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005219 }
5220 else
5221 {
5222 pos.col = list_find_nr(l, 1L, &error);
5223 if (error)
5224 return NULL;
5225 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005227 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005228 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005229 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005230 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005231
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005233 pos.coladd = list_find_nr(l, 2L, &error);
5234 if (error)
5235 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005236
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005237 return &pos;
5238 }
5239
Bram Moolenaarc5809432021-03-27 21:23:30 +01005240 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5241 return NULL;
5242
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005243 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005244 if (name == NULL)
5245 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005246 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005247 {
5248 pos = curwin->w_cursor;
5249 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005250 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005251 return &pos;
5252 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005253 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005254 {
5255 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005256 pos = VIsual;
5257 else
5258 pos = curwin->w_cursor;
5259 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005260 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005261 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005262 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005263 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005265 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5267 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005268 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005269 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 return pp;
5271 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005272
Bram Moolenaara5525202006-03-02 22:52:09 +00005273 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005274
Bram Moolenaar477933c2007-07-17 14:32:23 +00005275 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005276 {
5277 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005278 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005279 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005280 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005281 // In silent Ex mode topline is zero, but that's not a valid line
5282 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005283 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005284 return &pos;
5285 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005286 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005287 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005288 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005289 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005290 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005291 return &pos;
5292 }
5293 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005294 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005296 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 {
5298 pos.lnum = curbuf->b_ml.ml_line_count;
5299 pos.col = 0;
5300 }
5301 else
5302 {
5303 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005304 if (charcol)
5305 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5306 else
5307 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309 return &pos;
5310 }
5311 return NULL;
5312}
5313
5314/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005315 * Convert list in "arg" into a position and optional file number.
5316 * When "fnump" is NULL there is no file number, only 3 items.
5317 * Note that the column is passed on as-is, the caller may want to decrement
5318 * it to use 1 for the first column.
5319 * Return FAIL when conversion is not possible, doesn't check the position for
5320 * validity.
5321 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005322 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005323list2fpos(
5324 typval_T *arg,
5325 pos_T *posp,
5326 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005327 colnr_T *curswantp,
5328 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005329{
5330 list_T *l = arg->vval.v_list;
5331 long i = 0;
5332 long n;
5333
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005334 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5335 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005336 if (arg->v_type != VAR_LIST
5337 || l == NULL
5338 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005339 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005340 return FAIL;
5341
5342 if (fnump != NULL)
5343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005344 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005345 if (n < 0)
5346 return FAIL;
5347 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005348 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005349 *fnump = n;
5350 }
5351
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005352 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005353 if (n < 0)
5354 return FAIL;
5355 posp->lnum = n;
5356
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005357 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005358 if (n < 0)
5359 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005360 // If character position is specified, then convert to byte position
5361 if (charcol)
5362 {
5363 buf_T *buf;
5364
5365 // Get the text for the specified line in a loaded buffer
5366 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5367 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5368 return FAIL;
5369
Bram Moolenaar91458462021-01-13 20:08:38 +01005370 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005371 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005372 posp->col = n;
5373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005374 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005375 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005376 posp->coladd = 0;
5377 else
5378 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005379
Bram Moolenaar493c1782014-05-28 14:34:46 +02005380 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005381 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005382
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005383 return OK;
5384}
5385
5386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 * Get the length of an environment variable name.
5388 * Advance "arg" to the first character after the name.
5389 * Return 0 for error.
5390 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005391 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005392get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
5394 char_u *p;
5395 int len;
5396
5397 for (p = *arg; vim_isIDc(*p); ++p)
5398 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005399 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 return 0;
5401
5402 len = (int)(p - *arg);
5403 *arg = p;
5404 return len;
5405}
5406
5407/*
5408 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005409 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 * Return 0 if something is wrong.
5411 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005413get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 char_u *p;
5416 int len;
5417
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005418 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005420 {
5421 if (*p == ':')
5422 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005423 // "s:" is start of "s:var", but "n:" is not and can be used in
5424 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005425 len = (int)(p - *arg);
5426 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5427 || len > 1)
5428 break;
5429 }
5430 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005431 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 return 0;
5433
5434 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005435 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436
5437 return len;
5438}
5439
5440/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005441 * Get the length of the name of a variable or function.
5442 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005444 * Return -1 if curly braces expansion failed.
5445 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 * If the name contains 'magic' {}'s, expand them and return the
5447 * expanded name in an allocated string via 'alias' - caller must free.
5448 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005450get_name_len(
5451 char_u **arg,
5452 char_u **alias,
5453 int evaluate,
5454 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 char_u *p;
5458 char_u *expr_start;
5459 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005461 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
5463 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5464 && (*arg)[2] == (int)KE_SNR)
5465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005466 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 *arg += 3;
5468 return get_id_len(arg) + 3;
5469 }
5470 len = eval_fname_script(*arg);
5471 if (len > 0)
5472 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005473 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 *arg += len;
5475 }
5476
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005478 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005480 p = find_name_end(*arg, &expr_start, &expr_end,
5481 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 if (expr_start != NULL)
5483 {
5484 char_u *temp_string;
5485
5486 if (!evaluate)
5487 {
5488 len += (int)(p - *arg);
5489 *arg = skipwhite(p);
5490 return len;
5491 }
5492
5493 /*
5494 * Include any <SID> etc in the expanded string:
5495 * Thus the -len here.
5496 */
5497 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5498 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005499 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 *alias = temp_string;
5501 *arg = skipwhite(p);
5502 return (int)STRLEN(temp_string);
5503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005506 // Only give an error when there is something, otherwise it will be
5507 // reported at a higher level.
5508 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005509 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510
5511 return len;
5512}
5513
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514/*
5515 * Find the end of a variable or function name, taking care of magic braces.
5516 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5517 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005518 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 * Return a pointer to just after the name. Equal to "arg" if there is no
5520 * valid name.
5521 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005522 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005523find_name_end(
5524 char_u *arg,
5525 char_u **expr_start,
5526 char_u **expr_end,
5527 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 int mb_nest = 0;
5530 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005532 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005533 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 *expr_start = NULL;
5538 *expr_end = NULL;
5539 }
5540
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005541 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005542 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5543 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005544 return arg;
5545
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 for (p = arg; *p != NUL
5547 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005548 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005549 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005550 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005552 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005554 if (*p == '\'')
5555 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005556 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005557 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005558 ;
5559 if (*p == NUL)
5560 break;
5561 }
5562 else if (*p == '"')
5563 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005564 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005565 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005566 if (*p == '\\' && p[1] != NUL)
5567 ++p;
5568 if (*p == NUL)
5569 break;
5570 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005571 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5572 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005573 // "s:" is start of "s:var", but "n:" is not and can be used in
5574 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005575 len = (int)(p - arg);
5576 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005577 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005578 break;
5579 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005580
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 if (*p == '[')
5584 ++br_nest;
5585 else if (*p == ']')
5586 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005588
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005589 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005591 if (*p == '{')
5592 {
5593 mb_nest++;
5594 if (expr_start != NULL && *expr_start == NULL)
5595 *expr_start = p;
5596 }
5597 else if (*p == '}')
5598 {
5599 mb_nest--;
5600 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5601 *expr_end = p;
5602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 }
5605
5606 return p;
5607}
5608
5609/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005610 * Expands out the 'magic' {}'s in a variable/function name.
5611 * Note that this can call itself recursively, to deal with
5612 * constructs like foo{bar}{baz}{bam}
5613 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5614 * "in_start" ^
5615 * "expr_start" ^
5616 * "expr_end" ^
5617 * "in_end" ^
5618 *
5619 * Returns a new allocated string, which the caller must free.
5620 * Returns NULL for failure.
5621 */
5622 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005623make_expanded_name(
5624 char_u *in_start,
5625 char_u *expr_start,
5626 char_u *expr_end,
5627 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005628{
5629 char_u c1;
5630 char_u *retval = NULL;
5631 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005632
5633 if (expr_end == NULL || in_end == NULL)
5634 return NULL;
5635 *expr_start = NUL;
5636 *expr_end = NUL;
5637 c1 = *in_end;
5638 *in_end = NUL;
5639
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005640 temp_result = eval_to_string(expr_start + 1, FALSE);
5641 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005642 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005643 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5644 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005645 if (retval != NULL)
5646 {
5647 STRCPY(retval, in_start);
5648 STRCAT(retval, temp_result);
5649 STRCAT(retval, expr_end + 1);
5650 }
5651 }
5652 vim_free(temp_result);
5653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005654 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005655 *expr_start = '{';
5656 *expr_end = '}';
5657
5658 if (retval != NULL)
5659 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005660 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005661 if (expr_start != NULL)
5662 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005663 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005664 temp_result = make_expanded_name(retval, expr_start,
5665 expr_end, temp_result);
5666 vim_free(retval);
5667 retval = temp_result;
5668 }
5669 }
5670
5671 return retval;
5672}
5673
5674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005676 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005678 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005679eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005681 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005682}
5683
5684/*
5685 * Return TRUE if character "c" can be used as the first character in a
5686 * variable or function name (excluding '{' and '}').
5687 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005688 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005689eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005690{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005691 return ASCII_ISALPHA(c) || c == '_';
5692}
5693
5694/*
5695 * Return TRUE if character "c" can be used as the first character of a
5696 * dictionary key.
5697 */
5698 int
5699eval_isdictc(int c)
5700{
5701 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702}
5703
5704/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005705 * Handle:
5706 * - expr[expr], expr[expr:expr] subscript
5707 * - ".name" lookup
5708 * - function call with Funcref variable: func(expr)
5709 * - method call: var->method()
5710 *
5711 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005712 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005714handle_subscript(
5715 char_u **arg,
5716 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005717 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005718 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005719{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005720 int evaluate = evalarg != NULL
5721 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005722 int ret = OK;
5723 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005724 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005725 int getnext;
5726 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005727
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005728 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005729 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005730 // When at the end of the line and ".name" or "->{" or "->X" follows in
5731 // the next line then consume the line break.
5732 p = eval_next_non_blank(*arg, evalarg, &getnext);
5733 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005734 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005735 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005736 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005737 {
5738 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005739 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005740 check_white = FALSE;
5741 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005742
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005743 if (rettv->v_type == VAR_ANY)
5744 {
5745 char_u *exp_name;
5746 int cc;
5747 int idx;
5748 ufunc_T *ufunc;
5749 type_T *type;
5750
5751 // Found script from "import * as {name}", script item name must
5752 // follow.
5753 if (**arg != '.')
5754 {
5755 if (verbose)
5756 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5757 ret = FAIL;
5758 break;
5759 }
5760 ++*arg;
5761 if (IS_WHITE_OR_NUL(**arg))
5762 {
5763 if (verbose)
5764 emsg(_(e_no_white_space_allowed_after_dot));
5765 ret = FAIL;
5766 break;
5767 }
5768
5769 // isolate the name
5770 exp_name = *arg;
5771 while (eval_isnamec(**arg))
5772 ++*arg;
5773 cc = **arg;
5774 **arg = NUL;
5775
5776 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5777 evalarg->eval_cctx, verbose);
5778 **arg = cc;
5779 *arg = skipwhite(*arg);
5780
5781 if (idx < 0 && ufunc == NULL)
5782 {
5783 ret = FAIL;
5784 break;
5785 }
5786 if (idx >= 0)
5787 {
5788 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5789 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5790
5791 copy_tv(sv->sv_tv, rettv);
5792 }
5793 else
5794 {
5795 rettv->v_type = VAR_FUNC;
5796 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5797 }
5798 }
5799
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005800 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5801 || rettv->v_type == VAR_PARTIAL))
5802 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005803 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005804 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5805 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005806
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005807 // Stop the expression evaluation when immediately aborting on
5808 // error, or when an interrupt occurred or an exception was thrown
5809 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005810 if (aborting())
5811 {
5812 if (ret == OK)
5813 clear_tv(rettv);
5814 ret = FAIL;
5815 }
5816 dict_unref(selfdict);
5817 selfdict = NULL;
5818 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005819 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005820 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005821 *arg = skipwhite(p + 2);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005822 if (ret == OK)
5823 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005824 if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005825 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005826 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005827 else
5828 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005829 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005830 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005831 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005832 // "." is ".name" lookup when we found a dict or when evaluating and
5833 // scriptversion is at least 2, where string concatenation is "..".
5834 else if (**arg == '['
5835 || (**arg == '.' && (rettv->v_type == VAR_DICT
5836 || (!evaluate
5837 && (*arg)[1] != '.'
5838 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005839 {
5840 dict_unref(selfdict);
5841 if (rettv->v_type == VAR_DICT)
5842 {
5843 selfdict = rettv->vval.v_dict;
5844 if (selfdict != NULL)
5845 ++selfdict->dv_refcount;
5846 }
5847 else
5848 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005849 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005850 {
5851 clear_tv(rettv);
5852 ret = FAIL;
5853 }
5854 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005855 else
5856 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005857 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005859 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5860 // Don't do this when "Func" is already a partial that was bound
5861 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005862 if (selfdict != NULL
5863 && (rettv->v_type == VAR_FUNC
5864 || (rettv->v_type == VAR_PARTIAL
5865 && (rettv->vval.v_partial->pt_auto
5866 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005867 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005868
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005869 dict_unref(selfdict);
5870 return ret;
5871}
5872
5873/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005874 * Make a copy of an item.
5875 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005876 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5877 * reference to an already copied list/dict can be used.
5878 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005879 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005880 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005881item_copy(
5882 typval_T *from,
5883 typval_T *to,
5884 int deep,
5885 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005886{
5887 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005888 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005889
Bram Moolenaar33570922005-01-25 22:26:29 +00005890 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005892 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005893 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005894 }
5895 ++recurse;
5896
5897 switch (from->v_type)
5898 {
5899 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005900 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005901 case VAR_STRING:
5902 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005903 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005904 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005905 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005906 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005907 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005908 copy_tv(from, to);
5909 break;
5910 case VAR_LIST:
5911 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005912 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005913 if (from->vval.v_list == NULL)
5914 to->vval.v_list = NULL;
5915 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5916 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005917 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005918 to->vval.v_list = from->vval.v_list->lv_copylist;
5919 ++to->vval.v_list->lv_refcount;
5920 }
5921 else
5922 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5923 if (to->vval.v_list == NULL)
5924 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005925 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005926 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005927 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005928 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005929 case VAR_DICT:
5930 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005931 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005932 if (from->vval.v_dict == NULL)
5933 to->vval.v_dict = NULL;
5934 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5935 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005936 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005937 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5938 ++to->vval.v_dict->dv_refcount;
5939 }
5940 else
5941 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5942 if (to->vval.v_dict == NULL)
5943 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005944 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005945 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005946 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005947 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005948 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005949 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005950 }
5951 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005952 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005953}
5954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005955 void
5956echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5957{
5958 char_u *tofree;
5959 char_u numbuf[NUMBUFLEN];
5960 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5961
5962 if (*atstart)
5963 {
5964 *atstart = FALSE;
5965 // Call msg_start() after eval1(), evaluating the expression
5966 // may cause a message to appear.
5967 if (with_space)
5968 {
5969 // Mark the saved text as finishing the line, so that what
5970 // follows is displayed on a new line when scrolling back
5971 // at the more prompt.
5972 msg_sb_eol();
5973 msg_start();
5974 }
5975 }
5976 else if (with_space)
5977 msg_puts_attr(" ", echo_attr);
5978
5979 if (p != NULL)
5980 for ( ; *p != NUL && !got_int; ++p)
5981 {
5982 if (*p == '\n' || *p == '\r' || *p == TAB)
5983 {
5984 if (*p != TAB && *needclr)
5985 {
5986 // remove any text still there from the command
5987 msg_clr_eos();
5988 *needclr = FALSE;
5989 }
5990 msg_putchar_attr(*p, echo_attr);
5991 }
5992 else
5993 {
5994 if (has_mbyte)
5995 {
5996 int i = (*mb_ptr2len)(p);
5997
5998 (void)msg_outtrans_len_attr(p, i, echo_attr);
5999 p += i - 1;
6000 }
6001 else
6002 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6003 }
6004 }
6005 vim_free(tofree);
6006}
6007
Bram Moolenaare9a41262005-01-15 22:18:47 +00006008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 * ":echo expr1 ..." print each argument separated with a space, add a
6010 * newline at the end.
6011 * ":echon expr1 ..." print each argument plain.
6012 */
6013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006014ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015{
6016 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 char_u *p;
6019 int needclr = TRUE;
6020 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006021 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006022 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006023 evalarg_T evalarg;
6024
Bram Moolenaare707c882020-07-01 13:07:37 +02006025 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026
6027 if (eap->skip)
6028 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006029 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006031 // If eval1() causes an error message the text from the command may
6032 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006033 need_clr_eos = needclr;
6034
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006036 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 {
6038 /*
6039 * Report the invalid expression unless the expression evaluation
6040 * has been cancelled due to an aborting error, an interrupt, or an
6041 * exception.
6042 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006043 if (!aborting() && did_emsg == did_emsg_before
6044 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006045 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006046 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 break;
6048 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006049 need_clr_eos = FALSE;
6050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006053
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006054 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 arg = skipwhite(arg);
6056 }
6057 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006058 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
6060 if (eap->skip)
6061 --emsg_skip;
6062 else
6063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006064 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 if (needclr)
6066 msg_clr_eos();
6067 if (eap->cmdidx == CMD_echo)
6068 msg_end();
6069 }
6070}
6071
6072/*
6073 * ":echohl {name}".
6074 */
6075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006076ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006078 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079}
6080
6081/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006082 * Returns the :echo attribute
6083 */
6084 int
6085get_echo_attr(void)
6086{
6087 return echo_attr;
6088}
6089
6090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 * ":execute expr1 ..." execute the result of an expression.
6092 * ":echomsg expr1 ..." Print a message
6093 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006094 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 * Each gets spaces around each argument and a newline at the end for
6096 * echo commands
6097 */
6098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100{
6101 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 int ret = OK;
6104 char_u *p;
6105 garray_T ga;
6106 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107
6108 ga_init2(&ga, 1, 80);
6109
6110 if (eap->skip)
6111 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006112 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006114 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006115 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117
6118 if (!eap->skip)
6119 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006120 char_u buf[NUMBUFLEN];
6121
6122 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006123 {
6124 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6125 {
6126 emsg(_(e_inval_string));
6127 p = NULL;
6128 }
6129 else
6130 p = tv_get_string_buf(&rettv, buf);
6131 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006132 else
6133 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006134 if (p == NULL)
6135 {
6136 clear_tv(&rettv);
6137 ret = FAIL;
6138 break;
6139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 len = (int)STRLEN(p);
6141 if (ga_grow(&ga, len + 2) == FAIL)
6142 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006143 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 ret = FAIL;
6145 break;
6146 }
6147 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 ga.ga_len += len;
6151 }
6152
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006153 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 arg = skipwhite(arg);
6155 }
6156
6157 if (ret != FAIL && ga.ga_data != NULL)
6158 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006159 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6160 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006161 // Mark the already saved text as finishing the line, so that what
6162 // follows is displayed on a new line when scrolling back at the
6163 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006164 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006165 }
6166
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006168 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006169 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006170 out_flush();
6171 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006172 else if (eap->cmdidx == CMD_echoconsole)
6173 {
6174 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6175 ui_write((char_u *)"\r\n", 2, TRUE);
6176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 else if (eap->cmdidx == CMD_echoerr)
6178 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006179 int save_did_emsg = did_emsg;
6180
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006181 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006182 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (!force_abort)
6184 did_emsg = save_did_emsg;
6185 }
6186 else if (eap->cmdidx == CMD_execute)
6187 do_cmdline((char_u *)ga.ga_data,
6188 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6189 }
6190
6191 ga_clear(&ga);
6192
6193 if (eap->skip)
6194 --emsg_skip;
6195
6196 eap->nextcmd = check_nextcmd(arg);
6197}
6198
6199/*
6200 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6201 * "arg" points to the "&" or '+' when called, to "option" when returning.
6202 * Returns NULL when no option name found. Otherwise pointer to the char
6203 * after the option name.
6204 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006205 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006206find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
6208 char_u *p = *arg;
6209
6210 ++p;
6211 if (*p == 'g' && p[1] == ':')
6212 {
6213 *opt_flags = OPT_GLOBAL;
6214 p += 2;
6215 }
6216 else if (*p == 'l' && p[1] == ':')
6217 {
6218 *opt_flags = OPT_LOCAL;
6219 p += 2;
6220 }
6221 else
6222 *opt_flags = 0;
6223
6224 if (!ASCII_ISALPHA(*p))
6225 return NULL;
6226 *arg = p;
6227
6228 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006229 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 else
6231 while (ASCII_ISALPHA(*p))
6232 ++p;
6233 return p;
6234}
6235
6236/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006237 * Display script name where an item was last set.
6238 * Should only be invoked when 'verbose' is non-zero.
6239 */
6240 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006241last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006242{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006243 char_u *p;
6244
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006245 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006246 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006247 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006248 if (p != NULL)
6249 {
6250 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006251 msg_puts(_("\n\tLast set from "));
6252 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006253 if (script_ctx.sc_lnum > 0)
6254 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006255 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006256 msg_outnum((long)script_ctx.sc_lnum);
6257 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006258 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006259 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006260 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006261 }
6262}
6263
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006264#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265
6266/*
6267 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006268 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 * "flags" can be "g" to do a global substitute.
6270 * Returns an allocated string, NULL for error.
6271 */
6272 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006273do_string_sub(
6274 char_u *str,
6275 char_u *pat,
6276 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006277 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006278 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
6280 int sublen;
6281 regmatch_T regmatch;
6282 int i;
6283 int do_all;
6284 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006285 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 garray_T ga;
6287 char_u *ret;
6288 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006289 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006291 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006293 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
6295 ga_init2(&ga, 1, 200);
6296
6297 do_all = (flags[0] == 'g');
6298
6299 regmatch.rm_ic = p_ic;
6300 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6301 if (regmatch.regprog != NULL)
6302 {
6303 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006304 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6306 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006307 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006308 if (regmatch.startp[0] == regmatch.endp[0])
6309 {
6310 if (zero_width == regmatch.startp[0])
6311 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006312 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006313 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006314 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6315 (size_t)i);
6316 ga.ga_len += i;
6317 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006318 continue;
6319 }
6320 zero_width = regmatch.startp[0];
6321 }
6322
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 /*
6324 * Get some space for a temporary buffer to do the substitution
6325 * into. It will contain:
6326 * - The text up to where the match is.
6327 * - The substituted text.
6328 * - The text after the match.
6329 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006330 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006331 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6333 {
6334 ga_clear(&ga);
6335 break;
6336 }
6337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006338 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 i = (int)(regmatch.startp[0] - tail);
6340 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006341 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006342 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 + ga.ga_len + i, TRUE, TRUE, FALSE);
6344 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006345 tail = regmatch.endp[0];
6346 if (*tail == NUL)
6347 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 if (!do_all)
6349 break;
6350 }
6351
6352 if (ga.ga_data != NULL)
6353 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6354
Bram Moolenaar473de612013-06-08 18:19:48 +02006355 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 }
6357
6358 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6359 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006360 if (p_cpo == empty_option)
6361 p_cpo = save_cpo;
6362 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006363 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006364 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006365 // If it's still empty it was changed and restored, need to restore in
6366 // the complicated way.
6367 if (*p_cpo == NUL)
6368 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006369 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371
6372 return ret;
6373}