blob: 6f89030a6069fedf711b4b71932f8b2227849784 [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 Moolenaar9ef486d2005-01-17 22:23:00 +000023static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010025#define NAMESPACE_CHAR (char_u *)"abglstvw"
26
Bram Moolenaar532c7802005-01-27 14:44:31 +000027/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000028 * When recursively copying lists and dicts we need to remember which ones we
29 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000030 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000031 */
32static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020033
Bram Moolenaar071d4272004-06-13 20:20:40 +000034/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000035 * Info used by a ":for" loop.
36 */
Bram Moolenaar33570922005-01-25 22:26:29 +000037typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000038{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010039 int fi_semicolon; // TRUE if ending in '; var]'
40 int fi_varcount; // nr of variables in the list
41 listwatch_T fi_lw; // keep an eye on the item used.
42 list_T *fi_list; // list being used
43 int fi_bi; // index of blob
44 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000045} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000046
Bram Moolenaar48e697e2016-01-23 22:17:30 +010047static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar32e35112020-05-14 22:41:15 +020048static int eval2(char_u **arg, typval_T *rettv, int flags);
49static int eval3(char_u **arg, typval_T *rettv, int flags);
50static int eval4(char_u **arg, typval_T *rettv, int flags);
51static int eval5(char_u **arg, typval_T *rettv, int flags);
52static int eval6(char_u **arg, typval_T *rettv, int flags, int want_string);
53static int eval7(char_u **arg, typval_T *rettv, int flags, int want_string);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +020054static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000055
Bram Moolenaar48e697e2016-01-23 22:17:30 +010056static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020057static 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 +020058
Bram Moolenaare21c1582019-03-02 11:57:09 +010059/*
60 * Return "n1" divided by "n2", taking care of dividing by zero.
61 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020062 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010063num_divide(varnumber_T n1, varnumber_T n2)
64{
65 varnumber_T result;
66
67 if (n2 == 0) // give an error message?
68 {
69 if (n1 == 0)
70 result = VARNUM_MIN; // similar to NaN
71 else if (n1 < 0)
72 result = -VARNUM_MAX;
73 else
74 result = VARNUM_MAX;
75 }
76 else
77 result = n1 / n2;
78
79 return result;
80}
81
82/*
83 * Return "n1" modulus "n2", taking care of dividing by zero.
84 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010086num_modulus(varnumber_T n1, varnumber_T n2)
87{
88 // Give an error when n2 is 0?
89 return (n2 == 0) ? 0 : (n1 % n2);
90}
91
Bram Moolenaara1fa8922017-01-12 20:06:33 +010092#if defined(EBCDIC) || defined(PROTO)
93/*
94 * Compare struct fst by function name.
95 */
96 static int
97compare_func_name(const void *s1, const void *s2)
98{
99 struct fst *p1 = (struct fst *)s1;
100 struct fst *p2 = (struct fst *)s2;
101
102 return STRCMP(p1->f_name, p2->f_name);
103}
104
105/*
106 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100107 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100108 * On machines using EBCDIC we have to sort it.
109 */
110 static void
111sortFunctions(void)
112{
113 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
114
115 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
116}
117#endif
118
Bram Moolenaar33570922005-01-25 22:26:29 +0000119/*
120 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000121 */
122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100123eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000124{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200125 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200126 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000127
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200128#ifdef EBCDIC
129 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100130 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200131 */
132 sortFunctions();
133#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000134}
135
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000136#if defined(EXITFREE) || defined(PROTO)
137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100138eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000139{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200140 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100141 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200142 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000143
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200144 // autoloaded script names
145 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000146
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200147 // unreferenced lists and dicts
148 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200149
150 // functions not garbage collected
151 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000152}
153#endif
154
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155/*
156 * Top level evaluation function, returning a boolean.
157 * Sets "error" to TRUE if there was an error.
158 * Return TRUE or FALSE.
159 */
160 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100161eval_to_bool(
162 char_u *arg,
163 int *error,
164 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100165 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166{
Bram Moolenaar33570922005-01-25 22:26:29 +0000167 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200168 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
170 if (skip)
171 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200172 if (eval0(arg, &tv, nextcmd, skip ? 0 : EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 else
175 {
176 *error = FALSE;
177 if (!skip)
178 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
185
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200186 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187}
188
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100189/*
190 * Call eval1() and give an error message if not done at a lower level.
191 */
192 static int
193eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
194{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100195 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100196 int ret;
197 int did_emsg_before = did_emsg;
198 int called_emsg_before = called_emsg;
199
Bram Moolenaar32e35112020-05-14 22:41:15 +0200200 ret = eval1(arg, rettv, evaluate ? EVAL_EVALUATE : 0);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201 if (ret == FAIL)
202 {
203 // Report the invalid expression unless the expression evaluation has
204 // been cancelled due to an aborting error, an interrupt, or an
205 // exception, or we already gave a more specific error.
206 // Also check called_emsg for when using assert_fails().
207 if (!aborting() && did_emsg == did_emsg_before
208 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100209 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100210 }
211 return ret;
212}
213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214/*
215 * Evaluate an expression, which can be a function, partial or string.
216 * Pass arguments "argv[argc]".
217 * Return the result in "rettv" and OK or FAIL.
218 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200219 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100220eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
221{
222 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100223 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200224 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100225
226 if (expr->v_type == VAR_FUNC)
227 {
228 s = expr->vval.v_string;
229 if (s == NULL || *s == NUL)
230 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200231 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200232 funcexe.evaluate = TRUE;
233 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100234 return FAIL;
235 }
236 else if (expr->v_type == VAR_PARTIAL)
237 {
238 partial_T *partial = expr->vval.v_partial;
239
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200240 if (partial == NULL)
241 return FAIL;
242
Bram Moolenaar822ba242020-05-24 23:00:18 +0200243 if (partial->pt_func != NULL
244 && partial->pt_func->uf_dfunc_idx != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200246 if (call_def_function(partial->pt_func, argc, argv,
247 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 return FAIL;
249 }
250 else
251 {
252 s = partial_name(partial);
253 if (s == NULL || *s == NUL)
254 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200255 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 funcexe.evaluate = TRUE;
257 funcexe.partial = partial;
258 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
259 return FAIL;
260 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100261 }
262 else
263 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100264 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100265 if (s == NULL)
266 return FAIL;
267 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100268 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100269 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100270 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100271 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200272 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100273 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100274 return FAIL;
275 }
276 }
277 return OK;
278}
279
280/*
281 * Like eval_to_bool() but using a typval_T instead of a string.
282 * Works for string, funcref and partial.
283 */
284 int
285eval_expr_to_bool(typval_T *expr, int *error)
286{
287 typval_T rettv;
288 int res;
289
290 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
291 {
292 *error = TRUE;
293 return FALSE;
294 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100295 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100296 clear_tv(&rettv);
297 return res;
298}
299
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300/*
301 * Top level evaluation function, returning a string. If "skip" is TRUE,
302 * only parsing to "nextcmd" is done, without reporting errors. Return
303 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
304 */
305 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100306eval_to_string_skip(
307 char_u *arg,
308 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100309 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310{
Bram Moolenaar33570922005-01-25 22:26:29 +0000311 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 char_u *retval;
313
314 if (skip)
315 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200316 if (eval0(arg, &tv, nextcmd, skip ? 0 : EVAL_EVALUATE) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 retval = NULL;
318 else
319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100320 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000321 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 }
323 if (skip)
324 --emsg_skip;
325
326 return retval;
327}
328
329/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000330 * Skip over an expression at "*pp".
331 * Return FAIL for an error, OK otherwise.
332 */
333 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100334skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000335{
Bram Moolenaar33570922005-01-25 22:26:29 +0000336 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000337
338 *pp = skipwhite(*pp);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200339 return eval1(pp, &rettv, 0);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000340}
341
342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000344 * When "convert" is TRUE convert a List into a sequence of lines and convert
345 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 * Return pointer to allocated memory, or NULL for failure.
347 */
348 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100349eval_to_string(
350 char_u *arg,
351 char_u **nextcmd,
352 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353{
Bram Moolenaar33570922005-01-25 22:26:29 +0000354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000356 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000357#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar32e35112020-05-14 22:41:15 +0200361 if (eval0(arg, &tv, nextcmd, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 retval = NULL;
363 else
364 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000365 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000366 {
367 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000368 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200369 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200370 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200371 if (tv.vval.v_list->lv_len > 0)
372 ga_append(&ga, NL);
373 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000374 ga_append(&ga, NUL);
375 retval = (char_u *)ga.ga_data;
376 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000377#ifdef FEAT_FLOAT
378 else if (convert && tv.v_type == VAR_FLOAT)
379 {
380 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
381 retval = vim_strsave(numbuf);
382 }
383#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000384 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100385 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 }
388
389 return retval;
390}
391
392/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000393 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200394 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 */
396 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100397eval_to_string_safe(
398 char_u *arg,
399 char_u **nextcmd,
400 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
402 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200403 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200405 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000406 if (use_sandbox)
407 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200408 ++textwinlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000409 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000410 if (use_sandbox)
411 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200412 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200413 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 return retval;
415}
416
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417/*
418 * Top level evaluation function, returning a number.
419 * Evaluates "expr" silently.
420 * Returns -1 for an error.
421 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200422 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100423eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424{
Bram Moolenaar33570922005-01-25 22:26:29 +0000425 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200426 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000427 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428
429 ++emsg_off;
430
Bram Moolenaar32e35112020-05-14 22:41:15 +0200431 if (eval1(&p, &rettv, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 retval = -1;
433 else
434 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100435 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000436 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 }
438 --emsg_off;
439
440 return retval;
441}
442
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000443/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000444 * Top level evaluation function.
445 * Returns an allocated typval_T with the result.
446 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000447 */
448 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100449eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000450{
451 typval_T *tv;
452
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200453 tv = ALLOC_ONE(typval_T);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200454 if (tv != NULL && eval0(arg, tv, nextcmd, EVAL_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100455 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000456
457 return tv;
458}
459
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100461 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200462 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
463 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000464 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200466 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100467call_vim_function(
468 char_u *func,
469 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200470 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200471 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000473 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200474 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100476 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200477 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200478 funcexe.firstline = curwin->w_cursor.lnum;
479 funcexe.lastline = curwin->w_cursor.lnum;
480 funcexe.evaluate = TRUE;
481 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000482 if (ret == FAIL)
483 clear_tv(rettv);
484
485 return ret;
486}
487
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100488/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100489 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100490 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200491 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
492 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100493 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200494 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100495call_func_retnr(
496 char_u *func,
497 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200498 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100499{
500 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200501 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100502
Bram Moolenaarded27a12018-08-01 19:06:03 +0200503 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100504 return -1;
505
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100506 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100507 clear_tv(&rettv);
508 return retval;
509}
510
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000511/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100512 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000513 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200514 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
515 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000516 */
517 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100518call_func_retstr(
519 char_u *func,
520 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200521 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000522{
523 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000524 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000525
Bram Moolenaarded27a12018-08-01 19:06:03 +0200526 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000527 return NULL;
528
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100529 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000530 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 return retval;
532}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000533
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000534/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100535 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200536 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
537 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000538 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000539 */
540 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100541call_func_retlist(
542 char_u *func,
543 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200544 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000545{
546 typval_T rettv;
547
Bram Moolenaarded27a12018-08-01 19:06:03 +0200548 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000549 return NULL;
550
551 if (rettv.v_type != VAR_LIST)
552 {
553 clear_tv(&rettv);
554 return NULL;
555 }
556
557 return rettv.vval.v_list;
558}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#ifdef FEAT_FOLDING
561/*
562 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
563 * it in "*cp". Doesn't give error messages.
564 */
565 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100566eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
Bram Moolenaar33570922005-01-25 22:26:29 +0000568 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200569 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000571 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
572 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000575 if (use_sandbox)
576 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200577 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 *cp = NUL;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200579 if (eval0(arg, &tv, NULL, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 retval = 0;
581 else
582 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100583 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000584 if (tv.v_type == VAR_NUMBER)
585 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000586 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 retval = 0;
588 else
589 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100590 // If the result is a string, check if there is a non-digit before
591 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000592 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 if (!VIM_ISDIGIT(*s) && *s != '-')
594 *cp = *s++;
595 retval = atol((char *)s);
596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000597 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 }
599 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000600 if (use_sandbox)
601 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200602 --textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200604 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605}
606#endif
607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000609 * Get an lval: variable, Dict item or List item that can be assigned a value
610 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
611 * "name.key", "name.key[expr]" etc.
612 * Indexing only works if "name" is an existing List or Dictionary.
613 * "name" points to the start of the name.
614 * If "rettv" is not NULL it points to the value to be assigned.
615 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
616 * wrong; must end in space or cmd separator.
617 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100618 * flags:
619 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100620 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100621 * GLV_NO_AUTOLOAD: do not use script autoloading
622 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000623 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000624 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000625 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000626 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200627 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100628get_lval(
629 char_u *name,
630 typval_T *rettv,
631 lval_T *lp,
632 int unlet,
633 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100634 int flags, // GLV_ values
635 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000636{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000637 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000638 char_u *expr_start, *expr_end;
639 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000640 dictitem_T *v;
641 typval_T var1;
642 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000643 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000644 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000645 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000646 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000647 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100648 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100650 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200651 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000652
653 if (skip)
654 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100655 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000656 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000657 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000658 }
659
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100660 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000661 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100662 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000663 if (expr_start != NULL)
664 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100665 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100666 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000667 && *p != '[' && *p != '.')
668 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100669 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000670 return NULL;
671 }
672
673 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
674 if (lp->ll_exp_name == NULL)
675 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100676 // Report an invalid expression in braces, unless the
677 // expression evaluation has been cancelled due to an
678 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000679 if (!aborting() && !quiet)
680 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000681 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100682 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000683 return NULL;
684 }
685 }
686 lp->ll_name = lp->ll_exp_name;
687 }
688 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000690 lp->ll_name = name;
691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
693 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100694 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 char_u *tp = skipwhite(p + 1);
696
697 // parse the type after the name
698 lp->ll_type = parse_type(&tp, &si->sn_type_list);
699 lp->ll_name_end = tp;
700 }
701 }
702
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100703 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000704 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
705 return p;
706
707 cc = *p;
708 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100709 // Only pass &ht when we would write to the variable, it prevents autoload
710 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100711 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
712 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000713 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100714 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000715 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000716 if (v == NULL)
717 return NULL;
718
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000719 /*
720 * Loop until no more [idx] or .key is following.
721 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000722 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100723 var1.v_type = VAR_UNKNOWN;
724 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000725 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000727 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
728 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100729 && lp->ll_tv->vval.v_dict != NULL)
730 && !(lp->ll_tv->v_type == VAR_BLOB
731 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000733 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100734 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000735 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000736 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000737 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000738 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000739 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100740 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000741 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000742 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000743
Bram Moolenaar8c711452005-01-14 21:53:12 +0000744 len = -1;
745 if (*p == '.')
746 {
747 key = p + 1;
748 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
749 ;
750 if (len == 0)
751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000752 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100753 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000754 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000755 }
756 p = key + len;
757 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000758 else
759 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100760 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000761 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000762 if (*p == ':')
763 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000764 else
765 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000766 empty1 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200767 if (eval1(&p, &var1, EVAL_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000768 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100769 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100771 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000772 clear_tv(&var1);
773 return NULL;
774 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000775 }
776
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100777 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000778 if (*p == ':')
779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000780 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000781 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000782 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100783 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100784 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000785 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000786 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100787 if (rettv != NULL
788 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100789 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100790 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100791 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000792 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000793 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100794 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100795 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000796 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000797 }
798 p = skipwhite(p + 1);
799 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000800 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000801 else
802 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000803 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200804 // recursive!
805 if (eval1(&p, &var2, EVAL_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000806 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100807 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000809 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100810 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100812 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100813 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000814 clear_tv(&var2);
815 return NULL;
816 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000817 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000818 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000820 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000822
Bram Moolenaar8c711452005-01-14 21:53:12 +0000823 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000824 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100826 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100827 clear_tv(&var1);
828 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000829 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000830 }
831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100832 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000833 ++p;
834 }
835
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000837 {
838 if (len == -1)
839 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100840 // "[key]": get key from "var1"
841 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200842 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000843 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000844 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000845 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000846 }
847 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000848 lp->ll_list = NULL;
849 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000850 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 // When assigning to a scope dictionary check that a function and
853 // variable name is valid (only variable name unless it is l: or
854 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200855 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200856 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200857 int prevval;
858 int wrong;
859
860 if (len != -1)
861 {
862 prevval = key[len];
863 key[len] = NUL;
864 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200865 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100866 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200867 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
868 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200869 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200870 || !valid_varname(key);
871 if (len != -1)
872 key[len] = prevval;
873 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200874 {
875 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200876 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200877 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200878 }
879
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000881 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100882 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200883 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100884 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200885 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100886 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100887 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200888 return NULL;
889 }
890
Bram Moolenaar31b81602019-02-10 22:14:27 +0100891 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000892 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000893 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100895 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100896 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000898 }
899 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000901 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000902 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100903 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000904 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000905 p = NULL;
906 break;
907 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100908 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100909 else if ((flags & GLV_READ_ONLY) == 0
910 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100911 {
912 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200913 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100914 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200915
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100916 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000918 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100919 else if (lp->ll_tv->v_type == VAR_BLOB)
920 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100921 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
922
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100923 /*
924 * Get the number and item for the only or first index of the List.
925 */
926 if (empty1)
927 lp->ll_n1 = 0;
928 else
929 // is number or string
930 lp->ll_n1 = (long)tv_get_number(&var1);
931 clear_tv(&var1);
932
933 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100934 || lp->ll_n1 > bloblen
935 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100936 {
937 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100938 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100939 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100940 return NULL;
941 }
942 if (lp->ll_range && !lp->ll_empty2)
943 {
944 lp->ll_n2 = (long)tv_get_number(&var2);
945 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100946 if (lp->ll_n2 < 0
947 || lp->ll_n2 >= bloblen
948 || lp->ll_n2 < lp->ll_n1)
949 {
950 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100951 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100952 return NULL;
953 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100954 }
955 lp->ll_blob = lp->ll_tv->vval.v_blob;
956 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100957 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100958 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000959 else
960 {
961 /*
962 * Get the number and item for the only or first index of the List.
963 */
964 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000966 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100967 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100968 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100969 clear_tv(&var1);
970
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000971 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 lp->ll_list = lp->ll_tv->vval.v_list;
973 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
974 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000976 if (lp->ll_n1 < 0)
977 {
978 lp->ll_n1 = 0;
979 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
980 }
981 }
982 if (lp->ll_li == NULL)
983 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100984 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200985 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100986 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 }
989
990 /*
991 * May need to find the item or absolute index for the second
992 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 * When no index given: "lp->ll_empty2" is TRUE.
994 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000995 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000997 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100998 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100999 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001001 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001003 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001004 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001005 {
1006 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001007 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001011 }
1012
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001013 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014 if (lp->ll_n1 < 0)
1015 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1016 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001017 {
1018 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001019 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001021 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001022 }
1023
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001024 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001025 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001026 }
1027
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001028 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 return p;
1031}
1032
1033/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001034 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001036 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001037clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038{
1039 vim_free(lp->ll_exp_name);
1040 vim_free(lp->ll_newkey);
1041}
1042
1043/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001044 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001046 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1047 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001050set_var_lval(
1051 lval_T *lp,
1052 char_u *endp,
1053 typval_T *rettv,
1054 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001056 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057{
1058 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001059 listitem_T *ri;
1060 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061
1062 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001063 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001064 cc = *endp;
1065 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001066 if (lp->ll_blob != NULL)
1067 {
1068 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001069
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001070 if (op != NULL && *op != '=')
1071 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001072 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001073 return;
1074 }
1075
1076 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1077 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001078 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001079
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001080 if (lp->ll_empty2)
1081 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1082
1083 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001085 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001086 return;
1087 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001088 if (lp->ll_empty2)
1089 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001090
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001091 ir = 0;
1092 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1093 blob_set(lp->ll_blob, il,
1094 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001095 }
1096 else
1097 {
1098 val = (int)tv_get_number_chk(rettv, &error);
1099 if (!error)
1100 {
1101 garray_T *gap = &lp->ll_blob->bv_ga;
1102
1103 // Allow for appending a byte. Setting a byte beyond
1104 // the end is an error otherwise.
1105 if (lp->ll_n1 < gap->ga_len
1106 || (lp->ll_n1 == gap->ga_len
1107 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1108 {
1109 blob_set(lp->ll_blob, lp->ll_n1, val);
1110 if (lp->ll_n1 == gap->ga_len)
1111 ++gap->ga_len;
1112 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001113 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001114 }
1115 }
1116 }
1117 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001118 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001119 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001122 {
1123 emsg(_(e_cannot_mod));
1124 *endp = cc;
1125 return;
1126 }
1127
Bram Moolenaarff697e62019-02-12 22:28:33 +01001128 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001129 di = NULL;
1130 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1131 &tv, &di, TRUE, FALSE) == OK)
1132 {
1133 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001134 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1135 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001136 && tv_op(&tv, rettv, op) == OK)
1137 set_var(lp->ll_name, &tv, FALSE);
1138 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001140 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001141 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001143 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001145 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001146 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001147 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001148 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 else if (lp->ll_range)
1150 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001151 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001152 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001153
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001155 {
1156 emsg(_("E996: Cannot lock a range"));
1157 return;
1158 }
1159
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001160 /*
1161 * Check whether any of the list items is locked
1162 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001163 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001164 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001165 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001166 return;
1167 ri = ri->li_next;
1168 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1169 break;
1170 ll_li = ll_li->li_next;
1171 ++ll_n1;
1172 }
1173
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001174 /*
1175 * Assign the List values to the list items.
1176 */
1177 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001178 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001179 if (op != NULL && *op != '=')
1180 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1181 else
1182 {
1183 clear_tv(&lp->ll_li->li_tv);
1184 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1185 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001186 ri = ri->li_next;
1187 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1188 break;
1189 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001190 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001191 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001192 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001193 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 ri = NULL;
1195 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001196 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001197 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198 lp->ll_li = lp->ll_li->li_next;
1199 ++lp->ll_n1;
1200 }
1201 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001202 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001203 else if (lp->ll_empty2
1204 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001206 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 }
1208 else
1209 {
1210 /*
1211 * Assign to a List or Dictionary item.
1212 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001214 {
1215 emsg(_("E996: Cannot lock a list or dict"));
1216 return;
1217 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 if (lp->ll_newkey != NULL)
1219 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001220 if (op != NULL && *op != '=')
1221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001222 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001223 return;
1224 }
1225
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001226 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001227 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 if (di == NULL)
1229 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001230 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1231 {
1232 vim_free(di);
1233 return;
1234 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001236 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001237 else if (op != NULL && *op != '=')
1238 {
1239 tv_op(lp->ll_tv, rettv, op);
1240 return;
1241 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001242 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 /*
1246 * Assign the value to the variable or list item.
1247 */
1248 if (copy)
1249 copy_tv(rettv, lp->ll_tv);
1250 else
1251 {
1252 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001253 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001255 }
1256 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001257}
1258
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001259/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001260 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1261 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001262 * Returns OK or FAIL.
1263 */
1264 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001265tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001266{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001267 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001268 char_u numbuf[NUMBUFLEN];
1269 char_u *s;
1270
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001271 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001272 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001273 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001274 {
1275 switch (tv1->v_type)
1276 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001277 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001278 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001280 case VAR_DICT:
1281 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001282 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001283 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001284 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001285 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001286 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001287 break;
1288
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001289 case VAR_BLOB:
1290 if (*op != '+' || tv2->v_type != VAR_BLOB)
1291 break;
1292 // BLOB += BLOB
1293 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1294 {
1295 blob_T *b1 = tv1->vval.v_blob;
1296 blob_T *b2 = tv2->vval.v_blob;
1297 int i, len = blob_len(b2);
1298 for (i = 0; i < len; i++)
1299 ga_append(&b1->bv_ga, blob_get(b2, i));
1300 }
1301 return OK;
1302
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001303 case VAR_LIST:
1304 if (*op != '+' || tv2->v_type != VAR_LIST)
1305 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001306 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001307 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1308 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1309 return OK;
1310
1311 case VAR_NUMBER:
1312 case VAR_STRING:
1313 if (tv2->v_type == VAR_LIST)
1314 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001315 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001316 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001317 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001318 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001319#ifdef FEAT_FLOAT
1320 if (tv2->v_type == VAR_FLOAT)
1321 {
1322 float_T f = n;
1323
Bram Moolenaarff697e62019-02-12 22:28:33 +01001324 if (*op == '%')
1325 break;
1326 switch (*op)
1327 {
1328 case '+': f += tv2->vval.v_float; break;
1329 case '-': f -= tv2->vval.v_float; break;
1330 case '*': f *= tv2->vval.v_float; break;
1331 case '/': f /= tv2->vval.v_float; break;
1332 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001333 clear_tv(tv1);
1334 tv1->v_type = VAR_FLOAT;
1335 tv1->vval.v_float = f;
1336 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001337 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001338#endif
1339 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001340 switch (*op)
1341 {
1342 case '+': n += tv_get_number(tv2); break;
1343 case '-': n -= tv_get_number(tv2); break;
1344 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001345 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1346 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001347 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001348 clear_tv(tv1);
1349 tv1->v_type = VAR_NUMBER;
1350 tv1->vval.v_number = n;
1351 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001352 }
1353 else
1354 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001355 if (tv2->v_type == VAR_FLOAT)
1356 break;
1357
Bram Moolenaarff697e62019-02-12 22:28:33 +01001358 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001359 s = tv_get_string(tv1);
1360 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001361 clear_tv(tv1);
1362 tv1->v_type = VAR_STRING;
1363 tv1->vval.v_string = s;
1364 }
1365 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001366
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001367 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001368#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001369 {
1370 float_T f;
1371
Bram Moolenaarff697e62019-02-12 22:28:33 +01001372 if (*op == '%' || *op == '.'
1373 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001374 && tv2->v_type != VAR_NUMBER
1375 && tv2->v_type != VAR_STRING))
1376 break;
1377 if (tv2->v_type == VAR_FLOAT)
1378 f = tv2->vval.v_float;
1379 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001380 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001381 switch (*op)
1382 {
1383 case '+': tv1->vval.v_float += f; break;
1384 case '-': tv1->vval.v_float -= f; break;
1385 case '*': tv1->vval.v_float *= f; break;
1386 case '/': tv1->vval.v_float /= f; break;
1387 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001388 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001389#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001390 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001391 }
1392 }
1393
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001394 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001395 return FAIL;
1396}
1397
1398/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001399 * Evaluate the expression used in a ":for var in expr" command.
1400 * "arg" points to "var".
1401 * Set "*errp" to TRUE for an error, FALSE otherwise;
1402 * Return a pointer that holds the info. Null when there is an error.
1403 */
1404 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001405eval_for_line(
1406 char_u *arg,
1407 int *errp,
1408 char_u **nextcmdp,
1409 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 typval_T tv;
1414 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001416 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001417
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001418 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001419 if (fi == NULL)
1420 return NULL;
1421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001423 if (expr == NULL)
1424 return fi;
1425
1426 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001427 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001428 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001430 return fi;
1431 }
1432
1433 if (skip)
1434 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001435 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, skip ? 0 : EVAL_EVALUATE)
1436 == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001437 {
1438 *errp = FALSE;
1439 if (!skip)
1440 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001441 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001442 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001443 l = tv.vval.v_list;
1444 if (l == NULL)
1445 {
1446 // a null list is like an empty list: do nothing
1447 clear_tv(&tv);
1448 }
1449 else
1450 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001452 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001454 // No need to increment the refcount, it's already set for
1455 // the list being used in "tv".
1456 fi->fi_list = l;
1457 list_add_watch(l, &fi->fi_lw);
1458 fi->fi_lw.lw_item = l->lv_first;
1459 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001460 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001461 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001462 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001463 fi->fi_bi = 0;
1464 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001465 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001466 typval_T btv;
1467
1468 // Make a copy, so that the iteration still works when the
1469 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001471 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001472 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001473 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001474 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475 else
1476 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001477 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001478 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001479 }
1480 }
1481 }
1482 if (skip)
1483 --emsg_skip;
1484
1485 return fi;
1486}
1487
1488/*
1489 * Use the first item in a ":for" list. Advance to the next.
1490 * Assign the values to the variable (list). "arg" points to the first one.
1491 * Return TRUE when a valid item was found, FALSE when at end of list or
1492 * something wrong.
1493 */
1494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001495next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001496{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001497 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001499 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1500 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001501 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001502
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001503 if (fi->fi_blob != NULL)
1504 {
1505 typval_T tv;
1506
1507 if (fi->fi_bi >= blob_len(fi->fi_blob))
1508 return FALSE;
1509 tv.v_type = VAR_NUMBER;
1510 tv.v_lock = VAR_FIXED;
1511 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1512 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001513 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001514 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001515 }
1516
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517 item = fi->fi_lw.lw_item;
1518 if (item == NULL)
1519 result = FALSE;
1520 else
1521 {
1522 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001523 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001524 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001525 }
1526 return result;
1527}
1528
1529/*
1530 * Free the structure used to store info used by ":for".
1531 */
1532 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001533free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001534{
Bram Moolenaar33570922005-01-25 22:26:29 +00001535 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001536
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001537 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001538 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001539 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001540 list_unref(fi->fi_list);
1541 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001542 if (fi != NULL && fi->fi_blob != NULL)
1543 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001544 vim_free(fi);
1545}
1546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001548set_context_for_expression(
1549 expand_T *xp,
1550 char_u *arg,
1551 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 int got_eq = FALSE;
1554 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001555 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001557 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001558 {
1559 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001560 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001562 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001563 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001564 {
1565 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001566 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001567 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001568 break;
1569 }
1570 return;
1571 }
1572 }
1573 else
1574 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1575 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 while ((xp->xp_pattern = vim_strpbrk(arg,
1577 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1578 {
1579 c = *xp->xp_pattern;
1580 if (c == '&')
1581 {
1582 c = xp->xp_pattern[1];
1583 if (c == '&')
1584 {
1585 ++xp->xp_pattern;
1586 xp->xp_context = cmdidx != CMD_let || got_eq
1587 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1588 }
1589 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001592 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1593 xp->xp_pattern += 2;
1594
1595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 else if (c == '$')
1598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001599 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 xp->xp_context = EXPAND_ENV_VARS;
1601 }
1602 else if (c == '=')
1603 {
1604 got_eq = TRUE;
1605 xp->xp_context = EXPAND_EXPRESSION;
1606 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001607 else if (c == '#'
1608 && xp->xp_context == EXPAND_EXPRESSION)
1609 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001610 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001611 break;
1612 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001613 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 && xp->xp_context == EXPAND_FUNCTIONS
1615 && vim_strchr(xp->xp_pattern, '(') == NULL)
1616 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001617 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 break;
1619 }
1620 else if (cmdidx != CMD_let || got_eq)
1621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001622 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1625 if (c == '\\' && xp->xp_pattern[1] != NUL)
1626 ++xp->xp_pattern;
1627 xp->xp_context = EXPAND_NOTHING;
1628 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001629 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001631 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1633 /* skip */ ;
1634 xp->xp_context = EXPAND_NOTHING;
1635 }
1636 else if (c == '|')
1637 {
1638 if (xp->xp_pattern[1] == '|')
1639 {
1640 ++xp->xp_pattern;
1641 xp->xp_context = EXPAND_EXPRESSION;
1642 }
1643 else
1644 xp->xp_context = EXPAND_COMMANDS;
1645 }
1646 else
1647 xp->xp_context = EXPAND_EXPRESSION;
1648 }
1649 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001650 // Doesn't look like something valid, expand as an expression
1651 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 arg = xp->xp_pattern;
1654 if (*arg != NUL)
1655 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1656 /* skip */ ;
1657 }
1658 xp->xp_pattern = arg;
1659}
1660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001662 * Return TRUE if "pat" matches "text".
1663 * Does not use 'cpo' and always uses 'magic'.
1664 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001665 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001666pattern_match(char_u *pat, char_u *text, int ic)
1667{
1668 int matches = FALSE;
1669 char_u *save_cpo;
1670 regmatch_T regmatch;
1671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001672 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001673 save_cpo = p_cpo;
1674 p_cpo = (char_u *)"";
1675 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1676 if (regmatch.regprog != NULL)
1677 {
1678 regmatch.rm_ic = ic;
1679 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1680 vim_regfree(regmatch.regprog);
1681 }
1682 p_cpo = save_cpo;
1683 return matches;
1684}
1685
1686/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001687 * Handle a name followed by "(". Both for just "name(arg)" and for
1688 * "expr->name(arg)".
1689 * Returns OK or FAIL.
1690 */
1691 static int
1692eval_func(
1693 char_u **arg, // points to "(", will be advanced
1694 char_u *name,
1695 int name_len,
1696 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001697 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001698 typval_T *basetv) // "expr" for "expr->name(arg)"
1699{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001700 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001701 char_u *s = name;
1702 int len = name_len;
1703 partial_T *partial;
1704 int ret = OK;
1705
1706 if (!evaluate)
1707 check_vars(s, len);
1708
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001709 // If "s" is the name of a variable of type VAR_FUNC
1710 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001711 s = deref_func_name(s, &len, &partial, !evaluate);
1712
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001713 // Need to make a copy, in case evaluating the arguments makes
1714 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001715 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001716 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001717 ret = FAIL;
1718 else
1719 {
1720 funcexe_T funcexe;
1721
1722 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001723 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001724 funcexe.firstline = curwin->w_cursor.lnum;
1725 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001726 funcexe.evaluate = evaluate;
1727 funcexe.partial = partial;
1728 funcexe.basetv = basetv;
1729 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1730 }
1731 vim_free(s);
1732
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001733 // If evaluate is FALSE rettv->v_type was not set in
1734 // get_func_tv, but it's needed in handle_subscript() to parse
1735 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001736 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1737 {
1738 rettv->vval.v_string = NULL;
1739 rettv->v_type = VAR_FUNC;
1740 }
1741
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001742 // Stop the expression evaluation when immediately
1743 // aborting on error, or when an interrupt occurred or
1744 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001745 if (evaluate && aborting())
1746 {
1747 if (ret == OK)
1748 clear_tv(rettv);
1749 ret = FAIL;
1750 }
1751 return ret;
1752}
1753
1754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001756 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1758 */
1759
1760/*
1761 * Handle zero level expression.
1762 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001764 * Note: "rettv.v_lock" is not set.
Bram Moolenaar32e35112020-05-14 22:41:15 +02001765 * "flags" has EVAL_EVALUATE and similar flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 * Return OK or FAIL.
1767 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001768 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001769eval0(
1770 char_u *arg,
1771 typval_T *rettv,
1772 char_u **nextcmd,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001773 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 int ret;
1776 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001777 int did_emsg_before = did_emsg;
1778 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780 p = skipwhite(arg);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001781 ret = eval1(&p, rettv, flags);
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001782 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 {
1784 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001785 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 /*
1787 * Report the invalid expression unless the expression evaluation has
1788 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001789 * exception, or we already gave a more specific error.
1790 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001792 if (!aborting()
1793 && did_emsg == did_emsg_before
1794 && called_emsg == called_emsg_before
1795 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001796 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 ret = FAIL;
1798 }
1799 if (nextcmd != NULL)
1800 *nextcmd = check_nextcmd(p);
1801
1802 return ret;
1803}
1804
1805/*
1806 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001807 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 *
1809 * "arg" must point to the first non-white of the expression.
1810 * "arg" is advanced to the next non-white after the recognized expression.
1811 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001812 * Note: "rettv.v_lock" is not set.
1813 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 * Return OK or FAIL.
1815 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001816 int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001817eval1(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
1819 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001820 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 /*
1823 * Get the first variable.
1824 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001825 if (eval2(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 return FAIL;
1827
1828 if ((*arg)[0] == '?')
1829 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001830 int evaluate = flags & EVAL_EVALUATE;
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 result = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001833 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001835 int error = FALSE;
1836
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001837 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001839 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001840 if (error)
1841 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
1843
1844 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001845 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 */
1847 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001848 if (eval1(arg, rettv, result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 return FAIL;
1850
1851 /*
1852 * Check for the ":".
1853 */
1854 if ((*arg)[0] != ':')
1855 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 return FAIL;
1860 }
1861
1862 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001863 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 */
1865 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001866 if (eval1(arg, &var2, !result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 {
1868 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 return FAIL;
1871 }
1872 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 }
1875
1876 return OK;
1877}
1878
1879/*
1880 * Handle first level expression:
1881 * expr2 || expr2 || expr2 logical OR
1882 *
1883 * "arg" must point to the first non-white of the expression.
1884 * "arg" is advanced to the next non-white after the recognized expression.
1885 *
1886 * Return OK or FAIL.
1887 */
1888 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001889eval2(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 long result;
1893 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001894 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896 /*
1897 * Get the first variable.
1898 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001899 if (eval3(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 return FAIL;
1901
1902 /*
1903 * Repeat until there is no following "||".
1904 */
1905 first = TRUE;
1906 result = FALSE;
1907 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1908 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001909 int evaluate = flags & EVAL_EVALUATE;
1910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (evaluate && first)
1912 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001913 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001916 if (error)
1917 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 first = FALSE;
1919 }
1920
1921 /*
1922 * Get the second variable.
1923 */
1924 *arg = skipwhite(*arg + 2);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001925 if (eval3(arg, &var2, !result ? flags : flags & ~EVAL_EVALUATE)
1926 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 return FAIL;
1928
1929 /*
1930 * Compute the result.
1931 */
1932 if (evaluate && !result)
1933 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001934 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001937 if (error)
1938 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940 if (evaluate)
1941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 rettv->v_type = VAR_NUMBER;
1943 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
1945 }
1946
1947 return OK;
1948}
1949
1950/*
1951 * Handle second level expression:
1952 * expr3 && expr3 && expr3 logical AND
1953 *
1954 * "arg" must point to the first non-white of the expression.
1955 * "arg" is advanced to the next non-white after the recognized expression.
1956 *
1957 * Return OK or FAIL.
1958 */
1959 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001960eval3(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961{
Bram Moolenaar33570922005-01-25 22:26:29 +00001962 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 long result;
1964 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001965 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966
1967 /*
1968 * Get the first variable.
1969 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001970 if (eval4(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 return FAIL;
1972
1973 /*
1974 * Repeat until there is no following "&&".
1975 */
1976 first = TRUE;
1977 result = TRUE;
1978 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1979 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001980 int evaluate = flags & EVAL_EVALUATE;
1981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (evaluate && first)
1983 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001984 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001987 if (error)
1988 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 first = FALSE;
1990 }
1991
1992 /*
1993 * Get the second variable.
1994 */
1995 *arg = skipwhite(*arg + 2);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001996 if (eval4(arg, &var2, result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 return FAIL;
1998
1999 /*
2000 * Compute the result.
2001 */
2002 if (evaluate && result)
2003 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002004 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002006 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002007 if (error)
2008 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010 if (evaluate)
2011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002012 rettv->v_type = VAR_NUMBER;
2013 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 }
2015 }
2016
2017 return OK;
2018}
2019
2020/*
2021 * Handle third level expression:
2022 * var1 == var2
2023 * var1 =~ var2
2024 * var1 != var2
2025 * var1 !~ var2
2026 * var1 > var2
2027 * var1 >= var2
2028 * var1 < var2
2029 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002030 * var1 is var2
2031 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 *
2033 * "arg" must point to the first non-white of the expression.
2034 * "arg" is advanced to the next non-white after the recognized expression.
2035 *
2036 * Return OK or FAIL.
2037 */
2038 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02002039eval4(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040{
Bram Moolenaar33570922005-01-25 22:26:29 +00002041 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 char_u *p;
2043 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002044 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
2048 /*
2049 * Get the first variable.
2050 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002051 if (eval5(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 return FAIL;
2053
2054 p = *arg;
2055 switch (p[0])
2056 {
2057 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002058 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002060 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 break;
2062 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002063 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002065 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 break;
2067 case '>': if (p[1] != '=')
2068 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002069 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 len = 1;
2071 }
2072 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002073 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 break;
2075 case '<': if (p[1] != '=')
2076 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002077 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 len = 1;
2079 }
2080 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002081 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002083 case 'i': if (p[1] == 's')
2084 {
2085 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2086 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002087 i = p[len];
2088 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002089 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002090 }
2091 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093
2094 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002095 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002097 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002099 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 if (p[len] == '?')
2101 {
2102 ic = TRUE;
2103 ++len;
2104 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002105 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 else if (p[len] == '#')
2107 {
2108 ic = FALSE;
2109 ++len;
2110 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002111 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 else
2113 ic = p_ic;
2114
2115 /*
2116 * Get the second variable.
2117 */
2118 *arg = skipwhite(p + len);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002119 if (eval5(arg, &var2, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 return FAIL;
2123 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002124 if (flags & EVAL_EVALUATE)
Bram Moolenaar31988702018-02-13 12:57:42 +01002125 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002126 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002127
2128 clear_tv(&var2);
2129 return ret;
2130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
2132
2133 return OK;
2134}
2135
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 void
2137eval_addblob(typval_T *tv1, typval_T *tv2)
2138{
2139 blob_T *b1 = tv1->vval.v_blob;
2140 blob_T *b2 = tv2->vval.v_blob;
2141 blob_T *b = blob_alloc();
2142 int i;
2143
2144 if (b != NULL)
2145 {
2146 for (i = 0; i < blob_len(b1); i++)
2147 ga_append(&b->bv_ga, blob_get(b1, i));
2148 for (i = 0; i < blob_len(b2); i++)
2149 ga_append(&b->bv_ga, blob_get(b2, i));
2150
2151 clear_tv(tv1);
2152 rettv_blob_set(tv1, b);
2153 }
2154}
2155
2156 int
2157eval_addlist(typval_T *tv1, typval_T *tv2)
2158{
2159 typval_T var3;
2160
2161 // concatenate Lists
2162 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2163 {
2164 clear_tv(tv1);
2165 clear_tv(tv2);
2166 return FAIL;
2167 }
2168 clear_tv(tv1);
2169 *tv1 = var3;
2170 return OK;
2171}
2172
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173/*
2174 * Handle fourth level expression:
2175 * + number addition
2176 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002177 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002178 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 *
2180 * "arg" must point to the first non-white of the expression.
2181 * "arg" is advanced to the next non-white after the recognized expression.
2182 *
2183 * Return OK or FAIL.
2184 */
2185 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02002186eval5(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187{
Bram Moolenaar33570922005-01-25 22:26:29 +00002188 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002190 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002191#ifdef FEAT_FLOAT
2192 float_T f1 = 0, f2 = 0;
2193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 char_u *s1, *s2;
2195 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2196 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002197 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
2199 /*
2200 * Get the first variable.
2201 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002202 if (eval6(arg, rettv, flags, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 return FAIL;
2204
2205 /*
2206 * Repeat computing, until no '+', '-' or '.' is following.
2207 */
2208 for (;;)
2209 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002210 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002212 concat = op == '.'
2213 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2214 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 break;
2216
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002217 if ((op != '+' || (rettv->v_type != VAR_LIST
2218 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002219#ifdef FEAT_FLOAT
2220 && (op == '.' || rettv->v_type != VAR_FLOAT)
2221#endif
2222 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002223 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002224 // For "list + ...", an illegal use of the first operand as
2225 // a number cannot be determined before evaluating the 2nd
2226 // operand: if this is also a list, all is ok.
2227 // For "something . ...", "something - ..." or "non-list + ...",
2228 // we know that the first operand needs to be a string or number
2229 // without evaluating the 2nd operand. So check before to avoid
2230 // side effects after an error.
Bram Moolenaar32e35112020-05-14 22:41:15 +02002231 if ((flags & EVAL_EVALUATE) && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002232 {
2233 clear_tv(rettv);
2234 return FAIL;
2235 }
2236 }
2237
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 /*
2239 * Get the second variable.
2240 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002241 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2242 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002244 if (eval6(arg, &var2, flags, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 return FAIL;
2248 }
2249
Bram Moolenaar32e35112020-05-14 22:41:15 +02002250 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
2252 /*
2253 * Compute the result.
2254 */
2255 if (op == '.')
2256 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002257 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002258 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002259 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002260 {
2261 clear_tv(rettv);
2262 clear_tv(&var2);
2263 return FAIL;
2264 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002265 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 clear_tv(rettv);
2267 rettv->v_type = VAR_STRING;
2268 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002270 else if (op == '+' && rettv->v_type == VAR_BLOB
2271 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002273 else if (op == '+' && rettv->v_type == VAR_LIST
2274 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002275 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002277 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 else
2280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002281 int error = FALSE;
2282
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002283#ifdef FEAT_FLOAT
2284 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002285 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002286 f1 = rettv->vval.v_float;
2287 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002288 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002289 else
2290#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002291 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002292 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002293 if (error)
2294 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002295 // This can only happen for "list + non-list". For
2296 // "non-list + ..." or "something - ...", we returned
2297 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002298 clear_tv(rettv);
2299 return FAIL;
2300 }
2301#ifdef FEAT_FLOAT
2302 if (var2.v_type == VAR_FLOAT)
2303 f1 = n1;
2304#endif
2305 }
2306#ifdef FEAT_FLOAT
2307 if (var2.v_type == VAR_FLOAT)
2308 {
2309 f2 = var2.vval.v_float;
2310 n2 = 0;
2311 }
2312 else
2313#endif
2314 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002315 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002316 if (error)
2317 {
2318 clear_tv(rettv);
2319 clear_tv(&var2);
2320 return FAIL;
2321 }
2322#ifdef FEAT_FLOAT
2323 if (rettv->v_type == VAR_FLOAT)
2324 f2 = n2;
2325#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002326 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002328
2329#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002330 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002331 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2332 {
2333 if (op == '+')
2334 f1 = f1 + f2;
2335 else
2336 f1 = f1 - f2;
2337 rettv->v_type = VAR_FLOAT;
2338 rettv->vval.v_float = f1;
2339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002341#endif
2342 {
2343 if (op == '+')
2344 n1 = n1 + n2;
2345 else
2346 n1 = n1 - n2;
2347 rettv->v_type = VAR_NUMBER;
2348 rettv->vval.v_number = n1;
2349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353 }
2354 return OK;
2355}
2356
2357/*
2358 * Handle fifth level expression:
2359 * * number multiplication
2360 * / number division
2361 * % number modulo
2362 *
2363 * "arg" must point to the first non-white of the expression.
2364 * "arg" is advanced to the next non-white after the recognized expression.
2365 *
2366 * Return OK or FAIL.
2367 */
2368 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002369eval6(
2370 char_u **arg,
2371 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002372 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002373 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374{
Bram Moolenaar33570922005-01-25 22:26:29 +00002375 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002377 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002378#ifdef FEAT_FLOAT
2379 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002380 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002381#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002382 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384 /*
2385 * Get the first variable.
2386 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002387 if (eval7(arg, rettv, flags, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 return FAIL;
2389
2390 /*
2391 * Repeat computing, until no '*', '/' or '%' is following.
2392 */
2393 for (;;)
2394 {
2395 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002396 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 break;
2398
Bram Moolenaar32e35112020-05-14 22:41:15 +02002399 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002401#ifdef FEAT_FLOAT
2402 if (rettv->v_type == VAR_FLOAT)
2403 {
2404 f1 = rettv->vval.v_float;
2405 use_float = TRUE;
2406 n1 = 0;
2407 }
2408 else
2409#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002410 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002411 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002412 if (error)
2413 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415 else
2416 n1 = 0;
2417
2418 /*
2419 * Get the second variable.
2420 */
2421 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002422 if (eval7(arg, &var2, flags, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 return FAIL;
2424
Bram Moolenaar32e35112020-05-14 22:41:15 +02002425 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002427#ifdef FEAT_FLOAT
2428 if (var2.v_type == VAR_FLOAT)
2429 {
2430 if (!use_float)
2431 {
2432 f1 = n1;
2433 use_float = TRUE;
2434 }
2435 f2 = var2.vval.v_float;
2436 n2 = 0;
2437 }
2438 else
2439#endif
2440 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002441 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002442 clear_tv(&var2);
2443 if (error)
2444 return FAIL;
2445#ifdef FEAT_FLOAT
2446 if (use_float)
2447 f2 = n2;
2448#endif
2449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
2451 /*
2452 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002453 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002455#ifdef FEAT_FLOAT
2456 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002458 if (op == '*')
2459 f1 = f1 * f2;
2460 else if (op == '/')
2461 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002462# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002463 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002464 if (f2 == 0.0)
2465 {
2466 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002467 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002468 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002469 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002470 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002471 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002472 }
2473 else
2474 f1 = f1 / f2;
2475# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002476 // We rely on the floating point library to handle divide
2477 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002478 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002479# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002482 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002484 return FAIL;
2485 }
2486 rettv->v_type = VAR_FLOAT;
2487 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
2489 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002492 if (op == '*')
2493 n1 = n1 * n2;
2494 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002495 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002497 n1 = num_modulus(n1, n2);
2498
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002499 rettv->v_type = VAR_NUMBER;
2500 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 }
2503 }
2504
2505 return OK;
2506}
2507
2508/*
2509 * Handle sixth level expression:
2510 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002511 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002512 * "string" string constant
2513 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 * &option-name option value
2515 * @r register contents
2516 * identifier variable value
2517 * function() function call
2518 * $VAR environment variable
2519 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002520 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002522 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002523 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 *
2525 * Also handle:
2526 * ! in front logical NOT
2527 * - in front unary minus
2528 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002529 * trailing [] subscript in String or List
2530 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002531 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 *
2533 * "arg" must point to the first non-white of the expression.
2534 * "arg" is advanced to the next non-white after the recognized expression.
2535 *
2536 * Return OK or FAIL.
2537 */
2538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002539eval7(
2540 char_u **arg,
2541 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002542 int flags,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002545 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 int len;
2547 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 char_u *start_leader, *end_leader;
2549 int ret = OK;
2550 char_u *alias;
2551
2552 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002553 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002554 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002559 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 */
2561 start_leader = *arg;
2562 while (**arg == '!' || **arg == '-' || **arg == '+')
2563 *arg = skipwhite(*arg + 1);
2564 end_leader = *arg;
2565
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002566 if (**arg == '.' && (!isdigit(*(*arg + 1))
2567#ifdef FEAT_FLOAT
2568 || current_sctx.sc_version < 2
2569#endif
2570 ))
2571 {
2572 semsg(_(e_invexpr2), *arg);
2573 ++*arg;
2574 return FAIL;
2575 }
2576
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 switch (**arg)
2578 {
2579 /*
2580 * Number constant.
2581 */
2582 case '0':
2583 case '1':
2584 case '2':
2585 case '3':
2586 case '4':
2587 case '5':
2588 case '6':
2589 case '7':
2590 case '8':
2591 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 break;
2594
2595 /*
2596 * String constant: "string".
2597 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 break;
2600
2601 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002602 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002605 break;
2606
2607 /*
2608 * List: [expr, expr]
2609 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002610 case '[': ret = get_list_tv(arg, rettv, flags, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 break;
2612
2613 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002614 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002615 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002616 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002617 {
2618 ++*arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002619 ret = eval_dict(arg, rettv, flags, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002620 }
2621 else
2622 ret = NOTDONE;
2623 break;
2624
2625 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002626 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002627 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002629 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2630 if (ret == NOTDONE)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002631 ret = eval_dict(arg, rettv, flags, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 break;
2633
2634 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002635 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002637 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 break;
2639
2640 /*
2641 * Environment variable: $VAR.
2642 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 break;
2645
2646 /*
2647 * Register contents: @r.
2648 */
2649 case '@': ++*arg;
2650 if (evaluate)
2651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002653 rettv->vval.v_string = get_reg_contents(**arg,
2654 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
2656 if (**arg != NUL)
2657 ++*arg;
2658 break;
2659
2660 /*
2661 * nested expression: (expression).
2662 */
2663 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002664 ret = eval1(arg, rettv, flags); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (**arg == ')')
2666 ++*arg;
2667 else if (ret == OK)
2668 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 emsg(_(e_missing_close));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 ret = FAIL;
2672 }
2673 break;
2674
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 break;
2677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678
2679 if (ret == NOTDONE)
2680 {
2681 /*
2682 * Must be a variable or function name.
2683 * Can also be a curly-braces kind of name: {expr}.
2684 */
2685 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002686 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (alias != NULL)
2688 s = alias;
2689
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002690 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 ret = FAIL;
2692 else
2693 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002694 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002695 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002696 else if (flags & EVAL_CONSTANT)
2697 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002699 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002700 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002701 {
2702 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002703 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002706 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 }
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 *arg = skipwhite(*arg);
2710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002711 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2712 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002713 if (ret == OK)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002714 ret = handle_subscript(arg, rettv, flags, TRUE,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002715 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717 /*
2718 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2719 */
2720 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002721 ret = eval7_leader(rettv, start_leader, &end_leader);
2722 return ret;
2723}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002724
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002725/*
2726 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2727 * Adjusts "end_leaderp" until it is at "start_leader".
2728 */
2729 static int
2730eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2731{
2732 char_u *end_leader = *end_leaderp;
2733 int ret = OK;
2734 int error = FALSE;
2735 varnumber_T val = 0;
2736#ifdef FEAT_FLOAT
2737 float_T f = 0.0;
2738
2739 if (rettv->v_type == VAR_FLOAT)
2740 f = rettv->vval.v_float;
2741 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002742#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002743 val = tv_get_number_chk(rettv, &error);
2744 if (error)
2745 {
2746 clear_tv(rettv);
2747 ret = FAIL;
2748 }
2749 else
2750 {
2751 while (end_leader > start_leader)
2752 {
2753 --end_leader;
2754 if (*end_leader == '!')
2755 {
2756#ifdef FEAT_FLOAT
2757 if (rettv->v_type == VAR_FLOAT)
2758 f = !f;
2759 else
2760#endif
2761 val = !val;
2762 }
2763 else if (*end_leader == '-')
2764 {
2765#ifdef FEAT_FLOAT
2766 if (rettv->v_type == VAR_FLOAT)
2767 f = -f;
2768 else
2769#endif
2770 val = -val;
2771 }
2772 }
2773#ifdef FEAT_FLOAT
2774 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002776 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002777 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002779 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002780#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002781 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002782 clear_tv(rettv);
2783 rettv->v_type = VAR_NUMBER;
2784 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002787 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 return ret;
2789}
2790
2791/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002792 * Call the function referred to in "rettv".
2793 */
2794 static int
2795call_func_rettv(
2796 char_u **arg,
2797 typval_T *rettv,
2798 int evaluate,
2799 dict_T *selfdict,
2800 typval_T *basetv)
2801{
2802 partial_T *pt = NULL;
2803 funcexe_T funcexe;
2804 typval_T functv;
2805 char_u *s;
2806 int ret;
2807
2808 // need to copy the funcref so that we can clear rettv
2809 if (evaluate)
2810 {
2811 functv = *rettv;
2812 rettv->v_type = VAR_UNKNOWN;
2813
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002814 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002815 if (functv.v_type == VAR_PARTIAL)
2816 {
2817 pt = functv.vval.v_partial;
2818 s = partial_name(pt);
2819 }
2820 else
2821 s = functv.vval.v_string;
2822 }
2823 else
2824 s = (char_u *)"";
2825
Bram Moolenaara80faa82020-04-12 19:37:17 +02002826 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002827 funcexe.firstline = curwin->w_cursor.lnum;
2828 funcexe.lastline = curwin->w_cursor.lnum;
2829 funcexe.evaluate = evaluate;
2830 funcexe.partial = pt;
2831 funcexe.selfdict = selfdict;
2832 funcexe.basetv = basetv;
2833 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002835 // Clear the funcref afterwards, so that deleting it while
2836 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002837 if (evaluate)
2838 clear_tv(&functv);
2839
2840 return ret;
2841}
2842
2843/*
2844 * Evaluate "->method()".
2845 * "*arg" points to the '-'.
2846 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2847 */
2848 static int
2849eval_lambda(
2850 char_u **arg,
2851 typval_T *rettv,
2852 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002853 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002854{
2855 typval_T base = *rettv;
2856 int ret;
2857
2858 // Skip over the ->.
2859 *arg += 2;
2860 rettv->v_type = VAR_UNKNOWN;
2861
2862 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002863 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002864 return FAIL;
2865 else if (**arg != '(')
2866 {
2867 if (verbose)
2868 {
2869 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002870 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002871 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002873 }
2874 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002875 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002876 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002877 else
2878 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2879
2880 // Clear the funcref afterwards, so that deleting it while
2881 // evaluating the arguments is possible (see test55).
2882 if (evaluate)
2883 clear_tv(&base);
2884
2885 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002886}
2887
2888/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002889 * Evaluate "->method()".
2890 * "*arg" points to the '-'.
2891 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2892 */
2893 static int
2894eval_method(
2895 char_u **arg,
2896 typval_T *rettv,
2897 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002898 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002899{
2900 char_u *name;
2901 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002902 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002903 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002904 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002905
2906 // Skip over the ->.
2907 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002908 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002909
Bram Moolenaarac92e252019-08-03 21:58:38 +02002910 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002911 len = get_name_len(arg, &alias, evaluate, TRUE);
2912 if (alias != NULL)
2913 name = alias;
2914
2915 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002916 {
2917 if (verbose)
2918 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002919 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002920 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002921 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002922 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002923 if (**arg != '(')
2924 {
2925 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002927 ret = FAIL;
2928 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002929 else if (VIM_ISWHITE((*arg)[-1]))
2930 {
2931 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002932 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002933 ret = FAIL;
2934 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002935 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02002936 ret = eval_func(arg, name, len, rettv,
2937 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002938 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002939
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002940 // Clear the funcref afterwards, so that deleting it while
2941 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002942 if (evaluate)
2943 clear_tv(&base);
2944
Bram Moolenaarac92e252019-08-03 21:58:38 +02002945 return ret;
2946}
2947
2948/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002949 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2950 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002951 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2952 */
2953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002954eval_index(
2955 char_u **arg,
2956 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002957 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002958 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002959{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002960 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002961 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002962 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002963 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002964 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002965 long len = -1;
2966 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002967 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002968 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002969
Bram Moolenaara03f2332016-02-06 18:09:59 +01002970 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002971 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002972 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002973 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002974 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002975 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002976 return FAIL;
2977 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002978#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01002979 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002980 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002981 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002982#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002983 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002984 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002985 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002986 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002987 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002988 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002989 return FAIL;
2990 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002991 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002993 if (evaluate)
2994 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002995 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01002996
2997 case VAR_STRING:
2998 case VAR_NUMBER:
2999 case VAR_LIST:
3000 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003001 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003002 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003003 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003004
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003005 init_tv(&var1);
3006 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003007 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003008 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003009 /*
3010 * dict.name
3011 */
3012 key = *arg + 1;
3013 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3014 ;
3015 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003016 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003017 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003018 }
3019 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003020 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003021 /*
3022 * something[idx]
3023 *
3024 * Get the (first) variable from inside the [].
3025 */
3026 *arg = skipwhite(*arg + 1);
3027 if (**arg == ':')
3028 empty1 = TRUE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003029 else if (eval1(arg, &var1, flags) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003030 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003031 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003032 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003033 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003034 clear_tv(&var1);
3035 return FAIL;
3036 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003037
3038 /*
3039 * Get the second variable from inside the [:].
3040 */
3041 if (**arg == ':')
3042 {
3043 range = TRUE;
3044 *arg = skipwhite(*arg + 1);
3045 if (**arg == ']')
3046 empty2 = TRUE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003047 else if (eval1(arg, &var2, flags) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003049 if (!empty1)
3050 clear_tv(&var1);
3051 return FAIL;
3052 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003053 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003054 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003055 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003056 if (!empty1)
3057 clear_tv(&var1);
3058 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003059 return FAIL;
3060 }
3061 }
3062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003063 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003064 if (**arg != ']')
3065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003066 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003067 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003068 clear_tv(&var1);
3069 if (range)
3070 clear_tv(&var2);
3071 return FAIL;
3072 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003073 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003074 }
3075
3076 if (evaluate)
3077 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003078 n1 = 0;
3079 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003080 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003081 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003082 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003083 }
3084 if (range)
3085 {
3086 if (empty2)
3087 n2 = -1;
3088 else
3089 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003090 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003091 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003092 }
3093 }
3094
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003095 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003096 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003097 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003098 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003100 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003101 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003102 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003103 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003104 case VAR_SPECIAL:
3105 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003106 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003107 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003108
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003109 case VAR_NUMBER:
3110 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003111 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003112 len = (long)STRLEN(s);
3113 if (range)
3114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003115 // The resulting variable is a substring. If the indexes
3116 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003117 if (n1 < 0)
3118 {
3119 n1 = len + n1;
3120 if (n1 < 0)
3121 n1 = 0;
3122 }
3123 if (n2 < 0)
3124 n2 = len + n2;
3125 else if (n2 >= len)
3126 n2 = len;
3127 if (n1 >= len || n2 < 0 || n1 > n2)
3128 s = NULL;
3129 else
3130 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3131 }
3132 else
3133 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003134 // The resulting variable is a string of a single
3135 // character. If the index is too big or negative the
3136 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003137 if (n1 >= len || n1 < 0)
3138 s = NULL;
3139 else
3140 s = vim_strnsave(s + n1, 1);
3141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003142 clear_tv(rettv);
3143 rettv->v_type = VAR_STRING;
3144 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003145 break;
3146
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003147 case VAR_BLOB:
3148 len = blob_len(rettv->vval.v_blob);
3149 if (range)
3150 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003151 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003152 // are out of range the result is empty.
3153 if (n1 < 0)
3154 {
3155 n1 = len + n1;
3156 if (n1 < 0)
3157 n1 = 0;
3158 }
3159 if (n2 < 0)
3160 n2 = len + n2;
3161 else if (n2 >= len)
3162 n2 = len - 1;
3163 if (n1 >= len || n2 < 0 || n1 > n2)
3164 {
3165 clear_tv(rettv);
3166 rettv->v_type = VAR_BLOB;
3167 rettv->vval.v_blob = NULL;
3168 }
3169 else
3170 {
3171 blob_T *blob = blob_alloc();
3172
3173 if (blob != NULL)
3174 {
3175 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3176 {
3177 blob_free(blob);
3178 return FAIL;
3179 }
3180 blob->bv_ga.ga_len = n2 - n1 + 1;
3181 for (i = n1; i <= n2; i++)
3182 blob_set(blob, i - n1,
3183 blob_get(rettv->vval.v_blob, i));
3184
3185 clear_tv(rettv);
3186 rettv_blob_set(rettv, blob);
3187 }
3188 }
3189 }
3190 else
3191 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003192 // The resulting variable is a byte value.
3193 // If the index is too big or negative that is an error.
3194 if (n1 < 0)
3195 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003196 if (n1 < len && n1 >= 0)
3197 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003198 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003199
3200 clear_tv(rettv);
3201 rettv->v_type = VAR_NUMBER;
3202 rettv->vval.v_number = v;
3203 }
3204 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003205 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003206 }
3207 break;
3208
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003209 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003210 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003211 if (n1 < 0)
3212 n1 = len + n1;
3213 if (!empty1 && (n1 < 0 || n1 >= len))
3214 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003215 // For a range we allow invalid values and return an empty
3216 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003217 if (!range)
3218 {
3219 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003220 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003221 return FAIL;
3222 }
3223 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003224 }
3225 if (range)
3226 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 list_T *l;
3228 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003229
3230 if (n2 < 0)
3231 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003232 else if (n2 >= len)
3233 n2 = len - 1;
3234 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003235 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003236 l = list_alloc();
3237 if (l == NULL)
3238 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003239 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003240 n1 <= n2; ++n1)
3241 {
3242 if (list_append_tv(l, &item->li_tv) == FAIL)
3243 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003244 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003245 return FAIL;
3246 }
3247 item = item->li_next;
3248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003249 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003250 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003251 }
3252 else
3253 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003254 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003255 clear_tv(rettv);
3256 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003257 }
3258 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003259
3260 case VAR_DICT:
3261 if (range)
3262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003263 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003264 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003265 if (len == -1)
3266 clear_tv(&var1);
3267 return FAIL;
3268 }
3269 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003270 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003271
3272 if (len == -1)
3273 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003274 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003275 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003276 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003277 clear_tv(&var1);
3278 return FAIL;
3279 }
3280 }
3281
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003282 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003283
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003284 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003285 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003286 if (len == -1)
3287 clear_tv(&var1);
3288 if (item == NULL)
3289 return FAIL;
3290
3291 copy_tv(&item->di_tv, &var1);
3292 clear_tv(rettv);
3293 *rettv = var1;
3294 }
3295 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003296 }
3297 }
3298
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003299 return OK;
3300}
3301
3302/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003303 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003304 */
3305 char_u *
3306partial_name(partial_T *pt)
3307{
3308 if (pt->pt_name != NULL)
3309 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003310 if (pt->pt_func != NULL)
3311 return pt->pt_func->uf_name;
3312 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003313}
3314
Bram Moolenaarddecc252016-04-06 22:59:37 +02003315 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003316partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003317{
3318 int i;
3319
3320 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003321 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003322 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003323 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003324 if (pt->pt_name != NULL)
3325 {
3326 func_unref(pt->pt_name);
3327 vim_free(pt->pt_name);
3328 }
3329 else
3330 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003331
3332 if (pt->pt_funcstack != NULL)
3333 {
3334 // Decrease the reference count for the context of a closure. If down
3335 // to zero free it and clear the variables on the stack.
3336 if (--pt->pt_funcstack->fs_refcount == 0)
3337 {
3338 garray_T *gap = &pt->pt_funcstack->fs_ga;
3339 typval_T *stack = gap->ga_data;
3340
3341 for (i = 0; i < gap->ga_len; ++i)
3342 clear_tv(stack + i);
3343 ga_clear(gap);
3344 vim_free(pt->pt_funcstack);
3345 }
3346 pt->pt_funcstack = NULL;
3347 }
3348
Bram Moolenaarddecc252016-04-06 22:59:37 +02003349 vim_free(pt);
3350}
3351
3352/*
3353 * Unreference a closure: decrement the reference count and free it when it
3354 * becomes zero.
3355 */
3356 void
3357partial_unref(partial_T *pt)
3358{
3359 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003360 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003361}
3362
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003363/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003364 * Return the next (unique) copy ID.
3365 * Used for serializing nested structures.
3366 */
3367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003368get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003369{
3370 current_copyID += COPYID_INC;
3371 return current_copyID;
3372}
3373
3374/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003375 * Garbage collection for lists and dictionaries.
3376 *
3377 * We use reference counts to be able to free most items right away when they
3378 * are no longer used. But for composite items it's possible that it becomes
3379 * unused while the reference count is > 0: When there is a recursive
3380 * reference. Example:
3381 * :let l = [1, 2, 3]
3382 * :let d = {9: l}
3383 * :let l[1] = d
3384 *
3385 * Since this is quite unusual we handle this with garbage collection: every
3386 * once in a while find out which lists and dicts are not referenced from any
3387 * variable.
3388 *
3389 * Here is a good reference text about garbage collection (refers to Python
3390 * but it applies to all reference-counting mechanisms):
3391 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003392 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003393
3394/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003395 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003396 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003397 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003398 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003399 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003400garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003401{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003402 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003403 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003404 buf_T *buf;
3405 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003406 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003407 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003408
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003409 if (!testing)
3410 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003411 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003412 want_garbage_collect = FALSE;
3413 may_garbage_collect = FALSE;
3414 garbage_collect_at_exit = FALSE;
3415 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003416
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003417 // The execution stack can grow big, limit the size.
3418 if (exestack.ga_maxlen - exestack.ga_len > 500)
3419 {
3420 size_t new_len;
3421 char_u *pp;
3422 int n;
3423
3424 // Keep 150% of the current size, with a minimum of the growth size.
3425 n = exestack.ga_len / 2;
3426 if (n < exestack.ga_growsize)
3427 n = exestack.ga_growsize;
3428
3429 // Don't make it bigger though.
3430 if (exestack.ga_len + n < exestack.ga_maxlen)
3431 {
3432 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3433 pp = vim_realloc(exestack.ga_data, new_len);
3434 if (pp == NULL)
3435 return FAIL;
3436 exestack.ga_maxlen = exestack.ga_len + n;
3437 exestack.ga_data = pp;
3438 }
3439 }
3440
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003441 // We advance by two because we add one for items referenced through
3442 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003443 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003444
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003445 /*
3446 * 1. Go through all accessible variables and mark all lists and dicts
3447 * with copyID.
3448 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003450 // Don't free variables in the previous_funccal list unless they are only
3451 // referenced through previous_funccal. This must be first, because if
3452 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003453 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003455 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003456 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003458 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003459 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003460 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3461 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003463 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003464 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003465 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3466 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003467 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003468 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3469 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003470#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003471 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003472 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3473 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003474 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003475 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003476 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3477 NULL, NULL);
3478#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003480 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003481 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003482 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3483 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003484 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003485 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003487 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003488 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003489
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003490 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003491 abort = abort || set_ref_in_functions(copyID);
3492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003493 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003494 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003496 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003497 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003498
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003499 // callbacks in buffers
3500 abort = abort || set_ref_in_buffers(copyID);
3501
Bram Moolenaar1dced572012-04-05 16:54:08 +02003502#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003503 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003504#endif
3505
Bram Moolenaardb913952012-06-29 12:54:53 +02003506#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003507 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003508#endif
3509
3510#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003511 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003512#endif
3513
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003514#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003515 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003516 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003517#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003518#ifdef FEAT_NETBEANS_INTG
3519 abort = abort || set_ref_in_nb_channel(copyID);
3520#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003521
Bram Moolenaare3188e22016-05-31 21:13:04 +02003522#ifdef FEAT_TIMERS
3523 abort = abort || set_ref_in_timer(copyID);
3524#endif
3525
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003526#ifdef FEAT_QUICKFIX
3527 abort = abort || set_ref_in_quickfix(copyID);
3528#endif
3529
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003530#ifdef FEAT_TERMINAL
3531 abort = abort || set_ref_in_term(copyID);
3532#endif
3533
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003534#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003535 abort = abort || set_ref_in_popups(copyID);
3536#endif
3537
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003538 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003539 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003540 /*
3541 * 2. Free lists and dictionaries that are not referenced.
3542 */
3543 did_free = free_unref_items(copyID);
3544
3545 /*
3546 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003547 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003548 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003549 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003550 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003551 else if (p_verbose > 0)
3552 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003553 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003554 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003555
3556 return did_free;
3557}
3558
3559/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003560 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003561 */
3562 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003563free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003564{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003565 int did_free = FALSE;
3566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003567 // Let all "free" functions know that we are here. This means no
3568 // dictionaries, lists, channels or jobs are to be freed, because we will
3569 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003570 in_free_unref_items = TRUE;
3571
3572 /*
3573 * PASS 1: free the contents of the items. We don't free the items
3574 * themselves yet, so that it is possible to decrement refcount counters
3575 */
3576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003577 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003578 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003579
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003580 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003581 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003582
3583#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003584 // Go through the list of jobs and free items without the copyID. This
3585 // must happen before doing channels, because jobs refer to channels, but
3586 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003587 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3588
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003589 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003590 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3591#endif
3592
3593 /*
3594 * PASS 2: free the items themselves.
3595 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003596 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003597 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003598
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003599#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003600 // Go through the list of jobs and free items without the copyID. This
3601 // must happen before doing channels, because jobs refer to channels, but
3602 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003603 free_unused_jobs(copyID, COPYID_MASK);
3604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003605 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003606 free_unused_channels(copyID, COPYID_MASK);
3607#endif
3608
3609 in_free_unref_items = FALSE;
3610
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003611 return did_free;
3612}
3613
3614/*
3615 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003616 * "list_stack" is used to add lists to be marked. Can be NULL.
3617 *
3618 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003619 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003620 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003621set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003622{
3623 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003624 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003625 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003626 hashtab_T *cur_ht;
3627 ht_stack_T *ht_stack = NULL;
3628 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003629
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003630 cur_ht = ht;
3631 for (;;)
3632 {
3633 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003634 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003635 // Mark each item in the hashtab. If the item contains a hashtab
3636 // it is added to ht_stack, if it contains a list it is added to
3637 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003638 todo = (int)cur_ht->ht_used;
3639 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3640 if (!HASHITEM_EMPTY(hi))
3641 {
3642 --todo;
3643 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3644 &ht_stack, list_stack);
3645 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003646 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003647
3648 if (ht_stack == NULL)
3649 break;
3650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003651 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003652 cur_ht = ht_stack->ht;
3653 tempitem = ht_stack;
3654 ht_stack = ht_stack->prev;
3655 free(tempitem);
3656 }
3657
3658 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003659}
3660
3661/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003662 * Mark a dict and its items with "copyID".
3663 * Returns TRUE if setting references failed somehow.
3664 */
3665 int
3666set_ref_in_dict(dict_T *d, int copyID)
3667{
3668 if (d != NULL && d->dv_copyID != copyID)
3669 {
3670 d->dv_copyID = copyID;
3671 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3672 }
3673 return FALSE;
3674}
3675
3676/*
3677 * Mark a list and its items with "copyID".
3678 * Returns TRUE if setting references failed somehow.
3679 */
3680 int
3681set_ref_in_list(list_T *ll, int copyID)
3682{
3683 if (ll != NULL && ll->lv_copyID != copyID)
3684 {
3685 ll->lv_copyID = copyID;
3686 return set_ref_in_list_items(ll, copyID, NULL);
3687 }
3688 return FALSE;
3689}
3690
3691/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003692 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003693 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3694 *
3695 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003696 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003697 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003698set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003699{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003700 listitem_T *li;
3701 int abort = FALSE;
3702 list_T *cur_l;
3703 list_stack_T *list_stack = NULL;
3704 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003705
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003706 cur_l = l;
3707 for (;;)
3708 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003709 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003710 // Mark each item in the list. If the item contains a hashtab
3711 // it is added to ht_stack, if it contains a list it is added to
3712 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003713 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
3714 abort = abort || set_ref_in_item(&li->li_tv, copyID,
3715 ht_stack, &list_stack);
3716 if (list_stack == NULL)
3717 break;
3718
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003719 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003720 cur_l = list_stack->list;
3721 tempitem = list_stack;
3722 list_stack = list_stack->prev;
3723 free(tempitem);
3724 }
3725
3726 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003727}
3728
3729/*
3730 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003731 * "list_stack" is used to add lists to be marked. Can be NULL.
3732 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3733 *
3734 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003735 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003736 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003737set_ref_in_item(
3738 typval_T *tv,
3739 int copyID,
3740 ht_stack_T **ht_stack,
3741 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003742{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003743 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00003744
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003745 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003746 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003747 dict_T *dd = tv->vval.v_dict;
3748
Bram Moolenaara03f2332016-02-06 18:09:59 +01003749 if (dd != NULL && dd->dv_copyID != copyID)
3750 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003751 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003752 dd->dv_copyID = copyID;
3753 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003754 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003755 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
3756 }
3757 else
3758 {
3759 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
3760 if (newitem == NULL)
3761 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003762 else
3763 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003764 newitem->ht = &dd->dv_hashtab;
3765 newitem->prev = *ht_stack;
3766 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003767 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00003768 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003769 }
3770 }
3771 else if (tv->v_type == VAR_LIST)
3772 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003773 list_T *ll = tv->vval.v_list;
3774
Bram Moolenaara03f2332016-02-06 18:09:59 +01003775 if (ll != NULL && ll->lv_copyID != copyID)
3776 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003777 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003778 ll->lv_copyID = copyID;
3779 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003780 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003781 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01003782 }
3783 else
3784 {
3785 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003786 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003787 if (newitem == NULL)
3788 abort = TRUE;
3789 else
3790 {
3791 newitem->list = ll;
3792 newitem->prev = *list_stack;
3793 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003794 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00003795 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003796 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00003797 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003798 else if (tv->v_type == VAR_FUNC)
3799 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003800 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003801 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003802 else if (tv->v_type == VAR_PARTIAL)
3803 {
3804 partial_T *pt = tv->vval.v_partial;
3805 int i;
3806
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003807 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003808 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003809 // Didn't see this partial yet.
3810 pt->pt_copyID = copyID;
3811
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003812 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003813
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003814 if (pt->pt_dict != NULL)
3815 {
3816 typval_T dtv;
3817
3818 dtv.v_type = VAR_DICT;
3819 dtv.vval.v_dict = pt->pt_dict;
3820 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3821 }
3822
3823 for (i = 0; i < pt->pt_argc; ++i)
3824 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
3825 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003826 if (pt->pt_funcstack != NULL)
3827 {
3828 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
3829
3830 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
3831 abort = abort || set_ref_in_item(stack + i, copyID,
3832 ht_stack, list_stack);
3833 }
3834
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003835 }
3836 }
3837#ifdef FEAT_JOB_CHANNEL
3838 else if (tv->v_type == VAR_JOB)
3839 {
3840 job_T *job = tv->vval.v_job;
3841 typval_T dtv;
3842
3843 if (job != NULL && job->jv_copyID != copyID)
3844 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02003845 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003846 if (job->jv_channel != NULL)
3847 {
3848 dtv.v_type = VAR_CHANNEL;
3849 dtv.vval.v_channel = job->jv_channel;
3850 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3851 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003852 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003853 {
3854 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003855 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003856 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3857 }
3858 }
3859 }
3860 else if (tv->v_type == VAR_CHANNEL)
3861 {
3862 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003863 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003864 typval_T dtv;
3865 jsonq_T *jq;
3866 cbq_T *cq;
3867
3868 if (ch != NULL && ch->ch_copyID != copyID)
3869 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02003870 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003871 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003872 {
3873 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
3874 jq = jq->jq_next)
3875 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
3876 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
3877 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003878 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003879 {
3880 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003881 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003882 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3883 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003884 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003885 {
3886 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003887 dtv.vval.v_partial =
3888 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003889 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3890 }
3891 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003892 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003893 {
3894 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003895 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003896 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3897 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003898 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003899 {
3900 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003901 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003902 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3903 }
3904 }
3905 }
3906#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003907 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00003908}
3909
Bram Moolenaar8c711452005-01-14 21:53:12 +00003910/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003911 * Return a string with the string representation of a variable.
3912 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003913 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003914 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02003915 * When both "echo_style" and "composite_val" are FALSE, put quotes around
3916 * stings as "string()", otherwise does not put quotes around strings, as
3917 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003918 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
3919 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00003920 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003921 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003922 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003923echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003924 typval_T *tv,
3925 char_u **tofree,
3926 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003927 int copyID,
3928 int echo_style,
3929 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02003930 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003931{
Bram Moolenaare9a41262005-01-15 22:18:47 +00003932 static int recurse = 0;
3933 char_u *r = NULL;
3934
Bram Moolenaar33570922005-01-25 22:26:29 +00003935 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00003936 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02003937 if (!did_echo_string_emsg)
3938 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003939 // Only give this message once for a recursive call to avoid
3940 // flooding the user with errors. And stop iterating over lists
3941 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02003942 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003943 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02003944 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003945 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02003946 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00003947 }
3948 ++recurse;
3949
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003950 switch (tv->v_type)
3951 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003952 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02003953 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003954 {
3955 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02003956 r = tv->vval.v_string;
3957 if (r == NULL)
3958 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003959 }
3960 else
3961 {
3962 *tofree = string_quote(tv->vval.v_string, FALSE);
3963 r = *tofree;
3964 }
3965 break;
3966
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003967 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003968 if (echo_style)
3969 {
3970 *tofree = NULL;
3971 r = tv->vval.v_string;
3972 }
3973 else
3974 {
3975 *tofree = string_quote(tv->vval.v_string, TRUE);
3976 r = *tofree;
3977 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003978 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003979
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003980 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01003981 {
3982 partial_T *pt = tv->vval.v_partial;
3983 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003984 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01003985 garray_T ga;
3986 int i;
3987 char_u *tf;
3988
3989 ga_init2(&ga, 1, 100);
3990 ga_concat(&ga, (char_u *)"function(");
3991 if (fname != NULL)
3992 {
3993 ga_concat(&ga, fname);
3994 vim_free(fname);
3995 }
3996 if (pt != NULL && pt->pt_argc > 0)
3997 {
3998 ga_concat(&ga, (char_u *)", [");
3999 for (i = 0; i < pt->pt_argc; ++i)
4000 {
4001 if (i > 0)
4002 ga_concat(&ga, (char_u *)", ");
4003 ga_concat(&ga,
4004 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4005 vim_free(tf);
4006 }
4007 ga_concat(&ga, (char_u *)"]");
4008 }
4009 if (pt != NULL && pt->pt_dict != NULL)
4010 {
4011 typval_T dtv;
4012
4013 ga_concat(&ga, (char_u *)", ");
4014 dtv.v_type = VAR_DICT;
4015 dtv.vval.v_dict = pt->pt_dict;
4016 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4017 vim_free(tf);
4018 }
4019 ga_concat(&ga, (char_u *)")");
4020
4021 *tofree = ga.ga_data;
4022 r = *tofree;
4023 break;
4024 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004025
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004026 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004027 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004028 break;
4029
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004030 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004031 if (tv->vval.v_list == NULL)
4032 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004033 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004034 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004035 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004036 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004037 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4038 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004039 {
4040 *tofree = NULL;
4041 r = (char_u *)"[...]";
4042 }
4043 else
4044 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004045 int old_copyID = tv->vval.v_list->lv_copyID;
4046
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004047 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004048 *tofree = list2string(tv, copyID, restore_copyID);
4049 if (restore_copyID)
4050 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004051 r = *tofree;
4052 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004053 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004054
Bram Moolenaar8c711452005-01-14 21:53:12 +00004055 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004056 if (tv->vval.v_dict == NULL)
4057 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004058 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004059 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004060 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004061 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004062 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4063 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004064 {
4065 *tofree = NULL;
4066 r = (char_u *)"{...}";
4067 }
4068 else
4069 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004070 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004071
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004072 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004073 *tofree = dict2string(tv, copyID, restore_copyID);
4074 if (restore_copyID)
4075 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004076 r = *tofree;
4077 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004078 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004079
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004081 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004082 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004084 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004085 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004086 break;
4087
Bram Moolenaar835dc632016-02-07 14:27:38 +01004088 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004089 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004090 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004091 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004092 if (composite_val)
4093 {
4094 *tofree = string_quote(r, FALSE);
4095 r = *tofree;
4096 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004097 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004098
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004099 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004100#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004101 *tofree = NULL;
4102 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4103 r = numbuf;
4104 break;
4105#endif
4106
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004107 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004108 case VAR_SPECIAL:
4109 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004110 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004111 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004112 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004113
Bram Moolenaar8502c702014-06-17 12:51:16 +02004114 if (--recurse == 0)
4115 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004116 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004117}
4118
4119/*
4120 * Return a string with the string representation of a variable.
4121 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4122 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004123 * Does not put quotes around strings, as ":echo" displays values.
4124 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4125 * May return NULL.
4126 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004127 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004128echo_string(
4129 typval_T *tv,
4130 char_u **tofree,
4131 char_u *numbuf,
4132 int copyID)
4133{
4134 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4135}
4136
4137/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004138 * Return string "str" in ' quotes, doubling ' characters.
4139 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004140 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004141 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004142 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004143string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004144{
Bram Moolenaar33570922005-01-25 22:26:29 +00004145 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004146 char_u *p, *r, *s;
4147
Bram Moolenaar33570922005-01-25 22:26:29 +00004148 len = (function ? 13 : 3);
4149 if (str != NULL)
4150 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004151 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004152 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004153 if (*p == '\'')
4154 ++len;
4155 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004156 s = r = alloc(len);
4157 if (r != NULL)
4158 {
4159 if (function)
4160 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004161 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004162 r += 10;
4163 }
4164 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004165 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004166 if (str != NULL)
4167 for (p = str; *p != NUL; )
4168 {
4169 if (*p == '\'')
4170 *r++ = '\'';
4171 MB_COPY_CHAR(p, r);
4172 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004173 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004174 if (function)
4175 *r++ = ')';
4176 *r++ = NUL;
4177 }
4178 return s;
4179}
4180
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004181#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004182/*
4183 * Convert the string "text" to a floating point number.
4184 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4185 * this always uses a decimal point.
4186 * Returns the length of the text that was consumed.
4187 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004189string2float(
4190 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004191 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004192{
4193 char *s = (char *)text;
4194 float_T f;
4195
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004196 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004197 if (STRNICMP(text, "inf", 3) == 0)
4198 {
4199 *value = INFINITY;
4200 return 3;
4201 }
4202 if (STRNICMP(text, "-inf", 3) == 0)
4203 {
4204 *value = -INFINITY;
4205 return 4;
4206 }
4207 if (STRNICMP(text, "nan", 3) == 0)
4208 {
4209 *value = NAN;
4210 return 3;
4211 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004212 f = strtod(s, &s);
4213 *value = f;
4214 return (int)((char_u *)s - text);
4215}
4216#endif
4217
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004220 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004222 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004223var2fpos(
4224 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004225 int dollar_lnum, // TRUE when $ is last line
4226 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004228 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004230 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004232 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004233 if (varp->v_type == VAR_LIST)
4234 {
4235 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004236 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004237 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004238 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004239
4240 l = varp->vval.v_list;
4241 if (l == NULL)
4242 return NULL;
4243
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004244 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004245 pos.lnum = list_find_nr(l, 0L, &error);
4246 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004247 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004248
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004249 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004250 pos.col = list_find_nr(l, 1L, &error);
4251 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004252 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004253 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004254
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004255 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004256 li = list_find(l, 1L);
4257 if (li != NULL && li->li_tv.v_type == VAR_STRING
4258 && li->li_tv.vval.v_string != NULL
4259 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4260 pos.col = len + 1;
4261
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004262 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004263 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004264 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004265 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004266
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004267 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004268 pos.coladd = list_find_nr(l, 2L, &error);
4269 if (error)
4270 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004271
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004272 return &pos;
4273 }
4274
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004275 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 if (name == NULL)
4277 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004278 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004280 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004281 {
4282 if (VIsual_active)
4283 return &VIsual;
4284 return &curwin->w_cursor;
4285 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004286 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004288 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4290 return NULL;
4291 return pp;
4292 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004293
Bram Moolenaara5525202006-03-02 22:52:09 +00004294 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004295
Bram Moolenaar477933c2007-07-17 14:32:23 +00004296 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004297 {
4298 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004299 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004300 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004301 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004302 // In silent Ex mode topline is zero, but that's not a valid line
4303 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004304 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004305 return &pos;
4306 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004307 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004308 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004309 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004310 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004311 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004312 return &pos;
4313 }
4314 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004315 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004317 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
4319 pos.lnum = curbuf->b_ml.ml_line_count;
4320 pos.col = 0;
4321 }
4322 else
4323 {
4324 pos.lnum = curwin->w_cursor.lnum;
4325 pos.col = (colnr_T)STRLEN(ml_get_curline());
4326 }
4327 return &pos;
4328 }
4329 return NULL;
4330}
4331
4332/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004333 * Convert list in "arg" into a position and optional file number.
4334 * When "fnump" is NULL there is no file number, only 3 items.
4335 * Note that the column is passed on as-is, the caller may want to decrement
4336 * it to use 1 for the first column.
4337 * Return FAIL when conversion is not possible, doesn't check the position for
4338 * validity.
4339 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004340 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004341list2fpos(
4342 typval_T *arg,
4343 pos_T *posp,
4344 int *fnump,
4345 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004346{
4347 list_T *l = arg->vval.v_list;
4348 long i = 0;
4349 long n;
4350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004351 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4352 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004353 if (arg->v_type != VAR_LIST
4354 || l == NULL
4355 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004356 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004357 return FAIL;
4358
4359 if (fnump != NULL)
4360 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004361 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004362 if (n < 0)
4363 return FAIL;
4364 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004365 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004366 *fnump = n;
4367 }
4368
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004369 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004370 if (n < 0)
4371 return FAIL;
4372 posp->lnum = n;
4373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004374 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004375 if (n < 0)
4376 return FAIL;
4377 posp->col = n;
4378
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004379 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004380 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004381 posp->coladd = 0;
4382 else
4383 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004384
Bram Moolenaar493c1782014-05-28 14:34:46 +02004385 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004386 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004387
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004388 return OK;
4389}
4390
4391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 * Get the length of an environment variable name.
4393 * Advance "arg" to the first character after the name.
4394 * Return 0 for error.
4395 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004396 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004397get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398{
4399 char_u *p;
4400 int len;
4401
4402 for (p = *arg; vim_isIDc(*p); ++p)
4403 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004404 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 return 0;
4406
4407 len = (int)(p - *arg);
4408 *arg = p;
4409 return len;
4410}
4411
4412/*
4413 * Get the length of the name of a function or internal variable.
4414 * "arg" is advanced to the first non-white character after the name.
4415 * Return 0 if something is wrong.
4416 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004417 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004418get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419{
4420 char_u *p;
4421 int len;
4422
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004423 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004425 {
4426 if (*p == ':')
4427 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004428 // "s:" is start of "s:var", but "n:" is not and can be used in
4429 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004430 len = (int)(p - *arg);
4431 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4432 || len > 1)
4433 break;
4434 }
4435 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004436 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 return 0;
4438
4439 len = (int)(p - *arg);
4440 *arg = skipwhite(p);
4441
4442 return len;
4443}
4444
4445/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004446 * Get the length of the name of a variable or function.
4447 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004449 * Return -1 if curly braces expansion failed.
4450 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 * If the name contains 'magic' {}'s, expand them and return the
4452 * expanded name in an allocated string via 'alias' - caller must free.
4453 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004455get_name_len(
4456 char_u **arg,
4457 char_u **alias,
4458 int evaluate,
4459 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460{
4461 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 char_u *p;
4463 char_u *expr_start;
4464 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004466 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
4468 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4469 && (*arg)[2] == (int)KE_SNR)
4470 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004471 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 *arg += 3;
4473 return get_id_len(arg) + 3;
4474 }
4475 len = eval_fname_script(*arg);
4476 if (len > 0)
4477 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004478 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 *arg += len;
4480 }
4481
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004485 p = find_name_end(*arg, &expr_start, &expr_end,
4486 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if (expr_start != NULL)
4488 {
4489 char_u *temp_string;
4490
4491 if (!evaluate)
4492 {
4493 len += (int)(p - *arg);
4494 *arg = skipwhite(p);
4495 return len;
4496 }
4497
4498 /*
4499 * Include any <SID> etc in the expanded string:
4500 * Thus the -len here.
4501 */
4502 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4503 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004504 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 *alias = temp_string;
4506 *arg = skipwhite(p);
4507 return (int)STRLEN(temp_string);
4508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
4510 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004511 // Only give an error when there is something, otherwise it will be
4512 // reported at a higher level.
4513 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004514 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
4516 return len;
4517}
4518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004519/*
4520 * Find the end of a variable or function name, taking care of magic braces.
4521 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4522 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004523 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004524 * Return a pointer to just after the name. Equal to "arg" if there is no
4525 * valid name.
4526 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004527 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004528find_name_end(
4529 char_u *arg,
4530 char_u **expr_start,
4531 char_u **expr_end,
4532 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004534 int mb_nest = 0;
4535 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004537 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004538 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004540 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004542 *expr_start = NULL;
4543 *expr_end = NULL;
4544 }
4545
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004546 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004547 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4548 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004549 return arg;
4550
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004551 for (p = arg; *p != NUL
4552 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004553 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004554 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004555 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004556 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004557 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004558 if (*p == '\'')
4559 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004560 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004561 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004562 ;
4563 if (*p == NUL)
4564 break;
4565 }
4566 else if (*p == '"')
4567 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004568 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004569 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004570 if (*p == '\\' && p[1] != NUL)
4571 ++p;
4572 if (*p == NUL)
4573 break;
4574 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004575 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004577 // "s:" is start of "s:var", but "n:" is not and can be used in
4578 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004579 len = (int)(p - arg);
4580 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004581 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004582 break;
4583 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004584
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004585 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004587 if (*p == '[')
4588 ++br_nest;
4589 else if (*p == ']')
4590 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004592
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004593 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595 if (*p == '{')
4596 {
4597 mb_nest++;
4598 if (expr_start != NULL && *expr_start == NULL)
4599 *expr_start = p;
4600 }
4601 else if (*p == '}')
4602 {
4603 mb_nest--;
4604 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4605 *expr_end = p;
4606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609
4610 return p;
4611}
4612
4613/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004614 * Expands out the 'magic' {}'s in a variable/function name.
4615 * Note that this can call itself recursively, to deal with
4616 * constructs like foo{bar}{baz}{bam}
4617 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4618 * "in_start" ^
4619 * "expr_start" ^
4620 * "expr_end" ^
4621 * "in_end" ^
4622 *
4623 * Returns a new allocated string, which the caller must free.
4624 * Returns NULL for failure.
4625 */
4626 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004627make_expanded_name(
4628 char_u *in_start,
4629 char_u *expr_start,
4630 char_u *expr_end,
4631 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004632{
4633 char_u c1;
4634 char_u *retval = NULL;
4635 char_u *temp_result;
4636 char_u *nextcmd = NULL;
4637
4638 if (expr_end == NULL || in_end == NULL)
4639 return NULL;
4640 *expr_start = NUL;
4641 *expr_end = NUL;
4642 c1 = *in_end;
4643 *in_end = NUL;
4644
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004645 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004646 if (temp_result != NULL && nextcmd == NULL)
4647 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004648 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4649 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004650 if (retval != NULL)
4651 {
4652 STRCPY(retval, in_start);
4653 STRCAT(retval, temp_result);
4654 STRCAT(retval, expr_end + 1);
4655 }
4656 }
4657 vim_free(temp_result);
4658
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004659 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004660 *expr_start = '{';
4661 *expr_end = '}';
4662
4663 if (retval != NULL)
4664 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004665 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004666 if (expr_start != NULL)
4667 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004668 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004669 temp_result = make_expanded_name(retval, expr_start,
4670 expr_end, temp_result);
4671 vim_free(retval);
4672 retval = temp_result;
4673 }
4674 }
4675
4676 return retval;
4677}
4678
4679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004681 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004683 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004684eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004686 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4687}
4688
4689/*
4690 * Return TRUE if character "c" can be used as the first character in a
4691 * variable or function name (excluding '{' and '}').
4692 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004693 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004694eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004695{
4696 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697}
4698
4699/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004700 * Handle:
4701 * - expr[expr], expr[expr:expr] subscript
4702 * - ".name" lookup
4703 * - function call with Funcref variable: func(expr)
4704 * - method call: var->method()
4705 *
4706 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004707 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004708 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004709handle_subscript(
4710 char_u **arg,
4711 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004712 int flags, // do more than finding the end
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004713 int verbose, // give error messages
4714 char_u *start_leader, // start of '!' and '-' prefixes
4715 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004716{
Bram Moolenaar32e35112020-05-14 22:41:15 +02004717 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004718 int ret = OK;
4719 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004720
Bram Moolenaar61343f02019-07-20 21:11:13 +02004721 // "." is ".name" lookup when we found a dict or when evaluating and
4722 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004723 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02004724 && (((**arg == '['
4725 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02004726 || (!evaluate
4727 && (*arg)[1] != '.'
4728 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004729 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004730 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004731 && !VIM_ISWHITE(*(*arg - 1)))
4732 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004733 {
4734 if (**arg == '(')
4735 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004736 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01004737
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004738 // Stop the expression evaluation when immediately aborting on
4739 // error, or when an interrupt occurred or an exception was thrown
4740 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004741 if (aborting())
4742 {
4743 if (ret == OK)
4744 clear_tv(rettv);
4745 ret = FAIL;
4746 }
4747 dict_unref(selfdict);
4748 selfdict = NULL;
4749 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004750 else if (**arg == '-')
4751 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004752 // Expression "-1.0->method()" applies the leader "-" before
4753 // applying ->.
4754 if (evaluate && *end_leaderp > start_leader)
4755 ret = eval7_leader(rettv, start_leader, end_leaderp);
4756 if (ret == OK)
4757 {
4758 if ((*arg)[2] == '{')
4759 // expr->{lambda}()
4760 ret = eval_lambda(arg, rettv, evaluate, verbose);
4761 else
4762 // expr->name()
4763 ret = eval_method(arg, rettv, evaluate, verbose);
4764 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004765 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004766 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004767 {
4768 dict_unref(selfdict);
4769 if (rettv->v_type == VAR_DICT)
4770 {
4771 selfdict = rettv->vval.v_dict;
4772 if (selfdict != NULL)
4773 ++selfdict->dv_refcount;
4774 }
4775 else
4776 selfdict = NULL;
Bram Moolenaar32e35112020-05-14 22:41:15 +02004777 if (eval_index(arg, rettv, flags, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004778 {
4779 clear_tv(rettv);
4780 ret = FAIL;
4781 }
4782 }
4783 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01004784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004785 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4786 // Don't do this when "Func" is already a partial that was bound
4787 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02004788 if (selfdict != NULL
4789 && (rettv->v_type == VAR_FUNC
4790 || (rettv->v_type == VAR_PARTIAL
4791 && (rettv->vval.v_partial->pt_auto
4792 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004793 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01004794
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004795 dict_unref(selfdict);
4796 return ret;
4797}
4798
4799/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004800 * Make a copy of an item.
4801 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004802 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
4803 * reference to an already copied list/dict can be used.
4804 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004805 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004807item_copy(
4808 typval_T *from,
4809 typval_T *to,
4810 int deep,
4811 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004812{
4813 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004814 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004815
Bram Moolenaar33570922005-01-25 22:26:29 +00004816 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004818 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004819 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004820 }
4821 ++recurse;
4822
4823 switch (from->v_type)
4824 {
4825 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004826 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004827 case VAR_STRING:
4828 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004829 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004830 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01004831 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004832 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004833 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004834 copy_tv(from, to);
4835 break;
4836 case VAR_LIST:
4837 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004838 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004839 if (from->vval.v_list == NULL)
4840 to->vval.v_list = NULL;
4841 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
4842 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004843 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004844 to->vval.v_list = from->vval.v_list->lv_copylist;
4845 ++to->vval.v_list->lv_refcount;
4846 }
4847 else
4848 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
4849 if (to->vval.v_list == NULL)
4850 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004851 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01004852 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004853 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01004854 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004855 case VAR_DICT:
4856 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004857 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004858 if (from->vval.v_dict == NULL)
4859 to->vval.v_dict = NULL;
4860 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
4861 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004862 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004863 to->vval.v_dict = from->vval.v_dict->dv_copydict;
4864 ++to->vval.v_dict->dv_refcount;
4865 }
4866 else
4867 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
4868 if (to->vval.v_dict == NULL)
4869 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004870 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01004871 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004872 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01004874 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004875 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004876 }
4877 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004878 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004879}
4880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 void
4882echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
4883{
4884 char_u *tofree;
4885 char_u numbuf[NUMBUFLEN];
4886 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
4887
4888 if (*atstart)
4889 {
4890 *atstart = FALSE;
4891 // Call msg_start() after eval1(), evaluating the expression
4892 // may cause a message to appear.
4893 if (with_space)
4894 {
4895 // Mark the saved text as finishing the line, so that what
4896 // follows is displayed on a new line when scrolling back
4897 // at the more prompt.
4898 msg_sb_eol();
4899 msg_start();
4900 }
4901 }
4902 else if (with_space)
4903 msg_puts_attr(" ", echo_attr);
4904
4905 if (p != NULL)
4906 for ( ; *p != NUL && !got_int; ++p)
4907 {
4908 if (*p == '\n' || *p == '\r' || *p == TAB)
4909 {
4910 if (*p != TAB && *needclr)
4911 {
4912 // remove any text still there from the command
4913 msg_clr_eos();
4914 *needclr = FALSE;
4915 }
4916 msg_putchar_attr(*p, echo_attr);
4917 }
4918 else
4919 {
4920 if (has_mbyte)
4921 {
4922 int i = (*mb_ptr2len)(p);
4923
4924 (void)msg_outtrans_len_attr(p, i, echo_attr);
4925 p += i - 1;
4926 }
4927 else
4928 (void)msg_outtrans_len_attr(p, 1, echo_attr);
4929 }
4930 }
4931 vim_free(tofree);
4932}
4933
Bram Moolenaare9a41262005-01-15 22:18:47 +00004934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 * ":echo expr1 ..." print each argument separated with a space, add a
4936 * newline at the end.
4937 * ":echon expr1 ..." print each argument plain.
4938 */
4939 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004940ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
4942 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004943 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 char_u *p;
4945 int needclr = TRUE;
4946 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01004947 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004948 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
4950 if (eap->skip)
4951 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02004952 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004954 // If eval1() causes an error message the text from the command may
4955 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 need_clr_eos = needclr;
4957
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 p = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02004959 if (eval1(&arg, &rettv, eap->skip ? 0 : EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
4961 /*
4962 * Report the invalid expression unless the expression evaluation
4963 * has been cancelled due to an aborting error, an interrupt, or an
4964 * exception.
4965 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004966 if (!aborting() && did_emsg == did_emsg_before
4967 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004968 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004969 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 break;
4971 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 need_clr_eos = FALSE;
4973
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004976
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004977 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 arg = skipwhite(arg);
4979 }
4980 eap->nextcmd = check_nextcmd(arg);
4981
4982 if (eap->skip)
4983 --emsg_skip;
4984 else
4985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (needclr)
4988 msg_clr_eos();
4989 if (eap->cmdidx == CMD_echo)
4990 msg_end();
4991 }
4992}
4993
4994/*
4995 * ":echohl {name}".
4996 */
4997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004998ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005000 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001}
5002
5003/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005004 * Returns the :echo attribute
5005 */
5006 int
5007get_echo_attr(void)
5008{
5009 return echo_attr;
5010}
5011
5012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 * ":execute expr1 ..." execute the result of an expression.
5014 * ":echomsg expr1 ..." Print a message
5015 * ":echoerr expr1 ..." Print an error
5016 * Each gets spaces around each argument and a newline at the end for
5017 * echo commands
5018 */
5019 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005020ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021{
5022 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005023 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 int ret = OK;
5025 char_u *p;
5026 garray_T ga;
5027 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
5029 ga_init2(&ga, 1, 80);
5030
5031 if (eap->skip)
5032 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005033 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005035 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5036 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038
5039 if (!eap->skip)
5040 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005041 char_u buf[NUMBUFLEN];
5042
5043 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005044 {
5045 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5046 {
5047 emsg(_(e_inval_string));
5048 p = NULL;
5049 }
5050 else
5051 p = tv_get_string_buf(&rettv, buf);
5052 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005053 else
5054 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005055 if (p == NULL)
5056 {
5057 clear_tv(&rettv);
5058 ret = FAIL;
5059 break;
5060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 len = (int)STRLEN(p);
5062 if (ga_grow(&ga, len + 2) == FAIL)
5063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 ret = FAIL;
5066 break;
5067 }
5068 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 ga.ga_len += len;
5072 }
5073
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 arg = skipwhite(arg);
5076 }
5077
5078 if (ret != FAIL && ga.ga_data != NULL)
5079 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005080 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5081 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005082 // Mark the already saved text as finishing the line, so that what
5083 // follows is displayed on a new line when scrolling back at the
5084 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005085 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005086 }
5087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005089 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005090 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005091 out_flush();
5092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 else if (eap->cmdidx == CMD_echoerr)
5094 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005095 int save_did_emsg = did_emsg;
5096
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005097 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005098 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 if (!force_abort)
5100 did_emsg = save_did_emsg;
5101 }
5102 else if (eap->cmdidx == CMD_execute)
5103 do_cmdline((char_u *)ga.ga_data,
5104 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5105 }
5106
5107 ga_clear(&ga);
5108
5109 if (eap->skip)
5110 --emsg_skip;
5111
5112 eap->nextcmd = check_nextcmd(arg);
5113}
5114
5115/*
5116 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5117 * "arg" points to the "&" or '+' when called, to "option" when returning.
5118 * Returns NULL when no option name found. Otherwise pointer to the char
5119 * after the option name.
5120 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005121 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005122find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123{
5124 char_u *p = *arg;
5125
5126 ++p;
5127 if (*p == 'g' && p[1] == ':')
5128 {
5129 *opt_flags = OPT_GLOBAL;
5130 p += 2;
5131 }
5132 else if (*p == 'l' && p[1] == ':')
5133 {
5134 *opt_flags = OPT_LOCAL;
5135 p += 2;
5136 }
5137 else
5138 *opt_flags = 0;
5139
5140 if (!ASCII_ISALPHA(*p))
5141 return NULL;
5142 *arg = p;
5143
5144 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005145 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 else
5147 while (ASCII_ISALPHA(*p))
5148 ++p;
5149 return p;
5150}
5151
5152/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005153 * Display script name where an item was last set.
5154 * Should only be invoked when 'verbose' is non-zero.
5155 */
5156 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005157last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005158{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005159 char_u *p;
5160
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005161 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005162 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005163 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005164 if (p != NULL)
5165 {
5166 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005167 msg_puts(_("\n\tLast set from "));
5168 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005169 if (script_ctx.sc_lnum > 0)
5170 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005171 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005172 msg_outnum((long)script_ctx.sc_lnum);
5173 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005174 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005175 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005176 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005177 }
5178}
5179
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005180#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181
5182/*
5183 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005184 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 * "flags" can be "g" to do a global substitute.
5186 * Returns an allocated string, NULL for error.
5187 */
5188 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005189do_string_sub(
5190 char_u *str,
5191 char_u *pat,
5192 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005193 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005194 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195{
5196 int sublen;
5197 regmatch_T regmatch;
5198 int i;
5199 int do_all;
5200 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005201 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 garray_T ga;
5203 char_u *ret;
5204 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005205 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005207 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005209 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
5211 ga_init2(&ga, 1, 200);
5212
5213 do_all = (flags[0] == 'g');
5214
5215 regmatch.rm_ic = p_ic;
5216 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5217 if (regmatch.regprog != NULL)
5218 {
5219 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005220 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5222 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005223 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005224 if (regmatch.startp[0] == regmatch.endp[0])
5225 {
5226 if (zero_width == regmatch.startp[0])
5227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005228 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005229 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005230 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5231 (size_t)i);
5232 ga.ga_len += i;
5233 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005234 continue;
5235 }
5236 zero_width = regmatch.startp[0];
5237 }
5238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 /*
5240 * Get some space for a temporary buffer to do the substitution
5241 * into. It will contain:
5242 * - The text up to where the match is.
5243 * - The substituted text.
5244 * - The text after the match.
5245 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005246 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005247 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5249 {
5250 ga_clear(&ga);
5251 break;
5252 }
5253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005254 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 i = (int)(regmatch.startp[0] - tail);
5256 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005257 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005258 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 + ga.ga_len + i, TRUE, TRUE, FALSE);
5260 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005261 tail = regmatch.endp[0];
5262 if (*tail == NUL)
5263 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 if (!do_all)
5265 break;
5266 }
5267
5268 if (ga.ga_data != NULL)
5269 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5270
Bram Moolenaar473de612013-06-08 18:19:48 +02005271 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
5273
5274 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5275 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005276 if (p_cpo == empty_option)
5277 p_cpo = save_cpo;
5278 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005279 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005280 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
5282 return ret;
5283}