blob: 2e2ca23b6974641a974c29484cff17b25c0c438c [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 Moolenaar1dcf55d2020-12-22 22:07:30 +01001354 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001355 {
1356 emsg(_(e_cannot_mod));
1357 *endp = cc;
1358 return;
1359 }
1360
Bram Moolenaarff697e62019-02-12 22:28:33 +01001361 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001362 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001363 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001364 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001365 {
1366 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001367 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1368 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001369 && tv_op(&tv, rettv, op) == OK)
1370 set_var(lp->ll_name, &tv, FALSE);
1371 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001372 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001374 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001375 {
1376 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001377 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001378 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001379 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1380 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001381 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001382 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001383 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001384 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001385 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001386 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001387 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001388 else if (lp->ll_range)
1389 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001390 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001391 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001392
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001393 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001394 {
1395 emsg(_("E996: Cannot lock a range"));
1396 return;
1397 }
1398
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001399 /*
1400 * Check whether any of the list items is locked
1401 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001402 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001403 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001404 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001405 return;
1406 ri = ri->li_next;
1407 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1408 break;
1409 ll_li = ll_li->li_next;
1410 ++ll_n1;
1411 }
1412
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001413 /*
1414 * Assign the List values to the list items.
1415 */
1416 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001417 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001418 if (op != NULL && *op != '=')
1419 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1420 else
1421 {
1422 clear_tv(&lp->ll_li->li_tv);
1423 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1424 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 ri = ri->li_next;
1426 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1427 break;
1428 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001429 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001430 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001431 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001432 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001433 ri = NULL;
1434 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001435 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001436 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001437 lp->ll_li = lp->ll_li->li_next;
1438 ++lp->ll_n1;
1439 }
1440 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001441 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001442 else if (lp->ll_empty2
1443 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001444 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001445 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 }
1447 else
1448 {
1449 /*
1450 * Assign to a List or Dictionary item.
1451 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001452 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001453 {
1454 emsg(_("E996: Cannot lock a list or dict"));
1455 return;
1456 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001457
1458 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001459 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001460 return;
1461
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001462 if (lp->ll_newkey != NULL)
1463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001464 if (op != NULL && *op != '=')
1465 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001466 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001467 return;
1468 }
1469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001470 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001471 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001472 if (di == NULL)
1473 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001474 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1475 {
1476 vim_free(di);
1477 return;
1478 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001479 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001480 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001481 else if (op != NULL && *op != '=')
1482 {
1483 tv_op(lp->ll_tv, rettv, op);
1484 return;
1485 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001486 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001487 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001488
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001489 /*
1490 * Assign the value to the variable or list item.
1491 */
1492 if (copy)
1493 copy_tv(rettv, lp->ll_tv);
1494 else
1495 {
1496 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001497 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001498 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 }
1500 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001501}
1502
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001503/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001504 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1505 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 * Returns OK or FAIL.
1507 */
1508 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001510{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001511 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001512 char_u numbuf[NUMBUFLEN];
1513 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001514 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001516 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001517 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001518 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001519 {
1520 switch (tv1->v_type)
1521 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001522 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001523 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001524 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001525 case VAR_DICT:
1526 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001527 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001528 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001529 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001530 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001531 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001532 break;
1533
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001534 case VAR_BLOB:
1535 if (*op != '+' || tv2->v_type != VAR_BLOB)
1536 break;
1537 // BLOB += BLOB
1538 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1539 {
1540 blob_T *b1 = tv1->vval.v_blob;
1541 blob_T *b2 = tv2->vval.v_blob;
1542 int i, len = blob_len(b2);
1543 for (i = 0; i < len; i++)
1544 ga_append(&b1->bv_ga, blob_get(b2, i));
1545 }
1546 return OK;
1547
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001548 case VAR_LIST:
1549 if (*op != '+' || tv2->v_type != VAR_LIST)
1550 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001551 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001552 if (tv2->vval.v_list != NULL)
1553 {
1554 if (tv1->vval.v_list == NULL)
1555 {
1556 tv1->vval.v_list = tv2->vval.v_list;
1557 ++tv1->vval.v_list->lv_refcount;
1558 }
1559 else
1560 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1561 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001562 return OK;
1563
1564 case VAR_NUMBER:
1565 case VAR_STRING:
1566 if (tv2->v_type == VAR_LIST)
1567 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001568 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001569 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001570 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001571 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001572#ifdef FEAT_FLOAT
1573 if (tv2->v_type == VAR_FLOAT)
1574 {
1575 float_T f = n;
1576
Bram Moolenaarff697e62019-02-12 22:28:33 +01001577 if (*op == '%')
1578 break;
1579 switch (*op)
1580 {
1581 case '+': f += tv2->vval.v_float; break;
1582 case '-': f -= tv2->vval.v_float; break;
1583 case '*': f *= tv2->vval.v_float; break;
1584 case '/': f /= tv2->vval.v_float; break;
1585 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001586 clear_tv(tv1);
1587 tv1->v_type = VAR_FLOAT;
1588 tv1->vval.v_float = f;
1589 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001590 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001591#endif
1592 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001593 switch (*op)
1594 {
1595 case '+': n += tv_get_number(tv2); break;
1596 case '-': n -= tv_get_number(tv2); break;
1597 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001598 case '/': n = num_divide(n, tv_get_number(tv2),
1599 &failed); break;
1600 case '%': n = num_modulus(n, tv_get_number(tv2),
1601 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001602 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001603 clear_tv(tv1);
1604 tv1->v_type = VAR_NUMBER;
1605 tv1->vval.v_number = n;
1606 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607 }
1608 else
1609 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001610 if (tv2->v_type == VAR_FLOAT)
1611 break;
1612
Bram Moolenaarff697e62019-02-12 22:28:33 +01001613 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001614 s = tv_get_string(tv1);
1615 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001616 clear_tv(tv1);
1617 tv1->v_type = VAR_STRING;
1618 tv1->vval.v_string = s;
1619 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001620 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001622 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001623#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 {
1625 float_T f;
1626
Bram Moolenaarff697e62019-02-12 22:28:33 +01001627 if (*op == '%' || *op == '.'
1628 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001629 && tv2->v_type != VAR_NUMBER
1630 && tv2->v_type != VAR_STRING))
1631 break;
1632 if (tv2->v_type == VAR_FLOAT)
1633 f = tv2->vval.v_float;
1634 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001635 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001636 switch (*op)
1637 {
1638 case '+': tv1->vval.v_float += f; break;
1639 case '-': tv1->vval.v_float -= f; break;
1640 case '*': tv1->vval.v_float *= f; break;
1641 case '/': tv1->vval.v_float /= f; break;
1642 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001643 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001644#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001645 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 }
1647 }
1648
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001649 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001650 return FAIL;
1651}
1652
1653/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654 * Evaluate the expression used in a ":for var in expr" command.
1655 * "arg" points to "var".
1656 * Set "*errp" to TRUE for an error, FALSE otherwise;
1657 * Return a pointer that holds the info. Null when there is an error.
1658 */
1659 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001660eval_for_line(
1661 char_u *arg,
1662 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001663 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001664 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665{
Bram Moolenaar33570922005-01-25 22:26:29 +00001666 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001668 typval_T tv;
1669 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001670 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001672 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001674 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 if (fi == NULL)
1676 return NULL;
1677
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001678 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 if (expr == NULL)
1680 return fi;
1681
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001682 expr = skipwhite_and_linebreak(expr, evalarg);
1683 if (expr[0] != 'i' || expr[1] != 'n'
1684 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687 return fi;
1688 }
1689
1690 if (skip)
1691 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001692 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1693 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 {
1695 *errp = FALSE;
1696 if (!skip)
1697 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001698 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001699 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001700 l = tv.vval.v_list;
1701 if (l == NULL)
1702 {
1703 // a null list is like an empty list: do nothing
1704 clear_tv(&tv);
1705 }
1706 else
1707 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001709 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001711 // No need to increment the refcount, it's already set for
1712 // the list being used in "tv".
1713 fi->fi_list = l;
1714 list_add_watch(l, &fi->fi_lw);
1715 fi->fi_lw.lw_item = l->lv_first;
1716 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001717 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001718 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001719 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001720 fi->fi_bi = 0;
1721 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001722 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001723 typval_T btv;
1724
1725 // Make a copy, so that the iteration still works when the
1726 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001728 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001729 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001730 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001731 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001732 else if (tv.v_type == VAR_STRING)
1733 {
1734 fi->fi_byte_idx = 0;
1735 fi->fi_string = tv.vval.v_string;
1736 tv.vval.v_string = NULL;
1737 if (fi->fi_string == NULL)
1738 fi->fi_string = vim_strsave((char_u *)"");
1739 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740 else
1741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001742 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001743 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 }
1745 }
1746 }
1747 if (skip)
1748 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001749 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750
1751 return fi;
1752}
1753
1754/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001755 * Used when looping over a :for line, skip the "in expr" part.
1756 */
1757 void
1758skip_for_lines(void *fi_void, evalarg_T *evalarg)
1759{
1760 forinfo_T *fi = (forinfo_T *)fi_void;
1761 int i;
1762
1763 for (i = 0; i < fi->fi_break_count; ++i)
1764 eval_next_line(evalarg);
1765}
1766
1767/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 * Use the first item in a ":for" list. Advance to the next.
1769 * Assign the values to the variable (list). "arg" points to the first one.
1770 * Return TRUE when a valid item was found, FALSE when at end of list or
1771 * something wrong.
1772 */
1773 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001774next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001776 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 int result;
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001778 int flag = in_vim9script() ? ASSIGN_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001779 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001781 if (fi->fi_blob != NULL)
1782 {
1783 typval_T tv;
1784
1785 if (fi->fi_bi >= blob_len(fi->fi_blob))
1786 return FALSE;
1787 tv.v_type = VAR_NUMBER;
1788 tv.v_lock = VAR_FIXED;
1789 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1790 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001791 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001792 fi->fi_varcount, flag, NULL) == OK;
1793 }
1794
1795 if (fi->fi_string != NULL)
1796 {
1797 typval_T tv;
1798 int len;
1799
1800 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1801 if (len == 0)
1802 return FALSE;
1803 tv.v_type = VAR_STRING;
1804 tv.v_lock = VAR_FIXED;
1805 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1806 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001807 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001808 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001809 vim_free(tv.vval.v_string);
1810 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001811 }
1812
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 item = fi->fi_lw.lw_item;
1814 if (item == NULL)
1815 result = FALSE;
1816 else
1817 {
1818 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001819 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001820 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821 }
1822 return result;
1823}
1824
1825/*
1826 * Free the structure used to store info used by ":for".
1827 */
1828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001829free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830{
Bram Moolenaar33570922005-01-25 22:26:29 +00001831 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001832
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001833 if (fi == NULL)
1834 return;
1835 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001836 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001838 list_unref(fi->fi_list);
1839 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001840 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001841 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001842 else
1843 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 vim_free(fi);
1845}
1846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001848set_context_for_expression(
1849 expand_T *xp,
1850 char_u *arg,
1851 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001853 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001857 if (cmdidx == CMD_let || cmdidx == CMD_var
1858 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 {
1860 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001861 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001862 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001863 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001864 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 {
1866 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001867 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001868 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 break;
1870 }
1871 return;
1872 }
1873 }
1874 else
1875 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1876 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 while ((xp->xp_pattern = vim_strpbrk(arg,
1878 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1879 {
1880 c = *xp->xp_pattern;
1881 if (c == '&')
1882 {
1883 c = xp->xp_pattern[1];
1884 if (c == '&')
1885 {
1886 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001887 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 }
1889 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001892 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1893 xp->xp_pattern += 2;
1894
1895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 else if (c == '$')
1898 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001899 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 xp->xp_context = EXPAND_ENV_VARS;
1901 }
1902 else if (c == '=')
1903 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001904 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 xp->xp_context = EXPAND_EXPRESSION;
1906 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001907 else if (c == '#'
1908 && xp->xp_context == EXPAND_EXPRESSION)
1909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001910 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001911 break;
1912 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001913 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 && xp->xp_context == EXPAND_FUNCTIONS
1915 && vim_strchr(xp->xp_pattern, '(') == NULL)
1916 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001917 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 break;
1919 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001920 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001922 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 {
1924 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1925 if (c == '\\' && xp->xp_pattern[1] != NUL)
1926 ++xp->xp_pattern;
1927 xp->xp_context = EXPAND_NOTHING;
1928 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001929 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001931 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1933 /* skip */ ;
1934 xp->xp_context = EXPAND_NOTHING;
1935 }
1936 else if (c == '|')
1937 {
1938 if (xp->xp_pattern[1] == '|')
1939 {
1940 ++xp->xp_pattern;
1941 xp->xp_context = EXPAND_EXPRESSION;
1942 }
1943 else
1944 xp->xp_context = EXPAND_COMMANDS;
1945 }
1946 else
1947 xp->xp_context = EXPAND_EXPRESSION;
1948 }
1949 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001950 // Doesn't look like something valid, expand as an expression
1951 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 arg = xp->xp_pattern;
1954 if (*arg != NUL)
1955 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1956 /* skip */ ;
1957 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001958
1959 // ":exe one two" completes "two"
1960 if ((cmdidx == CMD_execute
1961 || cmdidx == CMD_echo
1962 || cmdidx == CMD_echon
1963 || cmdidx == CMD_echomsg)
1964 && xp->xp_context == EXPAND_EXPRESSION)
1965 {
1966 for (;;)
1967 {
1968 char_u *n = skiptowhite(arg);
1969
1970 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1971 break;
1972 arg = skipwhite(n);
1973 }
1974 }
1975
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 xp->xp_pattern = arg;
1977}
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001980 * Return TRUE if "pat" matches "text".
1981 * Does not use 'cpo' and always uses 'magic'.
1982 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001983 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001984pattern_match(char_u *pat, char_u *text, int ic)
1985{
1986 int matches = FALSE;
1987 char_u *save_cpo;
1988 regmatch_T regmatch;
1989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001990 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001991 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001992 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001993 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1994 if (regmatch.regprog != NULL)
1995 {
1996 regmatch.rm_ic = ic;
1997 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1998 vim_regfree(regmatch.regprog);
1999 }
2000 p_cpo = save_cpo;
2001 return matches;
2002}
2003
2004/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002005 * Handle a name followed by "(". Both for just "name(arg)" and for
2006 * "expr->name(arg)".
2007 * Returns OK or FAIL.
2008 */
2009 static int
2010eval_func(
2011 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002012 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002013 char_u *name,
2014 int name_len,
2015 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002016 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002017 typval_T *basetv) // "expr" for "expr->name(arg)"
2018{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002019 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002020 char_u *s = name;
2021 int len = name_len;
2022 partial_T *partial;
2023 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002024 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002025
2026 if (!evaluate)
2027 check_vars(s, len);
2028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002029 // If "s" is the name of a variable of type VAR_FUNC
2030 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002031 s = deref_func_name(s, &len, &partial,
2032 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002034 // Need to make a copy, in case evaluating the arguments makes
2035 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002036 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002037 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002038 ret = FAIL;
2039 else
2040 {
2041 funcexe_T funcexe;
2042
2043 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002044 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002045 funcexe.firstline = curwin->w_cursor.lnum;
2046 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002047 funcexe.evaluate = evaluate;
2048 funcexe.partial = partial;
2049 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002050 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002051 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002052 }
2053 vim_free(s);
2054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002055 // If evaluate is FALSE rettv->v_type was not set in
2056 // get_func_tv, but it's needed in handle_subscript() to parse
2057 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002058 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2059 {
2060 rettv->vval.v_string = NULL;
2061 rettv->v_type = VAR_FUNC;
2062 }
2063
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002064 // Stop the expression evaluation when immediately
2065 // aborting on error, or when an interrupt occurred or
2066 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002067 if (evaluate && aborting())
2068 {
2069 if (ret == OK)
2070 clear_tv(rettv);
2071 ret = FAIL;
2072 }
2073 return ret;
2074}
2075
2076/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002077 * Get the next line source line without advancing. But do skip over comment
2078 * lines.
2079 */
2080 static char_u *
2081getline_peek_skip_comments(evalarg_T *evalarg)
2082{
2083 for (;;)
2084 {
2085 char_u *next = getline_peek(evalarg->eval_getline,
2086 evalarg->eval_cookie);
2087 char_u *p;
2088
2089 if (next == NULL)
2090 break;
2091 p = skipwhite(next);
2092 if (*p != NUL && !vim9_comment_start(p))
2093 return next;
2094 (void)eval_next_line(evalarg);
2095 }
2096 return NULL;
2097}
2098
2099/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002100 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2101 * comment) and there is a next line, return the next line (skipping blanks)
2102 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002103 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2104 * "arg" must point somewhere inside a line, not at the start.
2105 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002106 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002107eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2108{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002109 char_u *p = skipwhite(arg);
2110
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002111 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002112 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002113 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002114 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002115 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002117 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002118
2119 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002120 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002121 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002122 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002123
Bram Moolenaar9d489562020-07-30 20:08:50 +02002124 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002125 {
2126 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002127 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002128 }
2129 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002130 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002131}
2132
2133/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002134 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002135 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002136 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002137eval_next_line(evalarg_T *evalarg)
2138{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002139 garray_T *gap = &evalarg->eval_ga;
2140 char_u *line;
2141
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002142 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002143 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2144 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002145 else
2146 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002147 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002148 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2149 {
2150 // Going to concatenate the lines after parsing.
2151 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2152 ++gap->ga_len;
2153 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002154 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002155 {
2156 vim_free(evalarg->eval_tofree);
2157 evalarg->eval_tofree = line;
2158 }
2159 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002160}
2161
Bram Moolenaare6b53242020-07-01 17:28:33 +02002162/*
2163 * Call eval_next_non_blank() and get the next line if needed.
2164 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002165 char_u *
2166skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2167{
2168 int getnext;
2169 char_u *p = skipwhite(arg);
2170
Bram Moolenaar962d7212020-07-04 14:15:00 +02002171 if (evalarg == NULL)
2172 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002173 eval_next_non_blank(p, evalarg, &getnext);
2174 if (getnext)
2175 return eval_next_line(evalarg);
2176 return p;
2177}
2178
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002179/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002180 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002181 */
2182 void
2183clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2184{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002185 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002186 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002187 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002188 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002189 if (eap != NULL)
2190 {
2191 // We may need to keep the original command line, e.g. for
2192 // ":let" it has the variable names. But we may also need the
2193 // new one, "nextcmd" points into it. Keep both.
2194 vim_free(eap->cmdline_tofree);
2195 eap->cmdline_tofree = *eap->cmdlinep;
2196 *eap->cmdlinep = evalarg->eval_tofree;
2197 }
2198 else
2199 vim_free(evalarg->eval_tofree);
2200 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002201 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002202
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002203 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2204 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002205 }
2206}
2207
2208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2212 */
2213
2214/*
2215 * Handle zero level expression.
2216 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002218 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002219 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 * Return OK or FAIL.
2221 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002222 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002223eval0(
2224 char_u *arg,
2225 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002226 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002227 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229 int ret;
2230 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002231 int did_emsg_before = did_emsg;
2232 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002233 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234
2235 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002236 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002237 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002238
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002239 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
2241 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 /*
2244 * Report the invalid expression unless the expression evaluation has
2245 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002246 * exception, or we already gave a more specific error.
2247 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002249 if (!aborting()
2250 && did_emsg == did_emsg_before
2251 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002252 && (flags & EVAL_CONSTANT) == 0
2253 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002254 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002255
2256 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002257 // a next command to avoid more errors, unless "|" is following, which
2258 // could only be a command separator.
2259 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2260 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002263
2264 if (eap != NULL)
2265 eap->nextcmd = check_nextcmd(p);
2266
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 return ret;
2268}
2269
2270/*
2271 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002272 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002273 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 *
2275 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002276 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002278 * Note: "rettv.v_lock" is not set.
2279 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 * Return OK or FAIL.
2281 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002282 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002283eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002285 char_u *p;
2286 int getnext;
2287
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002288 CLEAR_POINTER(rettv);
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 /*
2291 * Get the first variable.
2292 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002293 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 return FAIL;
2295
Bram Moolenaar793648f2020-06-26 21:28:25 +02002296 p = eval_next_non_blank(*arg, evalarg, &getnext);
2297 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002299 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002300 int result;
2301 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002302 evalarg_T *evalarg_used = evalarg;
2303 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002304 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002305 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002306 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002307
2308 if (evalarg == NULL)
2309 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002310 CLEAR_FIELD(local_evalarg);
2311 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002312 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002313 orig_flags = evalarg_used->eval_flags;
2314 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002315
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002316 if (getnext)
2317 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002318 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002319 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002320 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002321 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002322 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002323 clear_tv(rettv);
2324 return FAIL;
2325 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002326 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002327 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002330 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002332 int error = FALSE;
2333
Bram Moolenaar13106602020-10-04 16:06:05 +02002334 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002335 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002336 else if (vim9script)
2337 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002338 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002340 if (error || !op_falsy || !result)
2341 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002342 if (error)
2343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
2345
2346 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002347 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002349 if (op_falsy)
2350 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002351 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002352 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002353 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002354 clear_tv(rettv);
2355 return FAIL;
2356 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002357 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002358 evalarg_used->eval_flags = (op_falsy ? !result : result)
2359 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2360 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002361 {
2362 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002364 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002365 if (!op_falsy || !result)
2366 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002368 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002370 /*
2371 * Check for the ":".
2372 */
2373 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2374 if (*p != ':')
2375 {
2376 emsg(_(e_missing_colon));
2377 if (evaluate && result)
2378 clear_tv(rettv);
2379 evalarg_used->eval_flags = orig_flags;
2380 return FAIL;
2381 }
2382 if (getnext)
2383 *arg = eval_next_line(evalarg_used);
2384 else
2385 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002386 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002387 {
2388 error_white_both(p, 1);
2389 clear_tv(rettv);
2390 evalarg_used->eval_flags = orig_flags;
2391 return FAIL;
2392 }
2393 *arg = p;
2394 }
2395
2396 /*
2397 * Get the third variable. Recursive!
2398 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002399 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002400 {
2401 error_white_both(p, 1);
2402 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002403 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002404 return FAIL;
2405 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002406 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2407 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002408 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002409 if (eval1(arg, &var2, evalarg_used) == FAIL)
2410 {
2411 if (evaluate && result)
2412 clear_tv(rettv);
2413 evalarg_used->eval_flags = orig_flags;
2414 return FAIL;
2415 }
2416 if (evaluate && !result)
2417 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002419
2420 if (evalarg == NULL)
2421 clear_evalarg(&local_evalarg, NULL);
2422 else
2423 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425
2426 return OK;
2427}
2428
2429/*
2430 * Handle first level expression:
2431 * expr2 || expr2 || expr2 logical OR
2432 *
2433 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002434 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 *
2436 * Return OK or FAIL.
2437 */
2438 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002439eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002441 char_u *p;
2442 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444 /*
2445 * Get the first variable.
2446 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002447 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 return FAIL;
2449
2450 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002451 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002453 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002454 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002456 evalarg_T *evalarg_used = evalarg;
2457 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002458 int evaluate;
2459 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002460 long result = FALSE;
2461 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002462 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002463 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002464
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465 if (evalarg == NULL)
2466 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002467 CLEAR_FIELD(local_evalarg);
2468 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002470 orig_flags = evalarg_used->eval_flags;
2471 evaluate = orig_flags & EVAL_EVALUATE;
2472 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002473 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002474 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002475 result = tv_get_bool_chk(rettv, &error);
2476 else if (tv_get_number_chk(rettv, &error) != 0)
2477 result = TRUE;
2478 clear_tv(rettv);
2479 if (error)
2480 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
2482
2483 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002484 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002486 while (p[0] == '|' && p[1] == '|')
2487 {
2488 if (getnext)
2489 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002490 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002491 {
2492 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2493 {
2494 error_white_both(p, 2);
2495 clear_tv(rettv);
2496 return FAIL;
2497 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002498 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002499 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002500
2501 /*
2502 * Get the second variable.
2503 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002504 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2505 {
2506 error_white_both(p, 2);
2507 clear_tv(rettv);
2508 return FAIL;
2509 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002510 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2511 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002512 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002513 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002514 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002515
2516 /*
2517 * Compute the result.
2518 */
2519 if (evaluate && !result)
2520 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002521 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002522 result = tv_get_bool_chk(&var2, &error);
2523 else if (tv_get_number_chk(&var2, &error) != 0)
2524 result = TRUE;
2525 clear_tv(&var2);
2526 if (error)
2527 return FAIL;
2528 }
2529 if (evaluate)
2530 {
2531 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002532 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002533 rettv->v_type = VAR_BOOL;
2534 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002535 }
2536 else
2537 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002538 rettv->v_type = VAR_NUMBER;
2539 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002540 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002541 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002542
2543 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002545
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002546 if (evalarg == NULL)
2547 clear_evalarg(&local_evalarg, NULL);
2548 else
2549 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 }
2551
2552 return OK;
2553}
2554
2555/*
2556 * Handle second level expression:
2557 * expr3 && expr3 && expr3 logical AND
2558 *
2559 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002560 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 *
2562 * Return OK or FAIL.
2563 */
2564 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002565eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002567 char_u *p;
2568 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
2570 /*
2571 * Get the first variable.
2572 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002573 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 return FAIL;
2575
2576 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002577 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002579 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002580 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 evalarg_T *evalarg_used = evalarg;
2583 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002584 int orig_flags;
2585 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002586 long result = TRUE;
2587 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002588 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002589 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002590
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002591 if (evalarg == NULL)
2592 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002593 CLEAR_FIELD(local_evalarg);
2594 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002595 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596 orig_flags = evalarg_used->eval_flags;
2597 evaluate = orig_flags & EVAL_EVALUATE;
2598 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002599 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002600 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002601 result = tv_get_bool_chk(rettv, &error);
2602 else if (tv_get_number_chk(rettv, &error) == 0)
2603 result = FALSE;
2604 clear_tv(rettv);
2605 if (error)
2606 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
2608
2609 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002610 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002612 while (p[0] == '&' && p[1] == '&')
2613 {
2614 if (getnext)
2615 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002616 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002617 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002618 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002619 {
2620 error_white_both(p, 2);
2621 clear_tv(rettv);
2622 return FAIL;
2623 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002624 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002625 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002626
2627 /*
2628 * Get the second variable.
2629 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002630 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2631 {
2632 error_white_both(p, 2);
2633 clear_tv(rettv);
2634 return FAIL;
2635 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002636 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2637 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002638 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002639 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002640 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002641 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002642
2643 /*
2644 * Compute the result.
2645 */
2646 if (evaluate && result)
2647 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002648 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002649 result = tv_get_bool_chk(&var2, &error);
2650 else if (tv_get_number_chk(&var2, &error) == 0)
2651 result = FALSE;
2652 clear_tv(&var2);
2653 if (error)
2654 return FAIL;
2655 }
2656 if (evaluate)
2657 {
2658 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002659 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002660 rettv->v_type = VAR_BOOL;
2661 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002662 }
2663 else
2664 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002665 rettv->v_type = VAR_NUMBER;
2666 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002667 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002668 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002669
2670 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002672
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002673 if (evalarg == NULL)
2674 clear_evalarg(&local_evalarg, NULL);
2675 else
2676 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678
2679 return OK;
2680}
2681
2682/*
2683 * Handle third level expression:
2684 * var1 == var2
2685 * var1 =~ var2
2686 * var1 != var2
2687 * var1 !~ var2
2688 * var1 > var2
2689 * var1 >= var2
2690 * var1 < var2
2691 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002692 * var1 is var2
2693 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 *
2695 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002696 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 *
2698 * Return OK or FAIL.
2699 */
2700 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002701eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002704 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002705 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002707 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
2709 /*
2710 * Get the first variable.
2711 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002712 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 return FAIL;
2714
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002715 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002716 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
2718 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002719 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002721 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002723 typval_T var2;
2724 int ic;
2725 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002726 int evaluate = evalarg == NULL
2727 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002728
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002729 if (getnext)
2730 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002731 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2732 {
2733 error_white_both(p, len);
2734 clear_tv(rettv);
2735 return FAIL;
2736 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002737
Bram Moolenaar696ba232020-07-29 21:20:41 +02002738 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2739 {
2740 semsg(_(e_invexpr2), p);
2741 clear_tv(rettv);
2742 return FAIL;
2743 }
2744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002745 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (p[len] == '?')
2747 {
2748 ic = TRUE;
2749 ++len;
2750 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002751 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 else if (p[len] == '#')
2753 {
2754 ic = FALSE;
2755 ++len;
2756 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002757 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002759 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 /*
2762 * Get the second variable.
2763 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002764 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2765 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002766 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002767 clear_tv(rettv);
2768 return FAIL;
2769 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002770 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002771 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002773 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 return FAIL;
2775 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002776 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002777 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002778 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002779
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002780 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002781 {
2782 ret = FAIL;
2783 clear_tv(rettv);
2784 }
2785 else
2786 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002787 clear_tv(&var2);
2788 return ret;
2789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
2791
2792 return OK;
2793}
2794
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002795/*
2796 * Make a copy of blob "tv1" and append blob "tv2".
2797 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 void
2799eval_addblob(typval_T *tv1, typval_T *tv2)
2800{
2801 blob_T *b1 = tv1->vval.v_blob;
2802 blob_T *b2 = tv2->vval.v_blob;
2803 blob_T *b = blob_alloc();
2804 int i;
2805
2806 if (b != NULL)
2807 {
2808 for (i = 0; i < blob_len(b1); i++)
2809 ga_append(&b->bv_ga, blob_get(b1, i));
2810 for (i = 0; i < blob_len(b2); i++)
2811 ga_append(&b->bv_ga, blob_get(b2, i));
2812
2813 clear_tv(tv1);
2814 rettv_blob_set(tv1, b);
2815 }
2816}
2817
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002818/*
2819 * Make a copy of list "tv1" and append list "tv2".
2820 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 int
2822eval_addlist(typval_T *tv1, typval_T *tv2)
2823{
2824 typval_T var3;
2825
2826 // concatenate Lists
2827 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2828 {
2829 clear_tv(tv1);
2830 clear_tv(tv2);
2831 return FAIL;
2832 }
2833 clear_tv(tv1);
2834 *tv1 = var3;
2835 return OK;
2836}
2837
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838/*
2839 * Handle fourth level expression:
2840 * + number addition
2841 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002842 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002843 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 *
2845 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002846 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 *
2848 * Return OK or FAIL.
2849 */
2850 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002851eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 /*
2854 * Get the first variable.
2855 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002856 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 return FAIL;
2858
2859 /*
2860 * Repeat computing, until no '+', '-' or '.' is following.
2861 */
2862 for (;;)
2863 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002864 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002865 int getnext;
2866 char_u *p;
2867 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002868 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002869 int concat;
2870 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002871 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002872
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002873 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002874 // "+=", "-=" and "..=" are assignments
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002875 p = eval_next_non_blank(*arg, evalarg, &getnext);
2876 op = *p;
2877 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002878 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2879 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002881
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002882 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002883 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002884 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002885 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002886 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002887 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002888 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002889 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002890 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002891 clear_tv(rettv);
2892 return FAIL;
2893 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002894 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002895 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002896 if ((op != '+' || (rettv->v_type != VAR_LIST
2897 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002898#ifdef FEAT_FLOAT
2899 && (op == '.' || rettv->v_type != VAR_FLOAT)
2900#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002901 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002902 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002903 int error = FALSE;
2904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002905 // For "list + ...", an illegal use of the first operand as
2906 // a number cannot be determined before evaluating the 2nd
2907 // operand: if this is also a list, all is ok.
2908 // For "something . ...", "something - ..." or "non-list + ...",
2909 // we know that the first operand needs to be a string or number
2910 // without evaluating the 2nd operand. So check before to avoid
2911 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002912 if (op != '.')
2913 tv_get_number_chk(rettv, &error);
2914 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002915 {
2916 clear_tv(rettv);
2917 return FAIL;
2918 }
2919 }
2920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 /*
2922 * Get the second variable.
2923 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002924 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002925 {
2926 error_white_both(p, oplen);
2927 clear_tv(rettv);
2928 return FAIL;
2929 }
2930 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002931 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 return FAIL;
2935 }
2936
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002937 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 {
2939 /*
2940 * Compute the result.
2941 */
2942 if (op == '.')
2943 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002944 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2945 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002946 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002947
Bram Moolenaar2e086612020-08-25 22:37:48 +02002948 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002949 || var2.v_type == VAR_CHANNEL
2950 || var2.v_type == VAR_JOB))
2951 emsg(_(e_inval_string));
2952#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002953 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002954 {
2955 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2956 var2.vval.v_float);
2957 s2 = buf2;
2958 }
2959#endif
2960 else
2961 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002962 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002963 {
2964 clear_tv(rettv);
2965 clear_tv(&var2);
2966 return FAIL;
2967 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002968 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002969 clear_tv(rettv);
2970 rettv->v_type = VAR_STRING;
2971 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002973 else if (op == '+' && rettv->v_type == VAR_BLOB
2974 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002976 else if (op == '+' && rettv->v_type == VAR_LIST
2977 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002978 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002980 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 else
2983 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002984 int error = FALSE;
2985 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002986#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002987 float_T f1 = 0, f2 = 0;
2988
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002989 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002990 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002991 f1 = rettv->vval.v_float;
2992 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002993 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002994 else
2995#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002996 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002997 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002998 if (error)
2999 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003000 // This can only happen for "list + non-list" or
3001 // "blob + non-blob". For "non-list + ..." or
3002 // "something - ...", we returned before evaluating the
3003 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003004 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003005 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003006 return FAIL;
3007 }
3008#ifdef FEAT_FLOAT
3009 if (var2.v_type == VAR_FLOAT)
3010 f1 = n1;
3011#endif
3012 }
3013#ifdef FEAT_FLOAT
3014 if (var2.v_type == VAR_FLOAT)
3015 {
3016 f2 = var2.vval.v_float;
3017 n2 = 0;
3018 }
3019 else
3020#endif
3021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003022 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003023 if (error)
3024 {
3025 clear_tv(rettv);
3026 clear_tv(&var2);
3027 return FAIL;
3028 }
3029#ifdef FEAT_FLOAT
3030 if (rettv->v_type == VAR_FLOAT)
3031 f2 = n2;
3032#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003033 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003034 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003035
3036#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003037 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003038 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3039 {
3040 if (op == '+')
3041 f1 = f1 + f2;
3042 else
3043 f1 = f1 - f2;
3044 rettv->v_type = VAR_FLOAT;
3045 rettv->vval.v_float = f1;
3046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003048#endif
3049 {
3050 if (op == '+')
3051 n1 = n1 + n2;
3052 else
3053 n1 = n1 - n2;
3054 rettv->v_type = VAR_NUMBER;
3055 rettv->vval.v_number = n1;
3056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003058 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 }
3060 }
3061 return OK;
3062}
3063
3064/*
3065 * Handle fifth level expression:
3066 * * number multiplication
3067 * / number division
3068 * % number modulo
3069 *
3070 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003071 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 *
3073 * Return OK or FAIL.
3074 */
3075 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003076eval6(
3077 char_u **arg,
3078 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003079 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003080 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003082#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003083 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085
3086 /*
3087 * Get the first variable.
3088 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003089 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 return FAIL;
3091
3092 /*
3093 * Repeat computing, until no '*', '/' or '%' is following.
3094 */
3095 for (;;)
3096 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003097 int evaluate;
3098 int getnext;
3099 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003100 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003101 int op;
3102 varnumber_T n1, n2;
3103#ifdef FEAT_FLOAT
3104 float_T f1, f2;
3105#endif
3106 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003107
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003108 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003109 p = eval_next_non_blank(*arg, evalarg, &getnext);
3110 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003111 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003113
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003114 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003115 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003116 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003117 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003118 {
3119 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3120 {
3121 error_white_both(p, 1);
3122 clear_tv(rettv);
3123 return FAIL;
3124 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003125 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003128#ifdef FEAT_FLOAT
3129 f1 = 0;
3130 f2 = 0;
3131#endif
3132 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003133 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135#ifdef FEAT_FLOAT
3136 if (rettv->v_type == VAR_FLOAT)
3137 {
3138 f1 = rettv->vval.v_float;
3139 use_float = TRUE;
3140 n1 = 0;
3141 }
3142 else
3143#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003144 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003145 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003146 if (error)
3147 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 }
3149 else
3150 n1 = 0;
3151
3152 /*
3153 * Get the second variable.
3154 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003155 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3156 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003157 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003158 clear_tv(rettv);
3159 return FAIL;
3160 }
3161 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003162 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 return FAIL;
3164
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003165 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003167#ifdef FEAT_FLOAT
3168 if (var2.v_type == VAR_FLOAT)
3169 {
3170 if (!use_float)
3171 {
3172 f1 = n1;
3173 use_float = TRUE;
3174 }
3175 f2 = var2.vval.v_float;
3176 n2 = 0;
3177 }
3178 else
3179#endif
3180 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003181 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182 clear_tv(&var2);
3183 if (error)
3184 return FAIL;
3185#ifdef FEAT_FLOAT
3186 if (use_float)
3187 f2 = n2;
3188#endif
3189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
3191 /*
3192 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003193 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003195#ifdef FEAT_FLOAT
3196 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003198 if (op == '*')
3199 f1 = f1 * f2;
3200 else if (op == '/')
3201 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003202# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003203 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003204 if (f2 == 0.0)
3205 {
3206 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003207 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003208 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003209 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003210 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003211 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003212 }
3213 else
3214 f1 = f1 / f2;
3215# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003216 // We rely on the floating point library to handle divide
3217 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003218 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003219# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003222 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003224 return FAIL;
3225 }
3226 rettv->v_type = VAR_FLOAT;
3227 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003232 int failed = FALSE;
3233
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003234 if (op == '*')
3235 n1 = n1 * n2;
3236 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003237 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003239 n1 = num_modulus(n1, n2, &failed);
3240 if (failed)
3241 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003242
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003243 rettv->v_type = VAR_NUMBER;
3244 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247 }
3248
3249 return OK;
3250}
3251
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003252 int
3253eval_leader(char_u **arg, int vim9)
3254{
3255 char_u *s = *arg;
3256 char_u *p = *arg;
3257
3258 while (*p == '!' || *p == '-' || *p == '+')
3259 {
3260 char_u *n = skipwhite(p + 1);
3261
3262 // ++, --, -+ and +- are not accepted in Vim9 script
3263 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3264 {
3265 semsg(_(e_invexpr2), s);
3266 return FAIL;
3267 }
3268 p = n;
3269 }
3270 *arg = p;
3271 return OK;
3272}
3273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274/*
3275 * Handle sixth level expression:
3276 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003277 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003278 * "string" string constant
3279 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 * &option-name option value
3281 * @r register contents
3282 * identifier variable value
3283 * function() function call
3284 * $VAR environment variable
3285 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003286 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003288 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003289 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 *
3291 * Also handle:
3292 * ! in front logical NOT
3293 * - in front unary minus
3294 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003295 * trailing [] subscript in String or List
3296 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003297 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 *
3299 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003300 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 *
3302 * Return OK or FAIL.
3303 */
3304 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003305eval7(
3306 char_u **arg,
3307 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003308 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003311 int evaluate = evalarg != NULL
3312 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 int len;
3314 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 char_u *start_leader, *end_leader;
3316 int ret = OK;
3317 char_u *alias;
3318
3319 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003320 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003321 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003323 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
3325 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003326 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 */
3328 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003329 if (eval_leader(arg, in_vim9script()) == FAIL)
3330 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 end_leader = *arg;
3332
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003333 if (**arg == '.' && (!isdigit(*(*arg + 1))
3334#ifdef FEAT_FLOAT
3335 || current_sctx.sc_version < 2
3336#endif
3337 ))
3338 {
3339 semsg(_(e_invexpr2), *arg);
3340 ++*arg;
3341 return FAIL;
3342 }
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 switch (**arg)
3345 {
3346 /*
3347 * Number constant.
3348 */
3349 case '0':
3350 case '1':
3351 case '2':
3352 case '3':
3353 case '4':
3354 case '5':
3355 case '6':
3356 case '7':
3357 case '8':
3358 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003359 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003360
3361 // Apply prefixed "-" and "+" now. Matters especially when
3362 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003363 if (ret == OK && evaluate && end_leader > start_leader
3364 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003365 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 break;
3367
3368 /*
3369 * String constant: "string".
3370 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003371 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 break;
3373
3374 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003375 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003377 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003378 break;
3379
3380 /*
3381 * List: [expr, expr]
3382 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003383 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 break;
3385
3386 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003387 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003388 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003389 case '#': if (in_vim9script())
3390 {
3391 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3392 }
3393 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003394 {
3395 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003396 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003397 }
3398 else
3399 ret = NOTDONE;
3400 break;
3401
3402 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003403 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003404 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003405 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003406 case '{': if (in_vim9script())
3407 ret = NOTDONE;
3408 else
3409 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003410 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003411 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003412 break;
3413
3414 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003415 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003417 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 break;
3419
3420 /*
3421 * Environment variable: $VAR.
3422 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003423 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 break;
3425
3426 /*
3427 * Register contents: @r.
3428 */
3429 case '@': ++*arg;
3430 if (evaluate)
3431 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003432 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3433 semsg(_(e_syntax_error_at_str), *arg);
3434 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3435 emsg_invreg(**arg);
3436 else
3437 {
3438 rettv->v_type = VAR_STRING;
3439 rettv->vval.v_string = get_reg_contents(**arg,
3440 GREG_EXPR_SRC);
3441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
3443 if (**arg != NUL)
3444 ++*arg;
3445 break;
3446
3447 /*
3448 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003449 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003451 case '(': ret = NOTDONE;
3452 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003453 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003454 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003455 if (ret == OK && evaluate)
3456 {
3457 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3458
3459 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003460 if (compile_def_function(ufunc,
3461 TRUE, PROFILING(ufunc), NULL) == FAIL)
3462 {
3463 clear_tv(rettv);
3464 ret = FAIL;
3465 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003466 }
3467 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003468 if (ret == NOTDONE)
3469 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003470 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003471 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003472
Bram Moolenaar9215f012020-06-27 21:18:00 +02003473 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003474 if (**arg == ')')
3475 ++*arg;
3476 else if (ret == OK)
3477 {
3478 emsg(_(e_missing_close));
3479 clear_tv(rettv);
3480 ret = FAIL;
3481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
3483 break;
3484
Bram Moolenaar8c711452005-01-14 21:53:12 +00003485 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 break;
3487 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003488
3489 if (ret == NOTDONE)
3490 {
3491 /*
3492 * Must be a variable or function name.
3493 * Can also be a curly-braces kind of name: {expr}.
3494 */
3495 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003496 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003497 if (alias != NULL)
3498 s = alias;
3499
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003500 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003501 ret = FAIL;
3502 else
3503 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003504 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3505
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003506 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003507 {
3508 emsg(_(e_cannot_use_underscore_here));
3509 ret = FAIL;
3510 }
3511 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003512 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003513 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003514 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003515 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003516 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003517 else if (flags & EVAL_CONSTANT)
3518 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003519 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003520 {
3521 // get the value of "true", "false" or a variable
3522 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3523 {
3524 rettv->v_type = VAR_BOOL;
3525 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003526 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003527 }
3528 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003529 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003530 {
3531 rettv->v_type = VAR_BOOL;
3532 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003533 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003534 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003535 else if (len == 4 && in_vim9script()
3536 && STRNCMP(s, "null", 4) == 0)
3537 {
3538 rettv->v_type = VAR_SPECIAL;
3539 rettv->vval.v_number = VVAL_NULL;
3540 ret = OK;
3541 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003542 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003543 ret = eval_variable(s, len, rettv, NULL,
3544 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003545 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003546 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003547 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003548 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003549 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003550 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003551 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003552 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003553 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003554 }
3555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003556 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3557 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003558 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003559 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561 /*
3562 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3563 */
3564 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003565 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003566 return ret;
3567}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003568
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003569/*
3570 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003571 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003572 * Adjusts "end_leaderp" until it is at "start_leader".
3573 */
3574 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003575eval7_leader(
3576 typval_T *rettv,
3577 int numeric_only,
3578 char_u *start_leader,
3579 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003580{
3581 char_u *end_leader = *end_leaderp;
3582 int ret = OK;
3583 int error = FALSE;
3584 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003585 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003586#ifdef FEAT_FLOAT
3587 float_T f = 0.0;
3588
3589 if (rettv->v_type == VAR_FLOAT)
3590 f = rettv->vval.v_float;
3591 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003592#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003593 {
3594 while (VIM_ISWHITE(end_leader[-1]))
3595 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003596 if (in_vim9script() && end_leader[-1] == '!')
3597 val = tv2bool(rettv);
3598 else
3599 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003600 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003601 if (error)
3602 {
3603 clear_tv(rettv);
3604 ret = FAIL;
3605 }
3606 else
3607 {
3608 while (end_leader > start_leader)
3609 {
3610 --end_leader;
3611 if (*end_leader == '!')
3612 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003613 if (numeric_only)
3614 {
3615 ++end_leader;
3616 break;
3617 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003618#ifdef FEAT_FLOAT
3619 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003620 {
3621 if (in_vim9script())
3622 {
3623 rettv->v_type = VAR_BOOL;
3624 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3625 }
3626 else
3627 f = !f;
3628 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003629 else
3630#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003631 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003632 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003633 type = VAR_BOOL;
3634 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003635 }
3636 else if (*end_leader == '-')
3637 {
3638#ifdef FEAT_FLOAT
3639 if (rettv->v_type == VAR_FLOAT)
3640 f = -f;
3641 else
3642#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003643 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003644 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003645 type = VAR_NUMBER;
3646 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003647 }
3648 }
3649#ifdef FEAT_FLOAT
3650 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003652 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003653 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003655 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003656#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003657 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003658 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003659 if (in_vim9script())
3660 rettv->v_type = type;
3661 else
3662 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003663 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003666 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 return ret;
3668}
3669
3670/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003671 * Call the function referred to in "rettv".
3672 */
3673 static int
3674call_func_rettv(
3675 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003676 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003677 typval_T *rettv,
3678 int evaluate,
3679 dict_T *selfdict,
3680 typval_T *basetv)
3681{
3682 partial_T *pt = NULL;
3683 funcexe_T funcexe;
3684 typval_T functv;
3685 char_u *s;
3686 int ret;
3687
3688 // need to copy the funcref so that we can clear rettv
3689 if (evaluate)
3690 {
3691 functv = *rettv;
3692 rettv->v_type = VAR_UNKNOWN;
3693
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003694 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003695 if (functv.v_type == VAR_PARTIAL)
3696 {
3697 pt = functv.vval.v_partial;
3698 s = partial_name(pt);
3699 }
3700 else
3701 s = functv.vval.v_string;
3702 }
3703 else
3704 s = (char_u *)"";
3705
Bram Moolenaara80faa82020-04-12 19:37:17 +02003706 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003707 funcexe.firstline = curwin->w_cursor.lnum;
3708 funcexe.lastline = curwin->w_cursor.lnum;
3709 funcexe.evaluate = evaluate;
3710 funcexe.partial = pt;
3711 funcexe.selfdict = selfdict;
3712 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003713 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003714
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003715 // Clear the funcref afterwards, so that deleting it while
3716 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003717 if (evaluate)
3718 clear_tv(&functv);
3719
3720 return ret;
3721}
3722
3723/*
3724 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003725 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003726 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3727 */
3728 static int
3729eval_lambda(
3730 char_u **arg,
3731 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003732 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003733 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003734{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003735 int evaluate = evalarg != NULL
3736 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003737 typval_T base = *rettv;
3738 int ret;
3739
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003740 rettv->v_type = VAR_UNKNOWN;
3741
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003742 if (**arg == '{')
3743 {
3744 // ->{lambda}()
3745 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3746 }
3747 else
3748 {
3749 // ->(lambda)()
3750 ++*arg;
3751 ret = eval1(arg, rettv, evalarg);
3752 *arg = skipwhite_and_linebreak(*arg, evalarg);
3753 if (**arg != ')')
3754 {
3755 emsg(_(e_missing_close));
3756 ret = FAIL;
3757 }
3758 ++*arg;
3759 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003760 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003761 return FAIL;
3762 else if (**arg != '(')
3763 {
3764 if (verbose)
3765 {
3766 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003767 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003768 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003770 }
3771 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003772 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003773 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003774 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003775 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003776
3777 // Clear the funcref afterwards, so that deleting it while
3778 // evaluating the arguments is possible (see test55).
3779 if (evaluate)
3780 clear_tv(&base);
3781
3782 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003783}
3784
3785/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003786 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003787 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003788 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3789 */
3790 static int
3791eval_method(
3792 char_u **arg,
3793 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003794 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003795 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003796{
3797 char_u *name;
3798 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003799 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003800 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003801 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003802 int evaluate = evalarg != NULL
3803 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003804
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003805 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003806
Bram Moolenaarac92e252019-08-03 21:58:38 +02003807 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003808 len = get_name_len(arg, &alias, evaluate, TRUE);
3809 if (alias != NULL)
3810 name = alias;
3811
3812 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003813 {
3814 if (verbose)
3815 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003816 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003817 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003818 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003819 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003820 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003821 if (**arg != '(')
3822 {
3823 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003825 ret = FAIL;
3826 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003827 else if (VIM_ISWHITE((*arg)[-1]))
3828 {
3829 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003830 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003831 ret = FAIL;
3832 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003833 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003834 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003835 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003836 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003837
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003838 // Clear the funcref afterwards, so that deleting it while
3839 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003840 if (evaluate)
3841 clear_tv(&base);
3842
Bram Moolenaarac92e252019-08-03 21:58:38 +02003843 return ret;
3844}
3845
3846/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003847 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3848 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003849 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3850 */
3851 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003852eval_index(
3853 char_u **arg,
3854 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003855 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003856 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003857{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003858 int evaluate = evalarg != NULL
3859 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003860 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003861 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003862 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003863 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003864 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003865 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003866
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003867 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3868 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003869
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003870 init_tv(&var1);
3871 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003872 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003873 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003874 /*
3875 * dict.name
3876 */
3877 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003878 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003879 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003880 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003881 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003882 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003883 }
3884 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003886 /*
3887 * something[idx]
3888 *
3889 * Get the (first) variable from inside the [].
3890 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003891 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003892 if (**arg == ':')
3893 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003894 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003895 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003896 else if (vim9 && **arg == ':')
3897 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003898 semsg(_(e_white_space_required_before_and_after_str_at_str),
3899 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003900 clear_tv(&var1);
3901 return FAIL;
3902 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003903 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003904 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003905#ifdef FEAT_FLOAT
3906 // allow for indexing with float
3907 if (vim9 && rettv->v_type == VAR_DICT
3908 && var1.v_type == VAR_FLOAT)
3909 {
3910 var1.vval.v_string = typval_tostring(&var1, TRUE);
3911 var1.v_type = VAR_STRING;
3912 }
3913#endif
3914 if (tv_get_string_chk(&var1) == NULL)
3915 {
3916 // not a number or string
3917 clear_tv(&var1);
3918 return FAIL;
3919 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003920 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003921
3922 /*
3923 * Get the second variable from inside the [:].
3924 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003925 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003926 if (**arg == ':')
3927 {
3928 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003929 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003930 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003931 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003932 semsg(_(e_white_space_required_before_and_after_str_at_str),
3933 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003934 if (!empty1)
3935 clear_tv(&var1);
3936 return FAIL;
3937 }
3938 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003939 if (**arg == ']')
3940 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003941 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 if (!empty1)
3944 clear_tv(&var1);
3945 return FAIL;
3946 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003947 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003948 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003949 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003950 if (!empty1)
3951 clear_tv(&var1);
3952 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003953 return FAIL;
3954 }
3955 }
3956
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003957 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003958 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003959 if (**arg != ']')
3960 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003961 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003962 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003963 clear_tv(&var1);
3964 if (range)
3965 clear_tv(&var2);
3966 return FAIL;
3967 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003968 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003969 }
3970
3971 if (evaluate)
3972 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003973 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003974 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003975 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003976
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003977 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003978 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003979 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003980 clear_tv(&var2);
3981 return res;
3982 }
3983 return OK;
3984}
3985
3986/*
3987 * Check if "rettv" can have an [index] or [sli:ce]
3988 */
3989 int
3990check_can_index(typval_T *rettv, int evaluate, int verbose)
3991{
3992 switch (rettv->v_type)
3993 {
3994 case VAR_FUNC:
3995 case VAR_PARTIAL:
3996 if (verbose)
3997 emsg(_("E695: Cannot index a Funcref"));
3998 return FAIL;
3999 case VAR_FLOAT:
4000#ifdef FEAT_FLOAT
4001 if (verbose)
4002 emsg(_(e_float_as_string));
4003 return FAIL;
4004#endif
4005 case VAR_BOOL:
4006 case VAR_SPECIAL:
4007 case VAR_JOB:
4008 case VAR_CHANNEL:
4009 if (verbose)
4010 emsg(_(e_cannot_index_special_variable));
4011 return FAIL;
4012 case VAR_UNKNOWN:
4013 case VAR_ANY:
4014 case VAR_VOID:
4015 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004016 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004017 emsg(_(e_cannot_index_special_variable));
4018 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004019 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004020 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004021
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004022 case VAR_STRING:
4023 case VAR_LIST:
4024 case VAR_DICT:
4025 case VAR_BLOB:
4026 break;
4027 case VAR_NUMBER:
4028 if (in_vim9script())
4029 emsg(_(e_cannot_index_number));
4030 break;
4031 }
4032 return OK;
4033}
4034
4035/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004036 * slice() function
4037 */
4038 void
4039f_slice(typval_T *argvars, typval_T *rettv)
4040{
4041 if (check_can_index(argvars, TRUE, FALSE) == OK)
4042 {
4043 copy_tv(argvars, rettv);
4044 eval_index_inner(rettv, TRUE, argvars + 1,
4045 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4046 TRUE, NULL, 0, FALSE);
4047 }
4048}
4049
4050/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004051 * Apply index or range to "rettv".
4052 * "var1" is the first index, NULL for [:expr].
4053 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004054 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4055 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004056 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4057 */
4058 int
4059eval_index_inner(
4060 typval_T *rettv,
4061 int is_range,
4062 typval_T *var1,
4063 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004064 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004065 char_u *key,
4066 int keylen,
4067 int verbose)
4068{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004069 varnumber_T n1, n2 = 0;
4070 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004071
4072 n1 = 0;
4073 if (var1 != NULL && rettv->v_type != VAR_DICT)
4074 n1 = tv_get_number(var1);
4075
4076 if (is_range)
4077 {
4078 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004079 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004080 if (verbose)
4081 emsg(_(e_cannot_slice_dictionary));
4082 return FAIL;
4083 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004084 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004085 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004086 else
4087 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004088 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004089
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004090 switch (rettv->v_type)
4091 {
4092 case VAR_UNKNOWN:
4093 case VAR_ANY:
4094 case VAR_VOID:
4095 case VAR_FUNC:
4096 case VAR_PARTIAL:
4097 case VAR_FLOAT:
4098 case VAR_BOOL:
4099 case VAR_SPECIAL:
4100 case VAR_JOB:
4101 case VAR_CHANNEL:
4102 break; // not evaluating, skipping over subscript
4103
4104 case VAR_NUMBER:
4105 case VAR_STRING:
4106 {
4107 char_u *s = tv_get_string(rettv);
4108
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004109 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004110 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004111 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004112 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004113 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004114 else
4115 s = char_from_string(s, n1);
4116 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004117 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004118 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004119 // The resulting variable is a substring. If the indexes
4120 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004121 if (n1 < 0)
4122 {
4123 n1 = len + n1;
4124 if (n1 < 0)
4125 n1 = 0;
4126 }
4127 if (n2 < 0)
4128 n2 = len + n2;
4129 else if (n2 >= len)
4130 n2 = len;
4131 if (n1 >= len || n2 < 0 || n1 > n2)
4132 s = NULL;
4133 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004134 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004135 }
4136 else
4137 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004138 // The resulting variable is a string of a single
4139 // character. If the index is too big or negative the
4140 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004141 if (n1 >= len || n1 < 0)
4142 s = NULL;
4143 else
4144 s = vim_strnsave(s + n1, 1);
4145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004146 clear_tv(rettv);
4147 rettv->v_type = VAR_STRING;
4148 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004149 }
4150 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004151
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004152 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004153 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4154 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004155 break;
4156
4157 case VAR_LIST:
4158 if (var1 == NULL)
4159 n1 = 0;
4160 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004161 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004162 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004163 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004164 return FAIL;
4165 break;
4166
4167 case VAR_DICT:
4168 {
4169 dictitem_T *item;
4170 typval_T tmp;
4171
4172 if (key == NULL)
4173 {
4174 key = tv_get_string_chk(var1);
4175 if (key == NULL)
4176 return FAIL;
4177 }
4178
4179 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4180
4181 if (item == NULL && verbose)
4182 semsg(_(e_dictkey), key);
4183 if (item == NULL)
4184 return FAIL;
4185
4186 copy_tv(&item->di_tv, &tmp);
4187 clear_tv(rettv);
4188 *rettv = tmp;
4189 }
4190 break;
4191 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004192 return OK;
4193}
4194
4195/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004196 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004197 */
4198 char_u *
4199partial_name(partial_T *pt)
4200{
4201 if (pt->pt_name != NULL)
4202 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004203 if (pt->pt_func != NULL)
4204 return pt->pt_func->uf_name;
4205 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004206}
4207
Bram Moolenaarddecc252016-04-06 22:59:37 +02004208 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004209partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004210{
4211 int i;
4212
4213 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004214 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004215 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004216 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004217 if (pt->pt_name != NULL)
4218 {
4219 func_unref(pt->pt_name);
4220 vim_free(pt->pt_name);
4221 }
4222 else
4223 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004224
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004225 // Decrease the reference count for the context of a closure. If down
4226 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004227 if (pt->pt_funcstack != NULL)
4228 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004229 --pt->pt_funcstack->fs_refcount;
4230 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004231 }
4232
Bram Moolenaarddecc252016-04-06 22:59:37 +02004233 vim_free(pt);
4234}
4235
4236/*
4237 * Unreference a closure: decrement the reference count and free it when it
4238 * becomes zero.
4239 */
4240 void
4241partial_unref(partial_T *pt)
4242{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004243 if (pt != NULL)
4244 {
4245 if (--pt->pt_refcount <= 0)
4246 partial_free(pt);
4247
4248 // If the reference count goes down to one, the funcstack may be the
4249 // only reference and can be freed if no other partials reference it.
4250 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4251 funcstack_check_refcount(pt->pt_funcstack);
4252 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004253}
4254
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004255/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004256 * Return the next (unique) copy ID.
4257 * Used for serializing nested structures.
4258 */
4259 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004260get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004261{
4262 current_copyID += COPYID_INC;
4263 return current_copyID;
4264}
4265
4266/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004267 * Garbage collection for lists and dictionaries.
4268 *
4269 * We use reference counts to be able to free most items right away when they
4270 * are no longer used. But for composite items it's possible that it becomes
4271 * unused while the reference count is > 0: When there is a recursive
4272 * reference. Example:
4273 * :let l = [1, 2, 3]
4274 * :let d = {9: l}
4275 * :let l[1] = d
4276 *
4277 * Since this is quite unusual we handle this with garbage collection: every
4278 * once in a while find out which lists and dicts are not referenced from any
4279 * variable.
4280 *
4281 * Here is a good reference text about garbage collection (refers to Python
4282 * but it applies to all reference-counting mechanisms):
4283 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004284 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004285
4286/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004287 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004288 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004289 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004290 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004291 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004292garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004293{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004294 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004295 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004296 buf_T *buf;
4297 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004298 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004299 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004300
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004301 if (!testing)
4302 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004303 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004304 want_garbage_collect = FALSE;
4305 may_garbage_collect = FALSE;
4306 garbage_collect_at_exit = FALSE;
4307 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004308
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004309 // The execution stack can grow big, limit the size.
4310 if (exestack.ga_maxlen - exestack.ga_len > 500)
4311 {
4312 size_t new_len;
4313 char_u *pp;
4314 int n;
4315
4316 // Keep 150% of the current size, with a minimum of the growth size.
4317 n = exestack.ga_len / 2;
4318 if (n < exestack.ga_growsize)
4319 n = exestack.ga_growsize;
4320
4321 // Don't make it bigger though.
4322 if (exestack.ga_len + n < exestack.ga_maxlen)
4323 {
4324 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4325 pp = vim_realloc(exestack.ga_data, new_len);
4326 if (pp == NULL)
4327 return FAIL;
4328 exestack.ga_maxlen = exestack.ga_len + n;
4329 exestack.ga_data = pp;
4330 }
4331 }
4332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004333 // We advance by two because we add one for items referenced through
4334 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004335 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004336
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004337 /*
4338 * 1. Go through all accessible variables and mark all lists and dicts
4339 * with copyID.
4340 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004341
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004342 // Don't free variables in the previous_funccal list unless they are only
4343 // referenced through previous_funccal. This must be first, because if
4344 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004345 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004346
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004347 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004348 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004349
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004350 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004351 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004352 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4353 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004354
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004355 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004356 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004357 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4358 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004359 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004360 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4361 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004362#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004363 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004364 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4365 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004366 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004367 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004368 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4369 NULL, NULL);
4370#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004371
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004372 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004373 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004374 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4375 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004376 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004377 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004378
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004379 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004380 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004382 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004383 abort = abort || set_ref_in_functions(copyID);
4384
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004385 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004386 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004387
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004388 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004389 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004390
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004391 // callbacks in buffers
4392 abort = abort || set_ref_in_buffers(copyID);
4393
Bram Moolenaar1dced572012-04-05 16:54:08 +02004394#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004395 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004396#endif
4397
Bram Moolenaardb913952012-06-29 12:54:53 +02004398#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004399 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004400#endif
4401
4402#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004403 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004404#endif
4405
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004406#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004407 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004408 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004409#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004410#ifdef FEAT_NETBEANS_INTG
4411 abort = abort || set_ref_in_nb_channel(copyID);
4412#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004413
Bram Moolenaare3188e22016-05-31 21:13:04 +02004414#ifdef FEAT_TIMERS
4415 abort = abort || set_ref_in_timer(copyID);
4416#endif
4417
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004418#ifdef FEAT_QUICKFIX
4419 abort = abort || set_ref_in_quickfix(copyID);
4420#endif
4421
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004422#ifdef FEAT_TERMINAL
4423 abort = abort || set_ref_in_term(copyID);
4424#endif
4425
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004426#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004427 abort = abort || set_ref_in_popups(copyID);
4428#endif
4429
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004430 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004431 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004432 /*
4433 * 2. Free lists and dictionaries that are not referenced.
4434 */
4435 did_free = free_unref_items(copyID);
4436
4437 /*
4438 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004439 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004440 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004441 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004442 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004443 else if (p_verbose > 0)
4444 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004445 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004446 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004447
4448 return did_free;
4449}
4450
4451/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004452 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004453 */
4454 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004455free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004456{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004457 int did_free = FALSE;
4458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004459 // Let all "free" functions know that we are here. This means no
4460 // dictionaries, lists, channels or jobs are to be freed, because we will
4461 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004462 in_free_unref_items = TRUE;
4463
4464 /*
4465 * PASS 1: free the contents of the items. We don't free the items
4466 * themselves yet, so that it is possible to decrement refcount counters
4467 */
4468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004469 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004470 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004472 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004473 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004474
4475#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004476 // Go through the list of jobs and free items without the copyID. This
4477 // must happen before doing channels, because jobs refer to channels, but
4478 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004479 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004481 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004482 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4483#endif
4484
4485 /*
4486 * PASS 2: free the items themselves.
4487 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004488 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004489 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004490
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004491#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004492 // Go through the list of jobs and free items without the copyID. This
4493 // must happen before doing channels, because jobs refer to channels, but
4494 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004495 free_unused_jobs(copyID, COPYID_MASK);
4496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004497 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004498 free_unused_channels(copyID, COPYID_MASK);
4499#endif
4500
4501 in_free_unref_items = FALSE;
4502
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004503 return did_free;
4504}
4505
4506/*
4507 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004508 * "list_stack" is used to add lists to be marked. Can be NULL.
4509 *
4510 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004511 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004512 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004513set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004514{
4515 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004516 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004517 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004518 hashtab_T *cur_ht;
4519 ht_stack_T *ht_stack = NULL;
4520 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004521
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004522 cur_ht = ht;
4523 for (;;)
4524 {
4525 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004526 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004527 // Mark each item in the hashtab. If the item contains a hashtab
4528 // it is added to ht_stack, if it contains a list it is added to
4529 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004530 todo = (int)cur_ht->ht_used;
4531 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4532 if (!HASHITEM_EMPTY(hi))
4533 {
4534 --todo;
4535 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4536 &ht_stack, list_stack);
4537 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004538 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004539
4540 if (ht_stack == NULL)
4541 break;
4542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004543 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004544 cur_ht = ht_stack->ht;
4545 tempitem = ht_stack;
4546 ht_stack = ht_stack->prev;
4547 free(tempitem);
4548 }
4549
4550 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004551}
4552
4553/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004554 * Mark a dict and its items with "copyID".
4555 * Returns TRUE if setting references failed somehow.
4556 */
4557 int
4558set_ref_in_dict(dict_T *d, int copyID)
4559{
4560 if (d != NULL && d->dv_copyID != copyID)
4561 {
4562 d->dv_copyID = copyID;
4563 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4564 }
4565 return FALSE;
4566}
4567
4568/*
4569 * Mark a list and its items with "copyID".
4570 * Returns TRUE if setting references failed somehow.
4571 */
4572 int
4573set_ref_in_list(list_T *ll, int copyID)
4574{
4575 if (ll != NULL && ll->lv_copyID != copyID)
4576 {
4577 ll->lv_copyID = copyID;
4578 return set_ref_in_list_items(ll, copyID, NULL);
4579 }
4580 return FALSE;
4581}
4582
4583/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004584 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004585 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4586 *
4587 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004588 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004589 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004590set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004591{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004592 listitem_T *li;
4593 int abort = FALSE;
4594 list_T *cur_l;
4595 list_stack_T *list_stack = NULL;
4596 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004597
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004598 cur_l = l;
4599 for (;;)
4600 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004601 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004602 // Mark each item in the list. If the item contains a hashtab
4603 // it is added to ht_stack, if it contains a list it is added to
4604 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004605 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4606 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4607 ht_stack, &list_stack);
4608 if (list_stack == NULL)
4609 break;
4610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004611 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004612 cur_l = list_stack->list;
4613 tempitem = list_stack;
4614 list_stack = list_stack->prev;
4615 free(tempitem);
4616 }
4617
4618 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004619}
4620
4621/*
4622 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004623 * "list_stack" is used to add lists to be marked. Can be NULL.
4624 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4625 *
4626 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004627 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004629set_ref_in_item(
4630 typval_T *tv,
4631 int copyID,
4632 ht_stack_T **ht_stack,
4633 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004634{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004635 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004636
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004637 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004638 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004639 dict_T *dd = tv->vval.v_dict;
4640
Bram Moolenaara03f2332016-02-06 18:09:59 +01004641 if (dd != NULL && dd->dv_copyID != copyID)
4642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004643 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004644 dd->dv_copyID = copyID;
4645 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004646 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004647 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4648 }
4649 else
4650 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004651 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4652
Bram Moolenaara03f2332016-02-06 18:09:59 +01004653 if (newitem == NULL)
4654 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004655 else
4656 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004657 newitem->ht = &dd->dv_hashtab;
4658 newitem->prev = *ht_stack;
4659 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004660 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004661 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004662 }
4663 }
4664 else if (tv->v_type == VAR_LIST)
4665 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004666 list_T *ll = tv->vval.v_list;
4667
Bram Moolenaara03f2332016-02-06 18:09:59 +01004668 if (ll != NULL && ll->lv_copyID != copyID)
4669 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004670 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004671 ll->lv_copyID = copyID;
4672 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004673 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004674 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004675 }
4676 else
4677 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004678 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4679
Bram Moolenaara03f2332016-02-06 18:09:59 +01004680 if (newitem == NULL)
4681 abort = TRUE;
4682 else
4683 {
4684 newitem->list = ll;
4685 newitem->prev = *list_stack;
4686 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004687 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004688 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004689 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004690 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004691 else if (tv->v_type == VAR_FUNC)
4692 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004693 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004694 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004695 else if (tv->v_type == VAR_PARTIAL)
4696 {
4697 partial_T *pt = tv->vval.v_partial;
4698 int i;
4699
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004700 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004701 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004702 // Didn't see this partial yet.
4703 pt->pt_copyID = copyID;
4704
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004705 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004706
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004707 if (pt->pt_dict != NULL)
4708 {
4709 typval_T dtv;
4710
4711 dtv.v_type = VAR_DICT;
4712 dtv.vval.v_dict = pt->pt_dict;
4713 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4714 }
4715
4716 for (i = 0; i < pt->pt_argc; ++i)
4717 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4718 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004719 if (pt->pt_funcstack != NULL)
4720 {
4721 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4722
4723 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4724 abort = abort || set_ref_in_item(stack + i, copyID,
4725 ht_stack, list_stack);
4726 }
4727
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004728 }
4729 }
4730#ifdef FEAT_JOB_CHANNEL
4731 else if (tv->v_type == VAR_JOB)
4732 {
4733 job_T *job = tv->vval.v_job;
4734 typval_T dtv;
4735
4736 if (job != NULL && job->jv_copyID != copyID)
4737 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004738 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004739 if (job->jv_channel != NULL)
4740 {
4741 dtv.v_type = VAR_CHANNEL;
4742 dtv.vval.v_channel = job->jv_channel;
4743 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4744 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004745 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004746 {
4747 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004748 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004749 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4750 }
4751 }
4752 }
4753 else if (tv->v_type == VAR_CHANNEL)
4754 {
4755 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004756 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004757 typval_T dtv;
4758 jsonq_T *jq;
4759 cbq_T *cq;
4760
4761 if (ch != NULL && ch->ch_copyID != copyID)
4762 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004763 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004764 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004765 {
4766 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4767 jq = jq->jq_next)
4768 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4769 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4770 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004771 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004772 {
4773 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004774 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004775 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4776 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004777 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004778 {
4779 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004780 dtv.vval.v_partial =
4781 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004782 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4783 }
4784 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004785 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004786 {
4787 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004788 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004789 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4790 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004791 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004792 {
4793 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004794 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004795 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4796 }
4797 }
4798 }
4799#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004800 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004801}
4802
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804 * Return a string with the string representation of a variable.
4805 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004806 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004807 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004808 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004809 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004810 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004811 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4812 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004813 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004814 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004815 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004816echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004817 typval_T *tv,
4818 char_u **tofree,
4819 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004820 int copyID,
4821 int echo_style,
4822 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004823 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004824{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004825 static int recurse = 0;
4826 char_u *r = NULL;
4827
Bram Moolenaar33570922005-01-25 22:26:29 +00004828 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004829 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004830 if (!did_echo_string_emsg)
4831 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004832 // Only give this message once for a recursive call to avoid
4833 // flooding the user with errors. And stop iterating over lists
4834 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004835 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004836 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004837 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004838 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004839 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004840 }
4841 ++recurse;
4842
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004843 switch (tv->v_type)
4844 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004845 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004846 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004847 {
4848 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004849 r = tv->vval.v_string;
4850 if (r == NULL)
4851 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004852 }
4853 else
4854 {
4855 *tofree = string_quote(tv->vval.v_string, FALSE);
4856 r = *tofree;
4857 }
4858 break;
4859
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004860 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004861 if (echo_style)
4862 {
4863 *tofree = NULL;
4864 r = tv->vval.v_string;
4865 }
4866 else
4867 {
4868 *tofree = string_quote(tv->vval.v_string, TRUE);
4869 r = *tofree;
4870 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004871 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004872
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004873 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004874 {
4875 partial_T *pt = tv->vval.v_partial;
4876 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004877 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004878 garray_T ga;
4879 int i;
4880 char_u *tf;
4881
4882 ga_init2(&ga, 1, 100);
4883 ga_concat(&ga, (char_u *)"function(");
4884 if (fname != NULL)
4885 {
4886 ga_concat(&ga, fname);
4887 vim_free(fname);
4888 }
4889 if (pt != NULL && pt->pt_argc > 0)
4890 {
4891 ga_concat(&ga, (char_u *)", [");
4892 for (i = 0; i < pt->pt_argc; ++i)
4893 {
4894 if (i > 0)
4895 ga_concat(&ga, (char_u *)", ");
4896 ga_concat(&ga,
4897 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4898 vim_free(tf);
4899 }
4900 ga_concat(&ga, (char_u *)"]");
4901 }
4902 if (pt != NULL && pt->pt_dict != NULL)
4903 {
4904 typval_T dtv;
4905
4906 ga_concat(&ga, (char_u *)", ");
4907 dtv.v_type = VAR_DICT;
4908 dtv.vval.v_dict = pt->pt_dict;
4909 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4910 vim_free(tf);
4911 }
4912 ga_concat(&ga, (char_u *)")");
4913
4914 *tofree = ga.ga_data;
4915 r = *tofree;
4916 break;
4917 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004918
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004919 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004920 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004921 break;
4922
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004923 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004924 if (tv->vval.v_list == NULL)
4925 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004926 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004927 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004928 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004929 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004930 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4931 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004932 {
4933 *tofree = NULL;
4934 r = (char_u *)"[...]";
4935 }
4936 else
4937 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004938 int old_copyID = tv->vval.v_list->lv_copyID;
4939
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004940 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004941 *tofree = list2string(tv, copyID, restore_copyID);
4942 if (restore_copyID)
4943 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004944 r = *tofree;
4945 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004946 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004947
Bram Moolenaar8c711452005-01-14 21:53:12 +00004948 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004949 if (tv->vval.v_dict == NULL)
4950 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004951 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004952 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004953 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004954 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004955 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4956 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004957 {
4958 *tofree = NULL;
4959 r = (char_u *)"{...}";
4960 }
4961 else
4962 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004963 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004964
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004965 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004966 *tofree = dict2string(tv, copyID, restore_copyID);
4967 if (restore_copyID)
4968 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004969 r = *tofree;
4970 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004971 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004972
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004973 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004974 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004975 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004977 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004978 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004979 break;
4980
Bram Moolenaar835dc632016-02-07 14:27:38 +01004981 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004982 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004983 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004984 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004985 if (composite_val)
4986 {
4987 *tofree = string_quote(r, FALSE);
4988 r = *tofree;
4989 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004990 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004991
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004993#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994 *tofree = NULL;
4995 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4996 r = numbuf;
4997 break;
4998#endif
4999
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005000 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005001 case VAR_SPECIAL:
5002 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005003 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005004 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005005 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005006
Bram Moolenaar8502c702014-06-17 12:51:16 +02005007 if (--recurse == 0)
5008 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005009 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005010}
5011
5012/*
5013 * Return a string with the string representation of a variable.
5014 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5015 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005016 * Does not put quotes around strings, as ":echo" displays values.
5017 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5018 * May return NULL.
5019 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005020 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005021echo_string(
5022 typval_T *tv,
5023 char_u **tofree,
5024 char_u *numbuf,
5025 int copyID)
5026{
5027 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5028}
5029
5030/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005031 * Return string "str" in ' quotes, doubling ' characters.
5032 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005034 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005035 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005036string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005037{
Bram Moolenaar33570922005-01-25 22:26:29 +00005038 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005039 char_u *p, *r, *s;
5040
Bram Moolenaar33570922005-01-25 22:26:29 +00005041 len = (function ? 13 : 3);
5042 if (str != NULL)
5043 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005044 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005045 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005046 if (*p == '\'')
5047 ++len;
5048 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005049 s = r = alloc(len);
5050 if (r != NULL)
5051 {
5052 if (function)
5053 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005054 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005055 r += 10;
5056 }
5057 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005059 if (str != NULL)
5060 for (p = str; *p != NUL; )
5061 {
5062 if (*p == '\'')
5063 *r++ = '\'';
5064 MB_COPY_CHAR(p, r);
5065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005067 if (function)
5068 *r++ = ')';
5069 *r++ = NUL;
5070 }
5071 return s;
5072}
5073
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005074#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005075/*
5076 * Convert the string "text" to a floating point number.
5077 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5078 * this always uses a decimal point.
5079 * Returns the length of the text that was consumed.
5080 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005082string2float(
5083 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005084 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005085{
5086 char *s = (char *)text;
5087 float_T f;
5088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005089 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005090 if (STRNICMP(text, "inf", 3) == 0)
5091 {
5092 *value = INFINITY;
5093 return 3;
5094 }
5095 if (STRNICMP(text, "-inf", 3) == 0)
5096 {
5097 *value = -INFINITY;
5098 return 4;
5099 }
5100 if (STRNICMP(text, "nan", 3) == 0)
5101 {
5102 *value = NAN;
5103 return 3;
5104 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 f = strtod(s, &s);
5106 *value = f;
5107 return (int)((char_u *)s - text);
5108}
5109#endif
5110
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005111/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005112 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5113 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005114 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005115 */
5116 int
5117buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5118{
5119 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005120 char_u *t;
5121 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005122
5123 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5124 return -1;
5125
5126 if (lnum > buf->b_ml.ml_line_count)
5127 lnum = buf->b_ml.ml_line_count;
5128
5129 str = ml_get_buf(buf, lnum, FALSE);
5130 if (str == NULL)
5131 return -1;
5132
5133 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005134 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005135
Bram Moolenaar91458462021-01-13 20:08:38 +01005136 // count the number of characters
5137 t = str;
5138 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5139 t += mb_ptr2len(t);
5140
5141 // In insert mode, when the cursor is at the end of a non-empty line,
5142 // byteidx points to the NUL character immediately past the end of the
5143 // string. In this case, add one to the character count.
5144 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5145 count++;
5146
5147 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005148}
5149
5150/*
5151 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005152 * byte index. Works only for loaded buffers. Returns -1 on failure.
5153 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005154 */
5155 int
5156buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5157{
5158 char_u *str;
5159 char_u *t;
5160
5161 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5162 return -1;
5163
5164 if (lnum > buf->b_ml.ml_line_count)
5165 lnum = buf->b_ml.ml_line_count;
5166
5167 str = ml_get_buf(buf, lnum, FALSE);
5168 if (str == NULL)
5169 return -1;
5170
5171 // Convert the character offset to a byte offset
5172 t = str;
5173 while (*t != NUL && --charidx > 0)
5174 t += mb_ptr2len(t);
5175
Bram Moolenaar91458462021-01-13 20:08:38 +01005176 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005177}
5178
5179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005181 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005183 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005184var2fpos(
5185 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005186 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005187 int *fnum, // set to fnum for '0, 'A, etc.
5188 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005190 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005192 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005194 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005195 if (varp->v_type == VAR_LIST)
5196 {
5197 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005198 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005199 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005200 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005201
5202 l = varp->vval.v_list;
5203 if (l == NULL)
5204 return NULL;
5205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005206 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005207 pos.lnum = list_find_nr(l, 0L, &error);
5208 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005209 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005210 if (charcol)
5211 len = (long)mb_charlen(ml_get(pos.lnum));
5212 else
5213 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005214
Bram Moolenaarec65d772020-08-20 22:29:12 +02005215 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005216 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005217 li = list_find(l, 1L);
5218 if (li != NULL && li->li_tv.v_type == VAR_STRING
5219 && li->li_tv.vval.v_string != NULL
5220 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005221 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005222 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005223 }
5224 else
5225 {
5226 pos.col = list_find_nr(l, 1L, &error);
5227 if (error)
5228 return NULL;
5229 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005231 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005232 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005233 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005234 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005237 pos.coladd = list_find_nr(l, 2L, &error);
5238 if (error)
5239 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005240
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005241 return &pos;
5242 }
5243
Bram Moolenaarc5809432021-03-27 21:23:30 +01005244 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5245 return NULL;
5246
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005247 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005248 if (name == NULL)
5249 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005250 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005251 {
5252 pos = curwin->w_cursor;
5253 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005254 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005255 return &pos;
5256 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005257 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005258 {
5259 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005260 pos = VIsual;
5261 else
5262 pos = curwin->w_cursor;
5263 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005264 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005265 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005266 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005267 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005269 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5271 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005272 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005273 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 return pp;
5275 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005276
Bram Moolenaara5525202006-03-02 22:52:09 +00005277 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005278
Bram Moolenaar477933c2007-07-17 14:32:23 +00005279 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005280 {
5281 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005282 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005283 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005284 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005285 // In silent Ex mode topline is zero, but that's not a valid line
5286 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005287 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005288 return &pos;
5289 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005290 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005291 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005292 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005293 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005294 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005295 return &pos;
5296 }
5297 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005298 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005300 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {
5302 pos.lnum = curbuf->b_ml.ml_line_count;
5303 pos.col = 0;
5304 }
5305 else
5306 {
5307 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005308 if (charcol)
5309 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5310 else
5311 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
5313 return &pos;
5314 }
5315 return NULL;
5316}
5317
5318/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005319 * Convert list in "arg" into a position and optional file number.
5320 * When "fnump" is NULL there is no file number, only 3 items.
5321 * Note that the column is passed on as-is, the caller may want to decrement
5322 * it to use 1 for the first column.
5323 * Return FAIL when conversion is not possible, doesn't check the position for
5324 * validity.
5325 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005326 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005327list2fpos(
5328 typval_T *arg,
5329 pos_T *posp,
5330 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005331 colnr_T *curswantp,
5332 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005333{
5334 list_T *l = arg->vval.v_list;
5335 long i = 0;
5336 long n;
5337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005338 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5339 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005340 if (arg->v_type != VAR_LIST
5341 || l == NULL
5342 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005343 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005344 return FAIL;
5345
5346 if (fnump != NULL)
5347 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005348 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005349 if (n < 0)
5350 return FAIL;
5351 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005352 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005353 *fnump = n;
5354 }
5355
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005356 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005357 if (n < 0)
5358 return FAIL;
5359 posp->lnum = n;
5360
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005361 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005362 if (n < 0)
5363 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005364 // If character position is specified, then convert to byte position
5365 if (charcol)
5366 {
5367 buf_T *buf;
5368
5369 // Get the text for the specified line in a loaded buffer
5370 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5371 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5372 return FAIL;
5373
Bram Moolenaar91458462021-01-13 20:08:38 +01005374 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005375 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005376 posp->col = n;
5377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005379 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005380 posp->coladd = 0;
5381 else
5382 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005383
Bram Moolenaar493c1782014-05-28 14:34:46 +02005384 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005385 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005386
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005387 return OK;
5388}
5389
5390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 * Get the length of an environment variable name.
5392 * Advance "arg" to the first character after the name.
5393 * Return 0 for error.
5394 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005395 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005396get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398 char_u *p;
5399 int len;
5400
5401 for (p = *arg; vim_isIDc(*p); ++p)
5402 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005403 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 return 0;
5405
5406 len = (int)(p - *arg);
5407 *arg = p;
5408 return len;
5409}
5410
5411/*
5412 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005413 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 * Return 0 if something is wrong.
5415 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005417get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
5419 char_u *p;
5420 int len;
5421
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005422 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005424 {
5425 if (*p == ':')
5426 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005427 // "s:" is start of "s:var", but "n:" is not and can be used in
5428 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005429 len = (int)(p - *arg);
5430 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5431 || len > 1)
5432 break;
5433 }
5434 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005435 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 return 0;
5437
5438 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005439 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
5441 return len;
5442}
5443
5444/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005445 * Get the length of the name of a variable or function.
5446 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005448 * Return -1 if curly braces expansion failed.
5449 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 * If the name contains 'magic' {}'s, expand them and return the
5451 * expanded name in an allocated string via 'alias' - caller must free.
5452 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005454get_name_len(
5455 char_u **arg,
5456 char_u **alias,
5457 int evaluate,
5458 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459{
5460 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 char_u *p;
5462 char_u *expr_start;
5463 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005465 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
5467 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5468 && (*arg)[2] == (int)KE_SNR)
5469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005470 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 *arg += 3;
5472 return get_id_len(arg) + 3;
5473 }
5474 len = eval_fname_script(*arg);
5475 if (len > 0)
5476 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005477 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 *arg += len;
5479 }
5480
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005482 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005484 p = find_name_end(*arg, &expr_start, &expr_end,
5485 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 if (expr_start != NULL)
5487 {
5488 char_u *temp_string;
5489
5490 if (!evaluate)
5491 {
5492 len += (int)(p - *arg);
5493 *arg = skipwhite(p);
5494 return len;
5495 }
5496
5497 /*
5498 * Include any <SID> etc in the expanded string:
5499 * Thus the -len here.
5500 */
5501 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5502 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005503 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 *alias = temp_string;
5505 *arg = skipwhite(p);
5506 return (int)STRLEN(temp_string);
5507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
5509 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005510 // Only give an error when there is something, otherwise it will be
5511 // reported at a higher level.
5512 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005513 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514
5515 return len;
5516}
5517
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518/*
5519 * Find the end of a variable or function name, taking care of magic braces.
5520 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5521 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005522 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 * Return a pointer to just after the name. Equal to "arg" if there is no
5524 * valid name.
5525 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005526 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005527find_name_end(
5528 char_u *arg,
5529 char_u **expr_start,
5530 char_u **expr_end,
5531 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 int mb_nest = 0;
5534 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005536 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005537 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 *expr_start = NULL;
5542 *expr_end = NULL;
5543 }
5544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005545 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005546 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5547 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005548 return arg;
5549
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 for (p = arg; *p != NUL
5551 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005552 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005553 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005554 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005556 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005558 if (*p == '\'')
5559 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005560 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005561 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005562 ;
5563 if (*p == NUL)
5564 break;
5565 }
5566 else if (*p == '"')
5567 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005568 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005569 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005570 if (*p == '\\' && p[1] != NUL)
5571 ++p;
5572 if (*p == NUL)
5573 break;
5574 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005575 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005577 // "s:" is start of "s:var", but "n:" is not and can be used in
5578 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005579 len = (int)(p - arg);
5580 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005581 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005582 break;
5583 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005584
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005587 if (*p == '[')
5588 ++br_nest;
5589 else if (*p == ']')
5590 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005592
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005593 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005595 if (*p == '{')
5596 {
5597 mb_nest++;
5598 if (expr_start != NULL && *expr_start == NULL)
5599 *expr_start = p;
5600 }
5601 else if (*p == '}')
5602 {
5603 mb_nest--;
5604 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5605 *expr_end = p;
5606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 }
5609
5610 return p;
5611}
5612
5613/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005614 * Expands out the 'magic' {}'s in a variable/function name.
5615 * Note that this can call itself recursively, to deal with
5616 * constructs like foo{bar}{baz}{bam}
5617 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5618 * "in_start" ^
5619 * "expr_start" ^
5620 * "expr_end" ^
5621 * "in_end" ^
5622 *
5623 * Returns a new allocated string, which the caller must free.
5624 * Returns NULL for failure.
5625 */
5626 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005627make_expanded_name(
5628 char_u *in_start,
5629 char_u *expr_start,
5630 char_u *expr_end,
5631 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005632{
5633 char_u c1;
5634 char_u *retval = NULL;
5635 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005636
5637 if (expr_end == NULL || in_end == NULL)
5638 return NULL;
5639 *expr_start = NUL;
5640 *expr_end = NUL;
5641 c1 = *in_end;
5642 *in_end = NUL;
5643
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005644 temp_result = eval_to_string(expr_start + 1, FALSE);
5645 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005646 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005647 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5648 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005649 if (retval != NULL)
5650 {
5651 STRCPY(retval, in_start);
5652 STRCAT(retval, temp_result);
5653 STRCAT(retval, expr_end + 1);
5654 }
5655 }
5656 vim_free(temp_result);
5657
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005658 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005659 *expr_start = '{';
5660 *expr_end = '}';
5661
5662 if (retval != NULL)
5663 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005664 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005665 if (expr_start != NULL)
5666 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005667 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005668 temp_result = make_expanded_name(retval, expr_start,
5669 expr_end, temp_result);
5670 vim_free(retval);
5671 retval = temp_result;
5672 }
5673 }
5674
5675 return retval;
5676}
5677
5678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005680 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005682 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005683eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005685 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005686}
5687
5688/*
5689 * Return TRUE if character "c" can be used as the first character in a
5690 * variable or function name (excluding '{' and '}').
5691 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005692 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005693eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005694{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005695 return ASCII_ISALPHA(c) || c == '_';
5696}
5697
5698/*
5699 * Return TRUE if character "c" can be used as the first character of a
5700 * dictionary key.
5701 */
5702 int
5703eval_isdictc(int c)
5704{
5705 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706}
5707
5708/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005709 * Handle:
5710 * - expr[expr], expr[expr:expr] subscript
5711 * - ".name" lookup
5712 * - function call with Funcref variable: func(expr)
5713 * - method call: var->method()
5714 *
5715 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005716 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005717 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005718handle_subscript(
5719 char_u **arg,
5720 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005721 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005722 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005723{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005724 int evaluate = evalarg != NULL
5725 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005726 int ret = OK;
5727 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005728 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005729 int getnext;
5730 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005731
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005732 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005733 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005734 // When at the end of the line and ".name" or "->{" or "->X" follows in
5735 // the next line then consume the line break.
5736 p = eval_next_non_blank(*arg, evalarg, &getnext);
5737 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005738 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005739 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005740 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005741 {
5742 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005743 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005744 check_white = FALSE;
5745 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005746
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005747 if (rettv->v_type == VAR_ANY)
5748 {
5749 char_u *exp_name;
5750 int cc;
5751 int idx;
5752 ufunc_T *ufunc;
5753 type_T *type;
5754
5755 // Found script from "import * as {name}", script item name must
5756 // follow.
5757 if (**arg != '.')
5758 {
5759 if (verbose)
5760 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5761 ret = FAIL;
5762 break;
5763 }
5764 ++*arg;
5765 if (IS_WHITE_OR_NUL(**arg))
5766 {
5767 if (verbose)
5768 emsg(_(e_no_white_space_allowed_after_dot));
5769 ret = FAIL;
5770 break;
5771 }
5772
5773 // isolate the name
5774 exp_name = *arg;
5775 while (eval_isnamec(**arg))
5776 ++*arg;
5777 cc = **arg;
5778 **arg = NUL;
5779
5780 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5781 evalarg->eval_cctx, verbose);
5782 **arg = cc;
5783 *arg = skipwhite(*arg);
5784
5785 if (idx < 0 && ufunc == NULL)
5786 {
5787 ret = FAIL;
5788 break;
5789 }
5790 if (idx >= 0)
5791 {
5792 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5793 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5794
5795 copy_tv(sv->sv_tv, rettv);
5796 }
5797 else
5798 {
5799 rettv->v_type = VAR_FUNC;
5800 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5801 }
5802 }
5803
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005804 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5805 || rettv->v_type == VAR_PARTIAL))
5806 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005807 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005808 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5809 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005810
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005811 // Stop the expression evaluation when immediately aborting on
5812 // error, or when an interrupt occurred or an exception was thrown
5813 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005814 if (aborting())
5815 {
5816 if (ret == OK)
5817 clear_tv(rettv);
5818 ret = FAIL;
5819 }
5820 dict_unref(selfdict);
5821 selfdict = NULL;
5822 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005823 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005824 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005825 *arg = skipwhite(p + 2);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005826 if (ret == OK)
5827 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005828 if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005829 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005830 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005831 else
5832 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005833 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005834 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005835 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005836 // "." is ".name" lookup when we found a dict or when evaluating and
5837 // scriptversion is at least 2, where string concatenation is "..".
5838 else if (**arg == '['
5839 || (**arg == '.' && (rettv->v_type == VAR_DICT
5840 || (!evaluate
5841 && (*arg)[1] != '.'
5842 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005843 {
5844 dict_unref(selfdict);
5845 if (rettv->v_type == VAR_DICT)
5846 {
5847 selfdict = rettv->vval.v_dict;
5848 if (selfdict != NULL)
5849 ++selfdict->dv_refcount;
5850 }
5851 else
5852 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005853 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005854 {
5855 clear_tv(rettv);
5856 ret = FAIL;
5857 }
5858 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005859 else
5860 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005861 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005862
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005863 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5864 // Don't do this when "Func" is already a partial that was bound
5865 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005866 if (selfdict != NULL
5867 && (rettv->v_type == VAR_FUNC
5868 || (rettv->v_type == VAR_PARTIAL
5869 && (rettv->vval.v_partial->pt_auto
5870 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005871 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005872
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005873 dict_unref(selfdict);
5874 return ret;
5875}
5876
5877/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005878 * Make a copy of an item.
5879 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005880 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5881 * reference to an already copied list/dict can be used.
5882 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005883 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005884 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005885item_copy(
5886 typval_T *from,
5887 typval_T *to,
5888 int deep,
5889 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005890{
5891 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005892 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005893
Bram Moolenaar33570922005-01-25 22:26:29 +00005894 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005895 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005896 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005897 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005898 }
5899 ++recurse;
5900
5901 switch (from->v_type)
5902 {
5903 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005904 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005905 case VAR_STRING:
5906 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005907 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005908 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005909 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005910 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005911 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005912 copy_tv(from, to);
5913 break;
5914 case VAR_LIST:
5915 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005916 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005917 if (from->vval.v_list == NULL)
5918 to->vval.v_list = NULL;
5919 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005921 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005922 to->vval.v_list = from->vval.v_list->lv_copylist;
5923 ++to->vval.v_list->lv_refcount;
5924 }
5925 else
5926 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5927 if (to->vval.v_list == NULL)
5928 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005929 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005930 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005931 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005932 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005933 case VAR_DICT:
5934 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005935 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005936 if (from->vval.v_dict == NULL)
5937 to->vval.v_dict = NULL;
5938 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5939 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005940 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005941 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5942 ++to->vval.v_dict->dv_refcount;
5943 }
5944 else
5945 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5946 if (to->vval.v_dict == NULL)
5947 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005948 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005949 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005950 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005951 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005952 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005953 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005954 }
5955 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005956 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005957}
5958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005959 void
5960echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5961{
5962 char_u *tofree;
5963 char_u numbuf[NUMBUFLEN];
5964 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5965
5966 if (*atstart)
5967 {
5968 *atstart = FALSE;
5969 // Call msg_start() after eval1(), evaluating the expression
5970 // may cause a message to appear.
5971 if (with_space)
5972 {
5973 // Mark the saved text as finishing the line, so that what
5974 // follows is displayed on a new line when scrolling back
5975 // at the more prompt.
5976 msg_sb_eol();
5977 msg_start();
5978 }
5979 }
5980 else if (with_space)
5981 msg_puts_attr(" ", echo_attr);
5982
5983 if (p != NULL)
5984 for ( ; *p != NUL && !got_int; ++p)
5985 {
5986 if (*p == '\n' || *p == '\r' || *p == TAB)
5987 {
5988 if (*p != TAB && *needclr)
5989 {
5990 // remove any text still there from the command
5991 msg_clr_eos();
5992 *needclr = FALSE;
5993 }
5994 msg_putchar_attr(*p, echo_attr);
5995 }
5996 else
5997 {
5998 if (has_mbyte)
5999 {
6000 int i = (*mb_ptr2len)(p);
6001
6002 (void)msg_outtrans_len_attr(p, i, echo_attr);
6003 p += i - 1;
6004 }
6005 else
6006 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6007 }
6008 }
6009 vim_free(tofree);
6010}
6011
Bram Moolenaare9a41262005-01-15 22:18:47 +00006012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 * ":echo expr1 ..." print each argument separated with a space, add a
6014 * newline at the end.
6015 * ":echon expr1 ..." print each argument plain.
6016 */
6017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006018ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019{
6020 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006021 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 char_u *p;
6023 int needclr = TRUE;
6024 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006025 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006026 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006027 evalarg_T evalarg;
6028
Bram Moolenaare707c882020-07-01 13:07:37 +02006029 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030
6031 if (eap->skip)
6032 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006033 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006035 // If eval1() causes an error message the text from the command may
6036 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006037 need_clr_eos = needclr;
6038
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006040 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 {
6042 /*
6043 * Report the invalid expression unless the expression evaluation
6044 * has been cancelled due to an aborting error, an interrupt, or an
6045 * exception.
6046 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006047 if (!aborting() && did_emsg == did_emsg_before
6048 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006049 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006050 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 break;
6052 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006053 need_clr_eos = FALSE;
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006057
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006058 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 arg = skipwhite(arg);
6060 }
6061 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006062 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063
6064 if (eap->skip)
6065 --emsg_skip;
6066 else
6067 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006068 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 if (needclr)
6070 msg_clr_eos();
6071 if (eap->cmdidx == CMD_echo)
6072 msg_end();
6073 }
6074}
6075
6076/*
6077 * ":echohl {name}".
6078 */
6079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006082 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083}
6084
6085/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006086 * Returns the :echo attribute
6087 */
6088 int
6089get_echo_attr(void)
6090{
6091 return echo_attr;
6092}
6093
6094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 * ":execute expr1 ..." execute the result of an expression.
6096 * ":echomsg expr1 ..." Print a message
6097 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006098 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 * Each gets spaces around each argument and a newline at the end for
6100 * echo commands
6101 */
6102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006103ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006106 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 int ret = OK;
6108 char_u *p;
6109 garray_T ga;
6110 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
6112 ga_init2(&ga, 1, 80);
6113
6114 if (eap->skip)
6115 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006116 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006118 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006119 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122 if (!eap->skip)
6123 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006124 char_u buf[NUMBUFLEN];
6125
6126 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006127 {
6128 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6129 {
6130 emsg(_(e_inval_string));
6131 p = NULL;
6132 }
6133 else
6134 p = tv_get_string_buf(&rettv, buf);
6135 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006136 else
6137 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006138 if (p == NULL)
6139 {
6140 clear_tv(&rettv);
6141 ret = FAIL;
6142 break;
6143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 len = (int)STRLEN(p);
6145 if (ga_grow(&ga, len + 2) == FAIL)
6146 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006147 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 ret = FAIL;
6149 break;
6150 }
6151 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 ga.ga_len += len;
6155 }
6156
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006157 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 arg = skipwhite(arg);
6159 }
6160
6161 if (ret != FAIL && ga.ga_data != NULL)
6162 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006163 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6164 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006165 // Mark the already saved text as finishing the line, so that what
6166 // follows is displayed on a new line when scrolling back at the
6167 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006168 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006169 }
6170
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006172 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006173 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006174 out_flush();
6175 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006176 else if (eap->cmdidx == CMD_echoconsole)
6177 {
6178 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6179 ui_write((char_u *)"\r\n", 2, TRUE);
6180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 else if (eap->cmdidx == CMD_echoerr)
6182 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006183 int save_did_emsg = did_emsg;
6184
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006185 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006186 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (!force_abort)
6188 did_emsg = save_did_emsg;
6189 }
6190 else if (eap->cmdidx == CMD_execute)
6191 do_cmdline((char_u *)ga.ga_data,
6192 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6193 }
6194
6195 ga_clear(&ga);
6196
6197 if (eap->skip)
6198 --emsg_skip;
6199
6200 eap->nextcmd = check_nextcmd(arg);
6201}
6202
6203/*
6204 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6205 * "arg" points to the "&" or '+' when called, to "option" when returning.
6206 * Returns NULL when no option name found. Otherwise pointer to the char
6207 * after the option name.
6208 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006209 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006210find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 char_u *p = *arg;
6213
6214 ++p;
6215 if (*p == 'g' && p[1] == ':')
6216 {
6217 *opt_flags = OPT_GLOBAL;
6218 p += 2;
6219 }
6220 else if (*p == 'l' && p[1] == ':')
6221 {
6222 *opt_flags = OPT_LOCAL;
6223 p += 2;
6224 }
6225 else
6226 *opt_flags = 0;
6227
6228 if (!ASCII_ISALPHA(*p))
6229 return NULL;
6230 *arg = p;
6231
6232 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006233 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 else
6235 while (ASCII_ISALPHA(*p))
6236 ++p;
6237 return p;
6238}
6239
6240/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006241 * Display script name where an item was last set.
6242 * Should only be invoked when 'verbose' is non-zero.
6243 */
6244 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006245last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006246{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006247 char_u *p;
6248
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006249 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006250 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006251 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006252 if (p != NULL)
6253 {
6254 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006255 msg_puts(_("\n\tLast set from "));
6256 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006257 if (script_ctx.sc_lnum > 0)
6258 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006259 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006260 msg_outnum((long)script_ctx.sc_lnum);
6261 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006262 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006263 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006264 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006265 }
6266}
6267
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006268#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
6270/*
6271 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006272 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 * "flags" can be "g" to do a global substitute.
6274 * Returns an allocated string, NULL for error.
6275 */
6276 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006277do_string_sub(
6278 char_u *str,
6279 char_u *pat,
6280 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006281 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006282 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283{
6284 int sublen;
6285 regmatch_T regmatch;
6286 int i;
6287 int do_all;
6288 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006289 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 garray_T ga;
6291 char_u *ret;
6292 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006293 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006295 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006297 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
6299 ga_init2(&ga, 1, 200);
6300
6301 do_all = (flags[0] == 'g');
6302
6303 regmatch.rm_ic = p_ic;
6304 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6305 if (regmatch.regprog != NULL)
6306 {
6307 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006308 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6310 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006311 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006312 if (regmatch.startp[0] == regmatch.endp[0])
6313 {
6314 if (zero_width == regmatch.startp[0])
6315 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006316 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006317 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006318 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6319 (size_t)i);
6320 ga.ga_len += i;
6321 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006322 continue;
6323 }
6324 zero_width = regmatch.startp[0];
6325 }
6326
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 /*
6328 * Get some space for a temporary buffer to do the substitution
6329 * into. It will contain:
6330 * - The text up to where the match is.
6331 * - The substituted text.
6332 * - The text after the match.
6333 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006334 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006335 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6337 {
6338 ga_clear(&ga);
6339 break;
6340 }
6341
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006342 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 i = (int)(regmatch.startp[0] - tail);
6344 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006345 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006346 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 + ga.ga_len + i, TRUE, TRUE, FALSE);
6348 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006349 tail = regmatch.endp[0];
6350 if (*tail == NUL)
6351 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 if (!do_all)
6353 break;
6354 }
6355
6356 if (ga.ga_data != NULL)
6357 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6358
Bram Moolenaar473de612013-06-08 18:19:48 +02006359 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 }
6361
6362 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6363 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006364 if (p_cpo == empty_option)
6365 p_cpo = save_cpo;
6366 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006367 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006368 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006369 // If it's still empty it was changed and restored, need to restore in
6370 // the complicated way.
6371 if (*p_cpo == NUL)
6372 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006373 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376 return ret;
6377}