blob: 99768ee280cda0c5a7ddb6be1e23ceca9d8b537d [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
1178 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001179 || lp->ll_n1 > bloblen
1180 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001181 {
1182 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001183 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001184 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001185 return NULL;
1186 }
1187 if (lp->ll_range && !lp->ll_empty2)
1188 {
1189 lp->ll_n2 = (long)tv_get_number(&var2);
1190 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001191 if (lp->ll_n2 < 0
1192 || lp->ll_n2 >= bloblen
1193 || lp->ll_n2 < lp->ll_n1)
1194 {
1195 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001196 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001197 return NULL;
1198 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001199 }
1200 lp->ll_blob = lp->ll_tv->vval.v_blob;
1201 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001202 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001203 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001204 else
1205 {
1206 /*
1207 * Get the number and item for the only or first index of the List.
1208 */
1209 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001211 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001212 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001213 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001214 clear_tv(&var1);
1215
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001216 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001218 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001219 if (lp->ll_li == NULL)
1220 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001221 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001222 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001225 }
1226
Bram Moolenaar10c65862020-10-08 21:16:42 +02001227 if (lp->ll_valtype != NULL)
1228 // use the type of the member
1229 lp->ll_valtype = lp->ll_valtype->tt_member;
1230
Bram Moolenaar8c711452005-01-14 21:53:12 +00001231 /*
1232 * May need to find the item or absolute index for the second
1233 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 * When no index given: "lp->ll_empty2" is TRUE.
1235 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001238 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001239 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001240 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001246 {
1247 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001248 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001250 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001252 }
1253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001254 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 if (lp->ll_n1 < 0)
1256 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1257 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001258 {
1259 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001260 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001262 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001263 }
1264
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001265 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001266 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 }
1268
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001269 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 return p;
1272}
1273
1274/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001275 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001278clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001279{
1280 vim_free(lp->ll_exp_name);
1281 vim_free(lp->ll_newkey);
1282}
1283
1284/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001285 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001286 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001287 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1288 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001289 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001291set_var_lval(
1292 lval_T *lp,
1293 char_u *endp,
1294 typval_T *rettv,
1295 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001296 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1297 char_u *op,
1298 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001299{
1300 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001301 listitem_T *ri;
1302 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001303
1304 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001306 cc = *endp;
1307 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001308 if (lp->ll_blob != NULL)
1309 {
1310 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001311
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001312 if (op != NULL && *op != '=')
1313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001314 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001315 return;
1316 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001317 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001318 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319
1320 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1321 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001322 if (lp->ll_empty2)
1323 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1324
Bram Moolenaar68452172021-04-12 21:21:02 +02001325 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1326 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001327 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001328 }
1329 else
1330 {
1331 val = (int)tv_get_number_chk(rettv, &error);
1332 if (!error)
1333 {
1334 garray_T *gap = &lp->ll_blob->bv_ga;
1335
1336 // Allow for appending a byte. Setting a byte beyond
1337 // the end is an error otherwise.
1338 if (lp->ll_n1 < gap->ga_len
1339 || (lp->ll_n1 == gap->ga_len
1340 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1341 {
1342 blob_set(lp->ll_blob, lp->ll_n1, val);
1343 if (lp->ll_n1 == gap->ga_len)
1344 ++gap->ga_len;
1345 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001346 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001347 }
1348 }
1349 }
1350 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001352 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001353
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001354 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1355 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001356 {
1357 emsg(_(e_cannot_mod));
1358 *endp = cc;
1359 return;
1360 }
1361
Bram Moolenaarff697e62019-02-12 22:28:33 +01001362 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001363 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001364 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001365 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001366 {
1367 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001368 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1369 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001370 && tv_op(&tv, rettv, op) == OK)
1371 set_var(lp->ll_name, &tv, FALSE);
1372 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001373 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001375 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001376 {
1377 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001378 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001379 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001380 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1381 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001382 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001383 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001384 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001385 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001386 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001387 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001388 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001389 else if (lp->ll_range)
1390 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001391 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001392 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001393
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001394 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1395 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001396 {
1397 emsg(_("E996: Cannot lock a range"));
1398 return;
1399 }
1400
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001401 /*
1402 * Check whether any of the list items is locked
1403 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001404 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001405 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001406 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001407 return;
1408 ri = ri->li_next;
1409 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1410 break;
1411 ll_li = ll_li->li_next;
1412 ++ll_n1;
1413 }
1414
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 /*
1416 * Assign the List values to the list items.
1417 */
1418 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001419 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001420 if (op != NULL && *op != '=')
1421 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1422 else
1423 {
1424 clear_tv(&lp->ll_li->li_tv);
1425 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1426 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001427 ri = ri->li_next;
1428 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1429 break;
1430 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001431 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001432 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001433 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001434 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001435 ri = NULL;
1436 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001437 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001438 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001439 lp->ll_li = lp->ll_li->li_next;
1440 ++lp->ll_n1;
1441 }
1442 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001443 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001444 else if (lp->ll_empty2
1445 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001447 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 }
1449 else
1450 {
1451 /*
1452 * Assign to a List or Dictionary item.
1453 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001454 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1455 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001456 {
1457 emsg(_("E996: Cannot lock a list or dict"));
1458 return;
1459 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001460
1461 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001462 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001463 return;
1464
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001465 if (lp->ll_newkey != NULL)
1466 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001467 if (op != NULL && *op != '=')
1468 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001469 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001470 return;
1471 }
1472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001473 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001474 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001475 if (di == NULL)
1476 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001477 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1478 {
1479 vim_free(di);
1480 return;
1481 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001482 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001483 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 else if (op != NULL && *op != '=')
1485 {
1486 tv_op(lp->ll_tv, rettv, op);
1487 return;
1488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001490 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001491
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001492 /*
1493 * Assign the value to the variable or list item.
1494 */
1495 if (copy)
1496 copy_tv(rettv, lp->ll_tv);
1497 else
1498 {
1499 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001500 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001501 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001502 }
1503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001504}
1505
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001506/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001507 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1508 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001509 * Returns OK or FAIL.
1510 */
1511 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001512tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001513{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001514 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 char_u numbuf[NUMBUFLEN];
1516 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001517 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001519 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001520 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001521 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001522 {
1523 switch (tv1->v_type)
1524 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001525 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001526 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001528 case VAR_DICT:
1529 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001530 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001531 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001532 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001533 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001534 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001535 break;
1536
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001537 case VAR_BLOB:
1538 if (*op != '+' || tv2->v_type != VAR_BLOB)
1539 break;
1540 // BLOB += BLOB
1541 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1542 {
1543 blob_T *b1 = tv1->vval.v_blob;
1544 blob_T *b2 = tv2->vval.v_blob;
1545 int i, len = blob_len(b2);
1546 for (i = 0; i < len; i++)
1547 ga_append(&b1->bv_ga, blob_get(b2, i));
1548 }
1549 return OK;
1550
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001551 case VAR_LIST:
1552 if (*op != '+' || tv2->v_type != VAR_LIST)
1553 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001554 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001555 if (tv2->vval.v_list != NULL)
1556 {
1557 if (tv1->vval.v_list == NULL)
1558 {
1559 tv1->vval.v_list = tv2->vval.v_list;
1560 ++tv1->vval.v_list->lv_refcount;
1561 }
1562 else
1563 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1564 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001565 return OK;
1566
1567 case VAR_NUMBER:
1568 case VAR_STRING:
1569 if (tv2->v_type == VAR_LIST)
1570 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001571 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001572 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001573 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001574 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001575#ifdef FEAT_FLOAT
1576 if (tv2->v_type == VAR_FLOAT)
1577 {
1578 float_T f = n;
1579
Bram Moolenaarff697e62019-02-12 22:28:33 +01001580 if (*op == '%')
1581 break;
1582 switch (*op)
1583 {
1584 case '+': f += tv2->vval.v_float; break;
1585 case '-': f -= tv2->vval.v_float; break;
1586 case '*': f *= tv2->vval.v_float; break;
1587 case '/': f /= tv2->vval.v_float; break;
1588 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589 clear_tv(tv1);
1590 tv1->v_type = VAR_FLOAT;
1591 tv1->vval.v_float = f;
1592 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001593 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001594#endif
1595 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001596 switch (*op)
1597 {
1598 case '+': n += tv_get_number(tv2); break;
1599 case '-': n -= tv_get_number(tv2); break;
1600 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001601 case '/': n = num_divide(n, tv_get_number(tv2),
1602 &failed); break;
1603 case '%': n = num_modulus(n, tv_get_number(tv2),
1604 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001605 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001606 clear_tv(tv1);
1607 tv1->v_type = VAR_NUMBER;
1608 tv1->vval.v_number = n;
1609 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001610 }
1611 else
1612 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001613 if (tv2->v_type == VAR_FLOAT)
1614 break;
1615
Bram Moolenaarff697e62019-02-12 22:28:33 +01001616 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001617 s = tv_get_string(tv1);
1618 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001619 clear_tv(tv1);
1620 tv1->v_type = VAR_STRING;
1621 tv1->vval.v_string = s;
1622 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001623 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001625 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001626#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001627 {
1628 float_T f;
1629
Bram Moolenaarff697e62019-02-12 22:28:33 +01001630 if (*op == '%' || *op == '.'
1631 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001632 && tv2->v_type != VAR_NUMBER
1633 && tv2->v_type != VAR_STRING))
1634 break;
1635 if (tv2->v_type == VAR_FLOAT)
1636 f = tv2->vval.v_float;
1637 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001638 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001639 switch (*op)
1640 {
1641 case '+': tv1->vval.v_float += f; break;
1642 case '-': tv1->vval.v_float -= f; break;
1643 case '*': tv1->vval.v_float *= f; break;
1644 case '/': tv1->vval.v_float /= f; break;
1645 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001646 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001647#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001648 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001649 }
1650 }
1651
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001652 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001653 return FAIL;
1654}
1655
1656/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657 * Evaluate the expression used in a ":for var in expr" command.
1658 * "arg" points to "var".
1659 * Set "*errp" to TRUE for an error, FALSE otherwise;
1660 * Return a pointer that holds the info. Null when there is an error.
1661 */
1662 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001663eval_for_line(
1664 char_u *arg,
1665 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001666 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001667 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668{
Bram Moolenaar33570922005-01-25 22:26:29 +00001669 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001671 typval_T tv;
1672 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001673 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001675 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001676
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001677 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 if (fi == NULL)
1679 return NULL;
1680
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001681 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682 if (expr == NULL)
1683 return fi;
1684
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001685 expr = skipwhite_and_linebreak(expr, evalarg);
1686 if (expr[0] != 'i' || expr[1] != 'n'
1687 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 return fi;
1691 }
1692
1693 if (skip)
1694 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001695 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1696 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 {
1698 *errp = FALSE;
1699 if (!skip)
1700 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001701 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001702 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001703 l = tv.vval.v_list;
1704 if (l == NULL)
1705 {
1706 // a null list is like an empty list: do nothing
1707 clear_tv(&tv);
1708 }
1709 else
1710 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001712 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001714 // No need to increment the refcount, it's already set for
1715 // the list being used in "tv".
1716 fi->fi_list = l;
1717 list_add_watch(l, &fi->fi_lw);
1718 fi->fi_lw.lw_item = l->lv_first;
1719 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001720 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001721 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001722 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001723 fi->fi_bi = 0;
1724 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001725 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001726 typval_T btv;
1727
1728 // Make a copy, so that the iteration still works when the
1729 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001731 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001732 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001733 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001734 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001735 else if (tv.v_type == VAR_STRING)
1736 {
1737 fi->fi_byte_idx = 0;
1738 fi->fi_string = tv.vval.v_string;
1739 tv.vval.v_string = NULL;
1740 if (fi->fi_string == NULL)
1741 fi->fi_string = vim_strsave((char_u *)"");
1742 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743 else
1744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001745 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001746 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 }
1748 }
1749 }
1750 if (skip)
1751 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001752 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753
1754 return fi;
1755}
1756
1757/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001758 * Used when looping over a :for line, skip the "in expr" part.
1759 */
1760 void
1761skip_for_lines(void *fi_void, evalarg_T *evalarg)
1762{
1763 forinfo_T *fi = (forinfo_T *)fi_void;
1764 int i;
1765
1766 for (i = 0; i < fi->fi_break_count; ++i)
1767 eval_next_line(evalarg);
1768}
1769
1770/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 * Use the first item in a ":for" list. Advance to the next.
1772 * Assign the values to the variable (list). "arg" points to the first one.
1773 * Return TRUE when a valid item was found, FALSE when at end of list or
1774 * something wrong.
1775 */
1776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001777next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001779 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001781 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1782 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1783 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001786 if (fi->fi_blob != NULL)
1787 {
1788 typval_T tv;
1789
1790 if (fi->fi_bi >= blob_len(fi->fi_blob))
1791 return FALSE;
1792 tv.v_type = VAR_NUMBER;
1793 tv.v_lock = VAR_FIXED;
1794 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1795 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001796 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001797 fi->fi_varcount, flag, NULL) == OK;
1798 }
1799
1800 if (fi->fi_string != NULL)
1801 {
1802 typval_T tv;
1803 int len;
1804
1805 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1806 if (len == 0)
1807 return FALSE;
1808 tv.v_type = VAR_STRING;
1809 tv.v_lock = VAR_FIXED;
1810 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1811 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001812 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001813 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001814 vim_free(tv.vval.v_string);
1815 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001816 }
1817
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 item = fi->fi_lw.lw_item;
1819 if (item == NULL)
1820 result = FALSE;
1821 else
1822 {
1823 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001824 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001825 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001826 }
1827 return result;
1828}
1829
1830/*
1831 * Free the structure used to store info used by ":for".
1832 */
1833 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001834free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001835{
Bram Moolenaar33570922005-01-25 22:26:29 +00001836 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001838 if (fi == NULL)
1839 return;
1840 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001841 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001843 list_unref(fi->fi_list);
1844 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001845 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001846 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001847 else
1848 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849 vim_free(fi);
1850}
1851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001853set_context_for_expression(
1854 expand_T *xp,
1855 char_u *arg,
1856 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001858 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001862 if (cmdidx == CMD_let || cmdidx == CMD_var
1863 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 {
1865 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001866 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001868 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001869 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001870 {
1871 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001872 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001873 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 break;
1875 }
1876 return;
1877 }
1878 }
1879 else
1880 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1881 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 while ((xp->xp_pattern = vim_strpbrk(arg,
1883 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1884 {
1885 c = *xp->xp_pattern;
1886 if (c == '&')
1887 {
1888 c = xp->xp_pattern[1];
1889 if (c == '&')
1890 {
1891 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001892 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 }
1894 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001897 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1898 xp->xp_pattern += 2;
1899
1900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
1902 else if (c == '$')
1903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001904 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 xp->xp_context = EXPAND_ENV_VARS;
1906 }
1907 else if (c == '=')
1908 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001909 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 xp->xp_context = EXPAND_EXPRESSION;
1911 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001912 else if (c == '#'
1913 && xp->xp_context == EXPAND_EXPRESSION)
1914 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001915 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001916 break;
1917 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001918 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 && xp->xp_context == EXPAND_FUNCTIONS
1920 && vim_strchr(xp->xp_pattern, '(') == NULL)
1921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001922 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 break;
1924 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001925 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001927 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
1929 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1930 if (c == '\\' && xp->xp_pattern[1] != NUL)
1931 ++xp->xp_pattern;
1932 xp->xp_context = EXPAND_NOTHING;
1933 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001934 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001936 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1938 /* skip */ ;
1939 xp->xp_context = EXPAND_NOTHING;
1940 }
1941 else if (c == '|')
1942 {
1943 if (xp->xp_pattern[1] == '|')
1944 {
1945 ++xp->xp_pattern;
1946 xp->xp_context = EXPAND_EXPRESSION;
1947 }
1948 else
1949 xp->xp_context = EXPAND_COMMANDS;
1950 }
1951 else
1952 xp->xp_context = EXPAND_EXPRESSION;
1953 }
1954 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001955 // Doesn't look like something valid, expand as an expression
1956 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 arg = xp->xp_pattern;
1959 if (*arg != NUL)
1960 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1961 /* skip */ ;
1962 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001963
1964 // ":exe one two" completes "two"
1965 if ((cmdidx == CMD_execute
1966 || cmdidx == CMD_echo
1967 || cmdidx == CMD_echon
1968 || cmdidx == CMD_echomsg)
1969 && xp->xp_context == EXPAND_EXPRESSION)
1970 {
1971 for (;;)
1972 {
1973 char_u *n = skiptowhite(arg);
1974
1975 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1976 break;
1977 arg = skipwhite(n);
1978 }
1979 }
1980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 xp->xp_pattern = arg;
1982}
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001985 * Return TRUE if "pat" matches "text".
1986 * Does not use 'cpo' and always uses 'magic'.
1987 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001988 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001989pattern_match(char_u *pat, char_u *text, int ic)
1990{
1991 int matches = FALSE;
1992 char_u *save_cpo;
1993 regmatch_T regmatch;
1994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001995 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001996 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001997 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001998 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1999 if (regmatch.regprog != NULL)
2000 {
2001 regmatch.rm_ic = ic;
2002 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2003 vim_regfree(regmatch.regprog);
2004 }
2005 p_cpo = save_cpo;
2006 return matches;
2007}
2008
2009/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002010 * Handle a name followed by "(". Both for just "name(arg)" and for
2011 * "expr->name(arg)".
2012 * Returns OK or FAIL.
2013 */
2014 static int
2015eval_func(
2016 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002017 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002018 char_u *name,
2019 int name_len,
2020 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002021 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002022 typval_T *basetv) // "expr" for "expr->name(arg)"
2023{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002024 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002025 char_u *s = name;
2026 int len = name_len;
2027 partial_T *partial;
2028 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002029 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002030
2031 if (!evaluate)
2032 check_vars(s, len);
2033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002034 // If "s" is the name of a variable of type VAR_FUNC
2035 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002036 s = deref_func_name(s, &len, &partial,
2037 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002039 // Need to make a copy, in case evaluating the arguments makes
2040 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002041 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002042 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002043 ret = FAIL;
2044 else
2045 {
2046 funcexe_T funcexe;
2047
2048 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002049 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002050 funcexe.firstline = curwin->w_cursor.lnum;
2051 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002052 funcexe.evaluate = evaluate;
2053 funcexe.partial = partial;
2054 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002055 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002056 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002057 }
2058 vim_free(s);
2059
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002060 // If evaluate is FALSE rettv->v_type was not set in
2061 // get_func_tv, but it's needed in handle_subscript() to parse
2062 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002063 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2064 {
2065 rettv->vval.v_string = NULL;
2066 rettv->v_type = VAR_FUNC;
2067 }
2068
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002069 // Stop the expression evaluation when immediately
2070 // aborting on error, or when an interrupt occurred or
2071 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002072 if (evaluate && aborting())
2073 {
2074 if (ret == OK)
2075 clear_tv(rettv);
2076 ret = FAIL;
2077 }
2078 return ret;
2079}
2080
2081/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002082 * Get the next line source line without advancing. But do skip over comment
2083 * lines.
2084 */
2085 static char_u *
2086getline_peek_skip_comments(evalarg_T *evalarg)
2087{
2088 for (;;)
2089 {
2090 char_u *next = getline_peek(evalarg->eval_getline,
2091 evalarg->eval_cookie);
2092 char_u *p;
2093
2094 if (next == NULL)
2095 break;
2096 p = skipwhite(next);
2097 if (*p != NUL && !vim9_comment_start(p))
2098 return next;
2099 (void)eval_next_line(evalarg);
2100 }
2101 return NULL;
2102}
2103
2104/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002105 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2106 * comment) and there is a next line, return the next line (skipping blanks)
2107 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002108 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2109 * "arg" must point somewhere inside a line, not at the start.
2110 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002111 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002112eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2113{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002114 char_u *p = skipwhite(arg);
2115
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002117 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002118 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002119 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002120 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002121 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002122 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002123
2124 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002125 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002126 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002127 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002128
Bram Moolenaar9d489562020-07-30 20:08:50 +02002129 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002130 {
2131 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002132 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002133 }
2134 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002135 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002136}
2137
2138/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002139 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002140 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002141 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002142eval_next_line(evalarg_T *evalarg)
2143{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002144 garray_T *gap = &evalarg->eval_ga;
2145 char_u *line;
2146
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002147 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002148 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2149 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002150 else
2151 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002152 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002153 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2154 {
2155 // Going to concatenate the lines after parsing.
2156 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2157 ++gap->ga_len;
2158 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002159 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002160 {
2161 vim_free(evalarg->eval_tofree);
2162 evalarg->eval_tofree = line;
2163 }
2164 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002165}
2166
Bram Moolenaare6b53242020-07-01 17:28:33 +02002167/*
2168 * Call eval_next_non_blank() and get the next line if needed.
2169 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002170 char_u *
2171skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2172{
2173 int getnext;
2174 char_u *p = skipwhite(arg);
2175
Bram Moolenaar962d7212020-07-04 14:15:00 +02002176 if (evalarg == NULL)
2177 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002178 eval_next_non_blank(p, evalarg, &getnext);
2179 if (getnext)
2180 return eval_next_line(evalarg);
2181 return p;
2182}
2183
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002184/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002185 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002186 */
2187 void
2188clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2189{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002190 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002191 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002192 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002193 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002194 if (eap != NULL)
2195 {
2196 // We may need to keep the original command line, e.g. for
2197 // ":let" it has the variable names. But we may also need the
2198 // new one, "nextcmd" points into it. Keep both.
2199 vim_free(eap->cmdline_tofree);
2200 eap->cmdline_tofree = *eap->cmdlinep;
2201 *eap->cmdlinep = evalarg->eval_tofree;
2202 }
2203 else
2204 vim_free(evalarg->eval_tofree);
2205 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002206 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002207
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002208 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2209 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002210 }
2211}
2212
2213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2217 */
2218
2219/*
2220 * Handle zero level expression.
2221 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002223 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002224 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 * Return OK or FAIL.
2226 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002227 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228eval0(
2229 char_u *arg,
2230 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002231 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002232 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
2234 int ret;
2235 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002236 int did_emsg_before = did_emsg;
2237 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002238 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002241 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002242 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002243
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002244 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
2246 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 /*
2249 * Report the invalid expression unless the expression evaluation has
2250 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002251 * exception, or we already gave a more specific error.
2252 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002254 if (!aborting()
2255 && did_emsg == did_emsg_before
2256 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002257 && (flags & EVAL_CONSTANT) == 0
2258 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002259 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002260
2261 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002262 // a next command to avoid more errors, unless "|" is following, which
2263 // could only be a command separator.
2264 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2265 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002266 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002268
2269 if (eap != NULL)
2270 eap->nextcmd = check_nextcmd(p);
2271
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 return ret;
2273}
2274
2275/*
2276 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002277 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002278 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 *
2280 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002281 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002283 * Note: "rettv.v_lock" is not set.
2284 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 * Return OK or FAIL.
2286 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002287 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002288eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002290 char_u *p;
2291 int getnext;
2292
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002293 CLEAR_POINTER(rettv);
2294
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 /*
2296 * Get the first variable.
2297 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002298 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 return FAIL;
2300
Bram Moolenaar793648f2020-06-26 21:28:25 +02002301 p = eval_next_non_blank(*arg, evalarg, &getnext);
2302 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002304 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002305 int result;
2306 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002307 evalarg_T *evalarg_used = evalarg;
2308 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002309 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002310 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002311 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002312
2313 if (evalarg == NULL)
2314 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002315 CLEAR_FIELD(local_evalarg);
2316 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002317 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002318 orig_flags = evalarg_used->eval_flags;
2319 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002320
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002321 if (getnext)
2322 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002323 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002324 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002325 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002326 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002327 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002328 clear_tv(rettv);
2329 return FAIL;
2330 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002331 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002332 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002335 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002337 int error = FALSE;
2338
Bram Moolenaar13106602020-10-04 16:06:05 +02002339 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002340 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002341 else if (vim9script)
2342 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002343 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002345 if (error || !op_falsy || !result)
2346 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002347 if (error)
2348 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 }
2350
2351 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002352 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002354 if (op_falsy)
2355 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002356 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002357 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002358 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002359 clear_tv(rettv);
2360 return FAIL;
2361 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002362 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002363 evalarg_used->eval_flags = (op_falsy ? !result : result)
2364 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2365 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002366 {
2367 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002369 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002370 if (!op_falsy || !result)
2371 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002373 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002375 /*
2376 * Check for the ":".
2377 */
2378 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2379 if (*p != ':')
2380 {
2381 emsg(_(e_missing_colon));
2382 if (evaluate && result)
2383 clear_tv(rettv);
2384 evalarg_used->eval_flags = orig_flags;
2385 return FAIL;
2386 }
2387 if (getnext)
2388 *arg = eval_next_line(evalarg_used);
2389 else
2390 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002391 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002392 {
2393 error_white_both(p, 1);
2394 clear_tv(rettv);
2395 evalarg_used->eval_flags = orig_flags;
2396 return FAIL;
2397 }
2398 *arg = p;
2399 }
2400
2401 /*
2402 * Get the third variable. Recursive!
2403 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002404 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002405 {
2406 error_white_both(p, 1);
2407 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002408 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002409 return FAIL;
2410 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002411 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2412 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002413 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002414 if (eval1(arg, &var2, evalarg_used) == FAIL)
2415 {
2416 if (evaluate && result)
2417 clear_tv(rettv);
2418 evalarg_used->eval_flags = orig_flags;
2419 return FAIL;
2420 }
2421 if (evaluate && !result)
2422 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002424
2425 if (evalarg == NULL)
2426 clear_evalarg(&local_evalarg, NULL);
2427 else
2428 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430
2431 return OK;
2432}
2433
2434/*
2435 * Handle first level expression:
2436 * expr2 || expr2 || expr2 logical OR
2437 *
2438 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002439 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 *
2441 * Return OK or FAIL.
2442 */
2443 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002444eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002446 char_u *p;
2447 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 /*
2450 * Get the first variable.
2451 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002452 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 return FAIL;
2454
2455 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002456 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002458 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002459 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002461 evalarg_T *evalarg_used = evalarg;
2462 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002463 int evaluate;
2464 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002465 long result = FALSE;
2466 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002467 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002468 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002469
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002470 if (evalarg == NULL)
2471 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002472 CLEAR_FIELD(local_evalarg);
2473 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002474 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002475 orig_flags = evalarg_used->eval_flags;
2476 evaluate = orig_flags & EVAL_EVALUATE;
2477 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002478 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002479 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002480 result = tv_get_bool_chk(rettv, &error);
2481 else if (tv_get_number_chk(rettv, &error) != 0)
2482 result = TRUE;
2483 clear_tv(rettv);
2484 if (error)
2485 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
2487
2488 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002489 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002491 while (p[0] == '|' && p[1] == '|')
2492 {
2493 if (getnext)
2494 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002495 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002496 {
2497 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2498 {
2499 error_white_both(p, 2);
2500 clear_tv(rettv);
2501 return FAIL;
2502 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002503 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002504 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002505
2506 /*
2507 * Get the second variable.
2508 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002509 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2510 {
2511 error_white_both(p, 2);
2512 clear_tv(rettv);
2513 return FAIL;
2514 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002515 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2516 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002517 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002518 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002519 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002520
2521 /*
2522 * Compute the result.
2523 */
2524 if (evaluate && !result)
2525 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002526 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002527 result = tv_get_bool_chk(&var2, &error);
2528 else if (tv_get_number_chk(&var2, &error) != 0)
2529 result = TRUE;
2530 clear_tv(&var2);
2531 if (error)
2532 return FAIL;
2533 }
2534 if (evaluate)
2535 {
2536 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002537 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002538 rettv->v_type = VAR_BOOL;
2539 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002540 }
2541 else
2542 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002543 rettv->v_type = VAR_NUMBER;
2544 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002545 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002546 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002547
2548 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002550
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002551 if (evalarg == NULL)
2552 clear_evalarg(&local_evalarg, NULL);
2553 else
2554 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 }
2556
2557 return OK;
2558}
2559
2560/*
2561 * Handle second level expression:
2562 * expr3 && expr3 && expr3 logical AND
2563 *
2564 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002565 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 *
2567 * Return OK or FAIL.
2568 */
2569 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002570eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002572 char_u *p;
2573 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
2575 /*
2576 * Get the first variable.
2577 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002578 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 return FAIL;
2580
2581 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002584 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002585 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002587 evalarg_T *evalarg_used = evalarg;
2588 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002589 int orig_flags;
2590 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002591 long result = TRUE;
2592 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002593 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002594 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002595
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002596 if (evalarg == NULL)
2597 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598 CLEAR_FIELD(local_evalarg);
2599 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002600 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002601 orig_flags = evalarg_used->eval_flags;
2602 evaluate = orig_flags & EVAL_EVALUATE;
2603 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002604 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002605 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002606 result = tv_get_bool_chk(rettv, &error);
2607 else if (tv_get_number_chk(rettv, &error) == 0)
2608 result = FALSE;
2609 clear_tv(rettv);
2610 if (error)
2611 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
2613
2614 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002615 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002617 while (p[0] == '&' && p[1] == '&')
2618 {
2619 if (getnext)
2620 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002621 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002622 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002623 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002624 {
2625 error_white_both(p, 2);
2626 clear_tv(rettv);
2627 return FAIL;
2628 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002629 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002630 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002631
2632 /*
2633 * Get the second variable.
2634 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002635 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2636 {
2637 error_white_both(p, 2);
2638 clear_tv(rettv);
2639 return FAIL;
2640 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002641 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2642 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002643 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002644 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002645 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002646 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002647
2648 /*
2649 * Compute the result.
2650 */
2651 if (evaluate && result)
2652 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002653 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002654 result = tv_get_bool_chk(&var2, &error);
2655 else if (tv_get_number_chk(&var2, &error) == 0)
2656 result = FALSE;
2657 clear_tv(&var2);
2658 if (error)
2659 return FAIL;
2660 }
2661 if (evaluate)
2662 {
2663 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002664 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002665 rettv->v_type = VAR_BOOL;
2666 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002667 }
2668 else
2669 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002670 rettv->v_type = VAR_NUMBER;
2671 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002672 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002673 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002674
2675 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002677
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002678 if (evalarg == NULL)
2679 clear_evalarg(&local_evalarg, NULL);
2680 else
2681 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 }
2683
2684 return OK;
2685}
2686
2687/*
2688 * Handle third level expression:
2689 * var1 == var2
2690 * var1 =~ var2
2691 * var1 != var2
2692 * var1 !~ var2
2693 * var1 > var2
2694 * var1 >= var2
2695 * var1 < var2
2696 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002697 * var1 is var2
2698 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 *
2700 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002701 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 *
2703 * Return OK or FAIL.
2704 */
2705 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002706eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002709 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002710 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002712 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
2714 /*
2715 * Get the first variable.
2716 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002717 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 return FAIL;
2719
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002720 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002721 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
2723 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002724 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002726 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002728 typval_T var2;
2729 int ic;
2730 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002731 int evaluate = evalarg == NULL
2732 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002733
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002734 if (getnext)
2735 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002736 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2737 {
2738 error_white_both(p, len);
2739 clear_tv(rettv);
2740 return FAIL;
2741 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002742
Bram Moolenaar696ba232020-07-29 21:20:41 +02002743 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2744 {
2745 semsg(_(e_invexpr2), p);
2746 clear_tv(rettv);
2747 return FAIL;
2748 }
2749
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002750 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (p[len] == '?')
2752 {
2753 ic = TRUE;
2754 ++len;
2755 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002756 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 else if (p[len] == '#')
2758 {
2759 ic = FALSE;
2760 ++len;
2761 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002762 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002764 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766 /*
2767 * Get the second variable.
2768 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002769 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2770 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002771 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002772 clear_tv(rettv);
2773 return FAIL;
2774 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002775 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002776 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002778 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 return FAIL;
2780 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002781 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002782 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002783 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002784
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002785 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002786 {
2787 ret = FAIL;
2788 clear_tv(rettv);
2789 }
2790 else
2791 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002792 clear_tv(&var2);
2793 return ret;
2794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796
2797 return OK;
2798}
2799
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002800/*
2801 * Make a copy of blob "tv1" and append blob "tv2".
2802 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 void
2804eval_addblob(typval_T *tv1, typval_T *tv2)
2805{
2806 blob_T *b1 = tv1->vval.v_blob;
2807 blob_T *b2 = tv2->vval.v_blob;
2808 blob_T *b = blob_alloc();
2809 int i;
2810
2811 if (b != NULL)
2812 {
2813 for (i = 0; i < blob_len(b1); i++)
2814 ga_append(&b->bv_ga, blob_get(b1, i));
2815 for (i = 0; i < blob_len(b2); i++)
2816 ga_append(&b->bv_ga, blob_get(b2, i));
2817
2818 clear_tv(tv1);
2819 rettv_blob_set(tv1, b);
2820 }
2821}
2822
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002823/*
2824 * Make a copy of list "tv1" and append list "tv2".
2825 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 int
2827eval_addlist(typval_T *tv1, typval_T *tv2)
2828{
2829 typval_T var3;
2830
2831 // concatenate Lists
2832 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2833 {
2834 clear_tv(tv1);
2835 clear_tv(tv2);
2836 return FAIL;
2837 }
2838 clear_tv(tv1);
2839 *tv1 = var3;
2840 return OK;
2841}
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843/*
2844 * Handle fourth level expression:
2845 * + number addition
2846 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002847 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002848 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 *
2850 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002851 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 *
2853 * Return OK or FAIL.
2854 */
2855 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002856eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 /*
2859 * Get the first variable.
2860 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002861 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 return FAIL;
2863
2864 /*
2865 * Repeat computing, until no '+', '-' or '.' is following.
2866 */
2867 for (;;)
2868 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002869 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002870 int getnext;
2871 char_u *p;
2872 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002873 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002874 int concat;
2875 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002876 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002877
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002878 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002879 // "+=", "-=" and "..=" are assignments
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002880 p = eval_next_non_blank(*arg, evalarg, &getnext);
2881 op = *p;
2882 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002883 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2884 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002886
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002887 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002888 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002889 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002890 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002891 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002892 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002893 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002894 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002895 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002896 clear_tv(rettv);
2897 return FAIL;
2898 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002899 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002900 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002901 if ((op != '+' || (rettv->v_type != VAR_LIST
2902 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002903#ifdef FEAT_FLOAT
2904 && (op == '.' || rettv->v_type != VAR_FLOAT)
2905#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002906 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002907 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002908 int error = FALSE;
2909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002910 // For "list + ...", an illegal use of the first operand as
2911 // a number cannot be determined before evaluating the 2nd
2912 // operand: if this is also a list, all is ok.
2913 // For "something . ...", "something - ..." or "non-list + ...",
2914 // we know that the first operand needs to be a string or number
2915 // without evaluating the 2nd operand. So check before to avoid
2916 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002917 if (op != '.')
2918 tv_get_number_chk(rettv, &error);
2919 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002920 {
2921 clear_tv(rettv);
2922 return FAIL;
2923 }
2924 }
2925
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 /*
2927 * Get the second variable.
2928 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002929 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002930 {
2931 error_white_both(p, oplen);
2932 clear_tv(rettv);
2933 return FAIL;
2934 }
2935 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002936 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002938 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 return FAIL;
2940 }
2941
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002942 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 {
2944 /*
2945 * Compute the result.
2946 */
2947 if (op == '.')
2948 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002949 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2950 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002951 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002952
Bram Moolenaar2e086612020-08-25 22:37:48 +02002953 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002954 || var2.v_type == VAR_CHANNEL
2955 || var2.v_type == VAR_JOB))
2956 emsg(_(e_inval_string));
2957#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002958 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002959 {
2960 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2961 var2.vval.v_float);
2962 s2 = buf2;
2963 }
2964#endif
2965 else
2966 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002967 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002968 {
2969 clear_tv(rettv);
2970 clear_tv(&var2);
2971 return FAIL;
2972 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002974 clear_tv(rettv);
2975 rettv->v_type = VAR_STRING;
2976 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002978 else if (op == '+' && rettv->v_type == VAR_BLOB
2979 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002981 else if (op == '+' && rettv->v_type == VAR_LIST
2982 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002983 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002985 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 else
2988 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002989 int error = FALSE;
2990 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002991#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002992 float_T f1 = 0, f2 = 0;
2993
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002994 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002995 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002996 f1 = rettv->vval.v_float;
2997 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002998 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002999 else
3000#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003001 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003002 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003003 if (error)
3004 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003005 // This can only happen for "list + non-list" or
3006 // "blob + non-blob". For "non-list + ..." or
3007 // "something - ...", we returned before evaluating the
3008 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003009 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003010 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003011 return FAIL;
3012 }
3013#ifdef FEAT_FLOAT
3014 if (var2.v_type == VAR_FLOAT)
3015 f1 = n1;
3016#endif
3017 }
3018#ifdef FEAT_FLOAT
3019 if (var2.v_type == VAR_FLOAT)
3020 {
3021 f2 = var2.vval.v_float;
3022 n2 = 0;
3023 }
3024 else
3025#endif
3026 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003027 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003028 if (error)
3029 {
3030 clear_tv(rettv);
3031 clear_tv(&var2);
3032 return FAIL;
3033 }
3034#ifdef FEAT_FLOAT
3035 if (rettv->v_type == VAR_FLOAT)
3036 f2 = n2;
3037#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003038 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003039 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003040
3041#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003042 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003043 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3044 {
3045 if (op == '+')
3046 f1 = f1 + f2;
3047 else
3048 f1 = f1 - f2;
3049 rettv->v_type = VAR_FLOAT;
3050 rettv->vval.v_float = f1;
3051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003053#endif
3054 {
3055 if (op == '+')
3056 n1 = n1 + n2;
3057 else
3058 n1 = n1 - n2;
3059 rettv->v_type = VAR_NUMBER;
3060 rettv->vval.v_number = n1;
3061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003063 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 }
3065 }
3066 return OK;
3067}
3068
3069/*
3070 * Handle fifth level expression:
3071 * * number multiplication
3072 * / number division
3073 * % number modulo
3074 *
3075 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003076 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 *
3078 * Return OK or FAIL.
3079 */
3080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003081eval6(
3082 char_u **arg,
3083 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003084 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003085 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003087#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003088 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
3091 /*
3092 * Get the first variable.
3093 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003094 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 return FAIL;
3096
3097 /*
3098 * Repeat computing, until no '*', '/' or '%' is following.
3099 */
3100 for (;;)
3101 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003102 int evaluate;
3103 int getnext;
3104 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003105 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003106 int op;
3107 varnumber_T n1, n2;
3108#ifdef FEAT_FLOAT
3109 float_T f1, f2;
3110#endif
3111 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003112
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003113 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003114 p = eval_next_non_blank(*arg, evalarg, &getnext);
3115 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003116 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003118
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003119 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003120 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003121 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003122 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003123 {
3124 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3125 {
3126 error_white_both(p, 1);
3127 clear_tv(rettv);
3128 return FAIL;
3129 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003130 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003133#ifdef FEAT_FLOAT
3134 f1 = 0;
3135 f2 = 0;
3136#endif
3137 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003138 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003140#ifdef FEAT_FLOAT
3141 if (rettv->v_type == VAR_FLOAT)
3142 {
3143 f1 = rettv->vval.v_float;
3144 use_float = TRUE;
3145 n1 = 0;
3146 }
3147 else
3148#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003149 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003150 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003151 if (error)
3152 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 }
3154 else
3155 n1 = 0;
3156
3157 /*
3158 * Get the second variable.
3159 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003160 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3161 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003162 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003163 clear_tv(rettv);
3164 return FAIL;
3165 }
3166 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003167 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 return FAIL;
3169
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003170 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003172#ifdef FEAT_FLOAT
3173 if (var2.v_type == VAR_FLOAT)
3174 {
3175 if (!use_float)
3176 {
3177 f1 = n1;
3178 use_float = TRUE;
3179 }
3180 f2 = var2.vval.v_float;
3181 n2 = 0;
3182 }
3183 else
3184#endif
3185 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003186 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003187 clear_tv(&var2);
3188 if (error)
3189 return FAIL;
3190#ifdef FEAT_FLOAT
3191 if (use_float)
3192 f2 = n2;
3193#endif
3194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196 /*
3197 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003198 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003200#ifdef FEAT_FLOAT
3201 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003203 if (op == '*')
3204 f1 = f1 * f2;
3205 else if (op == '/')
3206 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003207# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003208 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003209 if (f2 == 0.0)
3210 {
3211 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003212 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003213 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003214 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003215 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003216 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003217 }
3218 else
3219 f1 = f1 / f2;
3220# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003221 // We rely on the floating point library to handle divide
3222 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003223 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003224# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003227 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003229 return FAIL;
3230 }
3231 rettv->v_type = VAR_FLOAT;
3232 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
3234 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003237 int failed = FALSE;
3238
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003239 if (op == '*')
3240 n1 = n1 * n2;
3241 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003242 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003244 n1 = num_modulus(n1, n2, &failed);
3245 if (failed)
3246 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003247
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003248 rettv->v_type = VAR_NUMBER;
3249 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 }
3252 }
3253
3254 return OK;
3255}
3256
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003257 int
3258eval_leader(char_u **arg, int vim9)
3259{
3260 char_u *s = *arg;
3261 char_u *p = *arg;
3262
3263 while (*p == '!' || *p == '-' || *p == '+')
3264 {
3265 char_u *n = skipwhite(p + 1);
3266
3267 // ++, --, -+ and +- are not accepted in Vim9 script
3268 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3269 {
3270 semsg(_(e_invexpr2), s);
3271 return FAIL;
3272 }
3273 p = n;
3274 }
3275 *arg = p;
3276 return OK;
3277}
3278
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279/*
3280 * Handle sixth level expression:
3281 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003282 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003283 * "string" string constant
3284 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 * &option-name option value
3286 * @r register contents
3287 * identifier variable value
3288 * function() function call
3289 * $VAR environment variable
3290 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003291 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003293 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003294 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 *
3296 * Also handle:
3297 * ! in front logical NOT
3298 * - in front unary minus
3299 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003300 * trailing [] subscript in String or List
3301 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003302 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 *
3304 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003305 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 *
3307 * Return OK or FAIL.
3308 */
3309 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003310eval7(
3311 char_u **arg,
3312 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003313 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003316 int evaluate = evalarg != NULL
3317 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 int len;
3319 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 char_u *start_leader, *end_leader;
3321 int ret = OK;
3322 char_u *alias;
3323
3324 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003325 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003326 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003328 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329
3330 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003331 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 */
3333 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003334 if (eval_leader(arg, in_vim9script()) == FAIL)
3335 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 end_leader = *arg;
3337
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003338 if (**arg == '.' && (!isdigit(*(*arg + 1))
3339#ifdef FEAT_FLOAT
3340 || current_sctx.sc_version < 2
3341#endif
3342 ))
3343 {
3344 semsg(_(e_invexpr2), *arg);
3345 ++*arg;
3346 return FAIL;
3347 }
3348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 switch (**arg)
3350 {
3351 /*
3352 * Number constant.
3353 */
3354 case '0':
3355 case '1':
3356 case '2':
3357 case '3':
3358 case '4':
3359 case '5':
3360 case '6':
3361 case '7':
3362 case '8':
3363 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003364 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003365
3366 // Apply prefixed "-" and "+" now. Matters especially when
3367 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003368 if (ret == OK && evaluate && end_leader > start_leader
3369 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003370 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 break;
3372
3373 /*
3374 * String constant: "string".
3375 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003376 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 break;
3378
3379 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003380 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003382 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003383 break;
3384
3385 /*
3386 * List: [expr, expr]
3387 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003388 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 break;
3390
3391 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003392 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003393 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003394 case '#': if (in_vim9script())
3395 {
3396 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3397 }
3398 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003399 {
3400 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003401 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003402 }
3403 else
3404 ret = NOTDONE;
3405 break;
3406
3407 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003408 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003409 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003410 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003411 case '{': if (in_vim9script())
3412 ret = NOTDONE;
3413 else
3414 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003415 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003416 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003417 break;
3418
3419 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003420 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003422 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 break;
3424
3425 /*
3426 * Environment variable: $VAR.
3427 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003428 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 break;
3430
3431 /*
3432 * Register contents: @r.
3433 */
3434 case '@': ++*arg;
3435 if (evaluate)
3436 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003437 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3438 semsg(_(e_syntax_error_at_str), *arg);
3439 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3440 emsg_invreg(**arg);
3441 else
3442 {
3443 rettv->v_type = VAR_STRING;
3444 rettv->vval.v_string = get_reg_contents(**arg,
3445 GREG_EXPR_SRC);
3446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448 if (**arg != NUL)
3449 ++*arg;
3450 break;
3451
3452 /*
3453 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003454 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003456 case '(': ret = NOTDONE;
3457 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003458 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003459 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003460 if (ret == OK && evaluate)
3461 {
3462 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3463
3464 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003465 if (compile_def_function(ufunc,
3466 TRUE, PROFILING(ufunc), NULL) == FAIL)
3467 {
3468 clear_tv(rettv);
3469 ret = FAIL;
3470 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003471 }
3472 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003473 if (ret == NOTDONE)
3474 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003475 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003476 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003477
Bram Moolenaar9215f012020-06-27 21:18:00 +02003478 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003479 if (**arg == ')')
3480 ++*arg;
3481 else if (ret == OK)
3482 {
3483 emsg(_(e_missing_close));
3484 clear_tv(rettv);
3485 ret = FAIL;
3486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488 break;
3489
Bram Moolenaar8c711452005-01-14 21:53:12 +00003490 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 break;
3492 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003493
3494 if (ret == NOTDONE)
3495 {
3496 /*
3497 * Must be a variable or function name.
3498 * Can also be a curly-braces kind of name: {expr}.
3499 */
3500 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003501 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003502 if (alias != NULL)
3503 s = alias;
3504
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003505 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003506 ret = FAIL;
3507 else
3508 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003509 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3510
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003511 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003512 {
3513 emsg(_(e_cannot_use_underscore_here));
3514 ret = FAIL;
3515 }
3516 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003517 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003518 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003519 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003520 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003521 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003522 else if (flags & EVAL_CONSTANT)
3523 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003524 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003525 {
3526 // get the value of "true", "false" or a variable
3527 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3528 {
3529 rettv->v_type = VAR_BOOL;
3530 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003531 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003532 }
3533 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003534 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003535 {
3536 rettv->v_type = VAR_BOOL;
3537 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003538 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003539 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003540 else if (len == 4 && in_vim9script()
3541 && STRNCMP(s, "null", 4) == 0)
3542 {
3543 rettv->v_type = VAR_SPECIAL;
3544 rettv->vval.v_number = VVAL_NULL;
3545 ret = OK;
3546 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003547 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003548 ret = eval_variable(s, len, rettv, NULL,
3549 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003550 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003551 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003552 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003553 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003554 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003555 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003556 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003557 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003558 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003559 }
3560
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003561 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3562 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003563 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003564 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
3566 /*
3567 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3568 */
3569 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003570 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003571 return ret;
3572}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003573
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003574/*
3575 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003576 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003577 * Adjusts "end_leaderp" until it is at "start_leader".
3578 */
3579 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003580eval7_leader(
3581 typval_T *rettv,
3582 int numeric_only,
3583 char_u *start_leader,
3584 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003585{
3586 char_u *end_leader = *end_leaderp;
3587 int ret = OK;
3588 int error = FALSE;
3589 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003590 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003591#ifdef FEAT_FLOAT
3592 float_T f = 0.0;
3593
3594 if (rettv->v_type == VAR_FLOAT)
3595 f = rettv->vval.v_float;
3596 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003597#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003598 {
3599 while (VIM_ISWHITE(end_leader[-1]))
3600 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003601 if (in_vim9script() && end_leader[-1] == '!')
3602 val = tv2bool(rettv);
3603 else
3604 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003605 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003606 if (error)
3607 {
3608 clear_tv(rettv);
3609 ret = FAIL;
3610 }
3611 else
3612 {
3613 while (end_leader > start_leader)
3614 {
3615 --end_leader;
3616 if (*end_leader == '!')
3617 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003618 if (numeric_only)
3619 {
3620 ++end_leader;
3621 break;
3622 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003623#ifdef FEAT_FLOAT
3624 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003625 {
3626 if (in_vim9script())
3627 {
3628 rettv->v_type = VAR_BOOL;
3629 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3630 }
3631 else
3632 f = !f;
3633 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003634 else
3635#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003636 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003637 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003638 type = VAR_BOOL;
3639 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003640 }
3641 else if (*end_leader == '-')
3642 {
3643#ifdef FEAT_FLOAT
3644 if (rettv->v_type == VAR_FLOAT)
3645 f = -f;
3646 else
3647#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003648 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003649 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003650 type = VAR_NUMBER;
3651 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003652 }
3653 }
3654#ifdef FEAT_FLOAT
3655 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003657 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003658 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003660 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003661#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003662 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003663 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003664 if (in_vim9script())
3665 rettv->v_type = type;
3666 else
3667 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003668 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003671 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 return ret;
3673}
3674
3675/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003676 * Call the function referred to in "rettv".
3677 */
3678 static int
3679call_func_rettv(
3680 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003681 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003682 typval_T *rettv,
3683 int evaluate,
3684 dict_T *selfdict,
3685 typval_T *basetv)
3686{
3687 partial_T *pt = NULL;
3688 funcexe_T funcexe;
3689 typval_T functv;
3690 char_u *s;
3691 int ret;
3692
3693 // need to copy the funcref so that we can clear rettv
3694 if (evaluate)
3695 {
3696 functv = *rettv;
3697 rettv->v_type = VAR_UNKNOWN;
3698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003699 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003700 if (functv.v_type == VAR_PARTIAL)
3701 {
3702 pt = functv.vval.v_partial;
3703 s = partial_name(pt);
3704 }
3705 else
3706 s = functv.vval.v_string;
3707 }
3708 else
3709 s = (char_u *)"";
3710
Bram Moolenaara80faa82020-04-12 19:37:17 +02003711 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003712 funcexe.firstline = curwin->w_cursor.lnum;
3713 funcexe.lastline = curwin->w_cursor.lnum;
3714 funcexe.evaluate = evaluate;
3715 funcexe.partial = pt;
3716 funcexe.selfdict = selfdict;
3717 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003718 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003719
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003720 // Clear the funcref afterwards, so that deleting it while
3721 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003722 if (evaluate)
3723 clear_tv(&functv);
3724
3725 return ret;
3726}
3727
3728/*
3729 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003730 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003731 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3732 */
3733 static int
3734eval_lambda(
3735 char_u **arg,
3736 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003737 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003738 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003739{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003740 int evaluate = evalarg != NULL
3741 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003742 typval_T base = *rettv;
3743 int ret;
3744
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003745 rettv->v_type = VAR_UNKNOWN;
3746
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003747 if (**arg == '{')
3748 {
3749 // ->{lambda}()
3750 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3751 }
3752 else
3753 {
3754 // ->(lambda)()
3755 ++*arg;
3756 ret = eval1(arg, rettv, evalarg);
3757 *arg = skipwhite_and_linebreak(*arg, evalarg);
3758 if (**arg != ')')
3759 {
3760 emsg(_(e_missing_close));
3761 ret = FAIL;
3762 }
3763 ++*arg;
3764 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003765 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003766 return FAIL;
3767 else if (**arg != '(')
3768 {
3769 if (verbose)
3770 {
3771 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003772 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003773 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003775 }
3776 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003777 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003778 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003779 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003780 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003781
3782 // Clear the funcref afterwards, so that deleting it while
3783 // evaluating the arguments is possible (see test55).
3784 if (evaluate)
3785 clear_tv(&base);
3786
3787 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003788}
3789
3790/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003791 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003792 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003793 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3794 */
3795 static int
3796eval_method(
3797 char_u **arg,
3798 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003799 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003800 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003801{
3802 char_u *name;
3803 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003804 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003805 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003806 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003807 int evaluate = evalarg != NULL
3808 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003809
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003810 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003811
Bram Moolenaarac92e252019-08-03 21:58:38 +02003812 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003813 len = get_name_len(arg, &alias, evaluate, TRUE);
3814 if (alias != NULL)
3815 name = alias;
3816
3817 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003818 {
3819 if (verbose)
3820 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003821 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003822 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003823 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003824 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003825 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003826 if (**arg != '(')
3827 {
3828 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003830 ret = FAIL;
3831 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003832 else if (VIM_ISWHITE((*arg)[-1]))
3833 {
3834 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003835 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003836 ret = FAIL;
3837 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003838 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003839 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003840 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003841 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003842
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003843 // Clear the funcref afterwards, so that deleting it while
3844 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003845 if (evaluate)
3846 clear_tv(&base);
3847
Bram Moolenaarac92e252019-08-03 21:58:38 +02003848 return ret;
3849}
3850
3851/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003852 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3853 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003854 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3855 */
3856 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003857eval_index(
3858 char_u **arg,
3859 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003860 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003861 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003862{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003863 int evaluate = evalarg != NULL
3864 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003865 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003866 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003867 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003868 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003869 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003870 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003871
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003872 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3873 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003874
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003875 init_tv(&var1);
3876 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003877 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003878 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003879 /*
3880 * dict.name
3881 */
3882 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003883 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003884 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003885 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003886 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003887 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003888 }
3889 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003891 /*
3892 * something[idx]
3893 *
3894 * Get the (first) variable from inside the [].
3895 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003896 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003897 if (**arg == ':')
3898 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003899 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003900 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003901 else if (vim9 && **arg == ':')
3902 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003903 semsg(_(e_white_space_required_before_and_after_str_at_str),
3904 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003905 clear_tv(&var1);
3906 return FAIL;
3907 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003908 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003909 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003910#ifdef FEAT_FLOAT
3911 // allow for indexing with float
3912 if (vim9 && rettv->v_type == VAR_DICT
3913 && var1.v_type == VAR_FLOAT)
3914 {
3915 var1.vval.v_string = typval_tostring(&var1, TRUE);
3916 var1.v_type = VAR_STRING;
3917 }
3918#endif
3919 if (tv_get_string_chk(&var1) == NULL)
3920 {
3921 // not a number or string
3922 clear_tv(&var1);
3923 return FAIL;
3924 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003925 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003926
3927 /*
3928 * Get the second variable from inside the [:].
3929 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003930 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003931 if (**arg == ':')
3932 {
3933 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003934 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003935 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003936 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003937 semsg(_(e_white_space_required_before_and_after_str_at_str),
3938 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003939 if (!empty1)
3940 clear_tv(&var1);
3941 return FAIL;
3942 }
3943 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003944 if (**arg == ']')
3945 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003946 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003947 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003948 if (!empty1)
3949 clear_tv(&var1);
3950 return FAIL;
3951 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003952 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003953 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003954 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003955 if (!empty1)
3956 clear_tv(&var1);
3957 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003958 return FAIL;
3959 }
3960 }
3961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003962 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003963 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003964 if (**arg != ']')
3965 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003966 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003967 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003968 clear_tv(&var1);
3969 if (range)
3970 clear_tv(&var2);
3971 return FAIL;
3972 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003973 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003974 }
3975
3976 if (evaluate)
3977 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003978 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003979 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003980 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003981
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003982 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003983 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003984 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003985 clear_tv(&var2);
3986 return res;
3987 }
3988 return OK;
3989}
3990
3991/*
3992 * Check if "rettv" can have an [index] or [sli:ce]
3993 */
3994 int
3995check_can_index(typval_T *rettv, int evaluate, int verbose)
3996{
3997 switch (rettv->v_type)
3998 {
3999 case VAR_FUNC:
4000 case VAR_PARTIAL:
4001 if (verbose)
4002 emsg(_("E695: Cannot index a Funcref"));
4003 return FAIL;
4004 case VAR_FLOAT:
4005#ifdef FEAT_FLOAT
4006 if (verbose)
4007 emsg(_(e_float_as_string));
4008 return FAIL;
4009#endif
4010 case VAR_BOOL:
4011 case VAR_SPECIAL:
4012 case VAR_JOB:
4013 case VAR_CHANNEL:
4014 if (verbose)
4015 emsg(_(e_cannot_index_special_variable));
4016 return FAIL;
4017 case VAR_UNKNOWN:
4018 case VAR_ANY:
4019 case VAR_VOID:
4020 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004021 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004022 emsg(_(e_cannot_index_special_variable));
4023 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004024 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004025 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004026
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004027 case VAR_STRING:
4028 case VAR_LIST:
4029 case VAR_DICT:
4030 case VAR_BLOB:
4031 break;
4032 case VAR_NUMBER:
4033 if (in_vim9script())
4034 emsg(_(e_cannot_index_number));
4035 break;
4036 }
4037 return OK;
4038}
4039
4040/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004041 * slice() function
4042 */
4043 void
4044f_slice(typval_T *argvars, typval_T *rettv)
4045{
4046 if (check_can_index(argvars, TRUE, FALSE) == OK)
4047 {
4048 copy_tv(argvars, rettv);
4049 eval_index_inner(rettv, TRUE, argvars + 1,
4050 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4051 TRUE, NULL, 0, FALSE);
4052 }
4053}
4054
4055/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004056 * Apply index or range to "rettv".
4057 * "var1" is the first index, NULL for [:expr].
4058 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004059 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4060 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004061 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4062 */
4063 int
4064eval_index_inner(
4065 typval_T *rettv,
4066 int is_range,
4067 typval_T *var1,
4068 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004069 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004070 char_u *key,
4071 int keylen,
4072 int verbose)
4073{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004074 varnumber_T n1, n2 = 0;
4075 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004076
4077 n1 = 0;
4078 if (var1 != NULL && rettv->v_type != VAR_DICT)
4079 n1 = tv_get_number(var1);
4080
4081 if (is_range)
4082 {
4083 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004084 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004085 if (verbose)
4086 emsg(_(e_cannot_slice_dictionary));
4087 return FAIL;
4088 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004089 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004090 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004091 else
4092 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004093 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004094
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004095 switch (rettv->v_type)
4096 {
4097 case VAR_UNKNOWN:
4098 case VAR_ANY:
4099 case VAR_VOID:
4100 case VAR_FUNC:
4101 case VAR_PARTIAL:
4102 case VAR_FLOAT:
4103 case VAR_BOOL:
4104 case VAR_SPECIAL:
4105 case VAR_JOB:
4106 case VAR_CHANNEL:
4107 break; // not evaluating, skipping over subscript
4108
4109 case VAR_NUMBER:
4110 case VAR_STRING:
4111 {
4112 char_u *s = tv_get_string(rettv);
4113
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004114 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004115 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004116 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004117 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004118 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004119 else
4120 s = char_from_string(s, n1);
4121 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004122 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004123 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004124 // The resulting variable is a substring. If the indexes
4125 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004126 if (n1 < 0)
4127 {
4128 n1 = len + n1;
4129 if (n1 < 0)
4130 n1 = 0;
4131 }
4132 if (n2 < 0)
4133 n2 = len + n2;
4134 else if (n2 >= len)
4135 n2 = len;
4136 if (n1 >= len || n2 < 0 || n1 > n2)
4137 s = NULL;
4138 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004139 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004140 }
4141 else
4142 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004143 // The resulting variable is a string of a single
4144 // character. If the index is too big or negative the
4145 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004146 if (n1 >= len || n1 < 0)
4147 s = NULL;
4148 else
4149 s = vim_strnsave(s + n1, 1);
4150 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 clear_tv(rettv);
4152 rettv->v_type = VAR_STRING;
4153 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004154 }
4155 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004156
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004157 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004158 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4159 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004160 break;
4161
4162 case VAR_LIST:
4163 if (var1 == NULL)
4164 n1 = 0;
4165 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004166 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004167 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004168 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004169 return FAIL;
4170 break;
4171
4172 case VAR_DICT:
4173 {
4174 dictitem_T *item;
4175 typval_T tmp;
4176
4177 if (key == NULL)
4178 {
4179 key = tv_get_string_chk(var1);
4180 if (key == NULL)
4181 return FAIL;
4182 }
4183
4184 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4185
4186 if (item == NULL && verbose)
4187 semsg(_(e_dictkey), key);
4188 if (item == NULL)
4189 return FAIL;
4190
4191 copy_tv(&item->di_tv, &tmp);
4192 clear_tv(rettv);
4193 *rettv = tmp;
4194 }
4195 break;
4196 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004197 return OK;
4198}
4199
4200/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004201 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004202 */
4203 char_u *
4204partial_name(partial_T *pt)
4205{
4206 if (pt->pt_name != NULL)
4207 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004208 if (pt->pt_func != NULL)
4209 return pt->pt_func->uf_name;
4210 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004211}
4212
Bram Moolenaarddecc252016-04-06 22:59:37 +02004213 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004214partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004215{
4216 int i;
4217
4218 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004219 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004220 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004221 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004222 if (pt->pt_name != NULL)
4223 {
4224 func_unref(pt->pt_name);
4225 vim_free(pt->pt_name);
4226 }
4227 else
4228 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004229
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004230 // Decrease the reference count for the context of a closure. If down
4231 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004232 if (pt->pt_funcstack != NULL)
4233 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004234 --pt->pt_funcstack->fs_refcount;
4235 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004236 }
4237
Bram Moolenaarddecc252016-04-06 22:59:37 +02004238 vim_free(pt);
4239}
4240
4241/*
4242 * Unreference a closure: decrement the reference count and free it when it
4243 * becomes zero.
4244 */
4245 void
4246partial_unref(partial_T *pt)
4247{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004248 if (pt != NULL)
4249 {
4250 if (--pt->pt_refcount <= 0)
4251 partial_free(pt);
4252
4253 // If the reference count goes down to one, the funcstack may be the
4254 // only reference and can be freed if no other partials reference it.
4255 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4256 funcstack_check_refcount(pt->pt_funcstack);
4257 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004258}
4259
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004260/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004261 * Return the next (unique) copy ID.
4262 * Used for serializing nested structures.
4263 */
4264 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004265get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004266{
4267 current_copyID += COPYID_INC;
4268 return current_copyID;
4269}
4270
4271/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004272 * Garbage collection for lists and dictionaries.
4273 *
4274 * We use reference counts to be able to free most items right away when they
4275 * are no longer used. But for composite items it's possible that it becomes
4276 * unused while the reference count is > 0: When there is a recursive
4277 * reference. Example:
4278 * :let l = [1, 2, 3]
4279 * :let d = {9: l}
4280 * :let l[1] = d
4281 *
4282 * Since this is quite unusual we handle this with garbage collection: every
4283 * once in a while find out which lists and dicts are not referenced from any
4284 * variable.
4285 *
4286 * Here is a good reference text about garbage collection (refers to Python
4287 * but it applies to all reference-counting mechanisms):
4288 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004289 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004290
4291/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004292 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004293 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004294 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004295 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004296 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004297garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004298{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004299 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004300 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004301 buf_T *buf;
4302 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004303 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004304 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004305
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004306 if (!testing)
4307 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004308 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004309 want_garbage_collect = FALSE;
4310 may_garbage_collect = FALSE;
4311 garbage_collect_at_exit = FALSE;
4312 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004313
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004314 // The execution stack can grow big, limit the size.
4315 if (exestack.ga_maxlen - exestack.ga_len > 500)
4316 {
4317 size_t new_len;
4318 char_u *pp;
4319 int n;
4320
4321 // Keep 150% of the current size, with a minimum of the growth size.
4322 n = exestack.ga_len / 2;
4323 if (n < exestack.ga_growsize)
4324 n = exestack.ga_growsize;
4325
4326 // Don't make it bigger though.
4327 if (exestack.ga_len + n < exestack.ga_maxlen)
4328 {
4329 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4330 pp = vim_realloc(exestack.ga_data, new_len);
4331 if (pp == NULL)
4332 return FAIL;
4333 exestack.ga_maxlen = exestack.ga_len + n;
4334 exestack.ga_data = pp;
4335 }
4336 }
4337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004338 // We advance by two because we add one for items referenced through
4339 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004340 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004341
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004342 /*
4343 * 1. Go through all accessible variables and mark all lists and dicts
4344 * with copyID.
4345 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004346
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004347 // Don't free variables in the previous_funccal list unless they are only
4348 // referenced through previous_funccal. This must be first, because if
4349 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004350 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004351
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004352 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004353 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004354
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004355 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004356 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004357 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4358 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004360 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004361 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004362 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4363 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004364 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004365 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4366 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004367#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004368 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004369 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4370 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004371 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004372 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004373 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4374 NULL, NULL);
4375#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004376
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004377 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004378 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004379 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4380 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004381 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004382 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004383
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004384 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004385 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004386
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004387 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004388 abort = abort || set_ref_in_functions(copyID);
4389
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004390 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004391 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004392
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004393 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004394 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004395
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004396 // callbacks in buffers
4397 abort = abort || set_ref_in_buffers(copyID);
4398
Bram Moolenaar1dced572012-04-05 16:54:08 +02004399#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004400 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004401#endif
4402
Bram Moolenaardb913952012-06-29 12:54:53 +02004403#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004404 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004405#endif
4406
4407#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004408 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004409#endif
4410
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004411#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004412 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004413 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004414#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004415#ifdef FEAT_NETBEANS_INTG
4416 abort = abort || set_ref_in_nb_channel(copyID);
4417#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004418
Bram Moolenaare3188e22016-05-31 21:13:04 +02004419#ifdef FEAT_TIMERS
4420 abort = abort || set_ref_in_timer(copyID);
4421#endif
4422
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004423#ifdef FEAT_QUICKFIX
4424 abort = abort || set_ref_in_quickfix(copyID);
4425#endif
4426
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004427#ifdef FEAT_TERMINAL
4428 abort = abort || set_ref_in_term(copyID);
4429#endif
4430
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004431#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004432 abort = abort || set_ref_in_popups(copyID);
4433#endif
4434
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004435 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004436 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004437 /*
4438 * 2. Free lists and dictionaries that are not referenced.
4439 */
4440 did_free = free_unref_items(copyID);
4441
4442 /*
4443 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004444 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004445 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004446 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004447 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004448 else if (p_verbose > 0)
4449 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004450 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004451 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004452
4453 return did_free;
4454}
4455
4456/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004457 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004458 */
4459 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004460free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004461{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004462 int did_free = FALSE;
4463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004464 // Let all "free" functions know that we are here. This means no
4465 // dictionaries, lists, channels or jobs are to be freed, because we will
4466 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004467 in_free_unref_items = TRUE;
4468
4469 /*
4470 * PASS 1: free the contents of the items. We don't free the items
4471 * themselves yet, so that it is possible to decrement refcount counters
4472 */
4473
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004474 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004475 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004476
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004477 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004478 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004479
4480#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004481 // Go through the list of jobs and free items without the copyID. This
4482 // must happen before doing channels, because jobs refer to channels, but
4483 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004484 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4485
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004486 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004487 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4488#endif
4489
4490 /*
4491 * PASS 2: free the items themselves.
4492 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004493 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004494 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004495
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004496#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004497 // Go through the list of jobs and free items without the copyID. This
4498 // must happen before doing channels, because jobs refer to channels, but
4499 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004500 free_unused_jobs(copyID, COPYID_MASK);
4501
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004502 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004503 free_unused_channels(copyID, COPYID_MASK);
4504#endif
4505
4506 in_free_unref_items = FALSE;
4507
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004508 return did_free;
4509}
4510
4511/*
4512 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004513 * "list_stack" is used to add lists to be marked. Can be NULL.
4514 *
4515 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004516 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004518set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004519{
4520 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004521 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004522 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004523 hashtab_T *cur_ht;
4524 ht_stack_T *ht_stack = NULL;
4525 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004526
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004527 cur_ht = ht;
4528 for (;;)
4529 {
4530 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004531 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004532 // Mark each item in the hashtab. If the item contains a hashtab
4533 // it is added to ht_stack, if it contains a list it is added to
4534 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004535 todo = (int)cur_ht->ht_used;
4536 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4537 if (!HASHITEM_EMPTY(hi))
4538 {
4539 --todo;
4540 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4541 &ht_stack, list_stack);
4542 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004543 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004544
4545 if (ht_stack == NULL)
4546 break;
4547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004548 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004549 cur_ht = ht_stack->ht;
4550 tempitem = ht_stack;
4551 ht_stack = ht_stack->prev;
4552 free(tempitem);
4553 }
4554
4555 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004556}
4557
4558/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004559 * Mark a dict and its items with "copyID".
4560 * Returns TRUE if setting references failed somehow.
4561 */
4562 int
4563set_ref_in_dict(dict_T *d, int copyID)
4564{
4565 if (d != NULL && d->dv_copyID != copyID)
4566 {
4567 d->dv_copyID = copyID;
4568 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4569 }
4570 return FALSE;
4571}
4572
4573/*
4574 * Mark a list and its items with "copyID".
4575 * Returns TRUE if setting references failed somehow.
4576 */
4577 int
4578set_ref_in_list(list_T *ll, int copyID)
4579{
4580 if (ll != NULL && ll->lv_copyID != copyID)
4581 {
4582 ll->lv_copyID = copyID;
4583 return set_ref_in_list_items(ll, copyID, NULL);
4584 }
4585 return FALSE;
4586}
4587
4588/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004589 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004590 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4591 *
4592 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004593 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004594 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004595set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004596{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004597 listitem_T *li;
4598 int abort = FALSE;
4599 list_T *cur_l;
4600 list_stack_T *list_stack = NULL;
4601 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004602
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004603 cur_l = l;
4604 for (;;)
4605 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004606 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004607 // Mark each item in the list. If the item contains a hashtab
4608 // it is added to ht_stack, if it contains a list it is added to
4609 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004610 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4611 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4612 ht_stack, &list_stack);
4613 if (list_stack == NULL)
4614 break;
4615
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004616 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004617 cur_l = list_stack->list;
4618 tempitem = list_stack;
4619 list_stack = list_stack->prev;
4620 free(tempitem);
4621 }
4622
4623 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004624}
4625
4626/*
4627 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004628 * "list_stack" is used to add lists to be marked. Can be NULL.
4629 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4630 *
4631 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004632 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004633 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004634set_ref_in_item(
4635 typval_T *tv,
4636 int copyID,
4637 ht_stack_T **ht_stack,
4638 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004639{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004640 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004641
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004642 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004643 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004644 dict_T *dd = tv->vval.v_dict;
4645
Bram Moolenaara03f2332016-02-06 18:09:59 +01004646 if (dd != NULL && dd->dv_copyID != copyID)
4647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004648 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004649 dd->dv_copyID = copyID;
4650 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004651 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004652 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4653 }
4654 else
4655 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004656 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4657
Bram Moolenaara03f2332016-02-06 18:09:59 +01004658 if (newitem == NULL)
4659 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004660 else
4661 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004662 newitem->ht = &dd->dv_hashtab;
4663 newitem->prev = *ht_stack;
4664 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004665 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004666 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004667 }
4668 }
4669 else if (tv->v_type == VAR_LIST)
4670 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004671 list_T *ll = tv->vval.v_list;
4672
Bram Moolenaara03f2332016-02-06 18:09:59 +01004673 if (ll != NULL && ll->lv_copyID != copyID)
4674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004675 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004676 ll->lv_copyID = copyID;
4677 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004678 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004679 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004680 }
4681 else
4682 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004683 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4684
Bram Moolenaara03f2332016-02-06 18:09:59 +01004685 if (newitem == NULL)
4686 abort = TRUE;
4687 else
4688 {
4689 newitem->list = ll;
4690 newitem->prev = *list_stack;
4691 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004692 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004693 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004694 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004695 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004696 else if (tv->v_type == VAR_FUNC)
4697 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004698 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004699 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004700 else if (tv->v_type == VAR_PARTIAL)
4701 {
4702 partial_T *pt = tv->vval.v_partial;
4703 int i;
4704
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004705 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004706 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004707 // Didn't see this partial yet.
4708 pt->pt_copyID = copyID;
4709
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004710 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004711
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004712 if (pt->pt_dict != NULL)
4713 {
4714 typval_T dtv;
4715
4716 dtv.v_type = VAR_DICT;
4717 dtv.vval.v_dict = pt->pt_dict;
4718 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4719 }
4720
4721 for (i = 0; i < pt->pt_argc; ++i)
4722 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4723 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004724 if (pt->pt_funcstack != NULL)
4725 {
4726 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4727
4728 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4729 abort = abort || set_ref_in_item(stack + i, copyID,
4730 ht_stack, list_stack);
4731 }
4732
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004733 }
4734 }
4735#ifdef FEAT_JOB_CHANNEL
4736 else if (tv->v_type == VAR_JOB)
4737 {
4738 job_T *job = tv->vval.v_job;
4739 typval_T dtv;
4740
4741 if (job != NULL && job->jv_copyID != copyID)
4742 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004743 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004744 if (job->jv_channel != NULL)
4745 {
4746 dtv.v_type = VAR_CHANNEL;
4747 dtv.vval.v_channel = job->jv_channel;
4748 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4749 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004750 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004751 {
4752 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004753 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004754 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4755 }
4756 }
4757 }
4758 else if (tv->v_type == VAR_CHANNEL)
4759 {
4760 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004761 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004762 typval_T dtv;
4763 jsonq_T *jq;
4764 cbq_T *cq;
4765
4766 if (ch != NULL && ch->ch_copyID != copyID)
4767 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004768 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004769 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004770 {
4771 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4772 jq = jq->jq_next)
4773 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4774 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4775 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004776 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004777 {
4778 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004779 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004780 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4781 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004782 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004783 {
4784 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004785 dtv.vval.v_partial =
4786 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004787 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4788 }
4789 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004790 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004791 {
4792 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004793 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004794 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4795 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004796 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004797 {
4798 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004799 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004800 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4801 }
4802 }
4803 }
4804#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004805 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004806}
4807
Bram Moolenaar8c711452005-01-14 21:53:12 +00004808/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004809 * Return a string with the string representation of a variable.
4810 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004811 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004812 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004813 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004814 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004815 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004816 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4817 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004818 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004819 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004820 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004821echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004822 typval_T *tv,
4823 char_u **tofree,
4824 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004825 int copyID,
4826 int echo_style,
4827 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004828 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004830 static int recurse = 0;
4831 char_u *r = NULL;
4832
Bram Moolenaar33570922005-01-25 22:26:29 +00004833 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004834 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004835 if (!did_echo_string_emsg)
4836 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004837 // Only give this message once for a recursive call to avoid
4838 // flooding the user with errors. And stop iterating over lists
4839 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004840 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004841 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004842 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004843 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004844 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004845 }
4846 ++recurse;
4847
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004848 switch (tv->v_type)
4849 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004850 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004851 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004852 {
4853 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004854 r = tv->vval.v_string;
4855 if (r == NULL)
4856 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004857 }
4858 else
4859 {
4860 *tofree = string_quote(tv->vval.v_string, FALSE);
4861 r = *tofree;
4862 }
4863 break;
4864
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004865 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004866 if (echo_style)
4867 {
4868 *tofree = NULL;
4869 r = tv->vval.v_string;
4870 }
4871 else
4872 {
4873 *tofree = string_quote(tv->vval.v_string, TRUE);
4874 r = *tofree;
4875 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004876 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004877
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004878 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004879 {
4880 partial_T *pt = tv->vval.v_partial;
4881 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004882 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004883 garray_T ga;
4884 int i;
4885 char_u *tf;
4886
4887 ga_init2(&ga, 1, 100);
4888 ga_concat(&ga, (char_u *)"function(");
4889 if (fname != NULL)
4890 {
4891 ga_concat(&ga, fname);
4892 vim_free(fname);
4893 }
4894 if (pt != NULL && pt->pt_argc > 0)
4895 {
4896 ga_concat(&ga, (char_u *)", [");
4897 for (i = 0; i < pt->pt_argc; ++i)
4898 {
4899 if (i > 0)
4900 ga_concat(&ga, (char_u *)", ");
4901 ga_concat(&ga,
4902 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4903 vim_free(tf);
4904 }
4905 ga_concat(&ga, (char_u *)"]");
4906 }
4907 if (pt != NULL && pt->pt_dict != NULL)
4908 {
4909 typval_T dtv;
4910
4911 ga_concat(&ga, (char_u *)", ");
4912 dtv.v_type = VAR_DICT;
4913 dtv.vval.v_dict = pt->pt_dict;
4914 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4915 vim_free(tf);
4916 }
4917 ga_concat(&ga, (char_u *)")");
4918
4919 *tofree = ga.ga_data;
4920 r = *tofree;
4921 break;
4922 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004923
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004924 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004925 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004926 break;
4927
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004928 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004929 if (tv->vval.v_list == NULL)
4930 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004931 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004932 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004933 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004934 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004935 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4936 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004937 {
4938 *tofree = NULL;
4939 r = (char_u *)"[...]";
4940 }
4941 else
4942 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004943 int old_copyID = tv->vval.v_list->lv_copyID;
4944
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004945 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004946 *tofree = list2string(tv, copyID, restore_copyID);
4947 if (restore_copyID)
4948 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004949 r = *tofree;
4950 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004951 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004952
Bram Moolenaar8c711452005-01-14 21:53:12 +00004953 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004954 if (tv->vval.v_dict == NULL)
4955 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004956 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004957 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004958 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004959 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004960 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4961 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004962 {
4963 *tofree = NULL;
4964 r = (char_u *)"{...}";
4965 }
4966 else
4967 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004968 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004969
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004970 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004971 *tofree = dict2string(tv, copyID, restore_copyID);
4972 if (restore_copyID)
4973 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004974 r = *tofree;
4975 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004976 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004977
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004978 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004979 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004980 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004982 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004983 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004984 break;
4985
Bram Moolenaar835dc632016-02-07 14:27:38 +01004986 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004987 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004988 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004989 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004990 if (composite_val)
4991 {
4992 *tofree = string_quote(r, FALSE);
4993 r = *tofree;
4994 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004995 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004996
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004998#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999 *tofree = NULL;
5000 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5001 r = numbuf;
5002 break;
5003#endif
5004
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005005 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005006 case VAR_SPECIAL:
5007 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005008 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005009 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005010 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005011
Bram Moolenaar8502c702014-06-17 12:51:16 +02005012 if (--recurse == 0)
5013 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005014 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005015}
5016
5017/*
5018 * Return a string with the string representation of a variable.
5019 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5020 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005021 * Does not put quotes around strings, as ":echo" displays values.
5022 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5023 * May return NULL.
5024 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005025 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005026echo_string(
5027 typval_T *tv,
5028 char_u **tofree,
5029 char_u *numbuf,
5030 int copyID)
5031{
5032 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5033}
5034
5035/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005036 * Return string "str" in ' quotes, doubling ' characters.
5037 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005039 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005040 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005041string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005042{
Bram Moolenaar33570922005-01-25 22:26:29 +00005043 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005044 char_u *p, *r, *s;
5045
Bram Moolenaar33570922005-01-25 22:26:29 +00005046 len = (function ? 13 : 3);
5047 if (str != NULL)
5048 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005049 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005050 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005051 if (*p == '\'')
5052 ++len;
5053 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005054 s = r = alloc(len);
5055 if (r != NULL)
5056 {
5057 if (function)
5058 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005060 r += 10;
5061 }
5062 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005064 if (str != NULL)
5065 for (p = str; *p != NUL; )
5066 {
5067 if (*p == '\'')
5068 *r++ = '\'';
5069 MB_COPY_CHAR(p, r);
5070 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005072 if (function)
5073 *r++ = ')';
5074 *r++ = NUL;
5075 }
5076 return s;
5077}
5078
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005079#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005080/*
5081 * Convert the string "text" to a floating point number.
5082 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5083 * this always uses a decimal point.
5084 * Returns the length of the text that was consumed.
5085 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005086 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005087string2float(
5088 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005089 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005090{
5091 char *s = (char *)text;
5092 float_T f;
5093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005094 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005095 if (STRNICMP(text, "inf", 3) == 0)
5096 {
5097 *value = INFINITY;
5098 return 3;
5099 }
5100 if (STRNICMP(text, "-inf", 3) == 0)
5101 {
5102 *value = -INFINITY;
5103 return 4;
5104 }
5105 if (STRNICMP(text, "nan", 3) == 0)
5106 {
5107 *value = NAN;
5108 return 3;
5109 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005110 f = strtod(s, &s);
5111 *value = f;
5112 return (int)((char_u *)s - text);
5113}
5114#endif
5115
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005116/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005117 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5118 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005119 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005120 */
5121 int
5122buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5123{
5124 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005125 char_u *t;
5126 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005127
5128 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5129 return -1;
5130
5131 if (lnum > buf->b_ml.ml_line_count)
5132 lnum = buf->b_ml.ml_line_count;
5133
5134 str = ml_get_buf(buf, lnum, FALSE);
5135 if (str == NULL)
5136 return -1;
5137
5138 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005139 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005140
Bram Moolenaar91458462021-01-13 20:08:38 +01005141 // count the number of characters
5142 t = str;
5143 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5144 t += mb_ptr2len(t);
5145
5146 // In insert mode, when the cursor is at the end of a non-empty line,
5147 // byteidx points to the NUL character immediately past the end of the
5148 // string. In this case, add one to the character count.
5149 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5150 count++;
5151
5152 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005153}
5154
5155/*
5156 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005157 * byte index. Works only for loaded buffers. Returns -1 on failure.
5158 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005159 */
5160 int
5161buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5162{
5163 char_u *str;
5164 char_u *t;
5165
5166 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5167 return -1;
5168
5169 if (lnum > buf->b_ml.ml_line_count)
5170 lnum = buf->b_ml.ml_line_count;
5171
5172 str = ml_get_buf(buf, lnum, FALSE);
5173 if (str == NULL)
5174 return -1;
5175
5176 // Convert the character offset to a byte offset
5177 t = str;
5178 while (*t != NUL && --charidx > 0)
5179 t += mb_ptr2len(t);
5180
Bram Moolenaar91458462021-01-13 20:08:38 +01005181 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005182}
5183
5184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005186 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005188 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005189var2fpos(
5190 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005191 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005192 int *fnum, // set to fnum for '0, 'A, etc.
5193 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005195 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005197 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005199 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005200 if (varp->v_type == VAR_LIST)
5201 {
5202 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005203 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005204 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005205 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005206
5207 l = varp->vval.v_list;
5208 if (l == NULL)
5209 return NULL;
5210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005211 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005212 pos.lnum = list_find_nr(l, 0L, &error);
5213 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005214 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005215 if (charcol)
5216 len = (long)mb_charlen(ml_get(pos.lnum));
5217 else
5218 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005219
Bram Moolenaarec65d772020-08-20 22:29:12 +02005220 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005221 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005222 li = list_find(l, 1L);
5223 if (li != NULL && li->li_tv.v_type == VAR_STRING
5224 && li->li_tv.vval.v_string != NULL
5225 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005226 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005227 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005228 }
5229 else
5230 {
5231 pos.col = list_find_nr(l, 1L, &error);
5232 if (error)
5233 return NULL;
5234 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005237 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005238 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005239 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005240
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005241 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005242 pos.coladd = list_find_nr(l, 2L, &error);
5243 if (error)
5244 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005245
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005246 return &pos;
5247 }
5248
Bram Moolenaarc5809432021-03-27 21:23:30 +01005249 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5250 return NULL;
5251
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005252 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005253 if (name == NULL)
5254 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005255 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005256 {
5257 pos = curwin->w_cursor;
5258 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005259 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005260 return &pos;
5261 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005262 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005263 {
5264 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005265 pos = VIsual;
5266 else
5267 pos = curwin->w_cursor;
5268 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005269 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005270 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005271 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005272 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005274 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5276 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005277 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005278 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 return pp;
5280 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005281
Bram Moolenaara5525202006-03-02 22:52:09 +00005282 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005283
Bram Moolenaar477933c2007-07-17 14:32:23 +00005284 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005285 {
5286 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005287 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005288 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005289 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005290 // In silent Ex mode topline is zero, but that's not a valid line
5291 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005292 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005293 return &pos;
5294 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005295 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005296 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005297 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005298 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005299 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005300 return &pos;
5301 }
5302 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005303 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005305 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 {
5307 pos.lnum = curbuf->b_ml.ml_line_count;
5308 pos.col = 0;
5309 }
5310 else
5311 {
5312 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005313 if (charcol)
5314 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5315 else
5316 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 }
5318 return &pos;
5319 }
5320 return NULL;
5321}
5322
5323/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005324 * Convert list in "arg" into a position and optional file number.
5325 * When "fnump" is NULL there is no file number, only 3 items.
5326 * Note that the column is passed on as-is, the caller may want to decrement
5327 * it to use 1 for the first column.
5328 * Return FAIL when conversion is not possible, doesn't check the position for
5329 * validity.
5330 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005332list2fpos(
5333 typval_T *arg,
5334 pos_T *posp,
5335 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005336 colnr_T *curswantp,
5337 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005338{
5339 list_T *l = arg->vval.v_list;
5340 long i = 0;
5341 long n;
5342
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005343 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5344 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005345 if (arg->v_type != VAR_LIST
5346 || l == NULL
5347 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005348 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005349 return FAIL;
5350
5351 if (fnump != NULL)
5352 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005353 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005354 if (n < 0)
5355 return FAIL;
5356 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005357 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005358 *fnump = n;
5359 }
5360
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005361 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005362 if (n < 0)
5363 return FAIL;
5364 posp->lnum = n;
5365
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005366 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005367 if (n < 0)
5368 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005369 // If character position is specified, then convert to byte position
5370 if (charcol)
5371 {
5372 buf_T *buf;
5373
5374 // Get the text for the specified line in a loaded buffer
5375 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5376 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5377 return FAIL;
5378
Bram Moolenaar91458462021-01-13 20:08:38 +01005379 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005380 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005381 posp->col = n;
5382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005384 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005385 posp->coladd = 0;
5386 else
5387 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005388
Bram Moolenaar493c1782014-05-28 14:34:46 +02005389 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005390 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005391
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005392 return OK;
5393}
5394
5395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 * Get the length of an environment variable name.
5397 * Advance "arg" to the first character after the name.
5398 * Return 0 for error.
5399 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005401get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 char_u *p;
5404 int len;
5405
5406 for (p = *arg; vim_isIDc(*p); ++p)
5407 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005408 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 return 0;
5410
5411 len = (int)(p - *arg);
5412 *arg = p;
5413 return len;
5414}
5415
5416/*
5417 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005418 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 * Return 0 if something is wrong.
5420 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005422get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 char_u *p;
5425 int len;
5426
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005427 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005429 {
5430 if (*p == ':')
5431 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005432 // "s:" is start of "s:var", but "n:" is not and can be used in
5433 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005434 len = (int)(p - *arg);
5435 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5436 || len > 1)
5437 break;
5438 }
5439 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005440 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 return 0;
5442
5443 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005444 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
5446 return len;
5447}
5448
5449/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005450 * Get the length of the name of a variable or function.
5451 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005453 * Return -1 if curly braces expansion failed.
5454 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 * If the name contains 'magic' {}'s, expand them and return the
5456 * expanded name in an allocated string via 'alias' - caller must free.
5457 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005459get_name_len(
5460 char_u **arg,
5461 char_u **alias,
5462 int evaluate,
5463 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
5465 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 char_u *p;
5467 char_u *expr_start;
5468 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005470 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
5472 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5473 && (*arg)[2] == (int)KE_SNR)
5474 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005475 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 *arg += 3;
5477 return get_id_len(arg) + 3;
5478 }
5479 len = eval_fname_script(*arg);
5480 if (len > 0)
5481 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005482 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 *arg += len;
5484 }
5485
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005487 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005489 p = find_name_end(*arg, &expr_start, &expr_end,
5490 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 if (expr_start != NULL)
5492 {
5493 char_u *temp_string;
5494
5495 if (!evaluate)
5496 {
5497 len += (int)(p - *arg);
5498 *arg = skipwhite(p);
5499 return len;
5500 }
5501
5502 /*
5503 * Include any <SID> etc in the expanded string:
5504 * Thus the -len here.
5505 */
5506 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5507 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005508 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 *alias = temp_string;
5510 *arg = skipwhite(p);
5511 return (int)STRLEN(temp_string);
5512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
5514 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005515 // Only give an error when there is something, otherwise it will be
5516 // reported at a higher level.
5517 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005518 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
5520 return len;
5521}
5522
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523/*
5524 * Find the end of a variable or function name, taking care of magic braces.
5525 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5526 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005527 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528 * Return a pointer to just after the name. Equal to "arg" if there is no
5529 * valid name.
5530 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005531 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005532find_name_end(
5533 char_u *arg,
5534 char_u **expr_start,
5535 char_u **expr_end,
5536 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005538 int mb_nest = 0;
5539 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005541 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005542 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 *expr_start = NULL;
5547 *expr_end = NULL;
5548 }
5549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005550 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005551 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5552 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005553 return arg;
5554
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 for (p = arg; *p != NUL
5556 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005557 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005558 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005559 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005561 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005563 if (*p == '\'')
5564 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005565 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005566 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005567 ;
5568 if (*p == NUL)
5569 break;
5570 }
5571 else if (*p == '"')
5572 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005573 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005574 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005575 if (*p == '\\' && p[1] != NUL)
5576 ++p;
5577 if (*p == NUL)
5578 break;
5579 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005580 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5581 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005582 // "s:" is start of "s:var", but "n:" is not and can be used in
5583 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005584 len = (int)(p - arg);
5585 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005586 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005587 break;
5588 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005589
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005590 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005592 if (*p == '[')
5593 ++br_nest;
5594 else if (*p == ']')
5595 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005597
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005598 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005600 if (*p == '{')
5601 {
5602 mb_nest++;
5603 if (expr_start != NULL && *expr_start == NULL)
5604 *expr_start = p;
5605 }
5606 else if (*p == '}')
5607 {
5608 mb_nest--;
5609 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5610 *expr_end = p;
5611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614
5615 return p;
5616}
5617
5618/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005619 * Expands out the 'magic' {}'s in a variable/function name.
5620 * Note that this can call itself recursively, to deal with
5621 * constructs like foo{bar}{baz}{bam}
5622 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5623 * "in_start" ^
5624 * "expr_start" ^
5625 * "expr_end" ^
5626 * "in_end" ^
5627 *
5628 * Returns a new allocated string, which the caller must free.
5629 * Returns NULL for failure.
5630 */
5631 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005632make_expanded_name(
5633 char_u *in_start,
5634 char_u *expr_start,
5635 char_u *expr_end,
5636 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005637{
5638 char_u c1;
5639 char_u *retval = NULL;
5640 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005641
5642 if (expr_end == NULL || in_end == NULL)
5643 return NULL;
5644 *expr_start = NUL;
5645 *expr_end = NUL;
5646 c1 = *in_end;
5647 *in_end = NUL;
5648
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005649 temp_result = eval_to_string(expr_start + 1, FALSE);
5650 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005651 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005652 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5653 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005654 if (retval != NULL)
5655 {
5656 STRCPY(retval, in_start);
5657 STRCAT(retval, temp_result);
5658 STRCAT(retval, expr_end + 1);
5659 }
5660 }
5661 vim_free(temp_result);
5662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005663 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005664 *expr_start = '{';
5665 *expr_end = '}';
5666
5667 if (retval != NULL)
5668 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005669 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005670 if (expr_start != NULL)
5671 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005672 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005673 temp_result = make_expanded_name(retval, expr_start,
5674 expr_end, temp_result);
5675 vim_free(retval);
5676 retval = temp_result;
5677 }
5678 }
5679
5680 return retval;
5681}
5682
5683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005685 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005687 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005688eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005690 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005691}
5692
5693/*
5694 * Return TRUE if character "c" can be used as the first character in a
5695 * variable or function name (excluding '{' and '}').
5696 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005698eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005699{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005700 return ASCII_ISALPHA(c) || c == '_';
5701}
5702
5703/*
5704 * Return TRUE if character "c" can be used as the first character of a
5705 * dictionary key.
5706 */
5707 int
5708eval_isdictc(int c)
5709{
5710 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711}
5712
5713/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005714 * Handle:
5715 * - expr[expr], expr[expr:expr] subscript
5716 * - ".name" lookup
5717 * - function call with Funcref variable: func(expr)
5718 * - method call: var->method()
5719 *
5720 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005721 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005722 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005723handle_subscript(
5724 char_u **arg,
5725 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005726 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005727 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005728{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005729 int evaluate = evalarg != NULL
5730 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005731 int ret = OK;
5732 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005733 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005734 int getnext;
5735 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005736
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005737 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005738 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005739 // When at the end of the line and ".name" or "->{" or "->X" follows in
5740 // the next line then consume the line break.
5741 p = eval_next_non_blank(*arg, evalarg, &getnext);
5742 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005743 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005744 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005745 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005746 {
5747 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005748 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005749 check_white = FALSE;
5750 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005751
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005752 if (rettv->v_type == VAR_ANY)
5753 {
5754 char_u *exp_name;
5755 int cc;
5756 int idx;
5757 ufunc_T *ufunc;
5758 type_T *type;
5759
5760 // Found script from "import * as {name}", script item name must
5761 // follow.
5762 if (**arg != '.')
5763 {
5764 if (verbose)
5765 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5766 ret = FAIL;
5767 break;
5768 }
5769 ++*arg;
5770 if (IS_WHITE_OR_NUL(**arg))
5771 {
5772 if (verbose)
5773 emsg(_(e_no_white_space_allowed_after_dot));
5774 ret = FAIL;
5775 break;
5776 }
5777
5778 // isolate the name
5779 exp_name = *arg;
5780 while (eval_isnamec(**arg))
5781 ++*arg;
5782 cc = **arg;
5783 **arg = NUL;
5784
5785 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5786 evalarg->eval_cctx, verbose);
5787 **arg = cc;
5788 *arg = skipwhite(*arg);
5789
5790 if (idx < 0 && ufunc == NULL)
5791 {
5792 ret = FAIL;
5793 break;
5794 }
5795 if (idx >= 0)
5796 {
5797 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5798 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5799
5800 copy_tv(sv->sv_tv, rettv);
5801 }
5802 else
5803 {
5804 rettv->v_type = VAR_FUNC;
5805 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5806 }
5807 }
5808
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005809 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5810 || rettv->v_type == VAR_PARTIAL))
5811 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005812 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005813 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5814 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005815
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005816 // Stop the expression evaluation when immediately aborting on
5817 // error, or when an interrupt occurred or an exception was thrown
5818 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005819 if (aborting())
5820 {
5821 if (ret == OK)
5822 clear_tv(rettv);
5823 ret = FAIL;
5824 }
5825 dict_unref(selfdict);
5826 selfdict = NULL;
5827 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005828 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005829 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005830 *arg = skipwhite(p + 2);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005831 if (ret == OK)
5832 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005833 if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005834 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005835 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005836 else
5837 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005838 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005839 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005840 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005841 // "." is ".name" lookup when we found a dict or when evaluating and
5842 // scriptversion is at least 2, where string concatenation is "..".
5843 else if (**arg == '['
5844 || (**arg == '.' && (rettv->v_type == VAR_DICT
5845 || (!evaluate
5846 && (*arg)[1] != '.'
5847 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005848 {
5849 dict_unref(selfdict);
5850 if (rettv->v_type == VAR_DICT)
5851 {
5852 selfdict = rettv->vval.v_dict;
5853 if (selfdict != NULL)
5854 ++selfdict->dv_refcount;
5855 }
5856 else
5857 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005858 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005859 {
5860 clear_tv(rettv);
5861 ret = FAIL;
5862 }
5863 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005864 else
5865 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005866 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005867
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005868 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5869 // Don't do this when "Func" is already a partial that was bound
5870 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005871 if (selfdict != NULL
5872 && (rettv->v_type == VAR_FUNC
5873 || (rettv->v_type == VAR_PARTIAL
5874 && (rettv->vval.v_partial->pt_auto
5875 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005876 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005877
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005878 dict_unref(selfdict);
5879 return ret;
5880}
5881
5882/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005883 * Make a copy of an item.
5884 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005885 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5886 * reference to an already copied list/dict can be used.
5887 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005888 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005889 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005890item_copy(
5891 typval_T *from,
5892 typval_T *to,
5893 int deep,
5894 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005895{
5896 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005897 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005898
Bram Moolenaar33570922005-01-25 22:26:29 +00005899 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005901 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005902 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005903 }
5904 ++recurse;
5905
5906 switch (from->v_type)
5907 {
5908 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005909 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005910 case VAR_STRING:
5911 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005912 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005913 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005914 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005915 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005916 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005917 copy_tv(from, to);
5918 break;
5919 case VAR_LIST:
5920 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005921 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005922 if (from->vval.v_list == NULL)
5923 to->vval.v_list = NULL;
5924 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5925 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005926 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005927 to->vval.v_list = from->vval.v_list->lv_copylist;
5928 ++to->vval.v_list->lv_refcount;
5929 }
5930 else
5931 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5932 if (to->vval.v_list == NULL)
5933 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005934 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005935 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005936 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005937 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005938 case VAR_DICT:
5939 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005940 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005941 if (from->vval.v_dict == NULL)
5942 to->vval.v_dict = NULL;
5943 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5944 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005945 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005946 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5947 ++to->vval.v_dict->dv_refcount;
5948 }
5949 else
5950 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5951 if (to->vval.v_dict == NULL)
5952 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005953 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005954 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005955 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005956 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005957 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005958 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005959 }
5960 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005961 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005962}
5963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005964 void
5965echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5966{
5967 char_u *tofree;
5968 char_u numbuf[NUMBUFLEN];
5969 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5970
5971 if (*atstart)
5972 {
5973 *atstart = FALSE;
5974 // Call msg_start() after eval1(), evaluating the expression
5975 // may cause a message to appear.
5976 if (with_space)
5977 {
5978 // Mark the saved text as finishing the line, so that what
5979 // follows is displayed on a new line when scrolling back
5980 // at the more prompt.
5981 msg_sb_eol();
5982 msg_start();
5983 }
5984 }
5985 else if (with_space)
5986 msg_puts_attr(" ", echo_attr);
5987
5988 if (p != NULL)
5989 for ( ; *p != NUL && !got_int; ++p)
5990 {
5991 if (*p == '\n' || *p == '\r' || *p == TAB)
5992 {
5993 if (*p != TAB && *needclr)
5994 {
5995 // remove any text still there from the command
5996 msg_clr_eos();
5997 *needclr = FALSE;
5998 }
5999 msg_putchar_attr(*p, echo_attr);
6000 }
6001 else
6002 {
6003 if (has_mbyte)
6004 {
6005 int i = (*mb_ptr2len)(p);
6006
6007 (void)msg_outtrans_len_attr(p, i, echo_attr);
6008 p += i - 1;
6009 }
6010 else
6011 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6012 }
6013 }
6014 vim_free(tofree);
6015}
6016
Bram Moolenaare9a41262005-01-15 22:18:47 +00006017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 * ":echo expr1 ..." print each argument separated with a space, add a
6019 * newline at the end.
6020 * ":echon expr1 ..." print each argument plain.
6021 */
6022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006023ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024{
6025 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006026 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 char_u *p;
6028 int needclr = TRUE;
6029 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006030 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006031 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006032 evalarg_T evalarg;
6033
Bram Moolenaare707c882020-07-01 13:07:37 +02006034 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035
6036 if (eap->skip)
6037 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006038 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006040 // If eval1() causes an error message the text from the command may
6041 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006042 need_clr_eos = needclr;
6043
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006045 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 {
6047 /*
6048 * Report the invalid expression unless the expression evaluation
6049 * has been cancelled due to an aborting error, an interrupt, or an
6050 * exception.
6051 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006052 if (!aborting() && did_emsg == did_emsg_before
6053 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006054 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006055 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 break;
6057 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006058 need_clr_eos = FALSE;
6059
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006062
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006063 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 arg = skipwhite(arg);
6065 }
6066 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006067 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068
6069 if (eap->skip)
6070 --emsg_skip;
6071 else
6072 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006073 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 if (needclr)
6075 msg_clr_eos();
6076 if (eap->cmdidx == CMD_echo)
6077 msg_end();
6078 }
6079}
6080
6081/*
6082 * ":echohl {name}".
6083 */
6084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006085ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006087 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088}
6089
6090/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006091 * Returns the :echo attribute
6092 */
6093 int
6094get_echo_attr(void)
6095{
6096 return echo_attr;
6097}
6098
6099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 * ":execute expr1 ..." execute the result of an expression.
6101 * ":echomsg expr1 ..." Print a message
6102 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006103 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 * Each gets spaces around each argument and a newline at the end for
6105 * echo commands
6106 */
6107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006108ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109{
6110 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006111 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 int ret = OK;
6113 char_u *p;
6114 garray_T ga;
6115 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116
6117 ga_init2(&ga, 1, 80);
6118
6119 if (eap->skip)
6120 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006121 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006123 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006124 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
6127 if (!eap->skip)
6128 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006129 char_u buf[NUMBUFLEN];
6130
6131 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006132 {
6133 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6134 {
6135 emsg(_(e_inval_string));
6136 p = NULL;
6137 }
6138 else
6139 p = tv_get_string_buf(&rettv, buf);
6140 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006141 else
6142 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006143 if (p == NULL)
6144 {
6145 clear_tv(&rettv);
6146 ret = FAIL;
6147 break;
6148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 len = (int)STRLEN(p);
6150 if (ga_grow(&ga, len + 2) == FAIL)
6151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006152 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 ret = FAIL;
6154 break;
6155 }
6156 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 ga.ga_len += len;
6160 }
6161
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006162 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 arg = skipwhite(arg);
6164 }
6165
6166 if (ret != FAIL && ga.ga_data != NULL)
6167 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006168 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6169 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006170 // Mark the already saved text as finishing the line, so that what
6171 // follows is displayed on a new line when scrolling back at the
6172 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006173 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006174 }
6175
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006177 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006178 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006179 out_flush();
6180 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006181 else if (eap->cmdidx == CMD_echoconsole)
6182 {
6183 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6184 ui_write((char_u *)"\r\n", 2, TRUE);
6185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 else if (eap->cmdidx == CMD_echoerr)
6187 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006188 int save_did_emsg = did_emsg;
6189
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006190 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006191 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 if (!force_abort)
6193 did_emsg = save_did_emsg;
6194 }
6195 else if (eap->cmdidx == CMD_execute)
6196 do_cmdline((char_u *)ga.ga_data,
6197 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6198 }
6199
6200 ga_clear(&ga);
6201
6202 if (eap->skip)
6203 --emsg_skip;
6204
6205 eap->nextcmd = check_nextcmd(arg);
6206}
6207
6208/*
6209 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6210 * "arg" points to the "&" or '+' when called, to "option" when returning.
6211 * Returns NULL when no option name found. Otherwise pointer to the char
6212 * after the option name.
6213 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006214 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006215find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217 char_u *p = *arg;
6218
6219 ++p;
6220 if (*p == 'g' && p[1] == ':')
6221 {
6222 *opt_flags = OPT_GLOBAL;
6223 p += 2;
6224 }
6225 else if (*p == 'l' && p[1] == ':')
6226 {
6227 *opt_flags = OPT_LOCAL;
6228 p += 2;
6229 }
6230 else
6231 *opt_flags = 0;
6232
6233 if (!ASCII_ISALPHA(*p))
6234 return NULL;
6235 *arg = p;
6236
6237 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006238 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 else
6240 while (ASCII_ISALPHA(*p))
6241 ++p;
6242 return p;
6243}
6244
6245/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006246 * Display script name where an item was last set.
6247 * Should only be invoked when 'verbose' is non-zero.
6248 */
6249 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006250last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006251{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006252 char_u *p;
6253
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006254 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006255 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006256 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006257 if (p != NULL)
6258 {
6259 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006260 msg_puts(_("\n\tLast set from "));
6261 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006262 if (script_ctx.sc_lnum > 0)
6263 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006264 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006265 msg_outnum((long)script_ctx.sc_lnum);
6266 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006267 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006268 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006269 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006270 }
6271}
6272
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006273#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
6275/*
6276 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006277 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 * "flags" can be "g" to do a global substitute.
6279 * Returns an allocated string, NULL for error.
6280 */
6281 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006282do_string_sub(
6283 char_u *str,
6284 char_u *pat,
6285 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006286 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006287 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288{
6289 int sublen;
6290 regmatch_T regmatch;
6291 int i;
6292 int do_all;
6293 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006294 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 garray_T ga;
6296 char_u *ret;
6297 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006298 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006300 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006302 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303
6304 ga_init2(&ga, 1, 200);
6305
6306 do_all = (flags[0] == 'g');
6307
6308 regmatch.rm_ic = p_ic;
6309 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6310 if (regmatch.regprog != NULL)
6311 {
6312 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006313 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6315 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006316 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006317 if (regmatch.startp[0] == regmatch.endp[0])
6318 {
6319 if (zero_width == regmatch.startp[0])
6320 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006321 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006322 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006323 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6324 (size_t)i);
6325 ga.ga_len += i;
6326 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006327 continue;
6328 }
6329 zero_width = regmatch.startp[0];
6330 }
6331
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 /*
6333 * Get some space for a temporary buffer to do the substitution
6334 * into. It will contain:
6335 * - The text up to where the match is.
6336 * - The substituted text.
6337 * - The text after the match.
6338 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006339 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006340 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6342 {
6343 ga_clear(&ga);
6344 break;
6345 }
6346
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006347 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 i = (int)(regmatch.startp[0] - tail);
6349 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006350 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006351 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 + ga.ga_len + i, TRUE, TRUE, FALSE);
6353 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006354 tail = regmatch.endp[0];
6355 if (*tail == NUL)
6356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 if (!do_all)
6358 break;
6359 }
6360
6361 if (ga.ga_data != NULL)
6362 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6363
Bram Moolenaar473de612013-06-08 18:19:48 +02006364 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 }
6366
6367 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6368 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006369 if (p_cpo == empty_option)
6370 p_cpo = save_cpo;
6371 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006372 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006373 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006374 // If it's still empty it was changed and restored, need to restore in
6375 // the complicated way.
6376 if (*p_cpo == NUL)
6377 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006378 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380
6381 return ret;
6382}