blob: 68fb9cddf88c393d150a45eeacc2a20ae7bca0f6 [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
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020041 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010042 listwatch_T fi_lw; // keep an eye on the item used.
43 list_T *fi_list; // list being used
44 int fi_bi; // index of blob
45 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
54static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
62 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020063 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010064num_divide(varnumber_T n1, varnumber_T n2)
65{
66 varnumber_T result;
67
68 if (n2 == 0) // give an error message?
69 {
70 if (n1 == 0)
71 result = VARNUM_MIN; // similar to NaN
72 else if (n1 < 0)
73 result = -VARNUM_MAX;
74 else
75 result = VARNUM_MAX;
76 }
77 else
78 result = n1 / n2;
79
80 return result;
81}
82
83/*
84 * Return "n1" modulus "n2", taking care of dividing by zero.
85 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020086 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010087num_modulus(varnumber_T n1, varnumber_T n2)
88{
89 // Give an error when n2 is 0?
90 return (n2 == 0) ? 0 : (n1 % n2);
91}
92
Bram Moolenaara1fa8922017-01-12 20:06:33 +010093#if defined(EBCDIC) || defined(PROTO)
94/*
95 * Compare struct fst by function name.
96 */
97 static int
98compare_func_name(const void *s1, const void *s2)
99{
100 struct fst *p1 = (struct fst *)s1;
101 struct fst *p2 = (struct fst *)s2;
102
103 return STRCMP(p1->f_name, p2->f_name);
104}
105
106/*
107 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100108 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100109 * On machines using EBCDIC we have to sort it.
110 */
111 static void
112sortFunctions(void)
113{
114 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
115
116 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
117}
118#endif
119
Bram Moolenaar33570922005-01-25 22:26:29 +0000120/*
121 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000122 */
123 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100124eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000125{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200127 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000128
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200129#ifdef EBCDIC
130 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100131 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200132 */
133 sortFunctions();
134#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000135}
136
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000137#if defined(EXITFREE) || defined(PROTO)
138 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100139eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200141 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100142 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200143 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000144
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200145 // autoloaded script names
146 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000147
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200148 // unreferenced lists and dicts
149 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200150
151 // functions not garbage collected
152 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000153}
154#endif
155
Bram Moolenaare6b53242020-07-01 17:28:33 +0200156 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200157fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
158{
159 CLEAR_FIELD(*evalarg);
160 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
161 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
162 {
163 evalarg->eval_getline = eap->getline;
164 evalarg->eval_cookie = eap->cookie;
165 }
166}
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
169 * Top level evaluation function, returning a boolean.
170 * Sets "error" to TRUE if there was an error.
171 * Return TRUE or FALSE.
172 */
173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100174eval_to_bool(
175 char_u *arg,
176 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200177 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100178 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179{
Bram Moolenaar33570922005-01-25 22:26:29 +0000180 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200181 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200182 evalarg_T evalarg;
183
Bram Moolenaar37c83712020-06-30 21:18:36 +0200184 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186 if (skip)
187 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200188 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 else
191 {
192 *error = FALSE;
193 if (!skip)
194 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100195 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000196 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 }
198 }
199 if (skip)
200 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200201 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200203 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204}
205
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206/*
207 * Call eval1() and give an error message if not done at a lower level.
208 */
209 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200210eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100211{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100212 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100213 int ret;
214 int did_emsg_before = did_emsg;
215 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200216 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100217
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200218 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
219
220 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100221 if (ret == FAIL)
222 {
223 // Report the invalid expression unless the expression evaluation has
224 // been cancelled due to an aborting error, an interrupt, or an
225 // exception, or we already gave a more specific error.
226 // Also check called_emsg for when using assert_fails().
227 if (!aborting() && did_emsg == did_emsg_before
228 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100229 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100230 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200231 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100232 return ret;
233}
234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200236 * Return whether a typval is a valid expression to pass to eval_expr_typval()
237 * or eval_expr_to_bool(). An empty string returns FALSE;
238 */
239 int
240eval_expr_valid_arg(typval_T *tv)
241{
242 return tv->v_type != VAR_UNKNOWN
243 && (tv->v_type != VAR_STRING
244 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
245}
246
247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 * Evaluate an expression, which can be a function, partial or string.
249 * Pass arguments "argv[argc]".
250 * Return the result in "rettv" and OK or FAIL.
251 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200252 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100253eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
254{
255 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100256 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200257 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100258
259 if (expr->v_type == VAR_FUNC)
260 {
261 s = expr->vval.v_string;
262 if (s == NULL || *s == NUL)
263 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200264 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200265 funcexe.evaluate = TRUE;
266 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267 return FAIL;
268 }
269 else if (expr->v_type == VAR_PARTIAL)
270 {
271 partial_T *partial = expr->vval.v_partial;
272
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200273 if (partial == NULL)
274 return FAIL;
275
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200277 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200279 if (call_def_function(partial->pt_func, argc, argv,
280 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 return FAIL;
282 }
283 else
284 {
285 s = partial_name(partial);
286 if (s == NULL || *s == NUL)
287 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200288 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 funcexe.evaluate = TRUE;
290 funcexe.partial = partial;
291 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
292 return FAIL;
293 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 }
295 else
296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100297 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 if (s == NULL)
299 return FAIL;
300 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200301 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100303 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100304 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200305 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100306 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100307 return FAIL;
308 }
309 }
310 return OK;
311}
312
313/*
314 * Like eval_to_bool() but using a typval_T instead of a string.
315 * Works for string, funcref and partial.
316 */
317 int
318eval_expr_to_bool(typval_T *expr, int *error)
319{
320 typval_T rettv;
321 int res;
322
323 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
324 {
325 *error = TRUE;
326 return FALSE;
327 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100328 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100329 clear_tv(&rettv);
330 return res;
331}
332
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333/*
334 * Top level evaluation function, returning a string. If "skip" is TRUE,
335 * only parsing to "nextcmd" is done, without reporting errors. Return
336 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
337 */
338 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100339eval_to_string_skip(
340 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200341 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100342 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
Bram Moolenaar33570922005-01-25 22:26:29 +0000344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200346 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar37c83712020-06-30 21:18:36 +0200348 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 if (skip)
350 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200351 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 retval = NULL;
353 else
354 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100355 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000356 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 }
358 if (skip)
359 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200360 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
362 return retval;
363}
364
365/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366 * Skip over an expression at "*pp".
367 * Return FAIL for an error, OK otherwise.
368 */
369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100370skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000371{
Bram Moolenaar33570922005-01-25 22:26:29 +0000372 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000373
374 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200375 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000376}
377
378/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200379 * Skip over an expression at "*pp".
380 * If in Vim9 script and line breaks are encountered, the lines are
381 * concatenated. "evalarg->eval_tofree" will be set accordingly.
382 * Return FAIL for an error, OK otherwise.
383 */
384 int
385skip_expr_concatenate(char_u **start, char_u **end, evalarg_T *evalarg)
386{
387 typval_T rettv;
388 int res;
389 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
390 garray_T *gap = &evalarg->eval_ga;
391 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
392
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200393 if (vim9script
394 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200395 {
396 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200397 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200398 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 ++gap->ga_len;
400 }
401
402 // Don't evaluate the expression.
403 if (evalarg != NULL)
404 evalarg->eval_flags &= ~EVAL_EVALUATE;
405 *end = skipwhite(*end);
406 res = eval1(end, &rettv, evalarg);
407 if (evalarg != NULL)
408 evalarg->eval_flags = save_flags;
409
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200410 if (vim9script
411 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200413 if (evalarg->eval_ga.ga_len == 1)
414 {
415 // just one line, no need to concatenate
416 ga_clear(gap);
417 gap->ga_itemsize = 0;
418 }
419 else
420 {
421 char_u *p;
422 size_t endoff = STRLEN(*end);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200423
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200424 // Line breaks encountered, concatenate all the lines.
425 *((char_u **)gap->ga_data) = *start;
426 p = ga_concat_strings(gap, "");
427
428 // free the lines only when using getsourceline()
429 if (evalarg->eval_cookie != NULL)
430 {
431 *((char_u **)gap->ga_data) = NULL;
432 ga_clear_strings(gap);
433 }
434 else
435 ga_clear(gap);
436 gap->ga_itemsize = 0;
437 if (p == NULL)
438 return FAIL;
439 *start = p;
440 vim_free(evalarg->eval_tofree);
441 evalarg->eval_tofree = p;
442 // Compute "end" relative to the end.
443 *end = *start + STRLEN(*start) - endoff;
444 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200445 }
446
447 return res;
448}
449
450/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 * Top level evaluation function, returning a string. Does not handle line
452 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000453 * When "convert" is TRUE convert a List into a sequence of lines and convert
454 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 * Return pointer to allocated memory, or NULL for failure.
456 */
457 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100458eval_to_string(
459 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100460 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461{
Bram Moolenaar33570922005-01-25 22:26:29 +0000462 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000464 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000465#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000466 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200469 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 retval = NULL;
471 else
472 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000473 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000474 {
475 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000476 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200477 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200478 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200479 if (tv.vval.v_list->lv_len > 0)
480 ga_append(&ga, NL);
481 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000482 ga_append(&ga, NUL);
483 retval = (char_u *)ga.ga_data;
484 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000485#ifdef FEAT_FLOAT
486 else if (convert && tv.v_type == VAR_FLOAT)
487 {
488 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
489 retval = vim_strsave(numbuf);
490 }
491#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000492 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100493 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000494 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200496 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
498 return retval;
499}
500
501/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000502 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200503 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 */
505 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100506eval_to_string_safe(
507 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100508 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509{
510 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200511 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200513 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000514 if (use_sandbox)
515 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200516 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200517 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000518 if (use_sandbox)
519 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200520 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200521 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 return retval;
523}
524
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525/*
526 * Top level evaluation function, returning a number.
527 * Evaluates "expr" silently.
528 * Returns -1 for an error.
529 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200530 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100531eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532{
Bram Moolenaar33570922005-01-25 22:26:29 +0000533 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200534 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000535 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536
537 ++emsg_off;
538
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200539 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 retval = -1;
541 else
542 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100543 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000544 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
546 --emsg_off;
547
548 return retval;
549}
550
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000551/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000552 * Top level evaluation function.
553 * Returns an allocated typval_T with the result.
554 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000555 */
556 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200557eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000558{
559 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200560 evalarg_T evalarg;
561
562 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000563
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200564 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200565 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100566 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000567
Bram Moolenaar37c83712020-06-30 21:18:36 +0200568 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000569 return tv;
570}
571
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100573 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200574 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
575 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000576 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100579call_vim_function(
580 char_u *func,
581 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200582 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200583 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000585 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200586 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100588 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200589 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200590 funcexe.firstline = curwin->w_cursor.lnum;
591 funcexe.lastline = curwin->w_cursor.lnum;
592 funcexe.evaluate = TRUE;
593 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000594 if (ret == FAIL)
595 clear_tv(rettv);
596
597 return ret;
598}
599
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100600/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100601 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100602 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200603 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
604 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100605 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200606 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100607call_func_retnr(
608 char_u *func,
609 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200610 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100611{
612 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200613 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100614
Bram Moolenaarded27a12018-08-01 19:06:03 +0200615 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100616 return -1;
617
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100618 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100619 clear_tv(&rettv);
620 return retval;
621}
622
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000623/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100624 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000625 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200626 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
627 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000628 */
629 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630call_func_retstr(
631 char_u *func,
632 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200633 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000634{
635 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000636 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000637
Bram Moolenaarded27a12018-08-01 19:06:03 +0200638 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000639 return NULL;
640
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100641 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000642 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 return retval;
644}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000645
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000646/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100647 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200648 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
649 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000650 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000651 */
652 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653call_func_retlist(
654 char_u *func,
655 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200656 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000657{
658 typval_T rettv;
659
Bram Moolenaarded27a12018-08-01 19:06:03 +0200660 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000661 return NULL;
662
663 if (rettv.v_type != VAR_LIST)
664 {
665 clear_tv(&rettv);
666 return NULL;
667 }
668
669 return rettv.vval.v_list;
670}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#ifdef FEAT_FOLDING
673/*
674 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
675 * it in "*cp". Doesn't give error messages.
676 */
677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100678eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679{
Bram Moolenaar33570922005-01-25 22:26:29 +0000680 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200681 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000683 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
684 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
686 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000687 if (use_sandbox)
688 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200689 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200691 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 retval = 0;
693 else
694 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100695 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000696 if (tv.v_type == VAR_NUMBER)
697 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000698 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 retval = 0;
700 else
701 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100702 // If the result is a string, check if there is a non-digit before
703 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000704 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 if (!VIM_ISDIGIT(*s) && *s != '-')
706 *cp = *s++;
707 retval = atol((char *)s);
708 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000709 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000712 if (use_sandbox)
713 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200714 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200715 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200717 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718}
719#endif
720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000722 * Get an lval: variable, Dict item or List item that can be assigned a value
723 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
724 * "name.key", "name.key[expr]" etc.
725 * Indexing only works if "name" is an existing List or Dictionary.
726 * "name" points to the start of the name.
727 * If "rettv" is not NULL it points to the value to be assigned.
728 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
729 * wrong; must end in space or cmd separator.
730 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100731 * flags:
732 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100733 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100734 * GLV_NO_AUTOLOAD: do not use script autoloading
735 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000736 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000737 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000738 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000739 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200740 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100741get_lval(
742 char_u *name,
743 typval_T *rettv,
744 lval_T *lp,
745 int unlet,
746 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100747 int flags, // GLV_ values
748 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000749{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000750 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000751 char_u *expr_start, *expr_end;
752 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000753 dictitem_T *v;
754 typval_T var1;
755 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000756 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000757 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000758 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000759 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000760 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100761 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100763 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200764 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000765
766 if (skip)
767 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100768 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000769 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200770 lp->ll_name_end = find_name_end(name, NULL, NULL,
771 FNE_INCL_BR | fne_flags);
772 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000773 }
774
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100775 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000776 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000778 if (expr_start != NULL)
779 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100780 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100781 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000782 && *p != '[' && *p != '.')
783 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100784 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000785 return NULL;
786 }
787
788 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
789 if (lp->ll_exp_name == NULL)
790 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100791 // Report an invalid expression in braces, unless the
792 // expression evaluation has been cancelled due to an
793 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000794 if (!aborting() && !quiet)
795 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000796 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100797 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000798 return NULL;
799 }
800 }
801 lp->ll_name = lp->ll_exp_name;
802 }
803 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 lp->ll_name = name;
806
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
808 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100809 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 char_u *tp = skipwhite(p + 1);
811
812 // parse the type after the name
813 lp->ll_type = parse_type(&tp, &si->sn_type_list);
814 lp->ll_name_end = tp;
815 }
816 }
817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100818 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
820 return p;
821
822 cc = *p;
823 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100824 // Only pass &ht when we would write to the variable, it prevents autoload
825 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100826 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
827 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100829 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000830 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000831 if (v == NULL)
832 return NULL;
833
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000834 /*
835 * Loop until no more [idx] or .key is following.
836 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000837 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100838 var1.v_type = VAR_UNKNOWN;
839 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000840 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000841 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000842 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
843 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100844 && lp->ll_tv->vval.v_dict != NULL)
845 && !(lp->ll_tv->v_type == VAR_BLOB
846 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000847 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100849 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000851 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000852 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000853 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000854 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100855 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000857 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000858
Bram Moolenaar8c711452005-01-14 21:53:12 +0000859 len = -1;
860 if (*p == '.')
861 {
862 key = p + 1;
863 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
864 ;
865 if (len == 0)
866 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000867 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100868 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000869 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000870 }
871 p = key + len;
872 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000873 else
874 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100875 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000876 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000877 if (*p == ':')
878 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000879 else
880 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000881 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200882 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100884 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100886 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000887 clear_tv(&var1);
888 return NULL;
889 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000890 }
891
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100892 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000893 if (*p == ':')
894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000895 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000896 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100898 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100899 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000901 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100902 if (rettv != NULL
903 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100904 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100905 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100906 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000907 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100909 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100910 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000912 }
913 p = skipwhite(p + 1);
914 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000916 else
917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000918 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200919 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200920 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000921 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100922 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000923 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000924 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100925 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100927 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100928 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000929 clear_tv(&var2);
930 return NULL;
931 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000932 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000935 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000936 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000937
Bram Moolenaar8c711452005-01-14 21:53:12 +0000938 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100941 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100942 clear_tv(&var1);
943 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000944 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000945 }
946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100947 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000948 ++p;
949 }
950
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000951 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000952 {
953 if (len == -1)
954 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100955 // "[key]": get key from "var1"
956 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200957 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000959 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000960 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000961 }
962 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000963 lp->ll_list = NULL;
964 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000965 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100967 // When assigning to a scope dictionary check that a function and
968 // variable name is valid (only variable name unless it is l: or
969 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200970 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200971 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200972 int prevval;
973 int wrong;
974
975 if (len != -1)
976 {
977 prevval = key[len];
978 key[len] = NUL;
979 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200980 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100981 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200982 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
983 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200984 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200985 || !valid_varname(key);
986 if (len != -1)
987 key[len] = prevval;
988 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200989 {
990 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200991 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200992 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200993 }
994
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000995 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000996 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100997 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200998 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100999 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001001 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001002 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001003 return NULL;
1004 }
1005
Bram Moolenaar31b81602019-02-10 22:14:27 +01001006 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001010 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001011 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001013 }
1014 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001016 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001017 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001018 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001020 p = NULL;
1021 break;
1022 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001023 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001024 else if ((flags & GLV_READ_ONLY) == 0
1025 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001026 {
1027 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001028 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001029 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001030
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001031 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001032 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001033 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001034 else if (lp->ll_tv->v_type == VAR_BLOB)
1035 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001036 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1037
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001038 /*
1039 * Get the number and item for the only or first index of the List.
1040 */
1041 if (empty1)
1042 lp->ll_n1 = 0;
1043 else
1044 // is number or string
1045 lp->ll_n1 = (long)tv_get_number(&var1);
1046 clear_tv(&var1);
1047
1048 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001049 || lp->ll_n1 > bloblen
1050 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001051 {
1052 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001053 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001054 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001055 return NULL;
1056 }
1057 if (lp->ll_range && !lp->ll_empty2)
1058 {
1059 lp->ll_n2 = (long)tv_get_number(&var2);
1060 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001061 if (lp->ll_n2 < 0
1062 || lp->ll_n2 >= bloblen
1063 || lp->ll_n2 < lp->ll_n1)
1064 {
1065 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001066 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001067 return NULL;
1068 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001069 }
1070 lp->ll_blob = lp->ll_tv->vval.v_blob;
1071 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001072 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001073 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001074 else
1075 {
1076 /*
1077 * Get the number and item for the only or first index of the List.
1078 */
1079 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001080 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001081 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001082 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001083 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001084 clear_tv(&var1);
1085
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001086 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001087 lp->ll_list = lp->ll_tv->vval.v_list;
1088 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1089 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001090 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001091 if (lp->ll_n1 < 0)
1092 {
1093 lp->ll_n1 = 0;
1094 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1095 }
1096 }
1097 if (lp->ll_li == NULL)
1098 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001099 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001100 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001101 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001102 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001103 }
1104
1105 /*
1106 * May need to find the item or absolute index for the second
1107 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001108 * When no index given: "lp->ll_empty2" is TRUE.
1109 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001110 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001112 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001113 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001114 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001115 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001116 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001117 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001118 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001119 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001120 {
1121 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001122 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001123 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001124 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001126 }
1127
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001128 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001129 if (lp->ll_n1 < 0)
1130 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1131 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001132 {
1133 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001134 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001136 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001137 }
1138
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001140 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001141 }
1142
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001143 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 return p;
1146}
1147
1148/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001149 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001152clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001153{
1154 vim_free(lp->ll_exp_name);
1155 vim_free(lp->ll_newkey);
1156}
1157
1158/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001159 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001161 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1162 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001163 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001165set_var_lval(
1166 lval_T *lp,
1167 char_u *endp,
1168 typval_T *rettv,
1169 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001171 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001172{
1173 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001174 listitem_T *ri;
1175 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176
1177 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001179 cc = *endp;
1180 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001181 if (lp->ll_blob != NULL)
1182 {
1183 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001184
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001185 if (op != NULL && *op != '=')
1186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001187 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001188 return;
1189 }
1190
1191 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1192 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001193 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001194
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001195 if (lp->ll_empty2)
1196 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1197
1198 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001201 return;
1202 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001203 if (lp->ll_empty2)
1204 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001205
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001206 ir = 0;
1207 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1208 blob_set(lp->ll_blob, il,
1209 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001210 }
1211 else
1212 {
1213 val = (int)tv_get_number_chk(rettv, &error);
1214 if (!error)
1215 {
1216 garray_T *gap = &lp->ll_blob->bv_ga;
1217
1218 // Allow for appending a byte. Setting a byte beyond
1219 // the end is an error otherwise.
1220 if (lp->ll_n1 < gap->ga_len
1221 || (lp->ll_n1 == gap->ga_len
1222 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1223 {
1224 blob_set(lp->ll_blob, lp->ll_n1, val);
1225 if (lp->ll_n1 == gap->ga_len)
1226 ++gap->ga_len;
1227 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001228 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001229 }
1230 }
1231 }
1232 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001234 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001236 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001237 {
1238 emsg(_(e_cannot_mod));
1239 *endp = cc;
1240 return;
1241 }
1242
Bram Moolenaarff697e62019-02-12 22:28:33 +01001243 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001244 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001245 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001246 &tv, &di, TRUE, FALSE) == OK)
1247 {
1248 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001249 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1250 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001251 && tv_op(&tv, rettv, op) == OK)
1252 set_var(lp->ll_name, &tv, FALSE);
1253 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001255 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001256 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001258 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001260 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001261 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001262 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001263 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001264 else if (lp->ll_range)
1265 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001266 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001267 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001270 {
1271 emsg(_("E996: Cannot lock a range"));
1272 return;
1273 }
1274
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001275 /*
1276 * Check whether any of the list items is locked
1277 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001278 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001279 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001280 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001281 return;
1282 ri = ri->li_next;
1283 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1284 break;
1285 ll_li = ll_li->li_next;
1286 ++ll_n1;
1287 }
1288
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001289 /*
1290 * Assign the List values to the list items.
1291 */
1292 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001293 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001294 if (op != NULL && *op != '=')
1295 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1296 else
1297 {
1298 clear_tv(&lp->ll_li->li_tv);
1299 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1300 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001301 ri = ri->li_next;
1302 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1303 break;
1304 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001305 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001306 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001307 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001308 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001309 ri = NULL;
1310 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001311 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001312 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001313 lp->ll_li = lp->ll_li->li_next;
1314 ++lp->ll_n1;
1315 }
1316 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001317 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001318 else if (lp->ll_empty2
1319 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001320 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001321 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 }
1323 else
1324 {
1325 /*
1326 * Assign to a List or Dictionary item.
1327 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001329 {
1330 emsg(_("E996: Cannot lock a list or dict"));
1331 return;
1332 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333 if (lp->ll_newkey != NULL)
1334 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001335 if (op != NULL && *op != '=')
1336 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001337 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001338 return;
1339 }
1340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001341 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001342 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001343 if (di == NULL)
1344 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001345 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1346 {
1347 vim_free(di);
1348 return;
1349 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001350 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001351 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001352 else if (op != NULL && *op != '=')
1353 {
1354 tv_op(lp->ll_tv, rettv, op);
1355 return;
1356 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001358 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001359
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001360 /*
1361 * Assign the value to the variable or list item.
1362 */
1363 if (copy)
1364 copy_tv(rettv, lp->ll_tv);
1365 else
1366 {
1367 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001368 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001369 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001370 }
1371 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372}
1373
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001374/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001375 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1376 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001377 * Returns OK or FAIL.
1378 */
1379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001380tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001381{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001382 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001383 char_u numbuf[NUMBUFLEN];
1384 char_u *s;
1385
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001386 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001387 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001388 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001389 {
1390 switch (tv1->v_type)
1391 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001392 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001393 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001395 case VAR_DICT:
1396 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001397 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001398 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001399 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001400 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001401 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001402 break;
1403
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001404 case VAR_BLOB:
1405 if (*op != '+' || tv2->v_type != VAR_BLOB)
1406 break;
1407 // BLOB += BLOB
1408 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1409 {
1410 blob_T *b1 = tv1->vval.v_blob;
1411 blob_T *b2 = tv2->vval.v_blob;
1412 int i, len = blob_len(b2);
1413 for (i = 0; i < len; i++)
1414 ga_append(&b1->bv_ga, blob_get(b2, i));
1415 }
1416 return OK;
1417
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001418 case VAR_LIST:
1419 if (*op != '+' || tv2->v_type != VAR_LIST)
1420 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001421 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001422 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1423 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1424 return OK;
1425
1426 case VAR_NUMBER:
1427 case VAR_STRING:
1428 if (tv2->v_type == VAR_LIST)
1429 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001430 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001431 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001432 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001433 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001434#ifdef FEAT_FLOAT
1435 if (tv2->v_type == VAR_FLOAT)
1436 {
1437 float_T f = n;
1438
Bram Moolenaarff697e62019-02-12 22:28:33 +01001439 if (*op == '%')
1440 break;
1441 switch (*op)
1442 {
1443 case '+': f += tv2->vval.v_float; break;
1444 case '-': f -= tv2->vval.v_float; break;
1445 case '*': f *= tv2->vval.v_float; break;
1446 case '/': f /= tv2->vval.v_float; break;
1447 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001448 clear_tv(tv1);
1449 tv1->v_type = VAR_FLOAT;
1450 tv1->vval.v_float = f;
1451 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001452 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001453#endif
1454 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001455 switch (*op)
1456 {
1457 case '+': n += tv_get_number(tv2); break;
1458 case '-': n -= tv_get_number(tv2); break;
1459 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001460 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1461 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001462 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001463 clear_tv(tv1);
1464 tv1->v_type = VAR_NUMBER;
1465 tv1->vval.v_number = n;
1466 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001467 }
1468 else
1469 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001470 if (tv2->v_type == VAR_FLOAT)
1471 break;
1472
Bram Moolenaarff697e62019-02-12 22:28:33 +01001473 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001474 s = tv_get_string(tv1);
1475 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001476 clear_tv(tv1);
1477 tv1->v_type = VAR_STRING;
1478 tv1->vval.v_string = s;
1479 }
1480 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001481
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001482 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001483#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001484 {
1485 float_T f;
1486
Bram Moolenaarff697e62019-02-12 22:28:33 +01001487 if (*op == '%' || *op == '.'
1488 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001489 && tv2->v_type != VAR_NUMBER
1490 && tv2->v_type != VAR_STRING))
1491 break;
1492 if (tv2->v_type == VAR_FLOAT)
1493 f = tv2->vval.v_float;
1494 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001495 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001496 switch (*op)
1497 {
1498 case '+': tv1->vval.v_float += f; break;
1499 case '-': tv1->vval.v_float -= f; break;
1500 case '*': tv1->vval.v_float *= f; break;
1501 case '/': tv1->vval.v_float /= f; break;
1502 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001503 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001504#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001505 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 }
1507 }
1508
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001509 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001510 return FAIL;
1511}
1512
1513/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001514 * Evaluate the expression used in a ":for var in expr" command.
1515 * "arg" points to "var".
1516 * Set "*errp" to TRUE for an error, FALSE otherwise;
1517 * Return a pointer that holds the info. Null when there is an error.
1518 */
1519 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001520eval_for_line(
1521 char_u *arg,
1522 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001523 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001524 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001525{
Bram Moolenaar33570922005-01-25 22:26:29 +00001526 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001527 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001528 typval_T tv;
1529 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001530 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001531
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001532 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001533
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001534 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001535 if (fi == NULL)
1536 return NULL;
1537
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001538 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001539 if (expr == NULL)
1540 return fi;
1541
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001542 expr = skipwhite_and_linebreak(expr, evalarg);
1543 if (expr[0] != 'i' || expr[1] != 'n'
1544 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001545 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001547 return fi;
1548 }
1549
1550 if (skip)
1551 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001552 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1553 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001554 {
1555 *errp = FALSE;
1556 if (!skip)
1557 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001558 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001559 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001560 l = tv.vval.v_list;
1561 if (l == NULL)
1562 {
1563 // a null list is like an empty list: do nothing
1564 clear_tv(&tv);
1565 }
1566 else
1567 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001569 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001571 // No need to increment the refcount, it's already set for
1572 // the list being used in "tv".
1573 fi->fi_list = l;
1574 list_add_watch(l, &fi->fi_lw);
1575 fi->fi_lw.lw_item = l->lv_first;
1576 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001577 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001578 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001579 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001580 fi->fi_bi = 0;
1581 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001582 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001583 typval_T btv;
1584
1585 // Make a copy, so that the iteration still works when the
1586 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001588 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001589 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001590 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001591 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001592 else
1593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001595 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001596 }
1597 }
1598 }
1599 if (skip)
1600 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001601 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001602
1603 return fi;
1604}
1605
1606/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001607 * Used when looping over a :for line, skip the "in expr" part.
1608 */
1609 void
1610skip_for_lines(void *fi_void, evalarg_T *evalarg)
1611{
1612 forinfo_T *fi = (forinfo_T *)fi_void;
1613 int i;
1614
1615 for (i = 0; i < fi->fi_break_count; ++i)
1616 eval_next_line(evalarg);
1617}
1618
1619/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001620 * Use the first item in a ":for" list. Advance to the next.
1621 * Assign the values to the variable (list). "arg" points to the first one.
1622 * Return TRUE when a valid item was found, FALSE when at end of list or
1623 * something wrong.
1624 */
1625 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001626next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001627{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001628 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001629 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001630 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1631 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001632 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001633
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001634 if (fi->fi_blob != NULL)
1635 {
1636 typval_T tv;
1637
1638 if (fi->fi_bi >= blob_len(fi->fi_blob))
1639 return FALSE;
1640 tv.v_type = VAR_NUMBER;
1641 tv.v_lock = VAR_FIXED;
1642 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1643 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001644 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001645 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001646 }
1647
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648 item = fi->fi_lw.lw_item;
1649 if (item == NULL)
1650 result = FALSE;
1651 else
1652 {
1653 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001654 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001655 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 }
1657 return result;
1658}
1659
1660/*
1661 * Free the structure used to store info used by ":for".
1662 */
1663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001664free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665{
Bram Moolenaar33570922005-01-25 22:26:29 +00001666 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001668 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001669 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001671 list_unref(fi->fi_list);
1672 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001673 if (fi != NULL && fi->fi_blob != NULL)
1674 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 vim_free(fi);
1676}
1677
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001679set_context_for_expression(
1680 expand_T *xp,
1681 char_u *arg,
1682 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
1684 int got_eq = FALSE;
1685 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001688 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 {
1690 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001691 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001693 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001694 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 {
1696 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001697 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001698 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001699 break;
1700 }
1701 return;
1702 }
1703 }
1704 else
1705 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1706 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 while ((xp->xp_pattern = vim_strpbrk(arg,
1708 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1709 {
1710 c = *xp->xp_pattern;
1711 if (c == '&')
1712 {
1713 c = xp->xp_pattern[1];
1714 if (c == '&')
1715 {
1716 ++xp->xp_pattern;
1717 xp->xp_context = cmdidx != CMD_let || got_eq
1718 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1719 }
1720 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001723 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1724 xp->xp_pattern += 2;
1725
1726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 }
1728 else if (c == '$')
1729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001730 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 xp->xp_context = EXPAND_ENV_VARS;
1732 }
1733 else if (c == '=')
1734 {
1735 got_eq = TRUE;
1736 xp->xp_context = EXPAND_EXPRESSION;
1737 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001738 else if (c == '#'
1739 && xp->xp_context == EXPAND_EXPRESSION)
1740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001741 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001742 break;
1743 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001744 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 && xp->xp_context == EXPAND_FUNCTIONS
1746 && vim_strchr(xp->xp_pattern, '(') == NULL)
1747 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001748 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 break;
1750 }
1751 else if (cmdidx != CMD_let || got_eq)
1752 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001753 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 {
1755 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1756 if (c == '\\' && xp->xp_pattern[1] != NUL)
1757 ++xp->xp_pattern;
1758 xp->xp_context = EXPAND_NOTHING;
1759 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001760 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001762 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1764 /* skip */ ;
1765 xp->xp_context = EXPAND_NOTHING;
1766 }
1767 else if (c == '|')
1768 {
1769 if (xp->xp_pattern[1] == '|')
1770 {
1771 ++xp->xp_pattern;
1772 xp->xp_context = EXPAND_EXPRESSION;
1773 }
1774 else
1775 xp->xp_context = EXPAND_COMMANDS;
1776 }
1777 else
1778 xp->xp_context = EXPAND_EXPRESSION;
1779 }
1780 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001781 // Doesn't look like something valid, expand as an expression
1782 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 arg = xp->xp_pattern;
1785 if (*arg != NUL)
1786 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1787 /* skip */ ;
1788 }
1789 xp->xp_pattern = arg;
1790}
1791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001793 * Return TRUE if "pat" matches "text".
1794 * Does not use 'cpo' and always uses 'magic'.
1795 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001796 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001797pattern_match(char_u *pat, char_u *text, int ic)
1798{
1799 int matches = FALSE;
1800 char_u *save_cpo;
1801 regmatch_T regmatch;
1802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001803 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001804 save_cpo = p_cpo;
1805 p_cpo = (char_u *)"";
1806 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1807 if (regmatch.regprog != NULL)
1808 {
1809 regmatch.rm_ic = ic;
1810 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1811 vim_regfree(regmatch.regprog);
1812 }
1813 p_cpo = save_cpo;
1814 return matches;
1815}
1816
1817/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001818 * Handle a name followed by "(". Both for just "name(arg)" and for
1819 * "expr->name(arg)".
1820 * Returns OK or FAIL.
1821 */
1822 static int
1823eval_func(
1824 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001825 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001826 char_u *name,
1827 int name_len,
1828 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001829 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001830 typval_T *basetv) // "expr" for "expr->name(arg)"
1831{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001832 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001833 char_u *s = name;
1834 int len = name_len;
1835 partial_T *partial;
1836 int ret = OK;
1837
1838 if (!evaluate)
1839 check_vars(s, len);
1840
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001841 // If "s" is the name of a variable of type VAR_FUNC
1842 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001843 s = deref_func_name(s, &len, &partial, !evaluate);
1844
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001845 // Need to make a copy, in case evaluating the arguments makes
1846 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001847 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001848 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001849 ret = FAIL;
1850 else
1851 {
1852 funcexe_T funcexe;
1853
1854 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001855 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001856 funcexe.firstline = curwin->w_cursor.lnum;
1857 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001858 funcexe.evaluate = evaluate;
1859 funcexe.partial = partial;
1860 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001861 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001862 }
1863 vim_free(s);
1864
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001865 // If evaluate is FALSE rettv->v_type was not set in
1866 // get_func_tv, but it's needed in handle_subscript() to parse
1867 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001868 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1869 {
1870 rettv->vval.v_string = NULL;
1871 rettv->v_type = VAR_FUNC;
1872 }
1873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001874 // Stop the expression evaluation when immediately
1875 // aborting on error, or when an interrupt occurred or
1876 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001877 if (evaluate && aborting())
1878 {
1879 if (ret == OK)
1880 clear_tv(rettv);
1881 ret = FAIL;
1882 }
1883 return ret;
1884}
1885
1886/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001887 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1888 * comment) and there is a next line, return the next line (skipping blanks)
1889 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001890 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1891 * "arg" must point somewhere inside a line, not at the start.
1892 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001893 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001894eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1895{
1896 *getnext = FALSE;
1897 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1898 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001899 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001900 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
Bram Moolenaar962d7212020-07-04 14:15:00 +02001901 && *arg == '#' && arg[1] != '{')))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001902 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001903 char_u *p;
1904
1905 if (evalarg->eval_cookie != NULL)
1906 p = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
1907 else
1908 p = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001909
1910 if (p != NULL)
1911 {
1912 *getnext = TRUE;
1913 return skipwhite(p);
1914 }
1915 }
1916 return arg;
1917}
1918
1919/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001920 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001921 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001922 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001923eval_next_line(evalarg_T *evalarg)
1924{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001925 garray_T *gap = &evalarg->eval_ga;
1926 char_u *line;
1927
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001928 if (evalarg->eval_cookie != NULL)
1929 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
1930 else
1931 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001932 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001933 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1934 {
1935 // Going to concatenate the lines after parsing.
1936 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1937 ++gap->ga_len;
1938 }
1939 else
1940 {
1941 vim_free(evalarg->eval_tofree);
1942 evalarg->eval_tofree = line;
1943 }
1944 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001945}
1946
Bram Moolenaare6b53242020-07-01 17:28:33 +02001947/*
1948 * Call eval_next_non_blank() and get the next line if needed.
1949 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001950 char_u *
1951skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1952{
1953 int getnext;
1954 char_u *p = skipwhite(arg);
1955
Bram Moolenaar962d7212020-07-04 14:15:00 +02001956 if (evalarg == NULL)
1957 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02001958 eval_next_non_blank(p, evalarg, &getnext);
1959 if (getnext)
1960 return eval_next_line(evalarg);
1961 return p;
1962}
1963
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001964/*
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001965 * After using "evalarg" filled from "eap" free the memory.
1966 */
1967 void
1968clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1969{
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001970 if (evalarg != NULL && evalarg->eval_tofree != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001971 {
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001972 if (eap != NULL)
1973 {
1974 // We may need to keep the original command line, e.g. for
1975 // ":let" it has the variable names. But we may also need the
1976 // new one, "nextcmd" points into it. Keep both.
1977 vim_free(eap->cmdline_tofree);
1978 eap->cmdline_tofree = *eap->cmdlinep;
1979 *eap->cmdlinep = evalarg->eval_tofree;
1980 }
1981 else
1982 vim_free(evalarg->eval_tofree);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001983 evalarg->eval_tofree = NULL;
1984 }
1985}
1986
1987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001989 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1991 */
1992
1993/*
1994 * Handle zero level expression.
1995 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001996 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001997 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001998 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 * Return OK or FAIL.
2000 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002001 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002002eval0(
2003 char_u *arg,
2004 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002005 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002006 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
2008 int ret;
2009 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002010 int did_emsg_before = did_emsg;
2011 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002012 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002015 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002016
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002017 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 {
2019 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 /*
2022 * Report the invalid expression unless the expression evaluation has
2023 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002024 * exception, or we already gave a more specific error.
2025 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002027 if (!aborting()
2028 && did_emsg == did_emsg_before
2029 && called_emsg == called_emsg_before
2030 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002031 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 ret = FAIL;
2033 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002034
2035 if (eap != NULL)
2036 eap->nextcmd = check_nextcmd(p);
2037
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 return ret;
2039}
2040
2041/*
2042 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002043 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 *
2045 * "arg" must point to the first non-white of the expression.
2046 * "arg" is advanced to the next non-white after the recognized expression.
2047 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002048 * Note: "rettv.v_lock" is not set.
2049 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 * Return OK or FAIL.
2051 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002052 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002053eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002055 char_u *p;
2056 int getnext;
2057
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 /*
2059 * Get the first variable.
2060 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002061 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 return FAIL;
2063
Bram Moolenaar793648f2020-06-26 21:28:25 +02002064 p = eval_next_non_blank(*arg, evalarg, &getnext);
2065 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002067 int result;
2068 typval_T var2;
2069 evalarg_T nested_evalarg;
2070 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002071 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002072
Bram Moolenaar793648f2020-06-26 21:28:25 +02002073 if (getnext)
2074 *arg = eval_next_line(evalarg);
2075
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002076 if (evalarg == NULL)
2077 {
2078 CLEAR_FIELD(nested_evalarg);
2079 orig_flags = 0;
2080 }
2081 else
2082 {
2083 nested_evalarg = *evalarg;
2084 orig_flags = evalarg->eval_flags;
2085 }
2086
Bram Moolenaar7acde512020-06-24 23:02:40 +02002087 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002089 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002091 int error = FALSE;
2092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002093 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002095 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002096 if (error)
2097 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 }
2099
2100 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002101 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002103 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002104 nested_evalarg.eval_flags = result ? orig_flags
2105 : orig_flags & ~EVAL_EVALUATE;
2106 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 return FAIL;
2108
2109 /*
2110 * Check for the ":".
2111 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002112 p = eval_next_non_blank(*arg, evalarg, &getnext);
2113 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002117 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 return FAIL;
2119 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002120 if (getnext)
2121 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002124 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002126 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002127 nested_evalarg.eval_flags = !result ? orig_flags
2128 : orig_flags & ~EVAL_EVALUATE;
2129 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
2131 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002132 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 return FAIL;
2134 }
2135 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002136 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
2138
2139 return OK;
2140}
2141
2142/*
2143 * Handle first level expression:
2144 * expr2 || expr2 || expr2 logical OR
2145 *
2146 * "arg" must point to the first non-white of the expression.
2147 * "arg" is advanced to the next non-white after the recognized expression.
2148 *
2149 * Return OK or FAIL.
2150 */
2151 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002152eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002154 char_u *p;
2155 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002156 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 long result;
2158 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002159 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
2161 /*
2162 * Get the first variable.
2163 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002164 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 return FAIL;
2166
2167 /*
2168 * Repeat until there is no following "||".
2169 */
2170 first = TRUE;
2171 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002172 p = eval_next_non_blank(*arg, evalarg, &getnext);
2173 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175 evalarg_T nested_evalarg;
2176 int evaluate;
2177 int orig_flags;
2178
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002179 if (getnext)
2180 *arg = eval_next_line(evalarg);
2181
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002182 if (evalarg == NULL)
2183 {
2184 CLEAR_FIELD(nested_evalarg);
2185 orig_flags = 0;
2186 evaluate = FALSE;
2187 }
2188 else
2189 {
2190 nested_evalarg = *evalarg;
2191 orig_flags = evalarg->eval_flags;
2192 evaluate = orig_flags & EVAL_EVALUATE;
2193 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002194
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 if (evaluate && first)
2196 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002197 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002200 if (error)
2201 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 first = FALSE;
2203 }
2204
2205 /*
2206 * Get the second variable.
2207 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002208 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002209 nested_evalarg.eval_flags = !result ? orig_flags
2210 : orig_flags & ~EVAL_EVALUATE;
2211 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 return FAIL;
2213
2214 /*
2215 * Compute the result.
2216 */
2217 if (evaluate && !result)
2218 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002219 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002222 if (error)
2223 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225 if (evaluate)
2226 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 rettv->v_type = VAR_NUMBER;
2228 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002230
2231 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233
2234 return OK;
2235}
2236
2237/*
2238 * Handle second level expression:
2239 * expr3 && expr3 && expr3 logical AND
2240 *
2241 * "arg" must point to the first non-white of the expression.
2242 * "arg" is advanced to the next non-white after the recognized expression.
2243 *
2244 * Return OK or FAIL.
2245 */
2246 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002247eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002249 char_u *p;
2250 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002251 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 long result;
2253 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002254 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
2256 /*
2257 * Get the first variable.
2258 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002259 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 return FAIL;
2261
2262 /*
2263 * Repeat until there is no following "&&".
2264 */
2265 first = TRUE;
2266 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002267 p = eval_next_non_blank(*arg, evalarg, &getnext);
2268 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002270 evalarg_T nested_evalarg;
2271 int orig_flags;
2272 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002273
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002274 if (getnext)
2275 *arg = eval_next_line(evalarg);
2276
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002277 if (evalarg == NULL)
2278 {
2279 CLEAR_FIELD(nested_evalarg);
2280 orig_flags = 0;
2281 evaluate = FALSE;
2282 }
2283 else
2284 {
2285 nested_evalarg = *evalarg;
2286 orig_flags = evalarg->eval_flags;
2287 evaluate = orig_flags & EVAL_EVALUATE;
2288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 if (evaluate && first)
2290 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002291 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002294 if (error)
2295 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 first = FALSE;
2297 }
2298
2299 /*
2300 * Get the second variable.
2301 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002302 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002303 nested_evalarg.eval_flags = result ? orig_flags
2304 : orig_flags & ~EVAL_EVALUATE;
2305 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 return FAIL;
2307
2308 /*
2309 * Compute the result.
2310 */
2311 if (evaluate && result)
2312 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002313 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002316 if (error)
2317 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319 if (evaluate)
2320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 rettv->v_type = VAR_NUMBER;
2322 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002324
2325 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327
2328 return OK;
2329}
2330
2331/*
2332 * Handle third level expression:
2333 * var1 == var2
2334 * var1 =~ var2
2335 * var1 != var2
2336 * var1 !~ var2
2337 * var1 > var2
2338 * var1 >= var2
2339 * var1 < var2
2340 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002341 * var1 is var2
2342 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 *
2344 * "arg" must point to the first non-white of the expression.
2345 * "arg" is advanced to the next non-white after the recognized expression.
2346 *
2347 * Return OK or FAIL.
2348 */
2349 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002350eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
Bram Moolenaar33570922005-01-25 22:26:29 +00002352 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002354 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002356 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360 /*
2361 * Get the first variable.
2362 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002363 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 return FAIL;
2365
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002366 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 switch (p[0])
2368 {
2369 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002370 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002372 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 break;
2374 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002375 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002377 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 break;
2379 case '>': if (p[1] != '=')
2380 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002381 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 len = 1;
2383 }
2384 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002385 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 break;
2387 case '<': if (p[1] != '=')
2388 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002389 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 len = 1;
2391 }
2392 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002393 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002395 case 'i': if (p[1] == 's')
2396 {
2397 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2398 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002399 i = p[len];
2400 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002401 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002402 }
2403 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
2405
2406 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002407 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002409 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002411 if (getnext)
2412 *arg = eval_next_line(evalarg);
2413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002414 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 if (p[len] == '?')
2416 {
2417 ic = TRUE;
2418 ++len;
2419 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002420 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 else if (p[len] == '#')
2422 {
2423 ic = FALSE;
2424 ++len;
2425 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002426 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 else
2428 ic = p_ic;
2429
2430 /*
2431 * Get the second variable.
2432 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002433 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002434 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 return FAIL;
2438 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002439 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002440 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002441 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002442
2443 clear_tv(&var2);
2444 return ret;
2445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
2447
2448 return OK;
2449}
2450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 void
2452eval_addblob(typval_T *tv1, typval_T *tv2)
2453{
2454 blob_T *b1 = tv1->vval.v_blob;
2455 blob_T *b2 = tv2->vval.v_blob;
2456 blob_T *b = blob_alloc();
2457 int i;
2458
2459 if (b != NULL)
2460 {
2461 for (i = 0; i < blob_len(b1); i++)
2462 ga_append(&b->bv_ga, blob_get(b1, i));
2463 for (i = 0; i < blob_len(b2); i++)
2464 ga_append(&b->bv_ga, blob_get(b2, i));
2465
2466 clear_tv(tv1);
2467 rettv_blob_set(tv1, b);
2468 }
2469}
2470
2471 int
2472eval_addlist(typval_T *tv1, typval_T *tv2)
2473{
2474 typval_T var3;
2475
2476 // concatenate Lists
2477 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2478 {
2479 clear_tv(tv1);
2480 clear_tv(tv2);
2481 return FAIL;
2482 }
2483 clear_tv(tv1);
2484 *tv1 = var3;
2485 return OK;
2486}
2487
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488/*
2489 * Handle fourth level expression:
2490 * + number addition
2491 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002492 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002493 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 *
2495 * "arg" must point to the first non-white of the expression.
2496 * "arg" is advanced to the next non-white after the recognized expression.
2497 *
2498 * Return OK or FAIL.
2499 */
2500 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002501eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002503 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
2505 /*
2506 * Get the first variable.
2507 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002508 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 return FAIL;
2510
2511 /*
2512 * Repeat computing, until no '+', '-' or '.' is following.
2513 */
2514 for (;;)
2515 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002516 int getnext;
2517 char_u *p;
2518 int op;
2519 int concat;
2520 typval_T var2;
2521
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002522 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002523 p = eval_next_non_blank(*arg, evalarg, &getnext);
2524 op = *p;
2525 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002526 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002529 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002531 if ((op != '+' || (rettv->v_type != VAR_LIST
2532 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002533#ifdef FEAT_FLOAT
2534 && (op == '.' || rettv->v_type != VAR_FLOAT)
2535#endif
2536 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002537 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002538 // For "list + ...", an illegal use of the first operand as
2539 // a number cannot be determined before evaluating the 2nd
2540 // operand: if this is also a list, all is ok.
2541 // For "something . ...", "something - ..." or "non-list + ...",
2542 // we know that the first operand needs to be a string or number
2543 // without evaluating the 2nd operand. So check before to avoid
2544 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002545 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002546 {
2547 clear_tv(rettv);
2548 return FAIL;
2549 }
2550 }
2551
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 /*
2553 * Get the second variable.
2554 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002555 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2556 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002557 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002558 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 return FAIL;
2562 }
2563
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002564 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
2566 /*
2567 * Compute the result.
2568 */
2569 if (op == '.')
2570 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002571 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2572 char_u *s1 = tv_get_string_buf(rettv, buf1);
2573 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2574
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002575 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002576 {
2577 clear_tv(rettv);
2578 clear_tv(&var2);
2579 return FAIL;
2580 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002581 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 clear_tv(rettv);
2583 rettv->v_type = VAR_STRING;
2584 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002586 else if (op == '+' && rettv->v_type == VAR_BLOB
2587 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002589 else if (op == '+' && rettv->v_type == VAR_LIST
2590 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002591 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002593 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 else
2596 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002597 int error = FALSE;
2598 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002599#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002600 float_T f1 = 0, f2 = 0;
2601
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002602 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002603 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002604 f1 = rettv->vval.v_float;
2605 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002606 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002607 else
2608#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002609 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002610 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002611 if (error)
2612 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002613 // This can only happen for "list + non-list". For
2614 // "non-list + ..." or "something - ...", we returned
2615 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002616 clear_tv(rettv);
2617 return FAIL;
2618 }
2619#ifdef FEAT_FLOAT
2620 if (var2.v_type == VAR_FLOAT)
2621 f1 = n1;
2622#endif
2623 }
2624#ifdef FEAT_FLOAT
2625 if (var2.v_type == VAR_FLOAT)
2626 {
2627 f2 = var2.vval.v_float;
2628 n2 = 0;
2629 }
2630 else
2631#endif
2632 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002633 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002634 if (error)
2635 {
2636 clear_tv(rettv);
2637 clear_tv(&var2);
2638 return FAIL;
2639 }
2640#ifdef FEAT_FLOAT
2641 if (rettv->v_type == VAR_FLOAT)
2642 f2 = n2;
2643#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002644 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002646
2647#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002648 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002649 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2650 {
2651 if (op == '+')
2652 f1 = f1 + f2;
2653 else
2654 f1 = f1 - f2;
2655 rettv->v_type = VAR_FLOAT;
2656 rettv->vval.v_float = f1;
2657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002659#endif
2660 {
2661 if (op == '+')
2662 n1 = n1 + n2;
2663 else
2664 n1 = n1 - n2;
2665 rettv->v_type = VAR_NUMBER;
2666 rettv->vval.v_number = n1;
2667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671 }
2672 return OK;
2673}
2674
2675/*
2676 * Handle fifth level expression:
2677 * * number multiplication
2678 * / number division
2679 * % number modulo
2680 *
2681 * "arg" must point to the first non-white of the expression.
2682 * "arg" is advanced to the next non-white after the recognized expression.
2683 *
2684 * Return OK or FAIL.
2685 */
2686 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002687eval6(
2688 char_u **arg,
2689 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002690 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002691 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
Bram Moolenaar33570922005-01-25 22:26:29 +00002693 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002695 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002696#ifdef FEAT_FLOAT
2697 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002698 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002699#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002700 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
2702 /*
2703 * Get the first variable.
2704 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002705 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return FAIL;
2707
2708 /*
2709 * Repeat computing, until no '*', '/' or '%' is following.
2710 */
2711 for (;;)
2712 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002713 int evaluate = evalarg == NULL ? 0
2714 : (evalarg->eval_flags & EVAL_EVALUATE);
2715 int getnext;
2716
2717 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002718 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002720 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002721 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002723 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002725#ifdef FEAT_FLOAT
2726 if (rettv->v_type == VAR_FLOAT)
2727 {
2728 f1 = rettv->vval.v_float;
2729 use_float = TRUE;
2730 n1 = 0;
2731 }
2732 else
2733#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002734 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002735 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002736 if (error)
2737 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 }
2739 else
2740 n1 = 0;
2741
2742 /*
2743 * Get the second variable.
2744 */
2745 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002746 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 return FAIL;
2748
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002749 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002751#ifdef FEAT_FLOAT
2752 if (var2.v_type == VAR_FLOAT)
2753 {
2754 if (!use_float)
2755 {
2756 f1 = n1;
2757 use_float = TRUE;
2758 }
2759 f2 = var2.vval.v_float;
2760 n2 = 0;
2761 }
2762 else
2763#endif
2764 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002765 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002766 clear_tv(&var2);
2767 if (error)
2768 return FAIL;
2769#ifdef FEAT_FLOAT
2770 if (use_float)
2771 f2 = n2;
2772#endif
2773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774
2775 /*
2776 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002777 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002779#ifdef FEAT_FLOAT
2780 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002782 if (op == '*')
2783 f1 = f1 * f2;
2784 else if (op == '/')
2785 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002786# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002787 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002788 if (f2 == 0.0)
2789 {
2790 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002791 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002792 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002793 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002794 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002795 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002796 }
2797 else
2798 f1 = f1 / f2;
2799# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002800 // We rely on the floating point library to handle divide
2801 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002802 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002803# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002806 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002808 return FAIL;
2809 }
2810 rettv->v_type = VAR_FLOAT;
2811 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
2813 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002816 if (op == '*')
2817 n1 = n1 * n2;
2818 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002819 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002821 n1 = num_modulus(n1, n2);
2822
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002823 rettv->v_type = VAR_NUMBER;
2824 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827 }
2828
2829 return OK;
2830}
2831
2832/*
2833 * Handle sixth level expression:
2834 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002835 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002836 * "string" string constant
2837 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 * &option-name option value
2839 * @r register contents
2840 * identifier variable value
2841 * function() function call
2842 * $VAR environment variable
2843 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002844 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002846 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002847 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 *
2849 * Also handle:
2850 * ! in front logical NOT
2851 * - in front unary minus
2852 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 * trailing [] subscript in String or List
2854 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002855 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 *
2857 * "arg" must point to the first non-white of the expression.
2858 * "arg" is advanced to the next non-white after the recognized expression.
2859 *
2860 * Return OK or FAIL.
2861 */
2862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002863eval7(
2864 char_u **arg,
2865 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002866 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002869 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2870 int evaluate = evalarg != NULL
2871 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 int len;
2873 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 char_u *start_leader, *end_leader;
2875 int ret = OK;
2876 char_u *alias;
2877
2878 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002879 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002880 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002882 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883
2884 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002885 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 */
2887 start_leader = *arg;
2888 while (**arg == '!' || **arg == '-' || **arg == '+')
2889 *arg = skipwhite(*arg + 1);
2890 end_leader = *arg;
2891
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002892 if (**arg == '.' && (!isdigit(*(*arg + 1))
2893#ifdef FEAT_FLOAT
2894 || current_sctx.sc_version < 2
2895#endif
2896 ))
2897 {
2898 semsg(_(e_invexpr2), *arg);
2899 ++*arg;
2900 return FAIL;
2901 }
2902
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 switch (**arg)
2904 {
2905 /*
2906 * Number constant.
2907 */
2908 case '0':
2909 case '1':
2910 case '2':
2911 case '3':
2912 case '4':
2913 case '5':
2914 case '6':
2915 case '7':
2916 case '8':
2917 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002918 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002919
2920 // Apply prefixed "-" and "+" now. Matters especially when
2921 // "->" follows.
2922 if (ret == OK && evaluate && end_leader > start_leader)
2923 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 break;
2925
2926 /*
2927 * String constant: "string".
2928 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002929 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 break;
2931
2932 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002933 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002935 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002936 break;
2937
2938 /*
2939 * List: [expr, expr]
2940 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002941 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 break;
2943
2944 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002945 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002946 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002947 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002948 {
2949 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002950 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002951 }
2952 else
2953 ret = NOTDONE;
2954 break;
2955
2956 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002957 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002958 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002959 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002960 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002961 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002962 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002963 break;
2964
2965 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002966 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002968 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 break;
2970
2971 /*
2972 * Environment variable: $VAR.
2973 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002974 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 break;
2976
2977 /*
2978 * Register contents: @r.
2979 */
2980 case '@': ++*arg;
2981 if (evaluate)
2982 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002984 rettv->vval.v_string = get_reg_contents(**arg,
2985 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 }
2987 if (**arg != NUL)
2988 ++*arg;
2989 break;
2990
2991 /*
2992 * nested expression: (expression).
2993 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002994 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02002995 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002996 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002997
Bram Moolenaar9215f012020-06-27 21:18:00 +02002998 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002999 if (**arg == ')')
3000 ++*arg;
3001 else if (ret == OK)
3002 {
3003 emsg(_(e_missing_close));
3004 clear_tv(rettv);
3005 ret = FAIL;
3006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
3008 break;
3009
Bram Moolenaar8c711452005-01-14 21:53:12 +00003010 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 break;
3012 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003013
3014 if (ret == NOTDONE)
3015 {
3016 /*
3017 * Must be a variable or function name.
3018 * Can also be a curly-braces kind of name: {expr}.
3019 */
3020 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003021 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003022 if (alias != NULL)
3023 s = alias;
3024
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003025 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003026 ret = FAIL;
3027 else
3028 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003029 if (**arg == '(')
3030 // "name(..." recursive!
Bram Moolenaare6b53242020-07-01 17:28:33 +02003031 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003032 else if (flags & EVAL_CONSTANT)
3033 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003034 else if (evaluate)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003035 // get value of variable
3036 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003038 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003039 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003040 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003041 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003042 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003043 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003044 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003045 }
3046
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 *arg = skipwhite(*arg);
3048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003049 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3050 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003051 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003052 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054 /*
3055 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3056 */
3057 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003058 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003059 return ret;
3060}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003061
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003062/*
3063 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003064 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003065 * Adjusts "end_leaderp" until it is at "start_leader".
3066 */
3067 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003068eval7_leader(
3069 typval_T *rettv,
3070 int numeric_only,
3071 char_u *start_leader,
3072 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003073{
3074 char_u *end_leader = *end_leaderp;
3075 int ret = OK;
3076 int error = FALSE;
3077 varnumber_T val = 0;
3078#ifdef FEAT_FLOAT
3079 float_T f = 0.0;
3080
3081 if (rettv->v_type == VAR_FLOAT)
3082 f = rettv->vval.v_float;
3083 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003084#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003085 val = tv_get_number_chk(rettv, &error);
3086 if (error)
3087 {
3088 clear_tv(rettv);
3089 ret = FAIL;
3090 }
3091 else
3092 {
3093 while (end_leader > start_leader)
3094 {
3095 --end_leader;
3096 if (*end_leader == '!')
3097 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003098 if (numeric_only)
3099 {
3100 ++end_leader;
3101 break;
3102 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003103#ifdef FEAT_FLOAT
3104 if (rettv->v_type == VAR_FLOAT)
3105 f = !f;
3106 else
3107#endif
3108 val = !val;
3109 }
3110 else if (*end_leader == '-')
3111 {
3112#ifdef FEAT_FLOAT
3113 if (rettv->v_type == VAR_FLOAT)
3114 f = -f;
3115 else
3116#endif
3117 val = -val;
3118 }
3119 }
3120#ifdef FEAT_FLOAT
3121 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003123 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003124 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003126 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003127#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003128 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003129 clear_tv(rettv);
3130 rettv->v_type = VAR_NUMBER;
3131 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003134 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 return ret;
3136}
3137
3138/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003139 * Call the function referred to in "rettv".
3140 */
3141 static int
3142call_func_rettv(
3143 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003144 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003145 typval_T *rettv,
3146 int evaluate,
3147 dict_T *selfdict,
3148 typval_T *basetv)
3149{
3150 partial_T *pt = NULL;
3151 funcexe_T funcexe;
3152 typval_T functv;
3153 char_u *s;
3154 int ret;
3155
3156 // need to copy the funcref so that we can clear rettv
3157 if (evaluate)
3158 {
3159 functv = *rettv;
3160 rettv->v_type = VAR_UNKNOWN;
3161
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003162 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003163 if (functv.v_type == VAR_PARTIAL)
3164 {
3165 pt = functv.vval.v_partial;
3166 s = partial_name(pt);
3167 }
3168 else
3169 s = functv.vval.v_string;
3170 }
3171 else
3172 s = (char_u *)"";
3173
Bram Moolenaara80faa82020-04-12 19:37:17 +02003174 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003175 funcexe.firstline = curwin->w_cursor.lnum;
3176 funcexe.lastline = curwin->w_cursor.lnum;
3177 funcexe.evaluate = evaluate;
3178 funcexe.partial = pt;
3179 funcexe.selfdict = selfdict;
3180 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003181 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003183 // Clear the funcref afterwards, so that deleting it while
3184 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003185 if (evaluate)
3186 clear_tv(&functv);
3187
3188 return ret;
3189}
3190
3191/*
3192 * Evaluate "->method()".
3193 * "*arg" points to the '-'.
3194 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3195 */
3196 static int
3197eval_lambda(
3198 char_u **arg,
3199 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003200 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003201 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003202{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003203 int evaluate = evalarg != NULL
3204 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003205 typval_T base = *rettv;
3206 int ret;
3207
3208 // Skip over the ->.
3209 *arg += 2;
3210 rettv->v_type = VAR_UNKNOWN;
3211
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003212 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003213 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003214 return FAIL;
3215 else if (**arg != '(')
3216 {
3217 if (verbose)
3218 {
3219 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003220 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003221 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003223 }
3224 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003225 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003226 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003227 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003228 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003229
3230 // Clear the funcref afterwards, so that deleting it while
3231 // evaluating the arguments is possible (see test55).
3232 if (evaluate)
3233 clear_tv(&base);
3234
3235 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003236}
3237
3238/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003239 * Evaluate "->method()".
3240 * "*arg" points to the '-'.
3241 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3242 */
3243 static int
3244eval_method(
3245 char_u **arg,
3246 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003247 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003248 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003249{
3250 char_u *name;
3251 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003252 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003253 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003254 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003255 int evaluate = evalarg != NULL
3256 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003257
3258 // Skip over the ->.
3259 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003260 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003261
Bram Moolenaarac92e252019-08-03 21:58:38 +02003262 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003263 len = get_name_len(arg, &alias, evaluate, TRUE);
3264 if (alias != NULL)
3265 name = alias;
3266
3267 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003268 {
3269 if (verbose)
3270 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003271 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003272 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003273 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003274 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003275 if (**arg != '(')
3276 {
3277 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003279 ret = FAIL;
3280 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003281 else if (VIM_ISWHITE((*arg)[-1]))
3282 {
3283 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003284 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003285 ret = FAIL;
3286 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003287 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003288 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003289 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003290 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003291
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003292 // Clear the funcref afterwards, so that deleting it while
3293 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003294 if (evaluate)
3295 clear_tv(&base);
3296
Bram Moolenaarac92e252019-08-03 21:58:38 +02003297 return ret;
3298}
3299
3300/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003301 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3302 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003303 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3304 */
3305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003306eval_index(
3307 char_u **arg,
3308 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003309 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003310 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003311{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003312 int evaluate = evalarg != NULL
3313 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003314 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003315 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003316 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003317 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003318 long len = -1;
3319 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003320 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003321 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003322
Bram Moolenaara03f2332016-02-06 18:09:59 +01003323 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003324 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003325 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003326 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003327 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003328 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003329 return FAIL;
3330 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003331#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003332 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003333 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003334 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003335#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003336 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003337 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003338 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003339 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003340 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003341 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003342 return FAIL;
3343 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003344 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003346 if (evaluate)
3347 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003348 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003349
3350 case VAR_STRING:
3351 case VAR_NUMBER:
3352 case VAR_LIST:
3353 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003354 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003355 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003356 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003357
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003358 init_tv(&var1);
3359 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003360 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003361 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003362 /*
3363 * dict.name
3364 */
3365 key = *arg + 1;
3366 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3367 ;
3368 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003369 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003370 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003371 }
3372 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003373 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003374 /*
3375 * something[idx]
3376 *
3377 * Get the (first) variable from inside the [].
3378 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003379 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003380 if (**arg == ':')
3381 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003382 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003383 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003384 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003385 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003386 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003387 clear_tv(&var1);
3388 return FAIL;
3389 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003390
3391 /*
3392 * Get the second variable from inside the [:].
3393 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003394 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003395 if (**arg == ':')
3396 {
3397 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003398 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003399 if (**arg == ']')
3400 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003401 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003403 if (!empty1)
3404 clear_tv(&var1);
3405 return FAIL;
3406 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003407 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003409 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003410 if (!empty1)
3411 clear_tv(&var1);
3412 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003413 return FAIL;
3414 }
3415 }
3416
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003417 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003418 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003419 if (**arg != ']')
3420 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003421 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003422 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003423 clear_tv(&var1);
3424 if (range)
3425 clear_tv(&var2);
3426 return FAIL;
3427 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003428 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003429 }
3430
3431 if (evaluate)
3432 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003433 n1 = 0;
3434 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003435 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003436 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003437 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003438 }
3439 if (range)
3440 {
3441 if (empty2)
3442 n2 = -1;
3443 else
3444 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003445 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003446 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003447 }
3448 }
3449
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003451 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003452 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003453 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003455 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003456 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003457 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003458 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003459 case VAR_SPECIAL:
3460 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003461 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003462 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003463
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003464 case VAR_NUMBER:
3465 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003466 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003467 len = (long)STRLEN(s);
3468 if (range)
3469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003470 // The resulting variable is a substring. If the indexes
3471 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003472 if (n1 < 0)
3473 {
3474 n1 = len + n1;
3475 if (n1 < 0)
3476 n1 = 0;
3477 }
3478 if (n2 < 0)
3479 n2 = len + n2;
3480 else if (n2 >= len)
3481 n2 = len;
3482 if (n1 >= len || n2 < 0 || n1 > n2)
3483 s = NULL;
3484 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003485 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003486 }
3487 else
3488 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003489 // The resulting variable is a string of a single
3490 // character. If the index is too big or negative the
3491 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003492 if (n1 >= len || n1 < 0)
3493 s = NULL;
3494 else
3495 s = vim_strnsave(s + n1, 1);
3496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003497 clear_tv(rettv);
3498 rettv->v_type = VAR_STRING;
3499 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003500 break;
3501
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003502 case VAR_BLOB:
3503 len = blob_len(rettv->vval.v_blob);
3504 if (range)
3505 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003506 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003507 // are out of range the result is empty.
3508 if (n1 < 0)
3509 {
3510 n1 = len + n1;
3511 if (n1 < 0)
3512 n1 = 0;
3513 }
3514 if (n2 < 0)
3515 n2 = len + n2;
3516 else if (n2 >= len)
3517 n2 = len - 1;
3518 if (n1 >= len || n2 < 0 || n1 > n2)
3519 {
3520 clear_tv(rettv);
3521 rettv->v_type = VAR_BLOB;
3522 rettv->vval.v_blob = NULL;
3523 }
3524 else
3525 {
3526 blob_T *blob = blob_alloc();
3527
3528 if (blob != NULL)
3529 {
3530 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3531 {
3532 blob_free(blob);
3533 return FAIL;
3534 }
3535 blob->bv_ga.ga_len = n2 - n1 + 1;
3536 for (i = n1; i <= n2; i++)
3537 blob_set(blob, i - n1,
3538 blob_get(rettv->vval.v_blob, i));
3539
3540 clear_tv(rettv);
3541 rettv_blob_set(rettv, blob);
3542 }
3543 }
3544 }
3545 else
3546 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003547 // The resulting variable is a byte value.
3548 // If the index is too big or negative that is an error.
3549 if (n1 < 0)
3550 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003551 if (n1 < len && n1 >= 0)
3552 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003553 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003554
3555 clear_tv(rettv);
3556 rettv->v_type = VAR_NUMBER;
3557 rettv->vval.v_number = v;
3558 }
3559 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003560 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003561 }
3562 break;
3563
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003564 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003565 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003566 if (n1 < 0)
3567 n1 = len + n1;
3568 if (!empty1 && (n1 < 0 || n1 >= len))
3569 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003570 // For a range we allow invalid values and return an empty
3571 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003572 if (!range)
3573 {
3574 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003575 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003576 return FAIL;
3577 }
3578 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003579 }
3580 if (range)
3581 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003582 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003583
3584 if (n2 < 0)
3585 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003586 else if (n2 >= len)
3587 n2 = len - 1;
3588 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003589 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003590 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003591 if (l == NULL)
3592 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003593 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003594 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003595 }
3596 else
3597 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003598 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003599 clear_tv(rettv);
3600 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003601 }
3602 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003603
3604 case VAR_DICT:
3605 if (range)
3606 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003607 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003608 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003609 if (len == -1)
3610 clear_tv(&var1);
3611 return FAIL;
3612 }
3613 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003614 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615
3616 if (len == -1)
3617 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003618 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003619 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003621 clear_tv(&var1);
3622 return FAIL;
3623 }
3624 }
3625
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003626 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003628 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003629 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003630 if (len == -1)
3631 clear_tv(&var1);
3632 if (item == NULL)
3633 return FAIL;
3634
3635 copy_tv(&item->di_tv, &var1);
3636 clear_tv(rettv);
3637 *rettv = var1;
3638 }
3639 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003640 }
3641 }
3642
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003643 return OK;
3644}
3645
3646/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003647 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003648 */
3649 char_u *
3650partial_name(partial_T *pt)
3651{
3652 if (pt->pt_name != NULL)
3653 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003654 if (pt->pt_func != NULL)
3655 return pt->pt_func->uf_name;
3656 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003657}
3658
Bram Moolenaarddecc252016-04-06 22:59:37 +02003659 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003660partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003661{
3662 int i;
3663
3664 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003665 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003666 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003667 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003668 if (pt->pt_name != NULL)
3669 {
3670 func_unref(pt->pt_name);
3671 vim_free(pt->pt_name);
3672 }
3673 else
3674 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003675
3676 if (pt->pt_funcstack != NULL)
3677 {
3678 // Decrease the reference count for the context of a closure. If down
3679 // to zero free it and clear the variables on the stack.
3680 if (--pt->pt_funcstack->fs_refcount == 0)
3681 {
3682 garray_T *gap = &pt->pt_funcstack->fs_ga;
3683 typval_T *stack = gap->ga_data;
3684
3685 for (i = 0; i < gap->ga_len; ++i)
3686 clear_tv(stack + i);
3687 ga_clear(gap);
3688 vim_free(pt->pt_funcstack);
3689 }
3690 pt->pt_funcstack = NULL;
3691 }
3692
Bram Moolenaarddecc252016-04-06 22:59:37 +02003693 vim_free(pt);
3694}
3695
3696/*
3697 * Unreference a closure: decrement the reference count and free it when it
3698 * becomes zero.
3699 */
3700 void
3701partial_unref(partial_T *pt)
3702{
3703 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003704 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003705}
3706
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003707/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003708 * Return the next (unique) copy ID.
3709 * Used for serializing nested structures.
3710 */
3711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003712get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003713{
3714 current_copyID += COPYID_INC;
3715 return current_copyID;
3716}
3717
3718/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003719 * Garbage collection for lists and dictionaries.
3720 *
3721 * We use reference counts to be able to free most items right away when they
3722 * are no longer used. But for composite items it's possible that it becomes
3723 * unused while the reference count is > 0: When there is a recursive
3724 * reference. Example:
3725 * :let l = [1, 2, 3]
3726 * :let d = {9: l}
3727 * :let l[1] = d
3728 *
3729 * Since this is quite unusual we handle this with garbage collection: every
3730 * once in a while find out which lists and dicts are not referenced from any
3731 * variable.
3732 *
3733 * Here is a good reference text about garbage collection (refers to Python
3734 * but it applies to all reference-counting mechanisms):
3735 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003736 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003737
3738/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003739 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003740 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003741 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003742 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003743 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003744garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003745{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003746 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003747 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003748 buf_T *buf;
3749 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003750 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003751 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003752
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003753 if (!testing)
3754 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003755 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003756 want_garbage_collect = FALSE;
3757 may_garbage_collect = FALSE;
3758 garbage_collect_at_exit = FALSE;
3759 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003760
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003761 // The execution stack can grow big, limit the size.
3762 if (exestack.ga_maxlen - exestack.ga_len > 500)
3763 {
3764 size_t new_len;
3765 char_u *pp;
3766 int n;
3767
3768 // Keep 150% of the current size, with a minimum of the growth size.
3769 n = exestack.ga_len / 2;
3770 if (n < exestack.ga_growsize)
3771 n = exestack.ga_growsize;
3772
3773 // Don't make it bigger though.
3774 if (exestack.ga_len + n < exestack.ga_maxlen)
3775 {
3776 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3777 pp = vim_realloc(exestack.ga_data, new_len);
3778 if (pp == NULL)
3779 return FAIL;
3780 exestack.ga_maxlen = exestack.ga_len + n;
3781 exestack.ga_data = pp;
3782 }
3783 }
3784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003785 // We advance by two because we add one for items referenced through
3786 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003787 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003788
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003789 /*
3790 * 1. Go through all accessible variables and mark all lists and dicts
3791 * with copyID.
3792 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003793
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003794 // Don't free variables in the previous_funccal list unless they are only
3795 // referenced through previous_funccal. This must be first, because if
3796 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003797 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003799 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003800 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003801
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003802 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003803 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003804 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3805 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003806
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003807 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003808 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003809 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3810 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003811 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003812 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3813 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003814#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003815 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003816 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3817 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003818 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003819 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003820 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3821 NULL, NULL);
3822#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003823
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003824 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003825 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003826 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3827 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003828 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003829 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003831 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003832 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003834 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003835 abort = abort || set_ref_in_functions(copyID);
3836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003837 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003838 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003840 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003841 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003842
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003843 // callbacks in buffers
3844 abort = abort || set_ref_in_buffers(copyID);
3845
Bram Moolenaar1dced572012-04-05 16:54:08 +02003846#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003847 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003848#endif
3849
Bram Moolenaardb913952012-06-29 12:54:53 +02003850#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003851 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003852#endif
3853
3854#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003855 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003856#endif
3857
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003858#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003859 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003860 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003861#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003862#ifdef FEAT_NETBEANS_INTG
3863 abort = abort || set_ref_in_nb_channel(copyID);
3864#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003865
Bram Moolenaare3188e22016-05-31 21:13:04 +02003866#ifdef FEAT_TIMERS
3867 abort = abort || set_ref_in_timer(copyID);
3868#endif
3869
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003870#ifdef FEAT_QUICKFIX
3871 abort = abort || set_ref_in_quickfix(copyID);
3872#endif
3873
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003874#ifdef FEAT_TERMINAL
3875 abort = abort || set_ref_in_term(copyID);
3876#endif
3877
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003878#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003879 abort = abort || set_ref_in_popups(copyID);
3880#endif
3881
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003882 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003883 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003884 /*
3885 * 2. Free lists and dictionaries that are not referenced.
3886 */
3887 did_free = free_unref_items(copyID);
3888
3889 /*
3890 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003891 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003892 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003893 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003894 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003895 else if (p_verbose > 0)
3896 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003897 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003898 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003899
3900 return did_free;
3901}
3902
3903/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003904 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003905 */
3906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003907free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003908{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003909 int did_free = FALSE;
3910
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003911 // Let all "free" functions know that we are here. This means no
3912 // dictionaries, lists, channels or jobs are to be freed, because we will
3913 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003914 in_free_unref_items = TRUE;
3915
3916 /*
3917 * PASS 1: free the contents of the items. We don't free the items
3918 * themselves yet, so that it is possible to decrement refcount counters
3919 */
3920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003921 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003922 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003924 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003925 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003926
3927#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003928 // Go through the list of jobs and free items without the copyID. This
3929 // must happen before doing channels, because jobs refer to channels, but
3930 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003931 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3932
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003933 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003934 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3935#endif
3936
3937 /*
3938 * PASS 2: free the items themselves.
3939 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003940 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003941 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003942
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003943#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003944 // Go through the list of jobs and free items without the copyID. This
3945 // must happen before doing channels, because jobs refer to channels, but
3946 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003947 free_unused_jobs(copyID, COPYID_MASK);
3948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003949 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003950 free_unused_channels(copyID, COPYID_MASK);
3951#endif
3952
3953 in_free_unref_items = FALSE;
3954
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003955 return did_free;
3956}
3957
3958/*
3959 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003960 * "list_stack" is used to add lists to be marked. Can be NULL.
3961 *
3962 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003963 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003965set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003966{
3967 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003968 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003969 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003970 hashtab_T *cur_ht;
3971 ht_stack_T *ht_stack = NULL;
3972 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003973
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003974 cur_ht = ht;
3975 for (;;)
3976 {
3977 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003979 // Mark each item in the hashtab. If the item contains a hashtab
3980 // it is added to ht_stack, if it contains a list it is added to
3981 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003982 todo = (int)cur_ht->ht_used;
3983 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3984 if (!HASHITEM_EMPTY(hi))
3985 {
3986 --todo;
3987 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3988 &ht_stack, list_stack);
3989 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003990 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003991
3992 if (ht_stack == NULL)
3993 break;
3994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003995 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003996 cur_ht = ht_stack->ht;
3997 tempitem = ht_stack;
3998 ht_stack = ht_stack->prev;
3999 free(tempitem);
4000 }
4001
4002 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004003}
4004
4005/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004006 * Mark a dict and its items with "copyID".
4007 * Returns TRUE if setting references failed somehow.
4008 */
4009 int
4010set_ref_in_dict(dict_T *d, int copyID)
4011{
4012 if (d != NULL && d->dv_copyID != copyID)
4013 {
4014 d->dv_copyID = copyID;
4015 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4016 }
4017 return FALSE;
4018}
4019
4020/*
4021 * Mark a list and its items with "copyID".
4022 * Returns TRUE if setting references failed somehow.
4023 */
4024 int
4025set_ref_in_list(list_T *ll, int copyID)
4026{
4027 if (ll != NULL && ll->lv_copyID != copyID)
4028 {
4029 ll->lv_copyID = copyID;
4030 return set_ref_in_list_items(ll, copyID, NULL);
4031 }
4032 return FALSE;
4033}
4034
4035/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004036 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004037 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4038 *
4039 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004040 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004041 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004042set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004043{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004044 listitem_T *li;
4045 int abort = FALSE;
4046 list_T *cur_l;
4047 list_stack_T *list_stack = NULL;
4048 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004049
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004050 cur_l = l;
4051 for (;;)
4052 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004053 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004054 // Mark each item in the list. If the item contains a hashtab
4055 // it is added to ht_stack, if it contains a list it is added to
4056 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004057 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4058 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4059 ht_stack, &list_stack);
4060 if (list_stack == NULL)
4061 break;
4062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004063 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004064 cur_l = list_stack->list;
4065 tempitem = list_stack;
4066 list_stack = list_stack->prev;
4067 free(tempitem);
4068 }
4069
4070 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004071}
4072
4073/*
4074 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004075 * "list_stack" is used to add lists to be marked. Can be NULL.
4076 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4077 *
4078 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004079 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004080 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004081set_ref_in_item(
4082 typval_T *tv,
4083 int copyID,
4084 ht_stack_T **ht_stack,
4085 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004086{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004087 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004088
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004089 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004090 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004091 dict_T *dd = tv->vval.v_dict;
4092
Bram Moolenaara03f2332016-02-06 18:09:59 +01004093 if (dd != NULL && dd->dv_copyID != copyID)
4094 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004095 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004096 dd->dv_copyID = copyID;
4097 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004098 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004099 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4100 }
4101 else
4102 {
4103 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4104 if (newitem == NULL)
4105 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004106 else
4107 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004108 newitem->ht = &dd->dv_hashtab;
4109 newitem->prev = *ht_stack;
4110 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004111 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004112 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004113 }
4114 }
4115 else if (tv->v_type == VAR_LIST)
4116 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004117 list_T *ll = tv->vval.v_list;
4118
Bram Moolenaara03f2332016-02-06 18:09:59 +01004119 if (ll != NULL && ll->lv_copyID != copyID)
4120 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004121 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004122 ll->lv_copyID = copyID;
4123 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004124 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004125 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004126 }
4127 else
4128 {
4129 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004130 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004131 if (newitem == NULL)
4132 abort = TRUE;
4133 else
4134 {
4135 newitem->list = ll;
4136 newitem->prev = *list_stack;
4137 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004138 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004139 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004140 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004141 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004142 else if (tv->v_type == VAR_FUNC)
4143 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004144 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004145 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004146 else if (tv->v_type == VAR_PARTIAL)
4147 {
4148 partial_T *pt = tv->vval.v_partial;
4149 int i;
4150
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004151 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004152 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004153 // Didn't see this partial yet.
4154 pt->pt_copyID = copyID;
4155
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004156 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004157
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004158 if (pt->pt_dict != NULL)
4159 {
4160 typval_T dtv;
4161
4162 dtv.v_type = VAR_DICT;
4163 dtv.vval.v_dict = pt->pt_dict;
4164 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4165 }
4166
4167 for (i = 0; i < pt->pt_argc; ++i)
4168 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4169 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004170 if (pt->pt_funcstack != NULL)
4171 {
4172 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4173
4174 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4175 abort = abort || set_ref_in_item(stack + i, copyID,
4176 ht_stack, list_stack);
4177 }
4178
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004179 }
4180 }
4181#ifdef FEAT_JOB_CHANNEL
4182 else if (tv->v_type == VAR_JOB)
4183 {
4184 job_T *job = tv->vval.v_job;
4185 typval_T dtv;
4186
4187 if (job != NULL && job->jv_copyID != copyID)
4188 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004189 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004190 if (job->jv_channel != NULL)
4191 {
4192 dtv.v_type = VAR_CHANNEL;
4193 dtv.vval.v_channel = job->jv_channel;
4194 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4195 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004196 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004197 {
4198 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004199 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004200 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4201 }
4202 }
4203 }
4204 else if (tv->v_type == VAR_CHANNEL)
4205 {
4206 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004207 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004208 typval_T dtv;
4209 jsonq_T *jq;
4210 cbq_T *cq;
4211
4212 if (ch != NULL && ch->ch_copyID != copyID)
4213 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004214 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004215 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004216 {
4217 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4218 jq = jq->jq_next)
4219 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4220 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4221 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004222 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004223 {
4224 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004225 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004226 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4227 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004228 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004229 {
4230 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004231 dtv.vval.v_partial =
4232 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004233 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4234 }
4235 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004236 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004237 {
4238 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004239 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004240 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4241 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004242 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004243 {
4244 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004245 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004246 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4247 }
4248 }
4249 }
4250#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004251 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004252}
4253
Bram Moolenaar8c711452005-01-14 21:53:12 +00004254/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004255 * Return a string with the string representation of a variable.
4256 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004257 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004258 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004259 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4260 * stings as "string()", otherwise does not put quotes around strings, as
4261 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004262 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4263 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004264 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004265 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004266 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004267echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004268 typval_T *tv,
4269 char_u **tofree,
4270 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004271 int copyID,
4272 int echo_style,
4273 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004274 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004275{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004276 static int recurse = 0;
4277 char_u *r = NULL;
4278
Bram Moolenaar33570922005-01-25 22:26:29 +00004279 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004280 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004281 if (!did_echo_string_emsg)
4282 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004283 // Only give this message once for a recursive call to avoid
4284 // flooding the user with errors. And stop iterating over lists
4285 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004286 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004287 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004288 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004289 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004290 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004291 }
4292 ++recurse;
4293
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004294 switch (tv->v_type)
4295 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004296 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004297 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004298 {
4299 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004300 r = tv->vval.v_string;
4301 if (r == NULL)
4302 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004303 }
4304 else
4305 {
4306 *tofree = string_quote(tv->vval.v_string, FALSE);
4307 r = *tofree;
4308 }
4309 break;
4310
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004311 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004312 if (echo_style)
4313 {
4314 *tofree = NULL;
4315 r = tv->vval.v_string;
4316 }
4317 else
4318 {
4319 *tofree = string_quote(tv->vval.v_string, TRUE);
4320 r = *tofree;
4321 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004322 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004323
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004324 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004325 {
4326 partial_T *pt = tv->vval.v_partial;
4327 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004328 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004329 garray_T ga;
4330 int i;
4331 char_u *tf;
4332
4333 ga_init2(&ga, 1, 100);
4334 ga_concat(&ga, (char_u *)"function(");
4335 if (fname != NULL)
4336 {
4337 ga_concat(&ga, fname);
4338 vim_free(fname);
4339 }
4340 if (pt != NULL && pt->pt_argc > 0)
4341 {
4342 ga_concat(&ga, (char_u *)", [");
4343 for (i = 0; i < pt->pt_argc; ++i)
4344 {
4345 if (i > 0)
4346 ga_concat(&ga, (char_u *)", ");
4347 ga_concat(&ga,
4348 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4349 vim_free(tf);
4350 }
4351 ga_concat(&ga, (char_u *)"]");
4352 }
4353 if (pt != NULL && pt->pt_dict != NULL)
4354 {
4355 typval_T dtv;
4356
4357 ga_concat(&ga, (char_u *)", ");
4358 dtv.v_type = VAR_DICT;
4359 dtv.vval.v_dict = pt->pt_dict;
4360 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4361 vim_free(tf);
4362 }
4363 ga_concat(&ga, (char_u *)")");
4364
4365 *tofree = ga.ga_data;
4366 r = *tofree;
4367 break;
4368 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004369
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004370 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004371 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004372 break;
4373
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004374 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004375 if (tv->vval.v_list == NULL)
4376 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004377 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004378 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004379 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004380 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004381 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4382 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004383 {
4384 *tofree = NULL;
4385 r = (char_u *)"[...]";
4386 }
4387 else
4388 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004389 int old_copyID = tv->vval.v_list->lv_copyID;
4390
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004391 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004392 *tofree = list2string(tv, copyID, restore_copyID);
4393 if (restore_copyID)
4394 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004395 r = *tofree;
4396 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004397 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004398
Bram Moolenaar8c711452005-01-14 21:53:12 +00004399 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004400 if (tv->vval.v_dict == NULL)
4401 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004402 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004403 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004404 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004405 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004406 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4407 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004408 {
4409 *tofree = NULL;
4410 r = (char_u *)"{...}";
4411 }
4412 else
4413 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004414 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004415
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004416 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004417 *tofree = dict2string(tv, copyID, restore_copyID);
4418 if (restore_copyID)
4419 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004420 r = *tofree;
4421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004422 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004423
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004425 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004426 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004428 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004429 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004430 break;
4431
Bram Moolenaar835dc632016-02-07 14:27:38 +01004432 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004433 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004434 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004435 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004436 if (composite_val)
4437 {
4438 *tofree = string_quote(r, FALSE);
4439 r = *tofree;
4440 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004442
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004443 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004444#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004445 *tofree = NULL;
4446 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4447 r = numbuf;
4448 break;
4449#endif
4450
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004451 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004452 case VAR_SPECIAL:
4453 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004454 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004455 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004457
Bram Moolenaar8502c702014-06-17 12:51:16 +02004458 if (--recurse == 0)
4459 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004460 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004461}
4462
4463/*
4464 * Return a string with the string representation of a variable.
4465 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4466 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004467 * Does not put quotes around strings, as ":echo" displays values.
4468 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4469 * May return NULL.
4470 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004471 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004472echo_string(
4473 typval_T *tv,
4474 char_u **tofree,
4475 char_u *numbuf,
4476 int copyID)
4477{
4478 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4479}
4480
4481/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004482 * Return string "str" in ' quotes, doubling ' characters.
4483 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004485 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004486 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004487string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004488{
Bram Moolenaar33570922005-01-25 22:26:29 +00004489 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004490 char_u *p, *r, *s;
4491
Bram Moolenaar33570922005-01-25 22:26:29 +00004492 len = (function ? 13 : 3);
4493 if (str != NULL)
4494 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004495 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004496 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004497 if (*p == '\'')
4498 ++len;
4499 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004500 s = r = alloc(len);
4501 if (r != NULL)
4502 {
4503 if (function)
4504 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004505 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004506 r += 10;
4507 }
4508 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004509 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004510 if (str != NULL)
4511 for (p = str; *p != NUL; )
4512 {
4513 if (*p == '\'')
4514 *r++ = '\'';
4515 MB_COPY_CHAR(p, r);
4516 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004517 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004518 if (function)
4519 *r++ = ')';
4520 *r++ = NUL;
4521 }
4522 return s;
4523}
4524
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004525#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004526/*
4527 * Convert the string "text" to a floating point number.
4528 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4529 * this always uses a decimal point.
4530 * Returns the length of the text that was consumed.
4531 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004533string2float(
4534 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004535 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004536{
4537 char *s = (char *)text;
4538 float_T f;
4539
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004540 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004541 if (STRNICMP(text, "inf", 3) == 0)
4542 {
4543 *value = INFINITY;
4544 return 3;
4545 }
4546 if (STRNICMP(text, "-inf", 3) == 0)
4547 {
4548 *value = -INFINITY;
4549 return 4;
4550 }
4551 if (STRNICMP(text, "nan", 3) == 0)
4552 {
4553 *value = NAN;
4554 return 3;
4555 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004556 f = strtod(s, &s);
4557 *value = f;
4558 return (int)((char_u *)s - text);
4559}
4560#endif
4561
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004564 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004566 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004567var2fpos(
4568 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004569 int dollar_lnum, // TRUE when $ is last line
4570 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004572 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004574 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004576 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004577 if (varp->v_type == VAR_LIST)
4578 {
4579 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004580 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004581 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004582 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004583
4584 l = varp->vval.v_list;
4585 if (l == NULL)
4586 return NULL;
4587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004588 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004589 pos.lnum = list_find_nr(l, 0L, &error);
4590 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004591 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004592
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004593 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004594 pos.col = list_find_nr(l, 1L, &error);
4595 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004596 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004597 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004599 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004600 li = list_find(l, 1L);
4601 if (li != NULL && li->li_tv.v_type == VAR_STRING
4602 && li->li_tv.vval.v_string != NULL
4603 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4604 pos.col = len + 1;
4605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004606 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004607 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004608 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004609 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004611 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004612 pos.coladd = list_find_nr(l, 2L, &error);
4613 if (error)
4614 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004615
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004616 return &pos;
4617 }
4618
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004619 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004620 if (name == NULL)
4621 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004622 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004624 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004625 {
4626 if (VIsual_active)
4627 return &VIsual;
4628 return &curwin->w_cursor;
4629 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004630 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004632 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4634 return NULL;
4635 return pp;
4636 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004637
Bram Moolenaara5525202006-03-02 22:52:09 +00004638 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004639
Bram Moolenaar477933c2007-07-17 14:32:23 +00004640 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004641 {
4642 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004643 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004644 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004645 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004646 // In silent Ex mode topline is zero, but that's not a valid line
4647 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004648 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004649 return &pos;
4650 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004651 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004652 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004653 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004654 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004655 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004656 return &pos;
4657 }
4658 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004659 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004661 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 {
4663 pos.lnum = curbuf->b_ml.ml_line_count;
4664 pos.col = 0;
4665 }
4666 else
4667 {
4668 pos.lnum = curwin->w_cursor.lnum;
4669 pos.col = (colnr_T)STRLEN(ml_get_curline());
4670 }
4671 return &pos;
4672 }
4673 return NULL;
4674}
4675
4676/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004677 * Convert list in "arg" into a position and optional file number.
4678 * When "fnump" is NULL there is no file number, only 3 items.
4679 * Note that the column is passed on as-is, the caller may want to decrement
4680 * it to use 1 for the first column.
4681 * Return FAIL when conversion is not possible, doesn't check the position for
4682 * validity.
4683 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004685list2fpos(
4686 typval_T *arg,
4687 pos_T *posp,
4688 int *fnump,
4689 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004690{
4691 list_T *l = arg->vval.v_list;
4692 long i = 0;
4693 long n;
4694
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004695 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4696 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004697 if (arg->v_type != VAR_LIST
4698 || l == NULL
4699 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004700 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004701 return FAIL;
4702
4703 if (fnump != NULL)
4704 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004705 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004706 if (n < 0)
4707 return FAIL;
4708 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004709 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004710 *fnump = n;
4711 }
4712
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004713 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004714 if (n < 0)
4715 return FAIL;
4716 posp->lnum = n;
4717
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004718 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004719 if (n < 0)
4720 return FAIL;
4721 posp->col = n;
4722
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004723 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004724 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004725 posp->coladd = 0;
4726 else
4727 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004728
Bram Moolenaar493c1782014-05-28 14:34:46 +02004729 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004730 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004731
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004732 return OK;
4733}
4734
4735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 * Get the length of an environment variable name.
4737 * Advance "arg" to the first character after the name.
4738 * Return 0 for error.
4739 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004740 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004741get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
4743 char_u *p;
4744 int len;
4745
4746 for (p = *arg; vim_isIDc(*p); ++p)
4747 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004748 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 return 0;
4750
4751 len = (int)(p - *arg);
4752 *arg = p;
4753 return len;
4754}
4755
4756/*
4757 * Get the length of the name of a function or internal variable.
4758 * "arg" is advanced to the first non-white character after the name.
4759 * Return 0 if something is wrong.
4760 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004761 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004762get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763{
4764 char_u *p;
4765 int len;
4766
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004767 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004769 {
4770 if (*p == ':')
4771 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004772 // "s:" is start of "s:var", but "n:" is not and can be used in
4773 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004774 len = (int)(p - *arg);
4775 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4776 || len > 1)
4777 break;
4778 }
4779 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004780 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 return 0;
4782
4783 len = (int)(p - *arg);
4784 *arg = skipwhite(p);
4785
4786 return len;
4787}
4788
4789/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004790 * Get the length of the name of a variable or function.
4791 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004793 * Return -1 if curly braces expansion failed.
4794 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 * If the name contains 'magic' {}'s, expand them and return the
4796 * expanded name in an allocated string via 'alias' - caller must free.
4797 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004798 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004799get_name_len(
4800 char_u **arg,
4801 char_u **alias,
4802 int evaluate,
4803 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
4805 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 char_u *p;
4807 char_u *expr_start;
4808 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004810 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4813 && (*arg)[2] == (int)KE_SNR)
4814 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004815 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 *arg += 3;
4817 return get_id_len(arg) + 3;
4818 }
4819 len = eval_fname_script(*arg);
4820 if (len > 0)
4821 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004822 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 *arg += len;
4824 }
4825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004829 p = find_name_end(*arg, &expr_start, &expr_end,
4830 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 if (expr_start != NULL)
4832 {
4833 char_u *temp_string;
4834
4835 if (!evaluate)
4836 {
4837 len += (int)(p - *arg);
4838 *arg = skipwhite(p);
4839 return len;
4840 }
4841
4842 /*
4843 * Include any <SID> etc in the expanded string:
4844 * Thus the -len here.
4845 */
4846 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4847 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004848 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 *alias = temp_string;
4850 *arg = skipwhite(p);
4851 return (int)STRLEN(temp_string);
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004855 // Only give an error when there is something, otherwise it will be
4856 // reported at a higher level.
4857 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004858 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
4860 return len;
4861}
4862
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004863/*
4864 * Find the end of a variable or function name, taking care of magic braces.
4865 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4866 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004867 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004868 * Return a pointer to just after the name. Equal to "arg" if there is no
4869 * valid name.
4870 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004871 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004872find_name_end(
4873 char_u *arg,
4874 char_u **expr_start,
4875 char_u **expr_end,
4876 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878 int mb_nest = 0;
4879 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004881 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004882 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004884 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 *expr_start = NULL;
4887 *expr_end = NULL;
4888 }
4889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004890 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004891 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4892 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004893 return arg;
4894
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 for (p = arg; *p != NUL
4896 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004897 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004898 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004899 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004900 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004901 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004902 if (*p == '\'')
4903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004904 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004905 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004906 ;
4907 if (*p == NUL)
4908 break;
4909 }
4910 else if (*p == '"')
4911 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004912 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004913 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004914 if (*p == '\\' && p[1] != NUL)
4915 ++p;
4916 if (*p == NUL)
4917 break;
4918 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004919 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 // "s:" is start of "s:var", but "n:" is not and can be used in
4922 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004923 len = (int)(p - arg);
4924 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004925 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004926 break;
4927 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004928
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004931 if (*p == '[')
4932 ++br_nest;
4933 else if (*p == ']')
4934 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004936
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004937 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004939 if (*p == '{')
4940 {
4941 mb_nest++;
4942 if (expr_start != NULL && *expr_start == NULL)
4943 *expr_start = p;
4944 }
4945 else if (*p == '}')
4946 {
4947 mb_nest--;
4948 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4949 *expr_end = p;
4950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953
4954 return p;
4955}
4956
4957/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004958 * Expands out the 'magic' {}'s in a variable/function name.
4959 * Note that this can call itself recursively, to deal with
4960 * constructs like foo{bar}{baz}{bam}
4961 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4962 * "in_start" ^
4963 * "expr_start" ^
4964 * "expr_end" ^
4965 * "in_end" ^
4966 *
4967 * Returns a new allocated string, which the caller must free.
4968 * Returns NULL for failure.
4969 */
4970 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004971make_expanded_name(
4972 char_u *in_start,
4973 char_u *expr_start,
4974 char_u *expr_end,
4975 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004976{
4977 char_u c1;
4978 char_u *retval = NULL;
4979 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004980
4981 if (expr_end == NULL || in_end == NULL)
4982 return NULL;
4983 *expr_start = NUL;
4984 *expr_end = NUL;
4985 c1 = *in_end;
4986 *in_end = NUL;
4987
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004988 temp_result = eval_to_string(expr_start + 1, FALSE);
4989 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004990 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004991 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4992 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004993 if (retval != NULL)
4994 {
4995 STRCPY(retval, in_start);
4996 STRCAT(retval, temp_result);
4997 STRCAT(retval, expr_end + 1);
4998 }
4999 }
5000 vim_free(temp_result);
5001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005002 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005003 *expr_start = '{';
5004 *expr_end = '}';
5005
5006 if (retval != NULL)
5007 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005008 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005009 if (expr_start != NULL)
5010 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005011 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005012 temp_result = make_expanded_name(retval, expr_start,
5013 expr_end, temp_result);
5014 vim_free(retval);
5015 retval = temp_result;
5016 }
5017 }
5018
5019 return retval;
5020}
5021
5022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005024 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005026 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005027eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005029 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5030}
5031
5032/*
5033 * Return TRUE if character "c" can be used as the first character in a
5034 * variable or function name (excluding '{' and '}').
5035 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005036 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005037eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005038{
5039 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040}
5041
5042/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005043 * Handle:
5044 * - expr[expr], expr[expr:expr] subscript
5045 * - ".name" lookup
5046 * - function call with Funcref variable: func(expr)
5047 * - method call: var->method()
5048 *
5049 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005050 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005051 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005052handle_subscript(
5053 char_u **arg,
5054 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005055 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005056 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005057{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005058 int evaluate = evalarg != NULL
5059 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005060 int ret = OK;
5061 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005062 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005063 int getnext;
5064 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005065
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005066 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005067 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005068 // When at the end of the line and ".name" or "->{" or "->X" follows in
5069 // the next line then consume the line break.
5070 p = eval_next_non_blank(*arg, evalarg, &getnext);
5071 if (getnext
5072 && ((rettv->v_type == VAR_DICT && *p == '.'
5073 && ASCII_ISALPHA(p[1]))
5074 || (*p == '-' && p[1] == '>'
5075 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005076 {
5077 *arg = eval_next_line(evalarg);
5078 check_white = FALSE;
5079 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005080
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005081 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5082 || rettv->v_type == VAR_PARTIAL))
5083 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005084 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005085 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5086 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005087
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005088 // Stop the expression evaluation when immediately aborting on
5089 // error, or when an interrupt occurred or an exception was thrown
5090 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005091 if (aborting())
5092 {
5093 if (ret == OK)
5094 clear_tv(rettv);
5095 ret = FAIL;
5096 }
5097 dict_unref(selfdict);
5098 selfdict = NULL;
5099 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005100 else if (**arg == '-' && (*arg)[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005101 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005102 if (ret == OK)
5103 {
5104 if ((*arg)[2] == '{')
5105 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005106 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005107 else
5108 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005109 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005110 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005111 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005112 // "." is ".name" lookup when we found a dict or when evaluating and
5113 // scriptversion is at least 2, where string concatenation is "..".
5114 else if (**arg == '['
5115 || (**arg == '.' && (rettv->v_type == VAR_DICT
5116 || (!evaluate
5117 && (*arg)[1] != '.'
5118 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005119 {
5120 dict_unref(selfdict);
5121 if (rettv->v_type == VAR_DICT)
5122 {
5123 selfdict = rettv->vval.v_dict;
5124 if (selfdict != NULL)
5125 ++selfdict->dv_refcount;
5126 }
5127 else
5128 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005129 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005130 {
5131 clear_tv(rettv);
5132 ret = FAIL;
5133 }
5134 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005135 else
5136 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005137 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005139 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5140 // Don't do this when "Func" is already a partial that was bound
5141 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005142 if (selfdict != NULL
5143 && (rettv->v_type == VAR_FUNC
5144 || (rettv->v_type == VAR_PARTIAL
5145 && (rettv->vval.v_partial->pt_auto
5146 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005147 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005148
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005149 dict_unref(selfdict);
5150 return ret;
5151}
5152
5153/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005154 * Make a copy of an item.
5155 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005156 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5157 * reference to an already copied list/dict can be used.
5158 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005159 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005160 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005161item_copy(
5162 typval_T *from,
5163 typval_T *to,
5164 int deep,
5165 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166{
5167 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005168 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005169
Bram Moolenaar33570922005-01-25 22:26:29 +00005170 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005172 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005173 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 }
5175 ++recurse;
5176
5177 switch (from->v_type)
5178 {
5179 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005180 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005181 case VAR_STRING:
5182 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005183 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005184 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005185 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005186 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005187 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005188 copy_tv(from, to);
5189 break;
5190 case VAR_LIST:
5191 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005192 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005193 if (from->vval.v_list == NULL)
5194 to->vval.v_list = NULL;
5195 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5196 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005197 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005198 to->vval.v_list = from->vval.v_list->lv_copylist;
5199 ++to->vval.v_list->lv_refcount;
5200 }
5201 else
5202 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5203 if (to->vval.v_list == NULL)
5204 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005205 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005206 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005208 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005209 case VAR_DICT:
5210 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005211 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005212 if (from->vval.v_dict == NULL)
5213 to->vval.v_dict = NULL;
5214 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5215 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005216 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005217 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5218 ++to->vval.v_dict->dv_refcount;
5219 }
5220 else
5221 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5222 if (to->vval.v_dict == NULL)
5223 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005224 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005225 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005226 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005227 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005228 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005229 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005230 }
5231 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005232 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005233}
5234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 void
5236echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5237{
5238 char_u *tofree;
5239 char_u numbuf[NUMBUFLEN];
5240 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5241
5242 if (*atstart)
5243 {
5244 *atstart = FALSE;
5245 // Call msg_start() after eval1(), evaluating the expression
5246 // may cause a message to appear.
5247 if (with_space)
5248 {
5249 // Mark the saved text as finishing the line, so that what
5250 // follows is displayed on a new line when scrolling back
5251 // at the more prompt.
5252 msg_sb_eol();
5253 msg_start();
5254 }
5255 }
5256 else if (with_space)
5257 msg_puts_attr(" ", echo_attr);
5258
5259 if (p != NULL)
5260 for ( ; *p != NUL && !got_int; ++p)
5261 {
5262 if (*p == '\n' || *p == '\r' || *p == TAB)
5263 {
5264 if (*p != TAB && *needclr)
5265 {
5266 // remove any text still there from the command
5267 msg_clr_eos();
5268 *needclr = FALSE;
5269 }
5270 msg_putchar_attr(*p, echo_attr);
5271 }
5272 else
5273 {
5274 if (has_mbyte)
5275 {
5276 int i = (*mb_ptr2len)(p);
5277
5278 (void)msg_outtrans_len_attr(p, i, echo_attr);
5279 p += i - 1;
5280 }
5281 else
5282 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5283 }
5284 }
5285 vim_free(tofree);
5286}
5287
Bram Moolenaare9a41262005-01-15 22:18:47 +00005288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 * ":echo expr1 ..." print each argument separated with a space, add a
5290 * newline at the end.
5291 * ":echon expr1 ..." print each argument plain.
5292 */
5293 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005294ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295{
5296 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005297 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 char_u *p;
5299 int needclr = TRUE;
5300 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005301 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005302 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005303 evalarg_T evalarg;
5304
Bram Moolenaare707c882020-07-01 13:07:37 +02005305 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
5307 if (eap->skip)
5308 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005309 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005311 // If eval1() causes an error message the text from the command may
5312 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005313 need_clr_eos = needclr;
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005316 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
5318 /*
5319 * Report the invalid expression unless the expression evaluation
5320 * has been cancelled due to an aborting error, an interrupt, or an
5321 * exception.
5322 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005323 if (!aborting() && did_emsg == did_emsg_before
5324 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005325 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005326 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 break;
5328 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005329 need_clr_eos = FALSE;
5330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005332 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005333
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005334 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 arg = skipwhite(arg);
5336 }
5337 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005338 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
5340 if (eap->skip)
5341 --emsg_skip;
5342 else
5343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005344 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 if (needclr)
5346 msg_clr_eos();
5347 if (eap->cmdidx == CMD_echo)
5348 msg_end();
5349 }
5350}
5351
5352/*
5353 * ":echohl {name}".
5354 */
5355 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005356ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005358 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359}
5360
5361/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005362 * Returns the :echo attribute
5363 */
5364 int
5365get_echo_attr(void)
5366{
5367 return echo_attr;
5368}
5369
5370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 * ":execute expr1 ..." execute the result of an expression.
5372 * ":echomsg expr1 ..." Print a message
5373 * ":echoerr expr1 ..." Print an error
5374 * Each gets spaces around each argument and a newline at the end for
5375 * echo commands
5376 */
5377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005378ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005381 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 int ret = OK;
5383 char_u *p;
5384 garray_T ga;
5385 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
5387 ga_init2(&ga, 1, 80);
5388
5389 if (eap->skip)
5390 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005391 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005393 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005394 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396
5397 if (!eap->skip)
5398 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005399 char_u buf[NUMBUFLEN];
5400
5401 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005402 {
5403 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5404 {
5405 emsg(_(e_inval_string));
5406 p = NULL;
5407 }
5408 else
5409 p = tv_get_string_buf(&rettv, buf);
5410 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005411 else
5412 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005413 if (p == NULL)
5414 {
5415 clear_tv(&rettv);
5416 ret = FAIL;
5417 break;
5418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 len = (int)STRLEN(p);
5420 if (ga_grow(&ga, len + 2) == FAIL)
5421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005422 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 ret = FAIL;
5424 break;
5425 }
5426 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 ga.ga_len += len;
5430 }
5431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005432 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 arg = skipwhite(arg);
5434 }
5435
5436 if (ret != FAIL && ga.ga_data != NULL)
5437 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005438 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5439 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005440 // Mark the already saved text as finishing the line, so that what
5441 // follows is displayed on a new line when scrolling back at the
5442 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005443 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005444 }
5445
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005447 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005448 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005449 out_flush();
5450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 else if (eap->cmdidx == CMD_echoerr)
5452 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005453 int save_did_emsg = did_emsg;
5454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005455 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005456 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 if (!force_abort)
5458 did_emsg = save_did_emsg;
5459 }
5460 else if (eap->cmdidx == CMD_execute)
5461 do_cmdline((char_u *)ga.ga_data,
5462 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5463 }
5464
5465 ga_clear(&ga);
5466
5467 if (eap->skip)
5468 --emsg_skip;
5469
5470 eap->nextcmd = check_nextcmd(arg);
5471}
5472
5473/*
5474 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5475 * "arg" points to the "&" or '+' when called, to "option" when returning.
5476 * Returns NULL when no option name found. Otherwise pointer to the char
5477 * after the option name.
5478 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005479 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005480find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481{
5482 char_u *p = *arg;
5483
5484 ++p;
5485 if (*p == 'g' && p[1] == ':')
5486 {
5487 *opt_flags = OPT_GLOBAL;
5488 p += 2;
5489 }
5490 else if (*p == 'l' && p[1] == ':')
5491 {
5492 *opt_flags = OPT_LOCAL;
5493 p += 2;
5494 }
5495 else
5496 *opt_flags = 0;
5497
5498 if (!ASCII_ISALPHA(*p))
5499 return NULL;
5500 *arg = p;
5501
5502 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005503 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 else
5505 while (ASCII_ISALPHA(*p))
5506 ++p;
5507 return p;
5508}
5509
5510/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005511 * Display script name where an item was last set.
5512 * Should only be invoked when 'verbose' is non-zero.
5513 */
5514 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005515last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005516{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005517 char_u *p;
5518
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005519 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005520 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005521 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005522 if (p != NULL)
5523 {
5524 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005525 msg_puts(_("\n\tLast set from "));
5526 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005527 if (script_ctx.sc_lnum > 0)
5528 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005529 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005530 msg_outnum((long)script_ctx.sc_lnum);
5531 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005532 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005533 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005534 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005535 }
5536}
5537
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005538#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540/*
5541 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005542 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 * "flags" can be "g" to do a global substitute.
5544 * Returns an allocated string, NULL for error.
5545 */
5546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005547do_string_sub(
5548 char_u *str,
5549 char_u *pat,
5550 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005551 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005552 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 int sublen;
5555 regmatch_T regmatch;
5556 int i;
5557 int do_all;
5558 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005559 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 garray_T ga;
5561 char_u *ret;
5562 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005563 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005565 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005567 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 ga_init2(&ga, 1, 200);
5570
5571 do_all = (flags[0] == 'g');
5572
5573 regmatch.rm_ic = p_ic;
5574 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5575 if (regmatch.regprog != NULL)
5576 {
5577 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005578 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5580 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005581 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005582 if (regmatch.startp[0] == regmatch.endp[0])
5583 {
5584 if (zero_width == regmatch.startp[0])
5585 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005586 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005587 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005588 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5589 (size_t)i);
5590 ga.ga_len += i;
5591 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005592 continue;
5593 }
5594 zero_width = regmatch.startp[0];
5595 }
5596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 /*
5598 * Get some space for a temporary buffer to do the substitution
5599 * into. It will contain:
5600 * - The text up to where the match is.
5601 * - The substituted text.
5602 * - The text after the match.
5603 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005604 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005605 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5607 {
5608 ga_clear(&ga);
5609 break;
5610 }
5611
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005612 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 i = (int)(regmatch.startp[0] - tail);
5614 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005615 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005616 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 + ga.ga_len + i, TRUE, TRUE, FALSE);
5618 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005619 tail = regmatch.endp[0];
5620 if (*tail == NUL)
5621 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 if (!do_all)
5623 break;
5624 }
5625
5626 if (ga.ga_data != NULL)
5627 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5628
Bram Moolenaar473de612013-06-08 18:19:48 +02005629 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
5631
5632 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5633 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005634 if (p_cpo == empty_option)
5635 p_cpo = save_cpo;
5636 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005637 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005638 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
5640 return ret;
5641}