blob: 4dbbc40961426171b41f1289918b68a56aa4649a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
54static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010062 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010063 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020064 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010065num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010066{
67 varnumber_T result;
68
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 if (failed != NULL)
75 *failed = TRUE;
76 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010077 if (n1 == 0)
78 result = VARNUM_MIN; // similar to NaN
79 else if (n1 < 0)
80 result = -VARNUM_MAX;
81 else
82 result = VARNUM_MAX;
83 }
84 else
85 result = n1 / n2;
86
87 return result;
88}
89
90/*
91 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010092 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010093 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020094 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010095num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010096{
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 if (failed != NULL)
101 *failed = TRUE;
102 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100103 return (n2 == 0) ? 0 : (n1 % n2);
104}
105
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100106#if defined(EBCDIC) || defined(PROTO)
107/*
108 * Compare struct fst by function name.
109 */
110 static int
111compare_func_name(const void *s1, const void *s2)
112{
113 struct fst *p1 = (struct fst *)s1;
114 struct fst *p2 = (struct fst *)s2;
115
116 return STRCMP(p1->f_name, p2->f_name);
117}
118
119/*
120 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100121 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100122 * On machines using EBCDIC we have to sort it.
123 */
124 static void
125sortFunctions(void)
126{
127 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
128
129 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
130}
131#endif
132
Bram Moolenaar33570922005-01-25 22:26:29 +0000133/*
134 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000135 */
136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000138{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200139 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200140 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000141
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200142#ifdef EBCDIC
143 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100144 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200145 */
146 sortFunctions();
147#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000148}
149
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000150#if defined(EXITFREE) || defined(PROTO)
151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100152eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000153{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200154 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100155 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200156 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000157
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200158 // autoloaded script names
159 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000160
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200161 // unreferenced lists and dicts
162 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200163
164 // functions not garbage collected
165 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000166}
167#endif
168
Bram Moolenaare6b53242020-07-01 17:28:33 +0200169 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200170fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
171{
172 CLEAR_FIELD(*evalarg);
173 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
174 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
175 {
176 evalarg->eval_getline = eap->getline;
177 evalarg->eval_cookie = eap->cookie;
178 }
179}
180
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181/*
182 * Top level evaluation function, returning a boolean.
183 * Sets "error" to TRUE if there was an error.
184 * Return TRUE or FALSE.
185 */
186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100187eval_to_bool(
188 char_u *arg,
189 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200190 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100191 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192{
Bram Moolenaar33570922005-01-25 22:26:29 +0000193 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200194 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200195 evalarg_T evalarg;
196
Bram Moolenaar37c83712020-06-30 21:18:36 +0200197 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199 if (skip)
200 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200201 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 else
204 {
205 *error = FALSE;
206 if (!skip)
207 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200208 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200209 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200210 else
211 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000212 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 }
214 }
215 if (skip)
216 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200217 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200219 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220}
221
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222/*
223 * Call eval1() and give an error message if not done at a lower level.
224 */
225 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200226eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100227{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100228 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100229 int ret;
230 int did_emsg_before = did_emsg;
231 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100233
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200234 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
235
236 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100237 if (ret == FAIL)
238 {
239 // Report the invalid expression unless the expression evaluation has
240 // been cancelled due to an aborting error, an interrupt, or an
241 // exception, or we already gave a more specific error.
242 // Also check called_emsg for when using assert_fails().
243 if (!aborting() && did_emsg == did_emsg_before
244 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100245 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100246 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200247 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100248 return ret;
249}
250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200252 * Return whether a typval is a valid expression to pass to eval_expr_typval()
253 * or eval_expr_to_bool(). An empty string returns FALSE;
254 */
255 int
256eval_expr_valid_arg(typval_T *tv)
257{
258 return tv->v_type != VAR_UNKNOWN
259 && (tv->v_type != VAR_STRING
260 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
261}
262
263/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 * Evaluate an expression, which can be a function, partial or string.
265 * Pass arguments "argv[argc]".
266 * Return the result in "rettv" and OK or FAIL.
267 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200268 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100269eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
270{
271 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100272 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200273 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100274
275 if (expr->v_type == VAR_FUNC)
276 {
277 s = expr->vval.v_string;
278 if (s == NULL || *s == NUL)
279 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200280 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200281 funcexe.evaluate = TRUE;
282 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100283 return FAIL;
284 }
285 else if (expr->v_type == VAR_PARTIAL)
286 {
287 partial_T *partial = expr->vval.v_partial;
288
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200289 if (partial == NULL)
290 return FAIL;
291
Bram Moolenaar822ba242020-05-24 23:00:18 +0200292 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200293 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200295 if (call_def_function(partial->pt_func, argc, argv,
296 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 return FAIL;
298 }
299 else
300 {
301 s = partial_name(partial);
302 if (s == NULL || *s == NUL)
303 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200304 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 funcexe.evaluate = TRUE;
306 funcexe.partial = partial;
307 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
308 return FAIL;
309 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100310 }
311 else
312 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100313 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100314 if (s == NULL)
315 return FAIL;
316 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200317 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100318 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200319 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100320 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200321 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100322 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100323 return FAIL;
324 }
325 }
326 return OK;
327}
328
329/*
330 * Like eval_to_bool() but using a typval_T instead of a string.
331 * Works for string, funcref and partial.
332 */
333 int
334eval_expr_to_bool(typval_T *expr, int *error)
335{
336 typval_T rettv;
337 int res;
338
339 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
340 {
341 *error = TRUE;
342 return FALSE;
343 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200344 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100345 clear_tv(&rettv);
346 return res;
347}
348
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349/*
350 * Top level evaluation function, returning a string. If "skip" is TRUE,
351 * only parsing to "nextcmd" is done, without reporting errors. Return
352 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
353 */
354 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100355eval_to_string_skip(
356 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200357 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100358 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359{
Bram Moolenaar33570922005-01-25 22:26:29 +0000360 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200362 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
Bram Moolenaar37c83712020-06-30 21:18:36 +0200364 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 if (skip)
366 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200367 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 retval = NULL;
369 else
370 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100371 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000372 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 }
374 if (skip)
375 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
378 return retval;
379}
380
381/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000382 * Skip over an expression at "*pp".
383 * Return FAIL for an error, OK otherwise.
384 */
385 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200386skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000387{
Bram Moolenaar33570922005-01-25 22:26:29 +0000388 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000389
390 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200391 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000392}
393
394/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200395 * Skip over an expression at "*pp".
396 * If in Vim9 script and line breaks are encountered, the lines are
397 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200398 * "arg" is advanced to just after the expression.
399 * "start" is set to the start of the expression, "end" to just after the end.
400 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200401 * Return FAIL for an error, OK otherwise.
402 */
403 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200404skip_expr_concatenate(
405 char_u **arg,
406 char_u **start,
407 char_u **end,
408 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409{
410 typval_T rettv;
411 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200412 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200413 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200414 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200415 int evaluate = evalarg == NULL
416 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200417
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200418 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200420 {
421 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200422 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200423 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 ++gap->ga_len;
425 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200426 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200427
428 // Don't evaluate the expression.
429 if (evalarg != NULL)
430 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200431 *arg = skipwhite(*arg);
432 res = eval1(arg, &rettv, evalarg);
433 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200434 if (evalarg != NULL)
435 evalarg->eval_flags = save_flags;
436
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200437 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200438 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200439 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200440 if (evalarg->eval_ga.ga_len == 1)
441 {
442 // just one line, no need to concatenate
443 ga_clear(gap);
444 gap->ga_itemsize = 0;
445 }
446 else
447 {
448 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200449 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200450
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 // Line breaks encountered, concatenate all the lines.
452 *((char_u **)gap->ga_data) = *start;
453 p = ga_concat_strings(gap, "");
454
455 // free the lines only when using getsourceline()
456 if (evalarg->eval_cookie != NULL)
457 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200458 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200460 // Do not free the last line, "arg" points into it, free it
461 // later.
462 vim_free(evalarg->eval_tofree);
463 evalarg->eval_tofree =
464 ((char_u **)gap->ga_data)[gap->ga_len - 1];
465 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200466 ga_clear_strings(gap);
467 }
468 else
469 ga_clear(gap);
470 gap->ga_itemsize = 0;
471 if (p == NULL)
472 return FAIL;
473 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200474 vim_free(evalarg->eval_tofree_lambda);
475 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200476 // Compute "end" relative to the end.
477 *end = *start + STRLEN(*start) - endoff;
478 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 }
480
481 return res;
482}
483
484/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100485 * Convert "tv" to a string.
486 * When "convert" is TRUE convert a List into a sequence of lines and convert
487 * a Float to a String.
488 * Returns an allocated string (NULL when out of memory).
489 */
490 char_u *
491typval2string(typval_T *tv, int convert)
492{
493 garray_T ga;
494 char_u *retval;
495#ifdef FEAT_FLOAT
496 char_u numbuf[NUMBUFLEN];
497#endif
498
499 if (convert && tv->v_type == VAR_LIST)
500 {
501 ga_init2(&ga, (int)sizeof(char), 80);
502 if (tv->vval.v_list != NULL)
503 {
504 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
505 if (tv->vval.v_list->lv_len > 0)
506 ga_append(&ga, NL);
507 }
508 ga_append(&ga, NUL);
509 retval = (char_u *)ga.ga_data;
510 }
511#ifdef FEAT_FLOAT
512 else if (convert && tv->v_type == VAR_FLOAT)
513 {
514 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
515 retval = vim_strsave(numbuf);
516 }
517#endif
518 else
519 retval = vim_strsave(tv_get_string(tv));
520 return retval;
521}
522
523/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200524 * Top level evaluation function, returning a string. Does not handle line
525 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000526 * When "convert" is TRUE convert a List into a sequence of lines and convert
527 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * Return pointer to allocated memory, or NULL for failure.
529 */
530 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100531eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100532 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100533 int convert,
534 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaar33570922005-01-25 22:26:29 +0000536 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100538 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100540 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
541 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 retval = NULL;
543 else
544 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100545 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000546 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100548 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
550 return retval;
551}
552
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100553 char_u *
554eval_to_string(
555 char_u *arg,
556 int convert)
557{
558 return eval_to_string_eap(arg, convert, NULL);
559}
560
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000562 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200563 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200564 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 */
566 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100567eval_to_string_safe(
568 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100569 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570{
571 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200572 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200573 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200575 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200576 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000577 if (use_sandbox)
578 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200579 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200580 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000581 if (use_sandbox)
582 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200583 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200584 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200585 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 return retval;
587}
588
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589/*
590 * Top level evaluation function, returning a number.
591 * Evaluates "expr" silently.
592 * Returns -1 for an error.
593 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200594 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100595eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596{
Bram Moolenaar33570922005-01-25 22:26:29 +0000597 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200598 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000599 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
601 ++emsg_off;
602
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200603 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 retval = -1;
605 else
606 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100607 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000608 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610 --emsg_off;
611
612 return retval;
613}
614
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000615/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000616 * Top level evaluation function.
617 * Returns an allocated typval_T with the result.
618 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619 */
620 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200621eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000622{
623 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200624 evalarg_T evalarg;
625
626 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000627
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200628 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200629 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100630 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000631
Bram Moolenaar37c83712020-06-30 21:18:36 +0200632 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000633 return tv;
634}
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100637 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200638 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
639 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000640 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100643call_vim_function(
644 char_u *func,
645 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200646 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200647 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000649 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200650 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100652 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200653 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200654 funcexe.firstline = curwin->w_cursor.lnum;
655 funcexe.lastline = curwin->w_cursor.lnum;
656 funcexe.evaluate = TRUE;
657 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000658 if (ret == FAIL)
659 clear_tv(rettv);
660
661 return ret;
662}
663
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100664/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100665 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100666 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200667 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
668 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100669 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200670 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100671call_func_retnr(
672 char_u *func,
673 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200674 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100675{
676 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200677 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100678
Bram Moolenaarded27a12018-08-01 19:06:03 +0200679 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100680 return -1;
681
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100682 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100683 clear_tv(&rettv);
684 return retval;
685}
686
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000687/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100688 * Call Vim script function like call_func_retnr() and drop the result.
689 * Returns FAIL when calling the function fails.
690 */
691 int
692call_func_noret(
693 char_u *func,
694 int argc,
695 typval_T *argv)
696{
697 typval_T rettv;
698
699 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
700 return FAIL;
701 clear_tv(&rettv);
702 return OK;
703}
704
705/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100706 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100707 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000708 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000709 */
710 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100711call_func_retstr(
712 char_u *func,
713 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200714 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000715{
716 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000717 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718
Bram Moolenaarded27a12018-08-01 19:06:03 +0200719 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000720 return NULL;
721
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100722 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000723 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 return retval;
725}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000726
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000727/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100728 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100729 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000730 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000731 */
732 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100733call_func_retlist(
734 char_u *func,
735 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200736 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000737{
738 typval_T rettv;
739
Bram Moolenaarded27a12018-08-01 19:06:03 +0200740 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000741 return NULL;
742
743 if (rettv.v_type != VAR_LIST)
744 {
745 clear_tv(&rettv);
746 return NULL;
747 }
748
749 return rettv.vval.v_list;
750}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752#ifdef FEAT_FOLDING
753/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100754 * Evaluate "arg", which is 'foldexpr'.
755 * Note: caller must set "curwin" to match "arg".
756 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
757 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 */
759 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100760eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761{
Bram Moolenaar33570922005-01-25 22:26:29 +0000762 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200763 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000765 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
766 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
768 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000769 if (use_sandbox)
770 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200771 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200773 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 retval = 0;
775 else
776 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100777 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000778 if (tv.v_type == VAR_NUMBER)
779 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000780 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 retval = 0;
782 else
783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100784 // If the result is a string, check if there is a non-digit before
785 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000786 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (!VIM_ISDIGIT(*s) && *s != '-')
788 *cp = *s++;
789 retval = atol((char *)s);
790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000791 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000794 if (use_sandbox)
795 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200796 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200797 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200799 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801#endif
802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000804 * Get an lval: variable, Dict item or List item that can be assigned a value
805 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
806 * "name.key", "name.key[expr]" etc.
807 * Indexing only works if "name" is an existing List or Dictionary.
808 * "name" points to the start of the name.
809 * If "rettv" is not NULL it points to the value to be assigned.
810 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
811 * wrong; must end in space or cmd separator.
812 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100813 * flags:
814 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100815 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100816 * GLV_NO_AUTOLOAD: do not use script autoloading
817 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000818 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000819 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000820 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200822 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100823get_lval(
824 char_u *name,
825 typval_T *rettv,
826 lval_T *lp,
827 int unlet,
828 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100829 int flags, // GLV_ values
830 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000831{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000832 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000833 char_u *expr_start, *expr_end;
834 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000835 dictitem_T *v;
836 typval_T var1;
837 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000838 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000840 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000841 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100842 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100843 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100844 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000845
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100846 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200847 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100849 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100851 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000852 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200853 lp->ll_name_end = find_name_end(name, NULL, NULL,
854 FNE_INCL_BR | fne_flags);
855 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 }
857
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100858 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000859 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000861 if (expr_start != NULL)
862 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100863 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100864 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 && *p != '[' && *p != '.')
866 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200867 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 return NULL;
869 }
870
871 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
872 if (lp->ll_exp_name == NULL)
873 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100874 // Report an invalid expression in braces, unless the
875 // expression evaluation has been cancelled due to an
876 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 if (!aborting() && !quiet)
878 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000879 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100880 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 return NULL;
882 }
883 }
884 lp->ll_name = lp->ll_exp_name;
885 }
886 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000888 lp->ll_name = name;
889
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200890 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200892 // "a: type" is declaring variable "a" with a type, not "a:".
893 if (p == name + 2 && p[-1] == ':')
894 {
895 --p;
896 lp->ll_name_end = p;
897 }
898 if (*p == ':')
899 {
900 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
901 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200903 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100904 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
905 if (lp->ll_type == NULL && !quiet)
906 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200907 lp->ll_name_end = tp;
908 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909 }
910 }
911
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100912 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
914 return p;
915
916 cc = *p;
917 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100918 // When we would write to the variable pass &ht and prevent autoload.
919 writing = !(flags & GLV_READ_ONLY);
920 v = find_var(lp->ll_name, writing ? &ht : NULL,
921 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200923 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000924 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000925 if (v == NULL)
926 return NULL;
927
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100928 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
929 {
930 if (!quiet)
931 semsg(_(e_variable_already_declared), lp->ll_name);
932 return NULL;
933 }
934
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 /*
936 * Loop until no more [idx] or .key is following.
937 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100939 var1.v_type = VAR_UNKNOWN;
940 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000942 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000943 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200944 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100945 && !(lp->ll_tv->v_type == VAR_BLOB
946 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100949 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000950 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000951 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000953 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100955 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000957 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000958
Bram Moolenaar10c65862020-10-08 21:16:42 +0200959 if (in_vim9script() && lp->ll_valtype == NULL
960 && lp->ll_tv == &v->di_tv
961 && ht != NULL && ht == get_script_local_ht())
962 {
963 svar_T *sv = find_typval_in_script(lp->ll_tv);
964
965 // Vim9 script local variable: get the type
966 if (sv != NULL)
967 lp->ll_valtype = sv->sv_type;
968 }
969
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 len = -1;
971 if (*p == '.')
972 {
973 key = p + 1;
974 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
975 ;
976 if (len == 0)
977 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100979 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 }
982 p = key + len;
983 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000984 else
985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100986 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000987 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 if (*p == ':')
989 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000990 else
991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200993 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100995 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100997 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000998 clear_tv(&var1);
999 return NULL;
1000 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001001 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 }
1003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001004 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001005 if (*p == ':')
1006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001010 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001011 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001013 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001014 if (rettv != NULL
1015 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001016 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001017 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001018 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001021 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001022 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 }
1025 p = skipwhite(p + 1);
1026 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 else
1029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001031 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001032 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001033 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001034 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001036 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001037 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001039 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001040 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001041 clear_tv(&var2);
1042 return NULL;
1043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001044 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001047 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001049
Bram Moolenaar8c711452005-01-14 21:53:12 +00001050 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001053 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001054 clear_tv(&var1);
1055 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001057 }
1058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001059 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001060 ++p;
1061 }
1062
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001063 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001064 {
1065 if (len == -1)
1066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001067 // "[key]": get key from "var1"
1068 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001069 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001072 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 }
1074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001075 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001076
1077 // a NULL dict is equivalent with an empty dict
1078 if (lp->ll_tv->vval.v_dict == NULL)
1079 {
1080 lp->ll_tv->vval.v_dict = dict_alloc();
1081 if (lp->ll_tv->vval.v_dict == NULL)
1082 {
1083 clear_tv(&var1);
1084 return NULL;
1085 }
1086 ++lp->ll_tv->vval.v_dict->dv_refcount;
1087 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001088 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001089
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001090 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 // When assigning to a scope dictionary check that a function and
1093 // variable name is valid (only variable name unless it is l: or
1094 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001095 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001096 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001097 int prevval;
1098 int wrong;
1099
1100 if (len != -1)
1101 {
1102 prevval = key[len];
1103 key[len] = NUL;
1104 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001105 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001106 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001107 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1108 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001109 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001110 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001111 if (len != -1)
1112 key[len] = prevval;
1113 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001114 {
1115 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001116 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001117 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001118 }
1119
Bram Moolenaar10c65862020-10-08 21:16:42 +02001120 if (lp->ll_valtype != NULL)
1121 // use the type of the member
1122 lp->ll_valtype = lp->ll_valtype->tt_member;
1123
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001125 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001126 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001127 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001128 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001130 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001131 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001132 return NULL;
1133 }
1134
Bram Moolenaar31b81602019-02-10 22:14:27 +01001135 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001139 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001140 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 }
1143 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001145 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001147 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 p = NULL;
1150 break;
1151 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001152 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001153 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001154 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1155 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001156 {
1157 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001158 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001159 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001160
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001161 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001164 else if (lp->ll_tv->v_type == VAR_BLOB)
1165 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001166 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1167
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001168 /*
1169 * Get the number and item for the only or first index of the List.
1170 */
1171 if (empty1)
1172 lp->ll_n1 = 0;
1173 else
1174 // is number or string
1175 lp->ll_n1 = (long)tv_get_number(&var1);
1176 clear_tv(&var1);
1177
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001178 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001180 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001181 return NULL;
1182 }
1183 if (lp->ll_range && !lp->ll_empty2)
1184 {
1185 lp->ll_n2 = (long)tv_get_number(&var2);
1186 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001187 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1188 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001189 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001190 }
1191 lp->ll_blob = lp->ll_tv->vval.v_blob;
1192 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001193 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001194 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001195 else
1196 {
1197 /*
1198 * Get the number and item for the only or first index of the List.
1199 */
1200 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001201 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001203 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001204 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001205 clear_tv(&var1);
1206
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001207 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001209 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001210 if (lp->ll_li == NULL)
1211 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001212 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001213 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001214 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 }
1217
Bram Moolenaar10c65862020-10-08 21:16:42 +02001218 if (lp->ll_valtype != NULL)
1219 // use the type of the member
1220 lp->ll_valtype = lp->ll_valtype->tt_member;
1221
Bram Moolenaar8c711452005-01-14 21:53:12 +00001222 /*
1223 * May need to find the item or absolute index for the second
1224 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 * When no index given: "lp->ll_empty2" is TRUE.
1226 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001227 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001229 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001230 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001231 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001237 {
1238 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001239 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001241 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 }
1244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001245 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 if (lp->ll_n1 < 0)
1247 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1248 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001249 {
1250 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001251 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001253 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001254 }
1255
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001257 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001258 }
1259
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001260 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 return p;
1263}
1264
1265/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001266 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001268 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001269clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001270{
1271 vim_free(lp->ll_exp_name);
1272 vim_free(lp->ll_newkey);
1273}
1274
1275/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001276 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001278 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1279 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001282set_var_lval(
1283 lval_T *lp,
1284 char_u *endp,
1285 typval_T *rettv,
1286 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001287 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1288 char_u *op,
1289 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290{
1291 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001292 listitem_T *ri;
1293 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294
1295 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001296 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001297 cc = *endp;
1298 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001299 if (lp->ll_blob != NULL)
1300 {
1301 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001302
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303 if (op != NULL && *op != '=')
1304 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001305 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001306 return;
1307 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001308 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001309 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001310
1311 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1312 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001313 if (lp->ll_empty2)
1314 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1315
Bram Moolenaar68452172021-04-12 21:21:02 +02001316 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1317 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001318 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319 }
1320 else
1321 {
1322 val = (int)tv_get_number_chk(rettv, &error);
1323 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001324 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001325 }
1326 }
1327 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001328 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001329 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001330
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001331 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1332 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001333 {
1334 emsg(_(e_cannot_mod));
1335 *endp = cc;
1336 return;
1337 }
1338
Bram Moolenaarff697e62019-02-12 22:28:33 +01001339 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001340 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001341 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001342 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001343 {
1344 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001345 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1346 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001347 && tv_op(&tv, rettv, op) == OK)
1348 set_var(lp->ll_name, &tv, FALSE);
1349 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001350 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001352 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001353 {
1354 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001355 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001356 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001357 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1358 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001359 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001360 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001361 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001362 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001363 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001364 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001365 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366 else if (lp->ll_range)
1367 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001368 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001369 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001370
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001371 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1372 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001373 {
1374 emsg(_("E996: Cannot lock a range"));
1375 return;
1376 }
1377
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001378 /*
1379 * Check whether any of the list items is locked
1380 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001381 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001382 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001383 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001384 return;
1385 ri = ri->li_next;
1386 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1387 break;
1388 ll_li = ll_li->li_next;
1389 ++ll_n1;
1390 }
1391
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001392 /*
1393 * Assign the List values to the list items.
1394 */
1395 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001397 if (op != NULL && *op != '=')
1398 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1399 else
1400 {
1401 clear_tv(&lp->ll_li->li_tv);
1402 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1403 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001404 ri = ri->li_next;
1405 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1406 break;
1407 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001409 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001410 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001411 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001412 ri = NULL;
1413 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001414 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001415 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 lp->ll_li = lp->ll_li->li_next;
1417 ++lp->ll_n1;
1418 }
1419 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001420 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001421 else if (lp->ll_empty2
1422 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001423 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001424 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 }
1426 else
1427 {
1428 /*
1429 * Assign to a List or Dictionary item.
1430 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001431 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1432 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001433 {
1434 emsg(_("E996: Cannot lock a list or dict"));
1435 return;
1436 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001437
1438 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001439 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001440 return;
1441
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001442 if (lp->ll_newkey != NULL)
1443 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001444 if (op != NULL && *op != '=')
1445 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001446 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001447 return;
1448 }
1449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001450 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001451 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001452 if (di == NULL)
1453 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001454 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1455 {
1456 vim_free(di);
1457 return;
1458 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001460 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001461 else if (op != NULL && *op != '=')
1462 {
1463 tv_op(lp->ll_tv, rettv, op);
1464 return;
1465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001466 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001467 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001468
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001469 /*
1470 * Assign the value to the variable or list item.
1471 */
1472 if (copy)
1473 copy_tv(rettv, lp->ll_tv);
1474 else
1475 {
1476 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001477 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001478 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001479 }
1480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481}
1482
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001483/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001484 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1485 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001486 * Returns OK or FAIL.
1487 */
1488 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001489tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001491 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492 char_u numbuf[NUMBUFLEN];
1493 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001494 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001496 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001497 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001498 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001499 {
1500 switch (tv1->v_type)
1501 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001502 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001503 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001505 case VAR_DICT:
1506 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001507 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001508 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001509 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001510 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001511 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001512 break;
1513
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001514 case VAR_BLOB:
1515 if (*op != '+' || tv2->v_type != VAR_BLOB)
1516 break;
1517 // BLOB += BLOB
1518 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1519 {
1520 blob_T *b1 = tv1->vval.v_blob;
1521 blob_T *b2 = tv2->vval.v_blob;
1522 int i, len = blob_len(b2);
1523 for (i = 0; i < len; i++)
1524 ga_append(&b1->bv_ga, blob_get(b2, i));
1525 }
1526 return OK;
1527
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001528 case VAR_LIST:
1529 if (*op != '+' || tv2->v_type != VAR_LIST)
1530 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001531 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001532 if (tv2->vval.v_list != NULL)
1533 {
1534 if (tv1->vval.v_list == NULL)
1535 {
1536 tv1->vval.v_list = tv2->vval.v_list;
1537 ++tv1->vval.v_list->lv_refcount;
1538 }
1539 else
1540 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1541 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001542 return OK;
1543
1544 case VAR_NUMBER:
1545 case VAR_STRING:
1546 if (tv2->v_type == VAR_LIST)
1547 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001548 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001549 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001550 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001551 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001552#ifdef FEAT_FLOAT
1553 if (tv2->v_type == VAR_FLOAT)
1554 {
1555 float_T f = n;
1556
Bram Moolenaarff697e62019-02-12 22:28:33 +01001557 if (*op == '%')
1558 break;
1559 switch (*op)
1560 {
1561 case '+': f += tv2->vval.v_float; break;
1562 case '-': f -= tv2->vval.v_float; break;
1563 case '*': f *= tv2->vval.v_float; break;
1564 case '/': f /= tv2->vval.v_float; break;
1565 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001566 clear_tv(tv1);
1567 tv1->v_type = VAR_FLOAT;
1568 tv1->vval.v_float = f;
1569 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001570 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001571#endif
1572 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001573 switch (*op)
1574 {
1575 case '+': n += tv_get_number(tv2); break;
1576 case '-': n -= tv_get_number(tv2); break;
1577 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001578 case '/': n = num_divide(n, tv_get_number(tv2),
1579 &failed); break;
1580 case '%': n = num_modulus(n, tv_get_number(tv2),
1581 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001582 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001583 clear_tv(tv1);
1584 tv1->v_type = VAR_NUMBER;
1585 tv1->vval.v_number = n;
1586 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001587 }
1588 else
1589 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001590 if (tv2->v_type == VAR_FLOAT)
1591 break;
1592
Bram Moolenaarff697e62019-02-12 22:28:33 +01001593 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001594 s = tv_get_string(tv1);
1595 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001596 clear_tv(tv1);
1597 tv1->v_type = VAR_STRING;
1598 tv1->vval.v_string = s;
1599 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001600 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001601
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001602 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001603#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001604 {
1605 float_T f;
1606
Bram Moolenaarff697e62019-02-12 22:28:33 +01001607 if (*op == '%' || *op == '.'
1608 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001609 && tv2->v_type != VAR_NUMBER
1610 && tv2->v_type != VAR_STRING))
1611 break;
1612 if (tv2->v_type == VAR_FLOAT)
1613 f = tv2->vval.v_float;
1614 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001615 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001616 switch (*op)
1617 {
1618 case '+': tv1->vval.v_float += f; break;
1619 case '-': tv1->vval.v_float -= f; break;
1620 case '*': tv1->vval.v_float *= f; break;
1621 case '/': tv1->vval.v_float /= f; break;
1622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001625 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001626 }
1627 }
1628
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001629 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001630 return FAIL;
1631}
1632
1633/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001634 * Evaluate the expression used in a ":for var in expr" command.
1635 * "arg" points to "var".
1636 * Set "*errp" to TRUE for an error, FALSE otherwise;
1637 * Return a pointer that holds the info. Null when there is an error.
1638 */
1639 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001640eval_for_line(
1641 char_u *arg,
1642 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001643 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001644 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001645{
Bram Moolenaar33570922005-01-25 22:26:29 +00001646 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001647 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001648 typval_T tv;
1649 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001650 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001652 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001654 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001655 if (fi == NULL)
1656 return NULL;
1657
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001658 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659 if (expr == NULL)
1660 return fi;
1661
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001662 expr = skipwhite_and_linebreak(expr, evalarg);
1663 if (expr[0] != 'i' || expr[1] != 'n'
1664 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 return fi;
1668 }
1669
1670 if (skip)
1671 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001672 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1673 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 {
1675 *errp = FALSE;
1676 if (!skip)
1677 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001678 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001679 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001680 l = tv.vval.v_list;
1681 if (l == NULL)
1682 {
1683 // a null list is like an empty list: do nothing
1684 clear_tv(&tv);
1685 }
1686 else
1687 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001689 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001691 // No need to increment the refcount, it's already set for
1692 // the list being used in "tv".
1693 fi->fi_list = l;
1694 list_add_watch(l, &fi->fi_lw);
1695 fi->fi_lw.lw_item = l->lv_first;
1696 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001697 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001698 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001699 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001700 fi->fi_bi = 0;
1701 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001702 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001703 typval_T btv;
1704
1705 // Make a copy, so that the iteration still works when the
1706 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001708 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001709 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001710 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001711 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001712 else if (tv.v_type == VAR_STRING)
1713 {
1714 fi->fi_byte_idx = 0;
1715 fi->fi_string = tv.vval.v_string;
1716 tv.vval.v_string = NULL;
1717 if (fi->fi_string == NULL)
1718 fi->fi_string = vim_strsave((char_u *)"");
1719 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 else
1721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001722 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001723 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001724 }
1725 }
1726 }
1727 if (skip)
1728 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001729 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730
1731 return fi;
1732}
1733
1734/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001735 * Used when looping over a :for line, skip the "in expr" part.
1736 */
1737 void
1738skip_for_lines(void *fi_void, evalarg_T *evalarg)
1739{
1740 forinfo_T *fi = (forinfo_T *)fi_void;
1741 int i;
1742
1743 for (i = 0; i < fi->fi_break_count; ++i)
1744 eval_next_line(evalarg);
1745}
1746
1747/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 * Use the first item in a ":for" list. Advance to the next.
1749 * Assign the values to the variable (list). "arg" points to the first one.
1750 * Return TRUE when a valid item was found, FALSE when at end of list or
1751 * something wrong.
1752 */
1753 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001754next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001756 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001757 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001758 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1759 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1760 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001761 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001763 if (fi->fi_blob != NULL)
1764 {
1765 typval_T tv;
1766
1767 if (fi->fi_bi >= blob_len(fi->fi_blob))
1768 return FALSE;
1769 tv.v_type = VAR_NUMBER;
1770 tv.v_lock = VAR_FIXED;
1771 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1772 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001773 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001774 fi->fi_varcount, flag, NULL) == OK;
1775 }
1776
1777 if (fi->fi_string != NULL)
1778 {
1779 typval_T tv;
1780 int len;
1781
1782 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1783 if (len == 0)
1784 return FALSE;
1785 tv.v_type = VAR_STRING;
1786 tv.v_lock = VAR_FIXED;
1787 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1788 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001789 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001790 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001791 vim_free(tv.vval.v_string);
1792 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001793 }
1794
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 item = fi->fi_lw.lw_item;
1796 if (item == NULL)
1797 result = FALSE;
1798 else
1799 {
1800 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001801 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001802 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803 }
1804 return result;
1805}
1806
1807/*
1808 * Free the structure used to store info used by ":for".
1809 */
1810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001811free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812{
Bram Moolenaar33570922005-01-25 22:26:29 +00001813 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001815 if (fi == NULL)
1816 return;
1817 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001818 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001820 list_unref(fi->fi_list);
1821 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001822 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001823 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001824 else
1825 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001826 vim_free(fi);
1827}
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001830set_context_for_expression(
1831 expand_T *xp,
1832 char_u *arg,
1833 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001835 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001839 if (cmdidx == CMD_let || cmdidx == CMD_var
1840 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 {
1842 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001843 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001845 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001846 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 {
1848 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001849 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001850 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 break;
1852 }
1853 return;
1854 }
1855 }
1856 else
1857 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1858 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 while ((xp->xp_pattern = vim_strpbrk(arg,
1860 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1861 {
1862 c = *xp->xp_pattern;
1863 if (c == '&')
1864 {
1865 c = xp->xp_pattern[1];
1866 if (c == '&')
1867 {
1868 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001869 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 }
1871 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001874 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1875 xp->xp_pattern += 2;
1876
1877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 }
1879 else if (c == '$')
1880 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001881 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 xp->xp_context = EXPAND_ENV_VARS;
1883 }
1884 else if (c == '=')
1885 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001886 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 xp->xp_context = EXPAND_EXPRESSION;
1888 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001889 else if (c == '#'
1890 && xp->xp_context == EXPAND_EXPRESSION)
1891 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001892 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001893 break;
1894 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001895 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 && xp->xp_context == EXPAND_FUNCTIONS
1897 && vim_strchr(xp->xp_pattern, '(') == NULL)
1898 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001899 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 break;
1901 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001902 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001904 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1907 if (c == '\\' && xp->xp_pattern[1] != NUL)
1908 ++xp->xp_pattern;
1909 xp->xp_context = EXPAND_NOTHING;
1910 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001911 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001913 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1915 /* skip */ ;
1916 xp->xp_context = EXPAND_NOTHING;
1917 }
1918 else if (c == '|')
1919 {
1920 if (xp->xp_pattern[1] == '|')
1921 {
1922 ++xp->xp_pattern;
1923 xp->xp_context = EXPAND_EXPRESSION;
1924 }
1925 else
1926 xp->xp_context = EXPAND_COMMANDS;
1927 }
1928 else
1929 xp->xp_context = EXPAND_EXPRESSION;
1930 }
1931 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001932 // Doesn't look like something valid, expand as an expression
1933 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 arg = xp->xp_pattern;
1936 if (*arg != NUL)
1937 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1938 /* skip */ ;
1939 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001940
1941 // ":exe one two" completes "two"
1942 if ((cmdidx == CMD_execute
1943 || cmdidx == CMD_echo
1944 || cmdidx == CMD_echon
1945 || cmdidx == CMD_echomsg)
1946 && xp->xp_context == EXPAND_EXPRESSION)
1947 {
1948 for (;;)
1949 {
1950 char_u *n = skiptowhite(arg);
1951
1952 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1953 break;
1954 arg = skipwhite(n);
1955 }
1956 }
1957
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 xp->xp_pattern = arg;
1959}
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001962 * Return TRUE if "pat" matches "text".
1963 * Does not use 'cpo' and always uses 'magic'.
1964 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001965 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001966pattern_match(char_u *pat, char_u *text, int ic)
1967{
1968 int matches = FALSE;
1969 char_u *save_cpo;
1970 regmatch_T regmatch;
1971
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001972 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001973 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001974 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001975 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1976 if (regmatch.regprog != NULL)
1977 {
1978 regmatch.rm_ic = ic;
1979 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1980 vim_regfree(regmatch.regprog);
1981 }
1982 p_cpo = save_cpo;
1983 return matches;
1984}
1985
1986/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001987 * Handle a name followed by "(". Both for just "name(arg)" and for
1988 * "expr->name(arg)".
1989 * Returns OK or FAIL.
1990 */
1991 static int
1992eval_func(
1993 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001994 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001995 char_u *name,
1996 int name_len,
1997 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001998 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001999 typval_T *basetv) // "expr" for "expr->name(arg)"
2000{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002001 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002002 char_u *s = name;
2003 int len = name_len;
2004 partial_T *partial;
2005 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002006 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002007
2008 if (!evaluate)
2009 check_vars(s, len);
2010
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002011 // If "s" is the name of a variable of type VAR_FUNC
2012 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002013 s = deref_func_name(s, &len, &partial,
2014 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002016 // Need to make a copy, in case evaluating the arguments makes
2017 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002018 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002019 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002020 ret = FAIL;
2021 else
2022 {
2023 funcexe_T funcexe;
2024
2025 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002026 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002027 funcexe.firstline = curwin->w_cursor.lnum;
2028 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002029 funcexe.evaluate = evaluate;
2030 funcexe.partial = partial;
2031 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002032 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002033 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002034 }
2035 vim_free(s);
2036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002037 // If evaluate is FALSE rettv->v_type was not set in
2038 // get_func_tv, but it's needed in handle_subscript() to parse
2039 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002040 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2041 {
2042 rettv->vval.v_string = NULL;
2043 rettv->v_type = VAR_FUNC;
2044 }
2045
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002046 // Stop the expression evaluation when immediately
2047 // aborting on error, or when an interrupt occurred or
2048 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002049 if (evaluate && aborting())
2050 {
2051 if (ret == OK)
2052 clear_tv(rettv);
2053 ret = FAIL;
2054 }
2055 return ret;
2056}
2057
2058/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002059 * Get the next line source line without advancing. But do skip over comment
2060 * lines.
2061 */
2062 static char_u *
2063getline_peek_skip_comments(evalarg_T *evalarg)
2064{
2065 for (;;)
2066 {
2067 char_u *next = getline_peek(evalarg->eval_getline,
2068 evalarg->eval_cookie);
2069 char_u *p;
2070
2071 if (next == NULL)
2072 break;
2073 p = skipwhite(next);
2074 if (*p != NUL && !vim9_comment_start(p))
2075 return next;
2076 (void)eval_next_line(evalarg);
2077 }
2078 return NULL;
2079}
2080
2081/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002082 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2083 * comment) and there is a next line, return the next line (skipping blanks)
2084 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002085 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2086 * "arg" must point somewhere inside a line, not at the start.
2087 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002088 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002089eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2090{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002091 char_u *p = skipwhite(arg);
2092
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002093 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002094 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002095 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002096 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002097 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002098 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002099 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002100
2101 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002102 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002103 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002104 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002105
Bram Moolenaar9d489562020-07-30 20:08:50 +02002106 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002107 {
2108 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002109 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002110 }
2111 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002112 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002113}
2114
2115/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002116 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002117 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002118 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002119eval_next_line(evalarg_T *evalarg)
2120{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002121 garray_T *gap = &evalarg->eval_ga;
2122 char_u *line;
2123
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002124 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002125 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2126 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002127 else
2128 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002129 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002130 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2131 {
2132 // Going to concatenate the lines after parsing.
2133 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2134 ++gap->ga_len;
2135 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002136 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002137 {
2138 vim_free(evalarg->eval_tofree);
2139 evalarg->eval_tofree = line;
2140 }
2141 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002142}
2143
Bram Moolenaare6b53242020-07-01 17:28:33 +02002144/*
2145 * Call eval_next_non_blank() and get the next line if needed.
2146 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002147 char_u *
2148skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2149{
2150 int getnext;
2151 char_u *p = skipwhite(arg);
2152
Bram Moolenaar962d7212020-07-04 14:15:00 +02002153 if (evalarg == NULL)
2154 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002155 eval_next_non_blank(p, evalarg, &getnext);
2156 if (getnext)
2157 return eval_next_line(evalarg);
2158 return p;
2159}
2160
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002161/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002162 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002163 */
2164 void
2165clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2166{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002167 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002168 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002169 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002170 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002171 if (eap != NULL)
2172 {
2173 // We may need to keep the original command line, e.g. for
2174 // ":let" it has the variable names. But we may also need the
2175 // new one, "nextcmd" points into it. Keep both.
2176 vim_free(eap->cmdline_tofree);
2177 eap->cmdline_tofree = *eap->cmdlinep;
2178 *eap->cmdlinep = evalarg->eval_tofree;
2179 }
2180 else
2181 vim_free(evalarg->eval_tofree);
2182 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002183 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002184
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002185 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2186 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002187 }
2188}
2189
2190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2194 */
2195
2196/*
2197 * Handle zero level expression.
2198 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002200 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002201 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 * Return OK or FAIL.
2203 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002204 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002205eval0(
2206 char_u *arg,
2207 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002208 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002209 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210{
2211 int ret;
2212 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002213 int did_emsg_before = did_emsg;
2214 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002215 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
2217 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002218 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002219 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002220
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002221 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
2223 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 /*
2226 * Report the invalid expression unless the expression evaluation has
2227 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002228 * exception, or we already gave a more specific error.
2229 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002231 if (!aborting()
2232 && did_emsg == did_emsg_before
2233 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002234 && (flags & EVAL_CONSTANT) == 0
2235 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002236 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002237
2238 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002239 // a next command to avoid more errors, unless "|" is following, which
2240 // could only be a command separator.
2241 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2242 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002245
2246 if (eap != NULL)
2247 eap->nextcmd = check_nextcmd(p);
2248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 return ret;
2250}
2251
2252/*
2253 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002254 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002255 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 *
2257 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002258 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002260 * Note: "rettv.v_lock" is not set.
2261 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 * Return OK or FAIL.
2263 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002264 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002265eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002267 char_u *p;
2268 int getnext;
2269
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002270 CLEAR_POINTER(rettv);
2271
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 /*
2273 * Get the first variable.
2274 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002275 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 return FAIL;
2277
Bram Moolenaar793648f2020-06-26 21:28:25 +02002278 p = eval_next_non_blank(*arg, evalarg, &getnext);
2279 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002281 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002282 int result;
2283 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002284 evalarg_T *evalarg_used = evalarg;
2285 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002286 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002287 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002288 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002289
2290 if (evalarg == NULL)
2291 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002292 CLEAR_FIELD(local_evalarg);
2293 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002295 orig_flags = evalarg_used->eval_flags;
2296 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002297
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002298 if (getnext)
2299 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002300 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002301 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002302 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002303 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002304 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002305 clear_tv(rettv);
2306 return FAIL;
2307 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002308 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002309 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002312 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002314 int error = FALSE;
2315
Bram Moolenaar13106602020-10-04 16:06:05 +02002316 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002317 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002318 else if (vim9script)
2319 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002320 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002322 if (error || !op_falsy || !result)
2323 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002324 if (error)
2325 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327
2328 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002329 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002331 if (op_falsy)
2332 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002333 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002334 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002335 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002336 clear_tv(rettv);
2337 return FAIL;
2338 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002339 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002340 evalarg_used->eval_flags = (op_falsy ? !result : result)
2341 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2342 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002343 {
2344 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002346 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002347 if (!op_falsy || !result)
2348 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002350 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002352 /*
2353 * Check for the ":".
2354 */
2355 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2356 if (*p != ':')
2357 {
2358 emsg(_(e_missing_colon));
2359 if (evaluate && result)
2360 clear_tv(rettv);
2361 evalarg_used->eval_flags = orig_flags;
2362 return FAIL;
2363 }
2364 if (getnext)
2365 *arg = eval_next_line(evalarg_used);
2366 else
2367 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002368 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002369 {
2370 error_white_both(p, 1);
2371 clear_tv(rettv);
2372 evalarg_used->eval_flags = orig_flags;
2373 return FAIL;
2374 }
2375 *arg = p;
2376 }
2377
2378 /*
2379 * Get the third variable. Recursive!
2380 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002381 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002382 {
2383 error_white_both(p, 1);
2384 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002385 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002386 return FAIL;
2387 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002388 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2389 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002390 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002391 if (eval1(arg, &var2, evalarg_used) == FAIL)
2392 {
2393 if (evaluate && result)
2394 clear_tv(rettv);
2395 evalarg_used->eval_flags = orig_flags;
2396 return FAIL;
2397 }
2398 if (evaluate && !result)
2399 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002401
2402 if (evalarg == NULL)
2403 clear_evalarg(&local_evalarg, NULL);
2404 else
2405 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407
2408 return OK;
2409}
2410
2411/*
2412 * Handle first level expression:
2413 * expr2 || expr2 || expr2 logical OR
2414 *
2415 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002416 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 *
2418 * Return OK or FAIL.
2419 */
2420 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002421eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002423 char_u *p;
2424 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 /*
2427 * Get the first variable.
2428 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002429 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 return FAIL;
2431
2432 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002433 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002435 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002436 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 evalarg_T *evalarg_used = evalarg;
2439 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002440 int evaluate;
2441 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002442 long result = FALSE;
2443 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002444 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002445 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002446
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002447 if (evalarg == NULL)
2448 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002449 CLEAR_FIELD(local_evalarg);
2450 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002451 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002452 orig_flags = evalarg_used->eval_flags;
2453 evaluate = orig_flags & EVAL_EVALUATE;
2454 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002455 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002456 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002457 result = tv_get_bool_chk(rettv, &error);
2458 else if (tv_get_number_chk(rettv, &error) != 0)
2459 result = TRUE;
2460 clear_tv(rettv);
2461 if (error)
2462 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464
2465 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002466 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002468 while (p[0] == '|' && p[1] == '|')
2469 {
2470 if (getnext)
2471 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002472 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002473 {
2474 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2475 {
2476 error_white_both(p, 2);
2477 clear_tv(rettv);
2478 return FAIL;
2479 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002480 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002481 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002482
2483 /*
2484 * Get the second variable.
2485 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002486 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2487 {
2488 error_white_both(p, 2);
2489 clear_tv(rettv);
2490 return FAIL;
2491 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002492 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2493 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002494 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002495 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497
2498 /*
2499 * Compute the result.
2500 */
2501 if (evaluate && !result)
2502 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002503 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002504 result = tv_get_bool_chk(&var2, &error);
2505 else if (tv_get_number_chk(&var2, &error) != 0)
2506 result = TRUE;
2507 clear_tv(&var2);
2508 if (error)
2509 return FAIL;
2510 }
2511 if (evaluate)
2512 {
2513 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002514 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002515 rettv->v_type = VAR_BOOL;
2516 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002517 }
2518 else
2519 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002520 rettv->v_type = VAR_NUMBER;
2521 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002522 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002523 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002524
2525 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002527
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002528 if (evalarg == NULL)
2529 clear_evalarg(&local_evalarg, NULL);
2530 else
2531 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
2533
2534 return OK;
2535}
2536
2537/*
2538 * Handle second level expression:
2539 * expr3 && expr3 && expr3 logical AND
2540 *
2541 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002542 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 *
2544 * Return OK or FAIL.
2545 */
2546 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002547eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002549 char_u *p;
2550 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552 /*
2553 * Get the first variable.
2554 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002555 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 return FAIL;
2557
2558 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002559 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002561 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002562 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002564 evalarg_T *evalarg_used = evalarg;
2565 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002566 int orig_flags;
2567 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002568 long result = TRUE;
2569 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002570 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002571 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002572
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002573 if (evalarg == NULL)
2574 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002575 CLEAR_FIELD(local_evalarg);
2576 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002577 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002578 orig_flags = evalarg_used->eval_flags;
2579 evaluate = orig_flags & EVAL_EVALUATE;
2580 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002581 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002582 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002583 result = tv_get_bool_chk(rettv, &error);
2584 else if (tv_get_number_chk(rettv, &error) == 0)
2585 result = FALSE;
2586 clear_tv(rettv);
2587 if (error)
2588 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
2590
2591 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002592 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594 while (p[0] == '&' && p[1] == '&')
2595 {
2596 if (getnext)
2597 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002598 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002599 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002600 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002601 {
2602 error_white_both(p, 2);
2603 clear_tv(rettv);
2604 return FAIL;
2605 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002606 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002607 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002608
2609 /*
2610 * Get the second variable.
2611 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002612 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2613 {
2614 error_white_both(p, 2);
2615 clear_tv(rettv);
2616 return FAIL;
2617 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002618 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2619 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002620 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002621 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002623 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002624
2625 /*
2626 * Compute the result.
2627 */
2628 if (evaluate && result)
2629 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002630 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002631 result = tv_get_bool_chk(&var2, &error);
2632 else if (tv_get_number_chk(&var2, &error) == 0)
2633 result = FALSE;
2634 clear_tv(&var2);
2635 if (error)
2636 return FAIL;
2637 }
2638 if (evaluate)
2639 {
2640 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002641 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002642 rettv->v_type = VAR_BOOL;
2643 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002644 }
2645 else
2646 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002647 rettv->v_type = VAR_NUMBER;
2648 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002649 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002650 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002651
2652 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002654
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655 if (evalarg == NULL)
2656 clear_evalarg(&local_evalarg, NULL);
2657 else
2658 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
2660
2661 return OK;
2662}
2663
2664/*
2665 * Handle third level expression:
2666 * var1 == var2
2667 * var1 =~ var2
2668 * var1 != var2
2669 * var1 !~ var2
2670 * var1 > var2
2671 * var1 >= var2
2672 * var1 < var2
2673 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002674 * var1 is var2
2675 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 *
2677 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002678 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 *
2680 * Return OK or FAIL.
2681 */
2682 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002683eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002686 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002687 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002689 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691 /*
2692 * Get the first variable.
2693 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002694 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 return FAIL;
2696
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002697 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002698 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
2700 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002701 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002703 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002705 typval_T var2;
2706 int ic;
2707 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002708 int evaluate = evalarg == NULL
2709 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002710
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002711 if (getnext)
2712 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002713 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2714 {
2715 error_white_both(p, len);
2716 clear_tv(rettv);
2717 return FAIL;
2718 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002719
Bram Moolenaar696ba232020-07-29 21:20:41 +02002720 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2721 {
2722 semsg(_(e_invexpr2), p);
2723 clear_tv(rettv);
2724 return FAIL;
2725 }
2726
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002727 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (p[len] == '?')
2729 {
2730 ic = TRUE;
2731 ++len;
2732 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002733 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 else if (p[len] == '#')
2735 {
2736 ic = FALSE;
2737 ++len;
2738 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002739 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002741 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
2743 /*
2744 * Get the second variable.
2745 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002746 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2747 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002748 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002749 clear_tv(rettv);
2750 return FAIL;
2751 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002752 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002753 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002755 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 return FAIL;
2757 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002758 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002759 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002760 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002761
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002762 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002763 {
2764 ret = FAIL;
2765 clear_tv(rettv);
2766 }
2767 else
2768 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002769 clear_tv(&var2);
2770 return ret;
2771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
2773
2774 return OK;
2775}
2776
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002777/*
2778 * Make a copy of blob "tv1" and append blob "tv2".
2779 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 void
2781eval_addblob(typval_T *tv1, typval_T *tv2)
2782{
2783 blob_T *b1 = tv1->vval.v_blob;
2784 blob_T *b2 = tv2->vval.v_blob;
2785 blob_T *b = blob_alloc();
2786 int i;
2787
2788 if (b != NULL)
2789 {
2790 for (i = 0; i < blob_len(b1); i++)
2791 ga_append(&b->bv_ga, blob_get(b1, i));
2792 for (i = 0; i < blob_len(b2); i++)
2793 ga_append(&b->bv_ga, blob_get(b2, i));
2794
2795 clear_tv(tv1);
2796 rettv_blob_set(tv1, b);
2797 }
2798}
2799
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002800/*
2801 * Make a copy of list "tv1" and append list "tv2".
2802 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 int
2804eval_addlist(typval_T *tv1, typval_T *tv2)
2805{
2806 typval_T var3;
2807
2808 // concatenate Lists
2809 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2810 {
2811 clear_tv(tv1);
2812 clear_tv(tv2);
2813 return FAIL;
2814 }
2815 clear_tv(tv1);
2816 *tv1 = var3;
2817 return OK;
2818}
2819
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820/*
2821 * Handle fourth level expression:
2822 * + number addition
2823 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002824 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002825 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 *
2827 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002828 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 *
2830 * Return OK or FAIL.
2831 */
2832 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002833eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 /*
2836 * Get the first variable.
2837 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002838 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 return FAIL;
2840
2841 /*
2842 * Repeat computing, until no '+', '-' or '.' is following.
2843 */
2844 for (;;)
2845 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002846 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002847 int getnext;
2848 char_u *p;
2849 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002850 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002851 int concat;
2852 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002853 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002854
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002855 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002856 // "+=", "-=" and "..=" are assignments
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002857 p = eval_next_non_blank(*arg, evalarg, &getnext);
2858 op = *p;
2859 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002860 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2861 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002863
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002864 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002865 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002866 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002867 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002868 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002869 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002870 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002871 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002872 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002873 clear_tv(rettv);
2874 return FAIL;
2875 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002876 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002877 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002878 if ((op != '+' || (rettv->v_type != VAR_LIST
2879 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002880#ifdef FEAT_FLOAT
2881 && (op == '.' || rettv->v_type != VAR_FLOAT)
2882#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002883 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002884 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002885 int error = FALSE;
2886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002887 // For "list + ...", an illegal use of the first operand as
2888 // a number cannot be determined before evaluating the 2nd
2889 // operand: if this is also a list, all is ok.
2890 // For "something . ...", "something - ..." or "non-list + ...",
2891 // we know that the first operand needs to be a string or number
2892 // without evaluating the 2nd operand. So check before to avoid
2893 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002894 if (op != '.')
2895 tv_get_number_chk(rettv, &error);
2896 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002897 {
2898 clear_tv(rettv);
2899 return FAIL;
2900 }
2901 }
2902
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 /*
2904 * Get the second variable.
2905 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002906 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002907 {
2908 error_white_both(p, oplen);
2909 clear_tv(rettv);
2910 return FAIL;
2911 }
2912 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002913 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002915 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 return FAIL;
2917 }
2918
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002919 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 {
2921 /*
2922 * Compute the result.
2923 */
2924 if (op == '.')
2925 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002926 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2927 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002928 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002929
Bram Moolenaar2e086612020-08-25 22:37:48 +02002930 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002931 || var2.v_type == VAR_CHANNEL
2932 || var2.v_type == VAR_JOB))
2933 emsg(_(e_inval_string));
2934#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002935 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002936 {
2937 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2938 var2.vval.v_float);
2939 s2 = buf2;
2940 }
2941#endif
2942 else
2943 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002944 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002945 {
2946 clear_tv(rettv);
2947 clear_tv(&var2);
2948 return FAIL;
2949 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002951 clear_tv(rettv);
2952 rettv->v_type = VAR_STRING;
2953 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002955 else if (op == '+' && rettv->v_type == VAR_BLOB
2956 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002958 else if (op == '+' && rettv->v_type == VAR_LIST
2959 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002960 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002962 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 else
2965 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002966 int error = FALSE;
2967 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002968#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002969 float_T f1 = 0, f2 = 0;
2970
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002971 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002972 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002973 f1 = rettv->vval.v_float;
2974 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002976 else
2977#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002978 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002979 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002980 if (error)
2981 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002982 // This can only happen for "list + non-list" or
2983 // "blob + non-blob". For "non-list + ..." or
2984 // "something - ...", we returned before evaluating the
2985 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002986 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002987 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002988 return FAIL;
2989 }
2990#ifdef FEAT_FLOAT
2991 if (var2.v_type == VAR_FLOAT)
2992 f1 = n1;
2993#endif
2994 }
2995#ifdef FEAT_FLOAT
2996 if (var2.v_type == VAR_FLOAT)
2997 {
2998 f2 = var2.vval.v_float;
2999 n2 = 0;
3000 }
3001 else
3002#endif
3003 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003004 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003005 if (error)
3006 {
3007 clear_tv(rettv);
3008 clear_tv(&var2);
3009 return FAIL;
3010 }
3011#ifdef FEAT_FLOAT
3012 if (rettv->v_type == VAR_FLOAT)
3013 f2 = n2;
3014#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003015 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003016 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003017
3018#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003019 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003020 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3021 {
3022 if (op == '+')
3023 f1 = f1 + f2;
3024 else
3025 f1 = f1 - f2;
3026 rettv->v_type = VAR_FLOAT;
3027 rettv->vval.v_float = f1;
3028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003030#endif
3031 {
3032 if (op == '+')
3033 n1 = n1 + n2;
3034 else
3035 n1 = n1 - n2;
3036 rettv->v_type = VAR_NUMBER;
3037 rettv->vval.v_number = n1;
3038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003040 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 }
3042 }
3043 return OK;
3044}
3045
3046/*
3047 * Handle fifth level expression:
3048 * * number multiplication
3049 * / number division
3050 * % number modulo
3051 *
3052 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003053 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 *
3055 * Return OK or FAIL.
3056 */
3057 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003058eval6(
3059 char_u **arg,
3060 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003061 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003062 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003064#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003065 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067
3068 /*
3069 * Get the first variable.
3070 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003071 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 return FAIL;
3073
3074 /*
3075 * Repeat computing, until no '*', '/' or '%' is following.
3076 */
3077 for (;;)
3078 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003079 int evaluate;
3080 int getnext;
3081 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003082 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003083 int op;
3084 varnumber_T n1, n2;
3085#ifdef FEAT_FLOAT
3086 float_T f1, f2;
3087#endif
3088 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003089
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003090 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003091 p = eval_next_non_blank(*arg, evalarg, &getnext);
3092 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003093 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003095
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003096 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003097 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003098 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003099 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003100 {
3101 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3102 {
3103 error_white_both(p, 1);
3104 clear_tv(rettv);
3105 return FAIL;
3106 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003107 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003110#ifdef FEAT_FLOAT
3111 f1 = 0;
3112 f2 = 0;
3113#endif
3114 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003115 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117#ifdef FEAT_FLOAT
3118 if (rettv->v_type == VAR_FLOAT)
3119 {
3120 f1 = rettv->vval.v_float;
3121 use_float = TRUE;
3122 n1 = 0;
3123 }
3124 else
3125#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003126 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003127 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003128 if (error)
3129 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 }
3131 else
3132 n1 = 0;
3133
3134 /*
3135 * Get the second variable.
3136 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003137 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3138 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003139 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003140 clear_tv(rettv);
3141 return FAIL;
3142 }
3143 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003144 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 return FAIL;
3146
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003147 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149#ifdef FEAT_FLOAT
3150 if (var2.v_type == VAR_FLOAT)
3151 {
3152 if (!use_float)
3153 {
3154 f1 = n1;
3155 use_float = TRUE;
3156 }
3157 f2 = var2.vval.v_float;
3158 n2 = 0;
3159 }
3160 else
3161#endif
3162 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003163 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164 clear_tv(&var2);
3165 if (error)
3166 return FAIL;
3167#ifdef FEAT_FLOAT
3168 if (use_float)
3169 f2 = n2;
3170#endif
3171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
3173 /*
3174 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003175 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177#ifdef FEAT_FLOAT
3178 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180 if (op == '*')
3181 f1 = f1 * f2;
3182 else if (op == '/')
3183 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003184# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003185 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003186 if (f2 == 0.0)
3187 {
3188 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003189 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003190 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003191 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003192 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003193 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003194 }
3195 else
3196 f1 = f1 / f2;
3197# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003198 // We rely on the floating point library to handle divide
3199 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003200 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003201# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003204 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003206 return FAIL;
3207 }
3208 rettv->v_type = VAR_FLOAT;
3209 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
3211 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003214 int failed = FALSE;
3215
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003216 if (op == '*')
3217 n1 = n1 * n2;
3218 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003219 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003221 n1 = num_modulus(n1, n2, &failed);
3222 if (failed)
3223 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003224
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003225 rettv->v_type = VAR_NUMBER;
3226 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229 }
3230
3231 return OK;
3232}
3233
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003234 int
3235eval_leader(char_u **arg, int vim9)
3236{
3237 char_u *s = *arg;
3238 char_u *p = *arg;
3239
3240 while (*p == '!' || *p == '-' || *p == '+')
3241 {
3242 char_u *n = skipwhite(p + 1);
3243
3244 // ++, --, -+ and +- are not accepted in Vim9 script
3245 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3246 {
3247 semsg(_(e_invexpr2), s);
3248 return FAIL;
3249 }
3250 p = n;
3251 }
3252 *arg = p;
3253 return OK;
3254}
3255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256/*
3257 * Handle sixth level expression:
3258 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003259 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003260 * "string" string constant
3261 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 * &option-name option value
3263 * @r register contents
3264 * identifier variable value
3265 * function() function call
3266 * $VAR environment variable
3267 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003268 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003270 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003271 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 *
3273 * Also handle:
3274 * ! in front logical NOT
3275 * - in front unary minus
3276 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003277 * trailing [] subscript in String or List
3278 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003279 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 *
3281 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003282 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 *
3284 * Return OK or FAIL.
3285 */
3286 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003287eval7(
3288 char_u **arg,
3289 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003290 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003293 int evaluate = evalarg != NULL
3294 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 int len;
3296 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 char_u *start_leader, *end_leader;
3298 int ret = OK;
3299 char_u *alias;
3300
3301 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003302 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003303 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003305 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
3307 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003308 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 */
3310 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003311 if (eval_leader(arg, in_vim9script()) == FAIL)
3312 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 end_leader = *arg;
3314
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003315 if (**arg == '.' && (!isdigit(*(*arg + 1))
3316#ifdef FEAT_FLOAT
3317 || current_sctx.sc_version < 2
3318#endif
3319 ))
3320 {
3321 semsg(_(e_invexpr2), *arg);
3322 ++*arg;
3323 return FAIL;
3324 }
3325
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 switch (**arg)
3327 {
3328 /*
3329 * Number constant.
3330 */
3331 case '0':
3332 case '1':
3333 case '2':
3334 case '3':
3335 case '4':
3336 case '5':
3337 case '6':
3338 case '7':
3339 case '8':
3340 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003341 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003342
3343 // Apply prefixed "-" and "+" now. Matters especially when
3344 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003345 if (ret == OK && evaluate && end_leader > start_leader
3346 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003347 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 break;
3349
3350 /*
3351 * String constant: "string".
3352 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003353 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 break;
3355
3356 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003357 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003359 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003360 break;
3361
3362 /*
3363 * List: [expr, expr]
3364 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003365 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 break;
3367
3368 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003369 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003370 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003371 case '#': if (in_vim9script())
3372 {
3373 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3374 }
3375 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003376 {
3377 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003378 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003379 }
3380 else
3381 ret = NOTDONE;
3382 break;
3383
3384 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003385 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003386 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003387 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003388 case '{': if (in_vim9script())
3389 ret = NOTDONE;
3390 else
3391 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003392 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003393 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003394 break;
3395
3396 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003397 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003399 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 break;
3401
3402 /*
3403 * Environment variable: $VAR.
3404 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003405 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 break;
3407
3408 /*
3409 * Register contents: @r.
3410 */
3411 case '@': ++*arg;
3412 if (evaluate)
3413 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003414 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3415 semsg(_(e_syntax_error_at_str), *arg);
3416 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3417 emsg_invreg(**arg);
3418 else
3419 {
3420 rettv->v_type = VAR_STRING;
3421 rettv->vval.v_string = get_reg_contents(**arg,
3422 GREG_EXPR_SRC);
3423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
3425 if (**arg != NUL)
3426 ++*arg;
3427 break;
3428
3429 /*
3430 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003431 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003433 case '(': ret = NOTDONE;
3434 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003435 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003436 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003437 if (ret == OK && evaluate)
3438 {
3439 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3440
3441 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003442 if (compile_def_function(ufunc,
3443 TRUE, PROFILING(ufunc), NULL) == FAIL)
3444 {
3445 clear_tv(rettv);
3446 ret = FAIL;
3447 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003448 }
3449 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003450 if (ret == NOTDONE)
3451 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003452 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003453 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003454
Bram Moolenaar9215f012020-06-27 21:18:00 +02003455 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003456 if (**arg == ')')
3457 ++*arg;
3458 else if (ret == OK)
3459 {
3460 emsg(_(e_missing_close));
3461 clear_tv(rettv);
3462 ret = FAIL;
3463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
3465 break;
3466
Bram Moolenaar8c711452005-01-14 21:53:12 +00003467 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 break;
3469 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003470
3471 if (ret == NOTDONE)
3472 {
3473 /*
3474 * Must be a variable or function name.
3475 * Can also be a curly-braces kind of name: {expr}.
3476 */
3477 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003478 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003479 if (alias != NULL)
3480 s = alias;
3481
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003482 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003483 ret = FAIL;
3484 else
3485 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003486 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3487
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003488 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003489 {
3490 emsg(_(e_cannot_use_underscore_here));
3491 ret = FAIL;
3492 }
3493 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003494 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003495 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003496 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003497 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003498 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003499 else if (flags & EVAL_CONSTANT)
3500 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003501 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003502 {
3503 // get the value of "true", "false" or a variable
3504 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3505 {
3506 rettv->v_type = VAR_BOOL;
3507 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003508 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003509 }
3510 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003511 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003512 {
3513 rettv->v_type = VAR_BOOL;
3514 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003515 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003516 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003517 else if (len == 4 && in_vim9script()
3518 && STRNCMP(s, "null", 4) == 0)
3519 {
3520 rettv->v_type = VAR_SPECIAL;
3521 rettv->vval.v_number = VVAL_NULL;
3522 ret = OK;
3523 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003524 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003525 ret = eval_variable(s, len, rettv, NULL,
3526 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003527 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003528 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003529 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003530 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003531 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003532 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003533 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003534 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003535 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003536 }
3537
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003538 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3539 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003540 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003541 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
3543 /*
3544 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3545 */
3546 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003547 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003548 return ret;
3549}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003550
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003551/*
3552 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003553 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003554 * Adjusts "end_leaderp" until it is at "start_leader".
3555 */
3556 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003557eval7_leader(
3558 typval_T *rettv,
3559 int numeric_only,
3560 char_u *start_leader,
3561 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003562{
3563 char_u *end_leader = *end_leaderp;
3564 int ret = OK;
3565 int error = FALSE;
3566 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003567 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003568#ifdef FEAT_FLOAT
3569 float_T f = 0.0;
3570
3571 if (rettv->v_type == VAR_FLOAT)
3572 f = rettv->vval.v_float;
3573 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003574#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003575 {
3576 while (VIM_ISWHITE(end_leader[-1]))
3577 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003578 if (in_vim9script() && end_leader[-1] == '!')
3579 val = tv2bool(rettv);
3580 else
3581 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003582 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003583 if (error)
3584 {
3585 clear_tv(rettv);
3586 ret = FAIL;
3587 }
3588 else
3589 {
3590 while (end_leader > start_leader)
3591 {
3592 --end_leader;
3593 if (*end_leader == '!')
3594 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003595 if (numeric_only)
3596 {
3597 ++end_leader;
3598 break;
3599 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003600#ifdef FEAT_FLOAT
3601 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003602 {
3603 if (in_vim9script())
3604 {
3605 rettv->v_type = VAR_BOOL;
3606 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3607 }
3608 else
3609 f = !f;
3610 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003611 else
3612#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003613 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003614 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003615 type = VAR_BOOL;
3616 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003617 }
3618 else if (*end_leader == '-')
3619 {
3620#ifdef FEAT_FLOAT
3621 if (rettv->v_type == VAR_FLOAT)
3622 f = -f;
3623 else
3624#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003625 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003626 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003627 type = VAR_NUMBER;
3628 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003629 }
3630 }
3631#ifdef FEAT_FLOAT
3632 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003634 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003635 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003637 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003638#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003639 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003640 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003641 if (in_vim9script())
3642 rettv->v_type = type;
3643 else
3644 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003645 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003648 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 return ret;
3650}
3651
3652/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003653 * Call the function referred to in "rettv".
3654 */
3655 static int
3656call_func_rettv(
3657 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003658 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003659 typval_T *rettv,
3660 int evaluate,
3661 dict_T *selfdict,
3662 typval_T *basetv)
3663{
3664 partial_T *pt = NULL;
3665 funcexe_T funcexe;
3666 typval_T functv;
3667 char_u *s;
3668 int ret;
3669
3670 // need to copy the funcref so that we can clear rettv
3671 if (evaluate)
3672 {
3673 functv = *rettv;
3674 rettv->v_type = VAR_UNKNOWN;
3675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003676 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003677 if (functv.v_type == VAR_PARTIAL)
3678 {
3679 pt = functv.vval.v_partial;
3680 s = partial_name(pt);
3681 }
3682 else
3683 s = functv.vval.v_string;
3684 }
3685 else
3686 s = (char_u *)"";
3687
Bram Moolenaara80faa82020-04-12 19:37:17 +02003688 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003689 funcexe.firstline = curwin->w_cursor.lnum;
3690 funcexe.lastline = curwin->w_cursor.lnum;
3691 funcexe.evaluate = evaluate;
3692 funcexe.partial = pt;
3693 funcexe.selfdict = selfdict;
3694 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003695 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003696
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003697 // Clear the funcref afterwards, so that deleting it while
3698 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003699 if (evaluate)
3700 clear_tv(&functv);
3701
3702 return ret;
3703}
3704
3705/*
3706 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003707 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003708 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3709 */
3710 static int
3711eval_lambda(
3712 char_u **arg,
3713 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003714 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003715 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003716{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003717 int evaluate = evalarg != NULL
3718 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003719 typval_T base = *rettv;
3720 int ret;
3721
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003722 rettv->v_type = VAR_UNKNOWN;
3723
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003724 if (**arg == '{')
3725 {
3726 // ->{lambda}()
3727 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3728 }
3729 else
3730 {
3731 // ->(lambda)()
3732 ++*arg;
3733 ret = eval1(arg, rettv, evalarg);
3734 *arg = skipwhite_and_linebreak(*arg, evalarg);
3735 if (**arg != ')')
3736 {
3737 emsg(_(e_missing_close));
3738 ret = FAIL;
3739 }
3740 ++*arg;
3741 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003742 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003743 return FAIL;
3744 else if (**arg != '(')
3745 {
3746 if (verbose)
3747 {
3748 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003749 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003750 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003752 }
3753 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003754 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003755 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003756 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003757 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003758
3759 // Clear the funcref afterwards, so that deleting it while
3760 // evaluating the arguments is possible (see test55).
3761 if (evaluate)
3762 clear_tv(&base);
3763
3764 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003765}
3766
3767/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003768 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003769 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003770 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3771 */
3772 static int
3773eval_method(
3774 char_u **arg,
3775 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003776 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003777 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003778{
3779 char_u *name;
3780 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003781 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003782 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003783 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003784 int evaluate = evalarg != NULL
3785 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003786
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003787 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003788
Bram Moolenaarac92e252019-08-03 21:58:38 +02003789 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003790 len = get_name_len(arg, &alias, evaluate, TRUE);
3791 if (alias != NULL)
3792 name = alias;
3793
3794 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003795 {
3796 if (verbose)
3797 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003798 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003799 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003800 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003801 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003802 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003803 if (**arg != '(')
3804 {
3805 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003807 ret = FAIL;
3808 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003809 else if (VIM_ISWHITE((*arg)[-1]))
3810 {
3811 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003812 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003813 ret = FAIL;
3814 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003815 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003816 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003817 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003818 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003819
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003820 // Clear the funcref afterwards, so that deleting it while
3821 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003822 if (evaluate)
3823 clear_tv(&base);
3824
Bram Moolenaarac92e252019-08-03 21:58:38 +02003825 return ret;
3826}
3827
3828/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003829 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3830 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003831 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3832 */
3833 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003834eval_index(
3835 char_u **arg,
3836 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003837 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003838 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003839{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003840 int evaluate = evalarg != NULL
3841 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003842 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003843 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003844 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003845 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003846 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003847 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003848
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003849 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3850 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003851
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003852 init_tv(&var1);
3853 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003854 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003856 /*
3857 * dict.name
3858 */
3859 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003860 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003861 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003862 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003863 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003864 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003865 }
3866 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003868 /*
3869 * something[idx]
3870 *
3871 * Get the (first) variable from inside the [].
3872 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003873 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003874 if (**arg == ':')
3875 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003876 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003877 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003878 else if (vim9 && **arg == ':')
3879 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003880 semsg(_(e_white_space_required_before_and_after_str_at_str),
3881 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003882 clear_tv(&var1);
3883 return FAIL;
3884 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003885 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003886 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003887#ifdef FEAT_FLOAT
3888 // allow for indexing with float
3889 if (vim9 && rettv->v_type == VAR_DICT
3890 && var1.v_type == VAR_FLOAT)
3891 {
3892 var1.vval.v_string = typval_tostring(&var1, TRUE);
3893 var1.v_type = VAR_STRING;
3894 }
3895#endif
3896 if (tv_get_string_chk(&var1) == NULL)
3897 {
3898 // not a number or string
3899 clear_tv(&var1);
3900 return FAIL;
3901 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003902 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003903
3904 /*
3905 * Get the second variable from inside the [:].
3906 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003907 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003908 if (**arg == ':')
3909 {
3910 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003911 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003912 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003913 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003914 semsg(_(e_white_space_required_before_and_after_str_at_str),
3915 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003916 if (!empty1)
3917 clear_tv(&var1);
3918 return FAIL;
3919 }
3920 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003921 if (**arg == ']')
3922 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003923 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003925 if (!empty1)
3926 clear_tv(&var1);
3927 return FAIL;
3928 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003929 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003930 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003931 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003932 if (!empty1)
3933 clear_tv(&var1);
3934 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003935 return FAIL;
3936 }
3937 }
3938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003939 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003940 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003941 if (**arg != ']')
3942 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003943 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003944 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003945 clear_tv(&var1);
3946 if (range)
3947 clear_tv(&var2);
3948 return FAIL;
3949 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003950 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003951 }
3952
3953 if (evaluate)
3954 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003955 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01003956 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003957 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01003958
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003959 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003960 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003961 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003962 clear_tv(&var2);
3963 return res;
3964 }
3965 return OK;
3966}
3967
3968/*
3969 * Check if "rettv" can have an [index] or [sli:ce]
3970 */
3971 int
3972check_can_index(typval_T *rettv, int evaluate, int verbose)
3973{
3974 switch (rettv->v_type)
3975 {
3976 case VAR_FUNC:
3977 case VAR_PARTIAL:
3978 if (verbose)
3979 emsg(_("E695: Cannot index a Funcref"));
3980 return FAIL;
3981 case VAR_FLOAT:
3982#ifdef FEAT_FLOAT
3983 if (verbose)
3984 emsg(_(e_float_as_string));
3985 return FAIL;
3986#endif
3987 case VAR_BOOL:
3988 case VAR_SPECIAL:
3989 case VAR_JOB:
3990 case VAR_CHANNEL:
3991 if (verbose)
3992 emsg(_(e_cannot_index_special_variable));
3993 return FAIL;
3994 case VAR_UNKNOWN:
3995 case VAR_ANY:
3996 case VAR_VOID:
3997 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003998 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003999 emsg(_(e_cannot_index_special_variable));
4000 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004001 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004002 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004003
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004004 case VAR_STRING:
4005 case VAR_LIST:
4006 case VAR_DICT:
4007 case VAR_BLOB:
4008 break;
4009 case VAR_NUMBER:
4010 if (in_vim9script())
4011 emsg(_(e_cannot_index_number));
4012 break;
4013 }
4014 return OK;
4015}
4016
4017/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004018 * slice() function
4019 */
4020 void
4021f_slice(typval_T *argvars, typval_T *rettv)
4022{
4023 if (check_can_index(argvars, TRUE, FALSE) == OK)
4024 {
4025 copy_tv(argvars, rettv);
4026 eval_index_inner(rettv, TRUE, argvars + 1,
4027 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4028 TRUE, NULL, 0, FALSE);
4029 }
4030}
4031
4032/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004033 * Apply index or range to "rettv".
4034 * "var1" is the first index, NULL for [:expr].
4035 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004036 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4037 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004038 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4039 */
4040 int
4041eval_index_inner(
4042 typval_T *rettv,
4043 int is_range,
4044 typval_T *var1,
4045 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004046 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004047 char_u *key,
4048 int keylen,
4049 int verbose)
4050{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004051 varnumber_T n1, n2 = 0;
4052 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004053
4054 n1 = 0;
4055 if (var1 != NULL && rettv->v_type != VAR_DICT)
4056 n1 = tv_get_number(var1);
4057
4058 if (is_range)
4059 {
4060 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004061 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004062 if (verbose)
4063 emsg(_(e_cannot_slice_dictionary));
4064 return FAIL;
4065 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004066 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004067 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004068 else
4069 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004070 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004071
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004072 switch (rettv->v_type)
4073 {
4074 case VAR_UNKNOWN:
4075 case VAR_ANY:
4076 case VAR_VOID:
4077 case VAR_FUNC:
4078 case VAR_PARTIAL:
4079 case VAR_FLOAT:
4080 case VAR_BOOL:
4081 case VAR_SPECIAL:
4082 case VAR_JOB:
4083 case VAR_CHANNEL:
4084 break; // not evaluating, skipping over subscript
4085
4086 case VAR_NUMBER:
4087 case VAR_STRING:
4088 {
4089 char_u *s = tv_get_string(rettv);
4090
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004091 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004092 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004093 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004094 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004095 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004096 else
4097 s = char_from_string(s, n1);
4098 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004099 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004101 // The resulting variable is a substring. If the indexes
4102 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004103 if (n1 < 0)
4104 {
4105 n1 = len + n1;
4106 if (n1 < 0)
4107 n1 = 0;
4108 }
4109 if (n2 < 0)
4110 n2 = len + n2;
4111 else if (n2 >= len)
4112 n2 = len;
4113 if (n1 >= len || n2 < 0 || n1 > n2)
4114 s = NULL;
4115 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004116 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004117 }
4118 else
4119 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004120 // The resulting variable is a string of a single
4121 // character. If the index is too big or negative the
4122 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004123 if (n1 >= len || n1 < 0)
4124 s = NULL;
4125 else
4126 s = vim_strnsave(s + n1, 1);
4127 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004128 clear_tv(rettv);
4129 rettv->v_type = VAR_STRING;
4130 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004131 }
4132 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004133
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004134 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004135 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4136 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004137 break;
4138
4139 case VAR_LIST:
4140 if (var1 == NULL)
4141 n1 = 0;
4142 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004143 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004144 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004145 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004146 return FAIL;
4147 break;
4148
4149 case VAR_DICT:
4150 {
4151 dictitem_T *item;
4152 typval_T tmp;
4153
4154 if (key == NULL)
4155 {
4156 key = tv_get_string_chk(var1);
4157 if (key == NULL)
4158 return FAIL;
4159 }
4160
4161 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4162
4163 if (item == NULL && verbose)
4164 semsg(_(e_dictkey), key);
4165 if (item == NULL)
4166 return FAIL;
4167
4168 copy_tv(&item->di_tv, &tmp);
4169 clear_tv(rettv);
4170 *rettv = tmp;
4171 }
4172 break;
4173 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004174 return OK;
4175}
4176
4177/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004178 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004179 */
4180 char_u *
4181partial_name(partial_T *pt)
4182{
4183 if (pt->pt_name != NULL)
4184 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004185 if (pt->pt_func != NULL)
4186 return pt->pt_func->uf_name;
4187 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004188}
4189
Bram Moolenaarddecc252016-04-06 22:59:37 +02004190 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004191partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004192{
4193 int i;
4194
4195 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004196 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004197 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004198 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004199 if (pt->pt_name != NULL)
4200 {
4201 func_unref(pt->pt_name);
4202 vim_free(pt->pt_name);
4203 }
4204 else
4205 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004206
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004207 // Decrease the reference count for the context of a closure. If down
4208 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004209 if (pt->pt_funcstack != NULL)
4210 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004211 --pt->pt_funcstack->fs_refcount;
4212 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004213 }
4214
Bram Moolenaarddecc252016-04-06 22:59:37 +02004215 vim_free(pt);
4216}
4217
4218/*
4219 * Unreference a closure: decrement the reference count and free it when it
4220 * becomes zero.
4221 */
4222 void
4223partial_unref(partial_T *pt)
4224{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004225 if (pt != NULL)
4226 {
4227 if (--pt->pt_refcount <= 0)
4228 partial_free(pt);
4229
4230 // If the reference count goes down to one, the funcstack may be the
4231 // only reference and can be freed if no other partials reference it.
4232 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4233 funcstack_check_refcount(pt->pt_funcstack);
4234 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004235}
4236
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004237/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004238 * Return the next (unique) copy ID.
4239 * Used for serializing nested structures.
4240 */
4241 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004242get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004243{
4244 current_copyID += COPYID_INC;
4245 return current_copyID;
4246}
4247
4248/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004249 * Garbage collection for lists and dictionaries.
4250 *
4251 * We use reference counts to be able to free most items right away when they
4252 * are no longer used. But for composite items it's possible that it becomes
4253 * unused while the reference count is > 0: When there is a recursive
4254 * reference. Example:
4255 * :let l = [1, 2, 3]
4256 * :let d = {9: l}
4257 * :let l[1] = d
4258 *
4259 * Since this is quite unusual we handle this with garbage collection: every
4260 * once in a while find out which lists and dicts are not referenced from any
4261 * variable.
4262 *
4263 * Here is a good reference text about garbage collection (refers to Python
4264 * but it applies to all reference-counting mechanisms):
4265 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004266 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004267
4268/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004269 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004270 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004271 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004272 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004273 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004274garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004275{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004276 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004277 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004278 buf_T *buf;
4279 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004280 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004281 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004282
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004283 if (!testing)
4284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004285 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004286 want_garbage_collect = FALSE;
4287 may_garbage_collect = FALSE;
4288 garbage_collect_at_exit = FALSE;
4289 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004290
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004291 // The execution stack can grow big, limit the size.
4292 if (exestack.ga_maxlen - exestack.ga_len > 500)
4293 {
4294 size_t new_len;
4295 char_u *pp;
4296 int n;
4297
4298 // Keep 150% of the current size, with a minimum of the growth size.
4299 n = exestack.ga_len / 2;
4300 if (n < exestack.ga_growsize)
4301 n = exestack.ga_growsize;
4302
4303 // Don't make it bigger though.
4304 if (exestack.ga_len + n < exestack.ga_maxlen)
4305 {
4306 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4307 pp = vim_realloc(exestack.ga_data, new_len);
4308 if (pp == NULL)
4309 return FAIL;
4310 exestack.ga_maxlen = exestack.ga_len + n;
4311 exestack.ga_data = pp;
4312 }
4313 }
4314
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004315 // We advance by two because we add one for items referenced through
4316 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004317 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004318
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004319 /*
4320 * 1. Go through all accessible variables and mark all lists and dicts
4321 * with copyID.
4322 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004323
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004324 // Don't free variables in the previous_funccal list unless they are only
4325 // referenced through previous_funccal. This must be first, because if
4326 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004327 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004328
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004329 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004330 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004331
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004332 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004333 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004334 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4335 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004336
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004337 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004338 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004339 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4340 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004341 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004342 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4343 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004344#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004345 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004346 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4347 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004348 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004349 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004350 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4351 NULL, NULL);
4352#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004353
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004354 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004355 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004356 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4357 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004358 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004359 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004360
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004361 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004362 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004363
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004364 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004365 abort = abort || set_ref_in_functions(copyID);
4366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004367 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004368 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004369
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004370 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004371 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004372
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004373 // callbacks in buffers
4374 abort = abort || set_ref_in_buffers(copyID);
4375
Bram Moolenaar1dced572012-04-05 16:54:08 +02004376#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004377 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004378#endif
4379
Bram Moolenaardb913952012-06-29 12:54:53 +02004380#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004381 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004382#endif
4383
4384#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004385 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004386#endif
4387
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004388#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004389 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004390 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004391#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004392#ifdef FEAT_NETBEANS_INTG
4393 abort = abort || set_ref_in_nb_channel(copyID);
4394#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004395
Bram Moolenaare3188e22016-05-31 21:13:04 +02004396#ifdef FEAT_TIMERS
4397 abort = abort || set_ref_in_timer(copyID);
4398#endif
4399
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004400#ifdef FEAT_QUICKFIX
4401 abort = abort || set_ref_in_quickfix(copyID);
4402#endif
4403
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004404#ifdef FEAT_TERMINAL
4405 abort = abort || set_ref_in_term(copyID);
4406#endif
4407
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004408#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004409 abort = abort || set_ref_in_popups(copyID);
4410#endif
4411
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004412 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004413 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004414 /*
4415 * 2. Free lists and dictionaries that are not referenced.
4416 */
4417 did_free = free_unref_items(copyID);
4418
4419 /*
4420 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004421 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004422 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004423 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004424 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004425 else if (p_verbose > 0)
4426 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004427 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004428 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004429
4430 return did_free;
4431}
4432
4433/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004434 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004435 */
4436 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004437free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004438{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004439 int did_free = FALSE;
4440
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004441 // Let all "free" functions know that we are here. This means no
4442 // dictionaries, lists, channels or jobs are to be freed, because we will
4443 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004444 in_free_unref_items = TRUE;
4445
4446 /*
4447 * PASS 1: free the contents of the items. We don't free the items
4448 * themselves yet, so that it is possible to decrement refcount counters
4449 */
4450
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004451 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004452 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004454 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004455 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004456
4457#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004458 // Go through the list of jobs and free items without the copyID. This
4459 // must happen before doing channels, because jobs refer to channels, but
4460 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004461 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004463 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004464 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4465#endif
4466
4467 /*
4468 * PASS 2: free the items themselves.
4469 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004470 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004471 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004472
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004473#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004474 // Go through the list of jobs and free items without the copyID. This
4475 // must happen before doing channels, because jobs refer to channels, but
4476 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004477 free_unused_jobs(copyID, COPYID_MASK);
4478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004480 free_unused_channels(copyID, COPYID_MASK);
4481#endif
4482
4483 in_free_unref_items = FALSE;
4484
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004485 return did_free;
4486}
4487
4488/*
4489 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004490 * "list_stack" is used to add lists to be marked. Can be NULL.
4491 *
4492 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004493 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004495set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004496{
4497 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004498 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004499 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004500 hashtab_T *cur_ht;
4501 ht_stack_T *ht_stack = NULL;
4502 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004503
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004504 cur_ht = ht;
4505 for (;;)
4506 {
4507 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004508 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004509 // Mark each item in the hashtab. If the item contains a hashtab
4510 // it is added to ht_stack, if it contains a list it is added to
4511 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004512 todo = (int)cur_ht->ht_used;
4513 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4514 if (!HASHITEM_EMPTY(hi))
4515 {
4516 --todo;
4517 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4518 &ht_stack, list_stack);
4519 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004520 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004521
4522 if (ht_stack == NULL)
4523 break;
4524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004525 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004526 cur_ht = ht_stack->ht;
4527 tempitem = ht_stack;
4528 ht_stack = ht_stack->prev;
4529 free(tempitem);
4530 }
4531
4532 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004533}
4534
4535/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004536 * Mark a dict and its items with "copyID".
4537 * Returns TRUE if setting references failed somehow.
4538 */
4539 int
4540set_ref_in_dict(dict_T *d, int copyID)
4541{
4542 if (d != NULL && d->dv_copyID != copyID)
4543 {
4544 d->dv_copyID = copyID;
4545 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4546 }
4547 return FALSE;
4548}
4549
4550/*
4551 * Mark a list and its items with "copyID".
4552 * Returns TRUE if setting references failed somehow.
4553 */
4554 int
4555set_ref_in_list(list_T *ll, int copyID)
4556{
4557 if (ll != NULL && ll->lv_copyID != copyID)
4558 {
4559 ll->lv_copyID = copyID;
4560 return set_ref_in_list_items(ll, copyID, NULL);
4561 }
4562 return FALSE;
4563}
4564
4565/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004566 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004567 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4568 *
4569 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004570 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004571 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004572set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004573{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004574 listitem_T *li;
4575 int abort = FALSE;
4576 list_T *cur_l;
4577 list_stack_T *list_stack = NULL;
4578 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004579
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004580 cur_l = l;
4581 for (;;)
4582 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004583 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004584 // Mark each item in the list. If the item contains a hashtab
4585 // it is added to ht_stack, if it contains a list it is added to
4586 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004587 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4588 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4589 ht_stack, &list_stack);
4590 if (list_stack == NULL)
4591 break;
4592
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004593 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004594 cur_l = list_stack->list;
4595 tempitem = list_stack;
4596 list_stack = list_stack->prev;
4597 free(tempitem);
4598 }
4599
4600 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004601}
4602
4603/*
4604 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004605 * "list_stack" is used to add lists to be marked. Can be NULL.
4606 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4607 *
4608 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004609 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004610 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004611set_ref_in_item(
4612 typval_T *tv,
4613 int copyID,
4614 ht_stack_T **ht_stack,
4615 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004616{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004617 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004618
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004619 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004620 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004621 dict_T *dd = tv->vval.v_dict;
4622
Bram Moolenaara03f2332016-02-06 18:09:59 +01004623 if (dd != NULL && dd->dv_copyID != copyID)
4624 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004625 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004626 dd->dv_copyID = copyID;
4627 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004628 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004629 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4630 }
4631 else
4632 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004633 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4634
Bram Moolenaara03f2332016-02-06 18:09:59 +01004635 if (newitem == NULL)
4636 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004637 else
4638 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004639 newitem->ht = &dd->dv_hashtab;
4640 newitem->prev = *ht_stack;
4641 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004642 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004643 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004644 }
4645 }
4646 else if (tv->v_type == VAR_LIST)
4647 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004648 list_T *ll = tv->vval.v_list;
4649
Bram Moolenaara03f2332016-02-06 18:09:59 +01004650 if (ll != NULL && ll->lv_copyID != copyID)
4651 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004652 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004653 ll->lv_copyID = copyID;
4654 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004655 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004656 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004657 }
4658 else
4659 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004660 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4661
Bram Moolenaara03f2332016-02-06 18:09:59 +01004662 if (newitem == NULL)
4663 abort = TRUE;
4664 else
4665 {
4666 newitem->list = ll;
4667 newitem->prev = *list_stack;
4668 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004669 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004670 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004671 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004672 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004673 else if (tv->v_type == VAR_FUNC)
4674 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004675 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004676 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004677 else if (tv->v_type == VAR_PARTIAL)
4678 {
4679 partial_T *pt = tv->vval.v_partial;
4680 int i;
4681
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004682 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004683 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004684 // Didn't see this partial yet.
4685 pt->pt_copyID = copyID;
4686
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004687 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004688
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004689 if (pt->pt_dict != NULL)
4690 {
4691 typval_T dtv;
4692
4693 dtv.v_type = VAR_DICT;
4694 dtv.vval.v_dict = pt->pt_dict;
4695 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4696 }
4697
4698 for (i = 0; i < pt->pt_argc; ++i)
4699 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4700 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004701 if (pt->pt_funcstack != NULL)
4702 {
4703 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4704
4705 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4706 abort = abort || set_ref_in_item(stack + i, copyID,
4707 ht_stack, list_stack);
4708 }
4709
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004710 }
4711 }
4712#ifdef FEAT_JOB_CHANNEL
4713 else if (tv->v_type == VAR_JOB)
4714 {
4715 job_T *job = tv->vval.v_job;
4716 typval_T dtv;
4717
4718 if (job != NULL && job->jv_copyID != copyID)
4719 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004720 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004721 if (job->jv_channel != NULL)
4722 {
4723 dtv.v_type = VAR_CHANNEL;
4724 dtv.vval.v_channel = job->jv_channel;
4725 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4726 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004727 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004728 {
4729 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004730 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004731 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4732 }
4733 }
4734 }
4735 else if (tv->v_type == VAR_CHANNEL)
4736 {
4737 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004738 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004739 typval_T dtv;
4740 jsonq_T *jq;
4741 cbq_T *cq;
4742
4743 if (ch != NULL && ch->ch_copyID != copyID)
4744 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004745 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004746 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004747 {
4748 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4749 jq = jq->jq_next)
4750 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4751 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4752 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004753 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004754 {
4755 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004756 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004757 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4758 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004759 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004760 {
4761 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004762 dtv.vval.v_partial =
4763 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004764 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4765 }
4766 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004767 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004768 {
4769 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004770 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004771 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4772 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004773 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004774 {
4775 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004776 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004777 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4778 }
4779 }
4780 }
4781#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004782 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004783}
4784
Bram Moolenaar8c711452005-01-14 21:53:12 +00004785/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004786 * Return a string with the string representation of a variable.
4787 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004788 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004789 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004790 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004791 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004792 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004793 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4794 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004795 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004796 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004797 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004798echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004799 typval_T *tv,
4800 char_u **tofree,
4801 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004802 int copyID,
4803 int echo_style,
4804 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004805 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004806{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004807 static int recurse = 0;
4808 char_u *r = NULL;
4809
Bram Moolenaar33570922005-01-25 22:26:29 +00004810 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004811 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004812 if (!did_echo_string_emsg)
4813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004814 // Only give this message once for a recursive call to avoid
4815 // flooding the user with errors. And stop iterating over lists
4816 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004817 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004818 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004819 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004820 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004821 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004822 }
4823 ++recurse;
4824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004825 switch (tv->v_type)
4826 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004827 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004828 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004829 {
4830 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004831 r = tv->vval.v_string;
4832 if (r == NULL)
4833 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004834 }
4835 else
4836 {
4837 *tofree = string_quote(tv->vval.v_string, FALSE);
4838 r = *tofree;
4839 }
4840 break;
4841
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004842 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004843 if (echo_style)
4844 {
4845 *tofree = NULL;
4846 r = tv->vval.v_string;
4847 }
4848 else
4849 {
4850 *tofree = string_quote(tv->vval.v_string, TRUE);
4851 r = *tofree;
4852 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004853 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004854
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004855 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004856 {
4857 partial_T *pt = tv->vval.v_partial;
4858 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004859 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004860 garray_T ga;
4861 int i;
4862 char_u *tf;
4863
4864 ga_init2(&ga, 1, 100);
4865 ga_concat(&ga, (char_u *)"function(");
4866 if (fname != NULL)
4867 {
4868 ga_concat(&ga, fname);
4869 vim_free(fname);
4870 }
4871 if (pt != NULL && pt->pt_argc > 0)
4872 {
4873 ga_concat(&ga, (char_u *)", [");
4874 for (i = 0; i < pt->pt_argc; ++i)
4875 {
4876 if (i > 0)
4877 ga_concat(&ga, (char_u *)", ");
4878 ga_concat(&ga,
4879 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4880 vim_free(tf);
4881 }
4882 ga_concat(&ga, (char_u *)"]");
4883 }
4884 if (pt != NULL && pt->pt_dict != NULL)
4885 {
4886 typval_T dtv;
4887
4888 ga_concat(&ga, (char_u *)", ");
4889 dtv.v_type = VAR_DICT;
4890 dtv.vval.v_dict = pt->pt_dict;
4891 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4892 vim_free(tf);
4893 }
4894 ga_concat(&ga, (char_u *)")");
4895
4896 *tofree = ga.ga_data;
4897 r = *tofree;
4898 break;
4899 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004900
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004901 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004902 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004903 break;
4904
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004905 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004906 if (tv->vval.v_list == NULL)
4907 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004908 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004909 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004910 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004911 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004912 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4913 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004914 {
4915 *tofree = NULL;
4916 r = (char_u *)"[...]";
4917 }
4918 else
4919 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004920 int old_copyID = tv->vval.v_list->lv_copyID;
4921
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004922 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004923 *tofree = list2string(tv, copyID, restore_copyID);
4924 if (restore_copyID)
4925 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004926 r = *tofree;
4927 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004928 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004929
Bram Moolenaar8c711452005-01-14 21:53:12 +00004930 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004931 if (tv->vval.v_dict == NULL)
4932 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004933 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004934 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004935 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004936 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004937 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4938 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004939 {
4940 *tofree = NULL;
4941 r = (char_u *)"{...}";
4942 }
4943 else
4944 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004945 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004946
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004947 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004948 *tofree = dict2string(tv, copyID, restore_copyID);
4949 if (restore_copyID)
4950 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004951 r = *tofree;
4952 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004953 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004954
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004955 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004956 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004957 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004959 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004960 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004961 break;
4962
Bram Moolenaar835dc632016-02-07 14:27:38 +01004963 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004964 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004965 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004966 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004967 if (composite_val)
4968 {
4969 *tofree = string_quote(r, FALSE);
4970 r = *tofree;
4971 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004972 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004975#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 *tofree = NULL;
4977 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4978 r = numbuf;
4979 break;
4980#endif
4981
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004982 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004983 case VAR_SPECIAL:
4984 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004985 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004986 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004987 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004988
Bram Moolenaar8502c702014-06-17 12:51:16 +02004989 if (--recurse == 0)
4990 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004991 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004992}
4993
4994/*
4995 * Return a string with the string representation of a variable.
4996 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4997 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004998 * Does not put quotes around strings, as ":echo" displays values.
4999 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5000 * May return NULL.
5001 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005002 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005003echo_string(
5004 typval_T *tv,
5005 char_u **tofree,
5006 char_u *numbuf,
5007 int copyID)
5008{
5009 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5010}
5011
5012/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005013 * Return string "str" in ' quotes, doubling ' characters.
5014 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005015 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005016 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005017 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005018string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005019{
Bram Moolenaar33570922005-01-25 22:26:29 +00005020 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005021 char_u *p, *r, *s;
5022
Bram Moolenaar33570922005-01-25 22:26:29 +00005023 len = (function ? 13 : 3);
5024 if (str != NULL)
5025 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005026 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005027 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005028 if (*p == '\'')
5029 ++len;
5030 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005031 s = r = alloc(len);
5032 if (r != NULL)
5033 {
5034 if (function)
5035 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005036 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005037 r += 10;
5038 }
5039 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005041 if (str != NULL)
5042 for (p = str; *p != NUL; )
5043 {
5044 if (*p == '\'')
5045 *r++ = '\'';
5046 MB_COPY_CHAR(p, r);
5047 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005049 if (function)
5050 *r++ = ')';
5051 *r++ = NUL;
5052 }
5053 return s;
5054}
5055
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005056#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005057/*
5058 * Convert the string "text" to a floating point number.
5059 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5060 * this always uses a decimal point.
5061 * Returns the length of the text that was consumed.
5062 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005063 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005064string2float(
5065 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005066 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005067{
5068 char *s = (char *)text;
5069 float_T f;
5070
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005071 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005072 if (STRNICMP(text, "inf", 3) == 0)
5073 {
5074 *value = INFINITY;
5075 return 3;
5076 }
5077 if (STRNICMP(text, "-inf", 3) == 0)
5078 {
5079 *value = -INFINITY;
5080 return 4;
5081 }
5082 if (STRNICMP(text, "nan", 3) == 0)
5083 {
5084 *value = NAN;
5085 return 3;
5086 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005087 f = strtod(s, &s);
5088 *value = f;
5089 return (int)((char_u *)s - text);
5090}
5091#endif
5092
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005093/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005094 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5095 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005096 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005097 */
5098 int
5099buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5100{
5101 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005102 char_u *t;
5103 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005104
5105 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5106 return -1;
5107
5108 if (lnum > buf->b_ml.ml_line_count)
5109 lnum = buf->b_ml.ml_line_count;
5110
5111 str = ml_get_buf(buf, lnum, FALSE);
5112 if (str == NULL)
5113 return -1;
5114
5115 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005116 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005117
Bram Moolenaar91458462021-01-13 20:08:38 +01005118 // count the number of characters
5119 t = str;
5120 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5121 t += mb_ptr2len(t);
5122
5123 // In insert mode, when the cursor is at the end of a non-empty line,
5124 // byteidx points to the NUL character immediately past the end of the
5125 // string. In this case, add one to the character count.
5126 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5127 count++;
5128
5129 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005130}
5131
5132/*
5133 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005134 * byte index. Works only for loaded buffers. Returns -1 on failure.
5135 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005136 */
5137 int
5138buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5139{
5140 char_u *str;
5141 char_u *t;
5142
5143 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5144 return -1;
5145
5146 if (lnum > buf->b_ml.ml_line_count)
5147 lnum = buf->b_ml.ml_line_count;
5148
5149 str = ml_get_buf(buf, lnum, FALSE);
5150 if (str == NULL)
5151 return -1;
5152
5153 // Convert the character offset to a byte offset
5154 t = str;
5155 while (*t != NUL && --charidx > 0)
5156 t += mb_ptr2len(t);
5157
Bram Moolenaar91458462021-01-13 20:08:38 +01005158 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005159}
5160
5161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005163 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005165 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005166var2fpos(
5167 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005168 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005169 int *fnum, // set to fnum for '0, 'A, etc.
5170 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005172 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005174 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005176 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005177 if (varp->v_type == VAR_LIST)
5178 {
5179 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005180 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005181 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005182 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005183
5184 l = varp->vval.v_list;
5185 if (l == NULL)
5186 return NULL;
5187
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005188 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005189 pos.lnum = list_find_nr(l, 0L, &error);
5190 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005191 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005192 if (charcol)
5193 len = (long)mb_charlen(ml_get(pos.lnum));
5194 else
5195 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005196
Bram Moolenaarec65d772020-08-20 22:29:12 +02005197 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005198 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005199 li = list_find(l, 1L);
5200 if (li != NULL && li->li_tv.v_type == VAR_STRING
5201 && li->li_tv.vval.v_string != NULL
5202 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005203 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005204 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005205 }
5206 else
5207 {
5208 pos.col = list_find_nr(l, 1L, &error);
5209 if (error)
5210 return NULL;
5211 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005212
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005213 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005214 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005215 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005216 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005218 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005219 pos.coladd = list_find_nr(l, 2L, &error);
5220 if (error)
5221 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005222
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005223 return &pos;
5224 }
5225
Bram Moolenaarc5809432021-03-27 21:23:30 +01005226 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5227 return NULL;
5228
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005229 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 if (name == NULL)
5231 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005233 {
5234 pos = curwin->w_cursor;
5235 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005236 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005237 return &pos;
5238 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005239 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005240 {
5241 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005242 pos = VIsual;
5243 else
5244 pos = curwin->w_cursor;
5245 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005246 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005247 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005248 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005249 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005251 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5253 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005254 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005255 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 return pp;
5257 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005258
Bram Moolenaara5525202006-03-02 22:52:09 +00005259 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005260
Bram Moolenaar477933c2007-07-17 14:32:23 +00005261 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005262 {
5263 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005264 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005265 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005266 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005267 // In silent Ex mode topline is zero, but that's not a valid line
5268 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005269 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005270 return &pos;
5271 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005272 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005273 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005274 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005275 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005276 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005277 return &pos;
5278 }
5279 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005280 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005282 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {
5284 pos.lnum = curbuf->b_ml.ml_line_count;
5285 pos.col = 0;
5286 }
5287 else
5288 {
5289 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005290 if (charcol)
5291 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5292 else
5293 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 }
5295 return &pos;
5296 }
5297 return NULL;
5298}
5299
5300/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005301 * Convert list in "arg" into a position and optional file number.
5302 * When "fnump" is NULL there is no file number, only 3 items.
5303 * Note that the column is passed on as-is, the caller may want to decrement
5304 * it to use 1 for the first column.
5305 * Return FAIL when conversion is not possible, doesn't check the position for
5306 * validity.
5307 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005308 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005309list2fpos(
5310 typval_T *arg,
5311 pos_T *posp,
5312 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005313 colnr_T *curswantp,
5314 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005315{
5316 list_T *l = arg->vval.v_list;
5317 long i = 0;
5318 long n;
5319
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005320 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5321 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005322 if (arg->v_type != VAR_LIST
5323 || l == NULL
5324 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005325 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005326 return FAIL;
5327
5328 if (fnump != NULL)
5329 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005330 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005331 if (n < 0)
5332 return FAIL;
5333 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005334 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005335 *fnump = n;
5336 }
5337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005338 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005339 if (n < 0)
5340 return FAIL;
5341 posp->lnum = n;
5342
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005343 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005344 if (n < 0)
5345 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005346 // If character position is specified, then convert to byte position
5347 if (charcol)
5348 {
5349 buf_T *buf;
5350
5351 // Get the text for the specified line in a loaded buffer
5352 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5353 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5354 return FAIL;
5355
Bram Moolenaar91458462021-01-13 20:08:38 +01005356 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005357 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005358 posp->col = n;
5359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005360 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005361 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005362 posp->coladd = 0;
5363 else
5364 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005365
Bram Moolenaar493c1782014-05-28 14:34:46 +02005366 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005367 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005368
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005369 return OK;
5370}
5371
5372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 * Get the length of an environment variable name.
5374 * Advance "arg" to the first character after the name.
5375 * Return 0 for error.
5376 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005377 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005378get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380 char_u *p;
5381 int len;
5382
5383 for (p = *arg; vim_isIDc(*p); ++p)
5384 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005385 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 return 0;
5387
5388 len = (int)(p - *arg);
5389 *arg = p;
5390 return len;
5391}
5392
5393/*
5394 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005395 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 * Return 0 if something is wrong.
5397 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005399get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400{
5401 char_u *p;
5402 int len;
5403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005404 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005406 {
5407 if (*p == ':')
5408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005409 // "s:" is start of "s:var", but "n:" is not and can be used in
5410 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005411 len = (int)(p - *arg);
5412 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5413 || len > 1)
5414 break;
5415 }
5416 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005417 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 return 0;
5419
5420 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005421 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423 return len;
5424}
5425
5426/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005427 * Get the length of the name of a variable or function.
5428 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005430 * Return -1 if curly braces expansion failed.
5431 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 * If the name contains 'magic' {}'s, expand them and return the
5433 * expanded name in an allocated string via 'alias' - caller must free.
5434 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005435 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005436get_name_len(
5437 char_u **arg,
5438 char_u **alias,
5439 int evaluate,
5440 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 char_u *p;
5444 char_u *expr_start;
5445 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005447 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
5449 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5450 && (*arg)[2] == (int)KE_SNR)
5451 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005452 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 *arg += 3;
5454 return get_id_len(arg) + 3;
5455 }
5456 len = eval_fname_script(*arg);
5457 if (len > 0)
5458 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 *arg += len;
5461 }
5462
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005466 p = find_name_end(*arg, &expr_start, &expr_end,
5467 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 if (expr_start != NULL)
5469 {
5470 char_u *temp_string;
5471
5472 if (!evaluate)
5473 {
5474 len += (int)(p - *arg);
5475 *arg = skipwhite(p);
5476 return len;
5477 }
5478
5479 /*
5480 * Include any <SID> etc in the expanded string:
5481 * Thus the -len here.
5482 */
5483 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5484 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005485 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 *alias = temp_string;
5487 *arg = skipwhite(p);
5488 return (int)STRLEN(temp_string);
5489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
5491 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005492 // Only give an error when there is something, otherwise it will be
5493 // reported at a higher level.
5494 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005495 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
5497 return len;
5498}
5499
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500/*
5501 * Find the end of a variable or function name, taking care of magic braces.
5502 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5503 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005504 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 * Return a pointer to just after the name. Equal to "arg" if there is no
5506 * valid name.
5507 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005508 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005509find_name_end(
5510 char_u *arg,
5511 char_u **expr_start,
5512 char_u **expr_end,
5513 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005515 int mb_nest = 0;
5516 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005518 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005519 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 *expr_start = NULL;
5524 *expr_end = NULL;
5525 }
5526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005527 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005528 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5529 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005530 return arg;
5531
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 for (p = arg; *p != NUL
5533 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005534 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005535 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005536 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005538 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005540 if (*p == '\'')
5541 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005542 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005543 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005544 ;
5545 if (*p == NUL)
5546 break;
5547 }
5548 else if (*p == '"')
5549 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005550 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005551 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005552 if (*p == '\\' && p[1] != NUL)
5553 ++p;
5554 if (*p == NUL)
5555 break;
5556 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005557 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5558 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005559 // "s:" is start of "s:var", but "n:" is not and can be used in
5560 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005561 len = (int)(p - arg);
5562 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005563 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005564 break;
5565 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005566
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 if (*p == '[')
5570 ++br_nest;
5571 else if (*p == ']')
5572 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005574
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005575 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 if (*p == '{')
5578 {
5579 mb_nest++;
5580 if (expr_start != NULL && *expr_start == NULL)
5581 *expr_start = p;
5582 }
5583 else if (*p == '}')
5584 {
5585 mb_nest--;
5586 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5587 *expr_end = p;
5588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 }
5591
5592 return p;
5593}
5594
5595/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005596 * Expands out the 'magic' {}'s in a variable/function name.
5597 * Note that this can call itself recursively, to deal with
5598 * constructs like foo{bar}{baz}{bam}
5599 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5600 * "in_start" ^
5601 * "expr_start" ^
5602 * "expr_end" ^
5603 * "in_end" ^
5604 *
5605 * Returns a new allocated string, which the caller must free.
5606 * Returns NULL for failure.
5607 */
5608 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005609make_expanded_name(
5610 char_u *in_start,
5611 char_u *expr_start,
5612 char_u *expr_end,
5613 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005614{
5615 char_u c1;
5616 char_u *retval = NULL;
5617 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005618
5619 if (expr_end == NULL || in_end == NULL)
5620 return NULL;
5621 *expr_start = NUL;
5622 *expr_end = NUL;
5623 c1 = *in_end;
5624 *in_end = NUL;
5625
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005626 temp_result = eval_to_string(expr_start + 1, FALSE);
5627 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005628 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005629 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5630 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005631 if (retval != NULL)
5632 {
5633 STRCPY(retval, in_start);
5634 STRCAT(retval, temp_result);
5635 STRCAT(retval, expr_end + 1);
5636 }
5637 }
5638 vim_free(temp_result);
5639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005640 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005641 *expr_start = '{';
5642 *expr_end = '}';
5643
5644 if (retval != NULL)
5645 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005646 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005647 if (expr_start != NULL)
5648 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005649 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005650 temp_result = make_expanded_name(retval, expr_start,
5651 expr_end, temp_result);
5652 vim_free(retval);
5653 retval = temp_result;
5654 }
5655 }
5656
5657 return retval;
5658}
5659
5660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005662 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005665eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005667 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005668}
5669
5670/*
5671 * Return TRUE if character "c" can be used as the first character in a
5672 * variable or function name (excluding '{' and '}').
5673 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005675eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005676{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005677 return ASCII_ISALPHA(c) || c == '_';
5678}
5679
5680/*
5681 * Return TRUE if character "c" can be used as the first character of a
5682 * dictionary key.
5683 */
5684 int
5685eval_isdictc(int c)
5686{
5687 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688}
5689
5690/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005691 * Handle:
5692 * - expr[expr], expr[expr:expr] subscript
5693 * - ".name" lookup
5694 * - function call with Funcref variable: func(expr)
5695 * - method call: var->method()
5696 *
5697 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005698 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005700handle_subscript(
5701 char_u **arg,
5702 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005703 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005704 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005705{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005706 int evaluate = evalarg != NULL
5707 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005708 int ret = OK;
5709 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005710 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005711 int getnext;
5712 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005713
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005714 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005715 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005716 // When at the end of the line and ".name" or "->{" or "->X" follows in
5717 // the next line then consume the line break.
5718 p = eval_next_non_blank(*arg, evalarg, &getnext);
5719 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005720 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005721 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005722 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005723 {
5724 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005725 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005726 check_white = FALSE;
5727 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005728
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005729 if (rettv->v_type == VAR_ANY)
5730 {
5731 char_u *exp_name;
5732 int cc;
5733 int idx;
5734 ufunc_T *ufunc;
5735 type_T *type;
5736
5737 // Found script from "import * as {name}", script item name must
5738 // follow.
5739 if (**arg != '.')
5740 {
5741 if (verbose)
5742 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5743 ret = FAIL;
5744 break;
5745 }
5746 ++*arg;
5747 if (IS_WHITE_OR_NUL(**arg))
5748 {
5749 if (verbose)
5750 emsg(_(e_no_white_space_allowed_after_dot));
5751 ret = FAIL;
5752 break;
5753 }
5754
5755 // isolate the name
5756 exp_name = *arg;
5757 while (eval_isnamec(**arg))
5758 ++*arg;
5759 cc = **arg;
5760 **arg = NUL;
5761
5762 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5763 evalarg->eval_cctx, verbose);
5764 **arg = cc;
5765 *arg = skipwhite(*arg);
5766
5767 if (idx < 0 && ufunc == NULL)
5768 {
5769 ret = FAIL;
5770 break;
5771 }
5772 if (idx >= 0)
5773 {
5774 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5775 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5776
5777 copy_tv(sv->sv_tv, rettv);
5778 }
5779 else
5780 {
5781 rettv->v_type = VAR_FUNC;
5782 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5783 }
5784 }
5785
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005786 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5787 || rettv->v_type == VAR_PARTIAL))
5788 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005789 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005790 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5791 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005792
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005793 // Stop the expression evaluation when immediately aborting on
5794 // error, or when an interrupt occurred or an exception was thrown
5795 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005796 if (aborting())
5797 {
5798 if (ret == OK)
5799 clear_tv(rettv);
5800 ret = FAIL;
5801 }
5802 dict_unref(selfdict);
5803 selfdict = NULL;
5804 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005805 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005806 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005807 *arg = skipwhite(p + 2);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005808 if (ret == OK)
5809 {
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01005810 if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005811 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005812 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005813 else
5814 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005815 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005816 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005817 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005818 // "." is ".name" lookup when we found a dict or when evaluating and
5819 // scriptversion is at least 2, where string concatenation is "..".
5820 else if (**arg == '['
5821 || (**arg == '.' && (rettv->v_type == VAR_DICT
5822 || (!evaluate
5823 && (*arg)[1] != '.'
5824 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005825 {
5826 dict_unref(selfdict);
5827 if (rettv->v_type == VAR_DICT)
5828 {
5829 selfdict = rettv->vval.v_dict;
5830 if (selfdict != NULL)
5831 ++selfdict->dv_refcount;
5832 }
5833 else
5834 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005835 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005836 {
5837 clear_tv(rettv);
5838 ret = FAIL;
5839 }
5840 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005841 else
5842 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005843 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005844
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005845 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5846 // Don't do this when "Func" is already a partial that was bound
5847 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005848 if (selfdict != NULL
5849 && (rettv->v_type == VAR_FUNC
5850 || (rettv->v_type == VAR_PARTIAL
5851 && (rettv->vval.v_partial->pt_auto
5852 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005853 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005854
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005855 dict_unref(selfdict);
5856 return ret;
5857}
5858
5859/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005860 * Make a copy of an item.
5861 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005862 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5863 * reference to an already copied list/dict can be used.
5864 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005865 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005866 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005867item_copy(
5868 typval_T *from,
5869 typval_T *to,
5870 int deep,
5871 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005872{
5873 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005874 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005875
Bram Moolenaar33570922005-01-25 22:26:29 +00005876 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005877 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005878 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005879 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005880 }
5881 ++recurse;
5882
5883 switch (from->v_type)
5884 {
5885 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005886 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005887 case VAR_STRING:
5888 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005889 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005890 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005891 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005892 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005893 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005894 copy_tv(from, to);
5895 break;
5896 case VAR_LIST:
5897 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005898 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005899 if (from->vval.v_list == NULL)
5900 to->vval.v_list = NULL;
5901 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005903 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005904 to->vval.v_list = from->vval.v_list->lv_copylist;
5905 ++to->vval.v_list->lv_refcount;
5906 }
5907 else
5908 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5909 if (to->vval.v_list == NULL)
5910 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005911 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005912 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005913 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005914 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005915 case VAR_DICT:
5916 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005917 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005918 if (from->vval.v_dict == NULL)
5919 to->vval.v_dict = NULL;
5920 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5921 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005922 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005923 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5924 ++to->vval.v_dict->dv_refcount;
5925 }
5926 else
5927 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5928 if (to->vval.v_dict == NULL)
5929 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005930 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005931 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005932 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005933 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005934 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005935 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005936 }
5937 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005938 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005939}
5940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941 void
5942echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5943{
5944 char_u *tofree;
5945 char_u numbuf[NUMBUFLEN];
5946 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5947
5948 if (*atstart)
5949 {
5950 *atstart = FALSE;
5951 // Call msg_start() after eval1(), evaluating the expression
5952 // may cause a message to appear.
5953 if (with_space)
5954 {
5955 // Mark the saved text as finishing the line, so that what
5956 // follows is displayed on a new line when scrolling back
5957 // at the more prompt.
5958 msg_sb_eol();
5959 msg_start();
5960 }
5961 }
5962 else if (with_space)
5963 msg_puts_attr(" ", echo_attr);
5964
5965 if (p != NULL)
5966 for ( ; *p != NUL && !got_int; ++p)
5967 {
5968 if (*p == '\n' || *p == '\r' || *p == TAB)
5969 {
5970 if (*p != TAB && *needclr)
5971 {
5972 // remove any text still there from the command
5973 msg_clr_eos();
5974 *needclr = FALSE;
5975 }
5976 msg_putchar_attr(*p, echo_attr);
5977 }
5978 else
5979 {
5980 if (has_mbyte)
5981 {
5982 int i = (*mb_ptr2len)(p);
5983
5984 (void)msg_outtrans_len_attr(p, i, echo_attr);
5985 p += i - 1;
5986 }
5987 else
5988 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5989 }
5990 }
5991 vim_free(tofree);
5992}
5993
Bram Moolenaare9a41262005-01-15 22:18:47 +00005994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 * ":echo expr1 ..." print each argument separated with a space, add a
5996 * newline at the end.
5997 * ":echon expr1 ..." print each argument plain.
5998 */
5999 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006000ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001{
6002 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 char_u *p;
6005 int needclr = TRUE;
6006 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006007 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006008 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006009 evalarg_T evalarg;
6010
Bram Moolenaare707c882020-07-01 13:07:37 +02006011 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012
6013 if (eap->skip)
6014 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006015 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006017 // If eval1() causes an error message the text from the command may
6018 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006019 need_clr_eos = needclr;
6020
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006022 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 {
6024 /*
6025 * Report the invalid expression unless the expression evaluation
6026 * has been cancelled due to an aborting error, an interrupt, or an
6027 * exception.
6028 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006029 if (!aborting() && did_emsg == did_emsg_before
6030 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006031 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006032 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 break;
6034 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006035 need_clr_eos = FALSE;
6036
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006038 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006039
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006040 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 arg = skipwhite(arg);
6042 }
6043 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006044 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045
6046 if (eap->skip)
6047 --emsg_skip;
6048 else
6049 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006050 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 if (needclr)
6052 msg_clr_eos();
6053 if (eap->cmdidx == CMD_echo)
6054 msg_end();
6055 }
6056}
6057
6058/*
6059 * ":echohl {name}".
6060 */
6061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006062ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006064 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065}
6066
6067/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006068 * Returns the :echo attribute
6069 */
6070 int
6071get_echo_attr(void)
6072{
6073 return echo_attr;
6074}
6075
6076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 * ":execute expr1 ..." execute the result of an expression.
6078 * ":echomsg expr1 ..." Print a message
6079 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006080 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 * Each gets spaces around each argument and a newline at the end for
6082 * echo commands
6083 */
6084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006085ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
6087 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 int ret = OK;
6090 char_u *p;
6091 garray_T ga;
6092 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093
6094 ga_init2(&ga, 1, 80);
6095
6096 if (eap->skip)
6097 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006098 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006100 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006101 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
6104 if (!eap->skip)
6105 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006106 char_u buf[NUMBUFLEN];
6107
6108 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006109 {
6110 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6111 {
6112 emsg(_(e_inval_string));
6113 p = NULL;
6114 }
6115 else
6116 p = tv_get_string_buf(&rettv, buf);
6117 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006118 else
6119 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006120 if (p == NULL)
6121 {
6122 clear_tv(&rettv);
6123 ret = FAIL;
6124 break;
6125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 len = (int)STRLEN(p);
6127 if (ga_grow(&ga, len + 2) == FAIL)
6128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006129 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 ret = FAIL;
6131 break;
6132 }
6133 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 ga.ga_len += len;
6137 }
6138
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006139 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 arg = skipwhite(arg);
6141 }
6142
6143 if (ret != FAIL && ga.ga_data != NULL)
6144 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006145 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6146 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006147 // Mark the already saved text as finishing the line, so that what
6148 // follows is displayed on a new line when scrolling back at the
6149 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006150 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006151 }
6152
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006154 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006155 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006156 out_flush();
6157 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006158 else if (eap->cmdidx == CMD_echoconsole)
6159 {
6160 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6161 ui_write((char_u *)"\r\n", 2, TRUE);
6162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 else if (eap->cmdidx == CMD_echoerr)
6164 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006165 int save_did_emsg = did_emsg;
6166
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006167 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006168 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 if (!force_abort)
6170 did_emsg = save_did_emsg;
6171 }
6172 else if (eap->cmdidx == CMD_execute)
6173 do_cmdline((char_u *)ga.ga_data,
6174 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6175 }
6176
6177 ga_clear(&ga);
6178
6179 if (eap->skip)
6180 --emsg_skip;
6181
6182 eap->nextcmd = check_nextcmd(arg);
6183}
6184
6185/*
6186 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6187 * "arg" points to the "&" or '+' when called, to "option" when returning.
6188 * Returns NULL when no option name found. Otherwise pointer to the char
6189 * after the option name.
6190 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006191 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006192find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
6194 char_u *p = *arg;
6195
6196 ++p;
6197 if (*p == 'g' && p[1] == ':')
6198 {
6199 *opt_flags = OPT_GLOBAL;
6200 p += 2;
6201 }
6202 else if (*p == 'l' && p[1] == ':')
6203 {
6204 *opt_flags = OPT_LOCAL;
6205 p += 2;
6206 }
6207 else
6208 *opt_flags = 0;
6209
6210 if (!ASCII_ISALPHA(*p))
6211 return NULL;
6212 *arg = p;
6213
6214 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006215 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 else
6217 while (ASCII_ISALPHA(*p))
6218 ++p;
6219 return p;
6220}
6221
6222/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006223 * Display script name where an item was last set.
6224 * Should only be invoked when 'verbose' is non-zero.
6225 */
6226 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006227last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006228{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006229 char_u *p;
6230
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006231 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006232 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006233 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006234 if (p != NULL)
6235 {
6236 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006237 msg_puts(_("\n\tLast set from "));
6238 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006239 if (script_ctx.sc_lnum > 0)
6240 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006241 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006242 msg_outnum((long)script_ctx.sc_lnum);
6243 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006244 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006245 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006246 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006247 }
6248}
6249
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006250#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251
6252/*
6253 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006254 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 * "flags" can be "g" to do a global substitute.
6256 * Returns an allocated string, NULL for error.
6257 */
6258 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006259do_string_sub(
6260 char_u *str,
6261 char_u *pat,
6262 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006263 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006264 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265{
6266 int sublen;
6267 regmatch_T regmatch;
6268 int i;
6269 int do_all;
6270 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006271 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 garray_T ga;
6273 char_u *ret;
6274 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006275 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006277 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006279 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
6281 ga_init2(&ga, 1, 200);
6282
6283 do_all = (flags[0] == 'g');
6284
6285 regmatch.rm_ic = p_ic;
6286 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6287 if (regmatch.regprog != NULL)
6288 {
6289 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006290 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6292 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006293 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006294 if (regmatch.startp[0] == regmatch.endp[0])
6295 {
6296 if (zero_width == regmatch.startp[0])
6297 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006298 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006299 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006300 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6301 (size_t)i);
6302 ga.ga_len += i;
6303 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006304 continue;
6305 }
6306 zero_width = regmatch.startp[0];
6307 }
6308
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 /*
6310 * Get some space for a temporary buffer to do the substitution
6311 * into. It will contain:
6312 * - The text up to where the match is.
6313 * - The substituted text.
6314 * - The text after the match.
6315 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006316 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006317 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6319 {
6320 ga_clear(&ga);
6321 break;
6322 }
6323
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006324 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 i = (int)(regmatch.startp[0] - tail);
6326 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006327 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006328 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 + ga.ga_len + i, TRUE, TRUE, FALSE);
6330 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006331 tail = regmatch.endp[0];
6332 if (*tail == NUL)
6333 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 if (!do_all)
6335 break;
6336 }
6337
6338 if (ga.ga_data != NULL)
6339 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6340
Bram Moolenaar473de612013-06-08 18:19:48 +02006341 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 }
6343
6344 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6345 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006346 if (p_cpo == empty_option)
6347 p_cpo = save_cpo;
6348 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006349 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006350 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006351 // If it's still empty it was changed and restored, need to restore in
6352 // the complicated way.
6353 if (*p_cpo == NUL)
6354 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006355 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357
6358 return ret;
6359}