blob: 877b3bc0ffcd8ace3ce0ce4c916ba8c973c7c2e0 [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 Moolenaarf96e9de2020-08-03 22:39:28 +0200303 if (*skipwhite(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.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200382 * "arg" is advanced to just after the expression.
383 * "start" is set to the start of the expression, "end" to just after the end.
384 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200385 * Return FAIL for an error, OK otherwise.
386 */
387 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200388skip_expr_concatenate(
389 char_u **arg,
390 char_u **start,
391 char_u **end,
392 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200393{
394 typval_T rettv;
395 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200396 int vim9script = in_vim9script();
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200397 garray_T *gap = &evalarg->eval_ga;
398 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
399
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200400 if (vim9script
401 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 {
403 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 ++gap->ga_len;
407 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200408 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409
410 // Don't evaluate the expression.
411 if (evalarg != NULL)
412 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200413 *arg = skipwhite(*arg);
414 res = eval1(arg, &rettv, evalarg);
415 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200416 if (evalarg != NULL)
417 evalarg->eval_flags = save_flags;
418
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 if (vim9script
420 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200421 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200422 if (evalarg->eval_ga.ga_len == 1)
423 {
424 // just one line, no need to concatenate
425 ga_clear(gap);
426 gap->ga_itemsize = 0;
427 }
428 else
429 {
430 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200431 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200432
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200433 // Line breaks encountered, concatenate all the lines.
434 *((char_u **)gap->ga_data) = *start;
435 p = ga_concat_strings(gap, "");
436
437 // free the lines only when using getsourceline()
438 if (evalarg->eval_cookie != NULL)
439 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200440 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200441 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200442 // Do not free the last line, "arg" points into it, free it
443 // later.
444 vim_free(evalarg->eval_tofree);
445 evalarg->eval_tofree =
446 ((char_u **)gap->ga_data)[gap->ga_len - 1];
447 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200448 ga_clear_strings(gap);
449 }
450 else
451 ga_clear(gap);
452 gap->ga_itemsize = 0;
453 if (p == NULL)
454 return FAIL;
455 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200456 vim_free(evalarg->eval_tofree_lambda);
457 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200458 // Compute "end" relative to the end.
459 *end = *start + STRLEN(*start) - endoff;
460 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461 }
462
463 return res;
464}
465
466/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200467 * Top level evaluation function, returning a string. Does not handle line
468 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000469 * When "convert" is TRUE convert a List into a sequence of lines and convert
470 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 * Return pointer to allocated memory, or NULL for failure.
472 */
473 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100474eval_to_string(
475 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100476 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477{
Bram Moolenaar33570922005-01-25 22:26:29 +0000478 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000480 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000481#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000482 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200485 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 retval = NULL;
487 else
488 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000489 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000490 {
491 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000492 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200493 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200494 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200495 if (tv.vval.v_list->lv_len > 0)
496 ga_append(&ga, NL);
497 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000498 ga_append(&ga, NUL);
499 retval = (char_u *)ga.ga_data;
500 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000501#ifdef FEAT_FLOAT
502 else if (convert && tv.v_type == VAR_FLOAT)
503 {
504 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
505 retval = vim_strsave(numbuf);
506 }
507#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000508 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100509 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000510 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200512 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
514 return retval;
515}
516
517/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000518 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200519 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 */
521 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100522eval_to_string_safe(
523 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100524 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
526 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200527 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200529 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000530 if (use_sandbox)
531 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200532 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200533 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000534 if (use_sandbox)
535 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200536 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200537 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 return retval;
539}
540
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541/*
542 * Top level evaluation function, returning a number.
543 * Evaluates "expr" silently.
544 * Returns -1 for an error.
545 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200546 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100547eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
Bram Moolenaar33570922005-01-25 22:26:29 +0000549 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200550 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000551 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
553 ++emsg_off;
554
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200555 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 retval = -1;
557 else
558 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100559 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000560 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 }
562 --emsg_off;
563
564 return retval;
565}
566
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000567/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000568 * Top level evaluation function.
569 * Returns an allocated typval_T with the result.
570 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000571 */
572 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200573eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000574{
575 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200576 evalarg_T evalarg;
577
578 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000579
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200580 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200581 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100582 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000583
Bram Moolenaar37c83712020-06-30 21:18:36 +0200584 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000585 return tv;
586}
587
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100589 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200590 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
591 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000592 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200594 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100595call_vim_function(
596 char_u *func,
597 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200598 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200599 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000601 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200602 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100604 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200605 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200606 funcexe.firstline = curwin->w_cursor.lnum;
607 funcexe.lastline = curwin->w_cursor.lnum;
608 funcexe.evaluate = TRUE;
609 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000610 if (ret == FAIL)
611 clear_tv(rettv);
612
613 return ret;
614}
615
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100616/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100617 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100618 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200619 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
620 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100621 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200622 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100623call_func_retnr(
624 char_u *func,
625 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200626 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100627{
628 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200629 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100630
Bram Moolenaarded27a12018-08-01 19:06:03 +0200631 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100632 return -1;
633
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100634 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100635 clear_tv(&rettv);
636 return retval;
637}
638
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000639/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100640 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000641 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200642 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
643 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000644 */
645 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100646call_func_retstr(
647 char_u *func,
648 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200649 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000650{
651 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000652 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000653
Bram Moolenaarded27a12018-08-01 19:06:03 +0200654 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000655 return NULL;
656
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100657 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000658 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 return retval;
660}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000661
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000662/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100663 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200664 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
665 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000666 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000667 */
668 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100669call_func_retlist(
670 char_u *func,
671 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200672 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000673{
674 typval_T rettv;
675
Bram Moolenaarded27a12018-08-01 19:06:03 +0200676 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000677 return NULL;
678
679 if (rettv.v_type != VAR_LIST)
680 {
681 clear_tv(&rettv);
682 return NULL;
683 }
684
685 return rettv.vval.v_list;
686}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#ifdef FEAT_FOLDING
689/*
690 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
691 * it in "*cp". Doesn't give error messages.
692 */
693 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100694eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695{
Bram Moolenaar33570922005-01-25 22:26:29 +0000696 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200697 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000699 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
700 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
702 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000703 if (use_sandbox)
704 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200705 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200707 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 retval = 0;
709 else
710 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100711 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000712 if (tv.v_type == VAR_NUMBER)
713 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000714 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 retval = 0;
716 else
717 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100718 // If the result is a string, check if there is a non-digit before
719 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000720 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 if (!VIM_ISDIGIT(*s) && *s != '-')
722 *cp = *s++;
723 retval = atol((char *)s);
724 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000725 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 }
727 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000728 if (use_sandbox)
729 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200730 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200731 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200733 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734}
735#endif
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000738 * Get an lval: variable, Dict item or List item that can be assigned a value
739 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
740 * "name.key", "name.key[expr]" etc.
741 * Indexing only works if "name" is an existing List or Dictionary.
742 * "name" points to the start of the name.
743 * If "rettv" is not NULL it points to the value to be assigned.
744 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
745 * wrong; must end in space or cmd separator.
746 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100747 * flags:
748 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100749 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100750 * GLV_NO_AUTOLOAD: do not use script autoloading
751 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000752 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000753 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000754 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000755 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200756 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100757get_lval(
758 char_u *name,
759 typval_T *rettv,
760 lval_T *lp,
761 int unlet,
762 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100763 int flags, // GLV_ values
764 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000765{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000766 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000767 char_u *expr_start, *expr_end;
768 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000769 dictitem_T *v;
770 typval_T var1;
771 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000773 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000774 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000775 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000776 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100777 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000778
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100779 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200780 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000781
782 if (skip)
783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100784 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000785 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200786 lp->ll_name_end = find_name_end(name, NULL, NULL,
787 FNE_INCL_BR | fne_flags);
788 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000789 }
790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100791 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000792 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000794 if (expr_start != NULL)
795 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100796 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100797 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000798 && *p != '[' && *p != '.')
799 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200800 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 return NULL;
802 }
803
804 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
805 if (lp->ll_exp_name == NULL)
806 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100807 // Report an invalid expression in braces, unless the
808 // expression evaluation has been cancelled due to an
809 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000810 if (!aborting() && !quiet)
811 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000812 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100813 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000814 return NULL;
815 }
816 }
817 lp->ll_name = lp->ll_exp_name;
818 }
819 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 lp->ll_name = name;
822
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200823 if (in_vim9script() && *p == ':')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100825 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 char_u *tp = skipwhite(p + 1);
827
828 // parse the type after the name
829 lp->ll_type = parse_type(&tp, &si->sn_type_list);
830 lp->ll_name_end = tp;
831 }
832 }
833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100834 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
836 return p;
837
838 cc = *p;
839 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100840 // Only pass &ht when we would write to the variable, it prevents autoload
841 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100842 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
843 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100845 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000846 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000847 if (v == NULL)
848 return NULL;
849
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 /*
851 * Loop until no more [idx] or .key is following.
852 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000853 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100854 var1.v_type = VAR_UNKNOWN;
855 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000857 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
859 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100860 && lp->ll_tv->vval.v_dict != NULL)
861 && !(lp->ll_tv->v_type == VAR_BLOB
862 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000864 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100865 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000867 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100871 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000873 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000874
Bram Moolenaar8c711452005-01-14 21:53:12 +0000875 len = -1;
876 if (*p == '.')
877 {
878 key = p + 1;
879 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
880 ;
881 if (len == 0)
882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100884 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000885 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000886 }
887 p = key + len;
888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000889 else
890 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100891 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000892 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000893 if (*p == ':')
894 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000895 else
896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000897 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200898 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100900 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100902 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000903 clear_tv(&var1);
904 return NULL;
905 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200906 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000907 }
908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100909 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000910 if (*p == ':')
911 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000912 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000913 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000914 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100915 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100916 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000918 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100919 if (rettv != NULL
920 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100921 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100922 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100923 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000924 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000925 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100926 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100927 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000928 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000929 }
930 p = skipwhite(p + 1);
931 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000932 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000933 else
934 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000935 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200936 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200937 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000938 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100939 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000941 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100942 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000943 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100944 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100945 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000946 clear_tv(&var2);
947 return NULL;
948 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000949 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000950 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000951 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000952 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000953 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000954
Bram Moolenaar8c711452005-01-14 21:53:12 +0000955 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000956 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000957 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100958 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100959 clear_tv(&var1);
960 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000961 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000962 }
963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100964 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000965 ++p;
966 }
967
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000968 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000969 {
970 if (len == -1)
971 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100972 // "[key]": get key from "var1"
973 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200974 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000976 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 }
979 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000980 lp->ll_list = NULL;
981 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000982 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200983
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100984 // When assigning to a scope dictionary check that a function and
985 // variable name is valid (only variable name unless it is l: or
986 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200987 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200988 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200989 int prevval;
990 int wrong;
991
992 if (len != -1)
993 {
994 prevval = key[len];
995 key[len] = NUL;
996 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200997 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100998 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200999 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1000 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001001 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001002 || !valid_varname(key);
1003 if (len != -1)
1004 key[len] = prevval;
1005 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001006 {
1007 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001008 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001009 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001010 }
1011
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001013 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001014 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001015 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001016 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001017 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001018 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001019 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001020 return NULL;
1021 }
1022
Bram Moolenaar31b81602019-02-10 22:14:27 +01001023 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001024 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001025 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001026 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001027 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001028 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001029 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001030 }
1031 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001032 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001033 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001034 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001035 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001037 p = NULL;
1038 break;
1039 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001040 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001041 else if ((flags & GLV_READ_ONLY) == 0
1042 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001043 {
1044 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001045 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001046 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001047
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001048 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001049 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001050 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001051 else if (lp->ll_tv->v_type == VAR_BLOB)
1052 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001053 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1054
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001055 /*
1056 * Get the number and item for the only or first index of the List.
1057 */
1058 if (empty1)
1059 lp->ll_n1 = 0;
1060 else
1061 // is number or string
1062 lp->ll_n1 = (long)tv_get_number(&var1);
1063 clear_tv(&var1);
1064
1065 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001066 || lp->ll_n1 > bloblen
1067 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001068 {
1069 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001070 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001071 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001072 return NULL;
1073 }
1074 if (lp->ll_range && !lp->ll_empty2)
1075 {
1076 lp->ll_n2 = (long)tv_get_number(&var2);
1077 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001078 if (lp->ll_n2 < 0
1079 || lp->ll_n2 >= bloblen
1080 || lp->ll_n2 < lp->ll_n1)
1081 {
1082 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001083 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001084 return NULL;
1085 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001086 }
1087 lp->ll_blob = lp->ll_tv->vval.v_blob;
1088 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001089 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001090 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001091 else
1092 {
1093 /*
1094 * Get the number and item for the only or first index of the List.
1095 */
1096 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001097 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001098 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001099 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001100 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001101 clear_tv(&var1);
1102
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001103 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001104 lp->ll_list = lp->ll_tv->vval.v_list;
1105 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1106 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001107 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001108 if (lp->ll_n1 < 0)
1109 {
1110 lp->ll_n1 = 0;
1111 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1112 }
1113 }
1114 if (lp->ll_li == NULL)
1115 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001116 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001117 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001118 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001119 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001120 }
1121
1122 /*
1123 * May need to find the item or absolute index for the second
1124 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 * When no index given: "lp->ll_empty2" is TRUE.
1126 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001127 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001128 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001129 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001130 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001131 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001132 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001134 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001136 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001137 {
1138 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001139 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001141 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 }
1144
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001145 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 if (lp->ll_n1 < 0)
1147 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1148 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001149 {
1150 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001151 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001153 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001154 }
1155
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001156 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001157 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 }
1159
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001160 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 return p;
1163}
1164
1165/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001166 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001169clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170{
1171 vim_free(lp->ll_exp_name);
1172 vim_free(lp->ll_newkey);
1173}
1174
1175/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001176 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001177 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001178 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1179 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001180 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001182set_var_lval(
1183 lval_T *lp,
1184 char_u *endp,
1185 typval_T *rettv,
1186 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001188 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001189{
1190 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001191 listitem_T *ri;
1192 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001193
1194 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001195 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001196 cc = *endp;
1197 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001198 if (lp->ll_blob != NULL)
1199 {
1200 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001201
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001202 if (op != NULL && *op != '=')
1203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001204 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001205 return;
1206 }
1207
1208 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1209 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001210 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001211
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001212 if (lp->ll_empty2)
1213 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1214
1215 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001217 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001218 return;
1219 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001220 if (lp->ll_empty2)
1221 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001222
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001223 ir = 0;
1224 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1225 blob_set(lp->ll_blob, il,
1226 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001227 }
1228 else
1229 {
1230 val = (int)tv_get_number_chk(rettv, &error);
1231 if (!error)
1232 {
1233 garray_T *gap = &lp->ll_blob->bv_ga;
1234
1235 // Allow for appending a byte. Setting a byte beyond
1236 // the end is an error otherwise.
1237 if (lp->ll_n1 < gap->ga_len
1238 || (lp->ll_n1 == gap->ga_len
1239 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1240 {
1241 blob_set(lp->ll_blob, lp->ll_n1, val);
1242 if (lp->ll_n1 == gap->ga_len)
1243 ++gap->ga_len;
1244 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001245 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001246 }
1247 }
1248 }
1249 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001250 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001251 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001254 {
1255 emsg(_(e_cannot_mod));
1256 *endp = cc;
1257 return;
1258 }
1259
Bram Moolenaarff697e62019-02-12 22:28:33 +01001260 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001261 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001262 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001263 &tv, &di, TRUE, FALSE) == OK)
1264 {
1265 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001266 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1267 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001268 && tv_op(&tv, rettv, op) == OK)
1269 set_var(lp->ll_name, &tv, FALSE);
1270 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001273 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001274 {
1275 if (lp->ll_type != NULL
1276 && check_typval_type(lp->ll_type, rettv) == FAIL)
1277 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001279 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001280 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001281 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001282 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001283 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001284 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001285 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001286 else if (lp->ll_range)
1287 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001288 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001289 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001292 {
1293 emsg(_("E996: Cannot lock a range"));
1294 return;
1295 }
1296
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001297 /*
1298 * Check whether any of the list items is locked
1299 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001300 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001301 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001302 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001303 return;
1304 ri = ri->li_next;
1305 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1306 break;
1307 ll_li = ll_li->li_next;
1308 ++ll_n1;
1309 }
1310
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001311 /*
1312 * Assign the List values to the list items.
1313 */
1314 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001315 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001316 if (op != NULL && *op != '=')
1317 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1318 else
1319 {
1320 clear_tv(&lp->ll_li->li_tv);
1321 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1322 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001323 ri = ri->li_next;
1324 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1325 break;
1326 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001327 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001328 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001329 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001330 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 ri = NULL;
1332 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001333 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001334 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335 lp->ll_li = lp->ll_li->li_next;
1336 ++lp->ll_n1;
1337 }
1338 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001339 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001340 else if (lp->ll_empty2
1341 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001343 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001344 }
1345 else
1346 {
1347 /*
1348 * Assign to a List or Dictionary item.
1349 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001351 {
1352 emsg(_("E996: Cannot lock a list or dict"));
1353 return;
1354 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001355 if (lp->ll_newkey != NULL)
1356 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001357 if (op != NULL && *op != '=')
1358 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001359 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001360 return;
1361 }
1362
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001363 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001364 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001365 if (di == NULL)
1366 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001367 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1368 {
1369 vim_free(di);
1370 return;
1371 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001372 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001373 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001374 else if (op != NULL && *op != '=')
1375 {
1376 tv_op(lp->ll_tv, rettv, op);
1377 return;
1378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001380 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001381
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001382 /*
1383 * Assign the value to the variable or list item.
1384 */
1385 if (copy)
1386 copy_tv(rettv, lp->ll_tv);
1387 else
1388 {
1389 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001390 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001391 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001392 }
1393 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394}
1395
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001396/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001397 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1398 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001399 * Returns OK or FAIL.
1400 */
1401 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001403{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001404 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001405 char_u numbuf[NUMBUFLEN];
1406 char_u *s;
1407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001408 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001409 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001410 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001411 {
1412 switch (tv1->v_type)
1413 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001414 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001415 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001417 case VAR_DICT:
1418 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001419 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001420 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001421 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001422 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001423 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001424 break;
1425
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001426 case VAR_BLOB:
1427 if (*op != '+' || tv2->v_type != VAR_BLOB)
1428 break;
1429 // BLOB += BLOB
1430 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1431 {
1432 blob_T *b1 = tv1->vval.v_blob;
1433 blob_T *b2 = tv2->vval.v_blob;
1434 int i, len = blob_len(b2);
1435 for (i = 0; i < len; i++)
1436 ga_append(&b1->bv_ga, blob_get(b2, i));
1437 }
1438 return OK;
1439
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001440 case VAR_LIST:
1441 if (*op != '+' || tv2->v_type != VAR_LIST)
1442 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001443 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001444 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1445 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1446 return OK;
1447
1448 case VAR_NUMBER:
1449 case VAR_STRING:
1450 if (tv2->v_type == VAR_LIST)
1451 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001452 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001454 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001455 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001456#ifdef FEAT_FLOAT
1457 if (tv2->v_type == VAR_FLOAT)
1458 {
1459 float_T f = n;
1460
Bram Moolenaarff697e62019-02-12 22:28:33 +01001461 if (*op == '%')
1462 break;
1463 switch (*op)
1464 {
1465 case '+': f += tv2->vval.v_float; break;
1466 case '-': f -= tv2->vval.v_float; break;
1467 case '*': f *= tv2->vval.v_float; break;
1468 case '/': f /= tv2->vval.v_float; break;
1469 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001470 clear_tv(tv1);
1471 tv1->v_type = VAR_FLOAT;
1472 tv1->vval.v_float = f;
1473 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001474 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001475#endif
1476 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001477 switch (*op)
1478 {
1479 case '+': n += tv_get_number(tv2); break;
1480 case '-': n -= tv_get_number(tv2); break;
1481 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001482 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1483 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001484 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001485 clear_tv(tv1);
1486 tv1->v_type = VAR_NUMBER;
1487 tv1->vval.v_number = n;
1488 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001489 }
1490 else
1491 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001492 if (tv2->v_type == VAR_FLOAT)
1493 break;
1494
Bram Moolenaarff697e62019-02-12 22:28:33 +01001495 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001496 s = tv_get_string(tv1);
1497 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001498 clear_tv(tv1);
1499 tv1->v_type = VAR_STRING;
1500 tv1->vval.v_string = s;
1501 }
1502 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001503
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001504 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001505#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001506 {
1507 float_T f;
1508
Bram Moolenaarff697e62019-02-12 22:28:33 +01001509 if (*op == '%' || *op == '.'
1510 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001511 && tv2->v_type != VAR_NUMBER
1512 && tv2->v_type != VAR_STRING))
1513 break;
1514 if (tv2->v_type == VAR_FLOAT)
1515 f = tv2->vval.v_float;
1516 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001517 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001518 switch (*op)
1519 {
1520 case '+': tv1->vval.v_float += f; break;
1521 case '-': tv1->vval.v_float -= f; break;
1522 case '*': tv1->vval.v_float *= f; break;
1523 case '/': tv1->vval.v_float /= f; break;
1524 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001525 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001526#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001527 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001528 }
1529 }
1530
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001531 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001532 return FAIL;
1533}
1534
1535/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001536 * Evaluate the expression used in a ":for var in expr" command.
1537 * "arg" points to "var".
1538 * Set "*errp" to TRUE for an error, FALSE otherwise;
1539 * Return a pointer that holds the info. Null when there is an error.
1540 */
1541 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001542eval_for_line(
1543 char_u *arg,
1544 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001545 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001546 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001547{
Bram Moolenaar33570922005-01-25 22:26:29 +00001548 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001549 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001550 typval_T tv;
1551 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001552 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001553
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001554 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001555
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001556 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001557 if (fi == NULL)
1558 return NULL;
1559
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001560 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 if (expr == NULL)
1562 return fi;
1563
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001564 expr = skipwhite_and_linebreak(expr, evalarg);
1565 if (expr[0] != 'i' || expr[1] != 'n'
1566 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001567 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001569 return fi;
1570 }
1571
1572 if (skip)
1573 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001574 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1575 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001576 {
1577 *errp = FALSE;
1578 if (!skip)
1579 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001580 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001581 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001582 l = tv.vval.v_list;
1583 if (l == NULL)
1584 {
1585 // a null list is like an empty list: do nothing
1586 clear_tv(&tv);
1587 }
1588 else
1589 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001591 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001593 // No need to increment the refcount, it's already set for
1594 // the list being used in "tv".
1595 fi->fi_list = l;
1596 list_add_watch(l, &fi->fi_lw);
1597 fi->fi_lw.lw_item = l->lv_first;
1598 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001599 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001600 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001601 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001602 fi->fi_bi = 0;
1603 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001604 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001605 typval_T btv;
1606
1607 // Make a copy, so that the iteration still works when the
1608 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001610 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001611 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001612 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001613 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001614 else
1615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001616 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001617 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001618 }
1619 }
1620 }
1621 if (skip)
1622 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001623 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001624
1625 return fi;
1626}
1627
1628/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001629 * Used when looping over a :for line, skip the "in expr" part.
1630 */
1631 void
1632skip_for_lines(void *fi_void, evalarg_T *evalarg)
1633{
1634 forinfo_T *fi = (forinfo_T *)fi_void;
1635 int i;
1636
1637 for (i = 0; i < fi->fi_break_count; ++i)
1638 eval_next_line(evalarg);
1639}
1640
1641/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001642 * Use the first item in a ":for" list. Advance to the next.
1643 * Assign the values to the variable (list). "arg" points to the first one.
1644 * Return TRUE when a valid item was found, FALSE when at end of list or
1645 * something wrong.
1646 */
1647 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001648next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001649{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001650 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 int result;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001652 int flag = in_vim9script() ? LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001653 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001655 if (fi->fi_blob != NULL)
1656 {
1657 typval_T tv;
1658
1659 if (fi->fi_bi >= blob_len(fi->fi_blob))
1660 return FALSE;
1661 tv.v_type = VAR_NUMBER;
1662 tv.v_lock = VAR_FIXED;
1663 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1664 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001665 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001666 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001667 }
1668
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 item = fi->fi_lw.lw_item;
1670 if (item == NULL)
1671 result = FALSE;
1672 else
1673 {
1674 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001675 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001676 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 }
1678 return result;
1679}
1680
1681/*
1682 * Free the structure used to store info used by ":for".
1683 */
1684 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001685free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686{
Bram Moolenaar33570922005-01-25 22:26:29 +00001687 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001689 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001690 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001692 list_unref(fi->fi_list);
1693 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001694 if (fi != NULL && fi->fi_blob != NULL)
1695 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 vim_free(fi);
1697}
1698
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001700set_context_for_expression(
1701 expand_T *xp,
1702 char_u *arg,
1703 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
1705 int got_eq = FALSE;
1706 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001709 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001710 {
1711 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001712 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001714 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001715 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 {
1717 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001718 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001719 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 break;
1721 }
1722 return;
1723 }
1724 }
1725 else
1726 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1727 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 while ((xp->xp_pattern = vim_strpbrk(arg,
1729 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1730 {
1731 c = *xp->xp_pattern;
1732 if (c == '&')
1733 {
1734 c = xp->xp_pattern[1];
1735 if (c == '&')
1736 {
1737 ++xp->xp_pattern;
1738 xp->xp_context = cmdidx != CMD_let || got_eq
1739 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1740 }
1741 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001744 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1745 xp->xp_pattern += 2;
1746
1747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
1749 else if (c == '$')
1750 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001751 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 xp->xp_context = EXPAND_ENV_VARS;
1753 }
1754 else if (c == '=')
1755 {
1756 got_eq = TRUE;
1757 xp->xp_context = EXPAND_EXPRESSION;
1758 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001759 else if (c == '#'
1760 && xp->xp_context == EXPAND_EXPRESSION)
1761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001762 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001763 break;
1764 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001765 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 && xp->xp_context == EXPAND_FUNCTIONS
1767 && vim_strchr(xp->xp_pattern, '(') == NULL)
1768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001769 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 break;
1771 }
1772 else if (cmdidx != CMD_let || got_eq)
1773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001774 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 {
1776 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1777 if (c == '\\' && xp->xp_pattern[1] != NUL)
1778 ++xp->xp_pattern;
1779 xp->xp_context = EXPAND_NOTHING;
1780 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001781 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001783 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1785 /* skip */ ;
1786 xp->xp_context = EXPAND_NOTHING;
1787 }
1788 else if (c == '|')
1789 {
1790 if (xp->xp_pattern[1] == '|')
1791 {
1792 ++xp->xp_pattern;
1793 xp->xp_context = EXPAND_EXPRESSION;
1794 }
1795 else
1796 xp->xp_context = EXPAND_COMMANDS;
1797 }
1798 else
1799 xp->xp_context = EXPAND_EXPRESSION;
1800 }
1801 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001802 // Doesn't look like something valid, expand as an expression
1803 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 arg = xp->xp_pattern;
1806 if (*arg != NUL)
1807 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1808 /* skip */ ;
1809 }
1810 xp->xp_pattern = arg;
1811}
1812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001814 * Return TRUE if "pat" matches "text".
1815 * Does not use 'cpo' and always uses 'magic'.
1816 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001817 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001818pattern_match(char_u *pat, char_u *text, int ic)
1819{
1820 int matches = FALSE;
1821 char_u *save_cpo;
1822 regmatch_T regmatch;
1823
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001824 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001825 save_cpo = p_cpo;
1826 p_cpo = (char_u *)"";
1827 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1828 if (regmatch.regprog != NULL)
1829 {
1830 regmatch.rm_ic = ic;
1831 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1832 vim_regfree(regmatch.regprog);
1833 }
1834 p_cpo = save_cpo;
1835 return matches;
1836}
1837
1838/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001839 * Handle a name followed by "(". Both for just "name(arg)" and for
1840 * "expr->name(arg)".
1841 * Returns OK or FAIL.
1842 */
1843 static int
1844eval_func(
1845 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001846 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001847 char_u *name,
1848 int name_len,
1849 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001850 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001851 typval_T *basetv) // "expr" for "expr->name(arg)"
1852{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001853 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001854 char_u *s = name;
1855 int len = name_len;
1856 partial_T *partial;
1857 int ret = OK;
1858
1859 if (!evaluate)
1860 check_vars(s, len);
1861
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001862 // If "s" is the name of a variable of type VAR_FUNC
1863 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001864 s = deref_func_name(s, &len, &partial, !evaluate);
1865
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001866 // Need to make a copy, in case evaluating the arguments makes
1867 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001868 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001869 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001870 ret = FAIL;
1871 else
1872 {
1873 funcexe_T funcexe;
1874
1875 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001876 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001877 funcexe.firstline = curwin->w_cursor.lnum;
1878 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001879 funcexe.evaluate = evaluate;
1880 funcexe.partial = partial;
1881 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001882 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001883 }
1884 vim_free(s);
1885
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001886 // If evaluate is FALSE rettv->v_type was not set in
1887 // get_func_tv, but it's needed in handle_subscript() to parse
1888 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001889 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1890 {
1891 rettv->vval.v_string = NULL;
1892 rettv->v_type = VAR_FUNC;
1893 }
1894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001895 // Stop the expression evaluation when immediately
1896 // aborting on error, or when an interrupt occurred or
1897 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001898 if (evaluate && aborting())
1899 {
1900 if (ret == OK)
1901 clear_tv(rettv);
1902 ret = FAIL;
1903 }
1904 return ret;
1905}
1906
1907/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001908 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1909 * comment) and there is a next line, return the next line (skipping blanks)
1910 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001911 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1912 * "arg" must point somewhere inside a line, not at the start.
1913 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001914 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001915eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1916{
Bram Moolenaar9d489562020-07-30 20:08:50 +02001917 char_u *p = skipwhite(arg);
1918
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001919 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001920 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001921 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001922 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001923 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001924 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02001925 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001926
1927 if (evalarg->eval_cookie != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001928 next = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001929 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001930 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001931
Bram Moolenaar9d489562020-07-30 20:08:50 +02001932 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001933 {
1934 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001935 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001936 }
1937 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02001938 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001939}
1940
1941/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001942 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001943 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001944 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001945eval_next_line(evalarg_T *evalarg)
1946{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001947 garray_T *gap = &evalarg->eval_ga;
1948 char_u *line;
1949
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001950 if (evalarg->eval_cookie != NULL)
1951 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
1952 else
1953 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001954 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001955 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1956 {
1957 // Going to concatenate the lines after parsing.
1958 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1959 ++gap->ga_len;
1960 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001961 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001962 {
1963 vim_free(evalarg->eval_tofree);
1964 evalarg->eval_tofree = line;
1965 }
1966 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001967}
1968
Bram Moolenaare6b53242020-07-01 17:28:33 +02001969/*
1970 * Call eval_next_non_blank() and get the next line if needed.
1971 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001972 char_u *
1973skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1974{
1975 int getnext;
1976 char_u *p = skipwhite(arg);
1977
Bram Moolenaar962d7212020-07-04 14:15:00 +02001978 if (evalarg == NULL)
1979 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02001980 eval_next_non_blank(p, evalarg, &getnext);
1981 if (getnext)
1982 return eval_next_line(evalarg);
1983 return p;
1984}
1985
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001986/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001987 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001988 */
1989 void
1990clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1991{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001992 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001993 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001994 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001995 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001996 if (eap != NULL)
1997 {
1998 // We may need to keep the original command line, e.g. for
1999 // ":let" it has the variable names. But we may also need the
2000 // new one, "nextcmd" points into it. Keep both.
2001 vim_free(eap->cmdline_tofree);
2002 eap->cmdline_tofree = *eap->cmdlinep;
2003 *eap->cmdlinep = evalarg->eval_tofree;
2004 }
2005 else
2006 vim_free(evalarg->eval_tofree);
2007 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002008 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002009
2010 vim_free(evalarg->eval_tofree_lambda);
2011 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002012 }
2013}
2014
2015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002017 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2019 */
2020
2021/*
2022 * Handle zero level expression.
2023 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002025 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002026 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 * Return OK or FAIL.
2028 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002029 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002030eval0(
2031 char_u *arg,
2032 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002033 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002034 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 int ret;
2037 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002038 int did_emsg_before = did_emsg;
2039 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002040 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002043 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002044 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002045
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002046 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
2048 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002049 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 /*
2051 * Report the invalid expression unless the expression evaluation has
2052 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002053 * exception, or we already gave a more specific error.
2054 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002056 if (!aborting()
2057 && did_emsg == did_emsg_before
2058 && called_emsg == called_emsg_before
2059 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002060 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 ret = FAIL;
2062 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002063
2064 if (eap != NULL)
2065 eap->nextcmd = check_nextcmd(p);
2066
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 return ret;
2068}
2069
2070/*
2071 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002072 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 *
2074 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002075 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002077 * Note: "rettv.v_lock" is not set.
2078 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 * Return OK or FAIL.
2080 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002081 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002082eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002084 char_u *p;
2085 int getnext;
2086
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 /*
2088 * Get the first variable.
2089 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 return FAIL;
2092
Bram Moolenaar793648f2020-06-26 21:28:25 +02002093 p = eval_next_non_blank(*arg, evalarg, &getnext);
2094 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002096 int result;
2097 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002098 evalarg_T *evalarg_used = evalarg;
2099 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002100 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002101 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002102
2103 if (evalarg == NULL)
2104 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002105 CLEAR_FIELD(local_evalarg);
2106 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002107 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002108 orig_flags = evalarg_used->eval_flags;
2109 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002110
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002111 if (getnext)
2112 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002113 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002114 {
2115 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2116 {
2117 error_white_both(p, 1);
2118 clear_tv(rettv);
2119 return FAIL;
2120 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002121 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002122 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002123
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002125 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002127 int error = FALSE;
2128
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002129 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002131 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002132 if (error)
2133 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135
2136 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002137 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002139 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2140 {
2141 error_white_both(p, 1);
2142 clear_tv(rettv);
2143 return FAIL;
2144 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002145 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2146 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002147 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002148 if (eval1(arg, rettv, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 return FAIL;
2150
2151 /*
2152 * Check for the ":".
2153 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002154 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar793648f2020-06-26 21:28:25 +02002155 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 return FAIL;
2161 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002162 if (getnext)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002163 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002164 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002165 {
2166 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2167 {
2168 error_white_both(p, 1);
2169 clear_tv(rettv);
2170 return FAIL;
2171 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002172 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
2175 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002176 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002178 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2179 {
2180 error_white_both(p, 1);
2181 clear_tv(rettv);
2182 return FAIL;
2183 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002184 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2185 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002186 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002187 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
2189 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 return FAIL;
2192 }
2193 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 *rettv = var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002195
2196 if (evalarg == NULL)
2197 clear_evalarg(&local_evalarg, NULL);
2198 else
2199 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 }
2201
2202 return OK;
2203}
2204
2205/*
2206 * Handle first level expression:
2207 * expr2 || expr2 || expr2 logical OR
2208 *
2209 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002210 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 *
2212 * Return OK or FAIL.
2213 */
2214 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002215eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002217 char_u *p;
2218 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 /*
2221 * Get the first variable.
2222 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002223 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 return FAIL;
2225
2226 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002227 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002229 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002230 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002232 evalarg_T *evalarg_used = evalarg;
2233 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002234 int evaluate;
2235 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002236 long result = FALSE;
2237 typval_T var2;
2238 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002239 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002240
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002241 if (evalarg == NULL)
2242 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002243 CLEAR_FIELD(local_evalarg);
2244 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002245 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002246 orig_flags = evalarg_used->eval_flags;
2247 evaluate = orig_flags & EVAL_EVALUATE;
2248 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002249 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002250 if (vim9script)
2251 {
2252 result = tv2bool(rettv);
2253 }
2254 else
2255 {
2256 error = FALSE;
2257 if (tv_get_number_chk(rettv, &error) != 0)
2258 result = TRUE;
2259 clear_tv(rettv);
2260 if (error)
2261 return FAIL;
2262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264
2265 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002266 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002268 while (p[0] == '|' && p[1] == '|')
2269 {
2270 if (getnext)
2271 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002272 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002273 {
2274 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2275 {
2276 error_white_both(p, 2);
2277 clear_tv(rettv);
2278 return FAIL;
2279 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002280 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002281 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002282
2283 /*
2284 * Get the second variable.
2285 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002286 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2287 {
2288 error_white_both(p, 2);
2289 clear_tv(rettv);
2290 return FAIL;
2291 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002292 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2293 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002295 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002296 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002297
2298 /*
2299 * Compute the result.
2300 */
2301 if (evaluate && !result)
2302 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002303 if (vim9script)
2304 {
2305 clear_tv(rettv);
2306 *rettv = var2;
2307 result = tv2bool(rettv);
2308 }
2309 else
2310 {
2311 if (tv_get_number_chk(&var2, &error) != 0)
2312 result = TRUE;
2313 clear_tv(&var2);
2314 if (error)
2315 return FAIL;
2316 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002317 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002318 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002319 {
2320 rettv->v_type = VAR_NUMBER;
2321 rettv->vval.v_number = result;
2322 }
2323
2324 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002326
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002327 if (evalarg == NULL)
2328 clear_evalarg(&local_evalarg, NULL);
2329 else
2330 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
2332
2333 return OK;
2334}
2335
2336/*
2337 * Handle second level expression:
2338 * expr3 && expr3 && expr3 logical AND
2339 *
2340 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002341 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 *
2343 * Return OK or FAIL.
2344 */
2345 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002346eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002348 char_u *p;
2349 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
2351 /*
2352 * Get the first variable.
2353 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002354 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 return FAIL;
2356
2357 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002358 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002360 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002361 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002363 evalarg_T *evalarg_used = evalarg;
2364 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002365 int orig_flags;
2366 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002367 long result = TRUE;
2368 typval_T var2;
2369 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002370 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002371
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002372 if (evalarg == NULL)
2373 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002374 CLEAR_FIELD(local_evalarg);
2375 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002376 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002377 orig_flags = evalarg_used->eval_flags;
2378 evaluate = orig_flags & EVAL_EVALUATE;
2379 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002380 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002381 if (vim9script)
2382 {
2383 result = tv2bool(rettv);
2384 }
2385 else
2386 {
2387 error = FALSE;
2388 if (tv_get_number_chk(rettv, &error) == 0)
2389 result = FALSE;
2390 clear_tv(rettv);
2391 if (error)
2392 return FAIL;
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395
2396 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002397 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002399 while (p[0] == '&' && p[1] == '&')
2400 {
2401 if (getnext)
2402 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002403 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002404 {
2405 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2406 {
2407 error_white_both(p, 2);
2408 clear_tv(rettv);
2409 return FAIL;
2410 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002411 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002412 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002413
2414 /*
2415 * Get the second variable.
2416 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002417 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2418 {
2419 error_white_both(p, 2);
2420 clear_tv(rettv);
2421 return FAIL;
2422 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002423 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2424 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002425 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002426 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002427 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002428
2429 /*
2430 * Compute the result.
2431 */
2432 if (evaluate && result)
2433 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002434 if (vim9script)
2435 {
2436 clear_tv(rettv);
2437 *rettv = var2;
2438 result = tv2bool(rettv);
2439 }
2440 else
2441 {
2442 if (tv_get_number_chk(&var2, &error) == 0)
2443 result = FALSE;
2444 clear_tv(&var2);
2445 if (error)
2446 return FAIL;
2447 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002448 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002449 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002450 {
2451 rettv->v_type = VAR_NUMBER;
2452 rettv->vval.v_number = result;
2453 }
2454
2455 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002457
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002458 if (evalarg == NULL)
2459 clear_evalarg(&local_evalarg, NULL);
2460 else
2461 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
2463
2464 return OK;
2465}
2466
2467/*
2468 * Handle third level expression:
2469 * var1 == var2
2470 * var1 =~ var2
2471 * var1 != var2
2472 * var1 !~ var2
2473 * var1 > var2
2474 * var1 >= var2
2475 * var1 < var2
2476 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002477 * var1 is var2
2478 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 *
2480 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002481 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 *
2483 * Return OK or FAIL.
2484 */
2485 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002486eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002489 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002490 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002492 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494 /*
2495 * Get the first variable.
2496 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002497 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 return FAIL;
2499
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002500 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002501 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002504 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002506 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002508 typval_T var2;
2509 int ic;
2510 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002511 int evaluate = evalarg == NULL
2512 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002513
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002514 if (getnext)
2515 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002516 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2517 {
2518 error_white_both(p, len);
2519 clear_tv(rettv);
2520 return FAIL;
2521 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002522
Bram Moolenaar696ba232020-07-29 21:20:41 +02002523 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2524 {
2525 semsg(_(e_invexpr2), p);
2526 clear_tv(rettv);
2527 return FAIL;
2528 }
2529
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002530 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (p[len] == '?')
2532 {
2533 ic = TRUE;
2534 ++len;
2535 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002536 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 else if (p[len] == '#')
2538 {
2539 ic = FALSE;
2540 ++len;
2541 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002542 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002544 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
2546 /*
2547 * Get the second variable.
2548 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002549 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2550 {
2551 error_white_both(p, 1);
2552 clear_tv(rettv);
2553 return FAIL;
2554 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002555 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002556 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 return FAIL;
2560 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002561 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002562 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002563 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002564
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002565 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002566 {
2567 ret = FAIL;
2568 clear_tv(rettv);
2569 }
2570 else
2571 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002572 clear_tv(&var2);
2573 return ret;
2574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 }
2576
2577 return OK;
2578}
2579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 void
2581eval_addblob(typval_T *tv1, typval_T *tv2)
2582{
2583 blob_T *b1 = tv1->vval.v_blob;
2584 blob_T *b2 = tv2->vval.v_blob;
2585 blob_T *b = blob_alloc();
2586 int i;
2587
2588 if (b != NULL)
2589 {
2590 for (i = 0; i < blob_len(b1); i++)
2591 ga_append(&b->bv_ga, blob_get(b1, i));
2592 for (i = 0; i < blob_len(b2); i++)
2593 ga_append(&b->bv_ga, blob_get(b2, i));
2594
2595 clear_tv(tv1);
2596 rettv_blob_set(tv1, b);
2597 }
2598}
2599
2600 int
2601eval_addlist(typval_T *tv1, typval_T *tv2)
2602{
2603 typval_T var3;
2604
2605 // concatenate Lists
2606 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2607 {
2608 clear_tv(tv1);
2609 clear_tv(tv2);
2610 return FAIL;
2611 }
2612 clear_tv(tv1);
2613 *tv1 = var3;
2614 return OK;
2615}
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617/*
2618 * Handle fourth level expression:
2619 * + number addition
2620 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002621 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002622 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 *
2624 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002625 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 *
2627 * Return OK or FAIL.
2628 */
2629 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002630eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 /*
2633 * Get the first variable.
2634 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002635 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 return FAIL;
2637
2638 /*
2639 * Repeat computing, until no '+', '-' or '.' is following.
2640 */
2641 for (;;)
2642 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002643 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644 int getnext;
2645 char_u *p;
2646 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002647 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002648 int concat;
2649 typval_T var2;
2650
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002651 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002652 p = eval_next_non_blank(*arg, evalarg, &getnext);
2653 op = *p;
2654 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002655 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002657
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002658 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002659 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002660 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002661 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002662 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002663 {
2664 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2665 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002666 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002667 clear_tv(rettv);
2668 return FAIL;
2669 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002670 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002671 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002672 if ((op != '+' || (rettv->v_type != VAR_LIST
2673 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002674#ifdef FEAT_FLOAT
2675 && (op == '.' || rettv->v_type != VAR_FLOAT)
2676#endif
2677 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002678 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002679 // For "list + ...", an illegal use of the first operand as
2680 // a number cannot be determined before evaluating the 2nd
2681 // operand: if this is also a list, all is ok.
2682 // For "something . ...", "something - ..." or "non-list + ...",
2683 // we know that the first operand needs to be a string or number
2684 // without evaluating the 2nd operand. So check before to avoid
2685 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002686 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002687 {
2688 clear_tv(rettv);
2689 return FAIL;
2690 }
2691 }
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 /*
2694 * Get the second variable.
2695 */
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002696 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[oplen]))
2697 {
2698 error_white_both(p, oplen);
2699 clear_tv(rettv);
2700 return FAIL;
2701 }
2702 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002703 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002705 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return FAIL;
2707 }
2708
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002709 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
2711 /*
2712 * Compute the result.
2713 */
2714 if (op == '.')
2715 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002716 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2717 char_u *s1 = tv_get_string_buf(rettv, buf1);
2718 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2719
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002720 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002721 {
2722 clear_tv(rettv);
2723 clear_tv(&var2);
2724 return FAIL;
2725 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002726 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002727 clear_tv(rettv);
2728 rettv->v_type = VAR_STRING;
2729 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002731 else if (op == '+' && rettv->v_type == VAR_BLOB
2732 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002734 else if (op == '+' && rettv->v_type == VAR_LIST
2735 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002736 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002738 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 else
2741 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002742 int error = FALSE;
2743 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002744#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002745 float_T f1 = 0, f2 = 0;
2746
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002747 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002748 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002749 f1 = rettv->vval.v_float;
2750 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002751 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002752 else
2753#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002754 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002755 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002756 if (error)
2757 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002758 // This can only happen for "list + non-list". For
2759 // "non-list + ..." or "something - ...", we returned
2760 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002761 clear_tv(rettv);
2762 return FAIL;
2763 }
2764#ifdef FEAT_FLOAT
2765 if (var2.v_type == VAR_FLOAT)
2766 f1 = n1;
2767#endif
2768 }
2769#ifdef FEAT_FLOAT
2770 if (var2.v_type == VAR_FLOAT)
2771 {
2772 f2 = var2.vval.v_float;
2773 n2 = 0;
2774 }
2775 else
2776#endif
2777 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002778 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002779 if (error)
2780 {
2781 clear_tv(rettv);
2782 clear_tv(&var2);
2783 return FAIL;
2784 }
2785#ifdef FEAT_FLOAT
2786 if (rettv->v_type == VAR_FLOAT)
2787 f2 = n2;
2788#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002790 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002791
2792#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002793 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002794 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2795 {
2796 if (op == '+')
2797 f1 = f1 + f2;
2798 else
2799 f1 = f1 - f2;
2800 rettv->v_type = VAR_FLOAT;
2801 rettv->vval.v_float = f1;
2802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002804#endif
2805 {
2806 if (op == '+')
2807 n1 = n1 + n2;
2808 else
2809 n1 = n1 - n2;
2810 rettv->v_type = VAR_NUMBER;
2811 rettv->vval.v_number = n1;
2812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002814 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
2816 }
2817 return OK;
2818}
2819
2820/*
2821 * Handle fifth level expression:
2822 * * number multiplication
2823 * / number division
2824 * % number modulo
2825 *
2826 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002827 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 *
2829 * Return OK or FAIL.
2830 */
2831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002832eval6(
2833 char_u **arg,
2834 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002835 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002836 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002838#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002839 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841
2842 /*
2843 * Get the first variable.
2844 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002845 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 return FAIL;
2847
2848 /*
2849 * Repeat computing, until no '*', '/' or '%' is following.
2850 */
2851 for (;;)
2852 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002853 int evaluate;
2854 int getnext;
2855 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002856 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002857 int op;
2858 varnumber_T n1, n2;
2859#ifdef FEAT_FLOAT
2860 float_T f1, f2;
2861#endif
2862 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002863
Bram Moolenaar9d489562020-07-30 20:08:50 +02002864 p = eval_next_non_blank(*arg, evalarg, &getnext);
2865 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002866 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002868
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002869 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002870 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002871 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002872 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002873 {
2874 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2875 {
2876 error_white_both(p, 1);
2877 clear_tv(rettv);
2878 return FAIL;
2879 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002880 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002883#ifdef FEAT_FLOAT
2884 f1 = 0;
2885 f2 = 0;
2886#endif
2887 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002888 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002890#ifdef FEAT_FLOAT
2891 if (rettv->v_type == VAR_FLOAT)
2892 {
2893 f1 = rettv->vval.v_float;
2894 use_float = TRUE;
2895 n1 = 0;
2896 }
2897 else
2898#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002899 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002900 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002901 if (error)
2902 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
2904 else
2905 n1 = 0;
2906
2907 /*
2908 * Get the second variable.
2909 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002910 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2911 {
2912 error_white_both(p, 1);
2913 clear_tv(rettv);
2914 return FAIL;
2915 }
2916 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002917 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 return FAIL;
2919
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002920 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002922#ifdef FEAT_FLOAT
2923 if (var2.v_type == VAR_FLOAT)
2924 {
2925 if (!use_float)
2926 {
2927 f1 = n1;
2928 use_float = TRUE;
2929 }
2930 f2 = var2.vval.v_float;
2931 n2 = 0;
2932 }
2933 else
2934#endif
2935 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002936 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002937 clear_tv(&var2);
2938 if (error)
2939 return FAIL;
2940#ifdef FEAT_FLOAT
2941 if (use_float)
2942 f2 = n2;
2943#endif
2944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
2946 /*
2947 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002948 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002950#ifdef FEAT_FLOAT
2951 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002953 if (op == '*')
2954 f1 = f1 * f2;
2955 else if (op == '/')
2956 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002957# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002958 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002959 if (f2 == 0.0)
2960 {
2961 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002962 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002963 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002964 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002965 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002966 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002967 }
2968 else
2969 f1 = f1 / f2;
2970# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002971 // We rely on the floating point library to handle divide
2972 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002973 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002974# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002977 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002979 return FAIL;
2980 }
2981 rettv->v_type = VAR_FLOAT;
2982 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
2984 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002987 if (op == '*')
2988 n1 = n1 * n2;
2989 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002990 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002992 n1 = num_modulus(n1, n2);
2993
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002994 rettv->v_type = VAR_NUMBER;
2995 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
2998 }
2999
3000 return OK;
3001}
3002
3003/*
3004 * Handle sixth level expression:
3005 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003006 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003007 * "string" string constant
3008 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 * &option-name option value
3010 * @r register contents
3011 * identifier variable value
3012 * function() function call
3013 * $VAR environment variable
3014 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003015 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003017 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003018 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 *
3020 * Also handle:
3021 * ! in front logical NOT
3022 * - in front unary minus
3023 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003024 * trailing [] subscript in String or List
3025 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003026 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 *
3028 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003029 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 *
3031 * Return OK or FAIL.
3032 */
3033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003034eval7(
3035 char_u **arg,
3036 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003037 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003040 int evaluate = evalarg != NULL
3041 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 int len;
3043 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 char_u *start_leader, *end_leader;
3045 int ret = OK;
3046 char_u *alias;
3047
3048 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003050 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003052 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003055 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 */
3057 start_leader = *arg;
3058 while (**arg == '!' || **arg == '-' || **arg == '+')
3059 *arg = skipwhite(*arg + 1);
3060 end_leader = *arg;
3061
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003062 if (**arg == '.' && (!isdigit(*(*arg + 1))
3063#ifdef FEAT_FLOAT
3064 || current_sctx.sc_version < 2
3065#endif
3066 ))
3067 {
3068 semsg(_(e_invexpr2), *arg);
3069 ++*arg;
3070 return FAIL;
3071 }
3072
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 switch (**arg)
3074 {
3075 /*
3076 * Number constant.
3077 */
3078 case '0':
3079 case '1':
3080 case '2':
3081 case '3':
3082 case '4':
3083 case '5':
3084 case '6':
3085 case '7':
3086 case '8':
3087 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003088 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003089
3090 // Apply prefixed "-" and "+" now. Matters especially when
3091 // "->" follows.
3092 if (ret == OK && evaluate && end_leader > start_leader)
3093 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 break;
3095
3096 /*
3097 * String constant: "string".
3098 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003099 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 break;
3101
3102 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003103 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003105 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003106 break;
3107
3108 /*
3109 * List: [expr, expr]
3110 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003111 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 break;
3113
3114 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003115 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003116 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003117 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003118 {
3119 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003120 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003121 }
3122 else
3123 ret = NOTDONE;
3124 break;
3125
3126 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003127 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003128 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003129 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003130 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003131 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003132 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003133 break;
3134
3135 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003136 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003138 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 break;
3140
3141 /*
3142 * Environment variable: $VAR.
3143 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003144 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 break;
3146
3147 /*
3148 * Register contents: @r.
3149 */
3150 case '@': ++*arg;
3151 if (evaluate)
3152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003153 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003154 rettv->vval.v_string = get_reg_contents(**arg,
3155 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 }
3157 if (**arg != NUL)
3158 ++*arg;
3159 break;
3160
3161 /*
3162 * nested expression: (expression).
3163 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003164 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003165 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003166 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003167
Bram Moolenaar9215f012020-06-27 21:18:00 +02003168 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003169 if (**arg == ')')
3170 ++*arg;
3171 else if (ret == OK)
3172 {
3173 emsg(_(e_missing_close));
3174 clear_tv(rettv);
3175 ret = FAIL;
3176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 }
3178 break;
3179
Bram Moolenaar8c711452005-01-14 21:53:12 +00003180 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 break;
3182 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003183
3184 if (ret == NOTDONE)
3185 {
3186 /*
3187 * Must be a variable or function name.
3188 * Can also be a curly-braces kind of name: {expr}.
3189 */
3190 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003191 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003192 if (alias != NULL)
3193 s = alias;
3194
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003195 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003196 ret = FAIL;
3197 else
3198 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003199 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3200
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003201 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3202 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003203 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003204 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003205 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003206 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003207 else if (flags & EVAL_CONSTANT)
3208 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003209 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003210 {
3211 // get the value of "true", "false" or a variable
3212 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3213 {
3214 rettv->v_type = VAR_BOOL;
3215 rettv->vval.v_number = VVAL_TRUE;
3216 }
3217 else if (len == 5 && in_vim9script()
3218 && STRNCMP(s, "false", 4) == 0)
3219 {
3220 rettv->v_type = VAR_BOOL;
3221 rettv->vval.v_number = VVAL_FALSE;
3222 }
3223 else
3224 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3225 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003226 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003227 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003228 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003229 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003230 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003232 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003233 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003234 }
3235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003236 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3237 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003238 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003239 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
3241 /*
3242 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3243 */
3244 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003245 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003246 return ret;
3247}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003248
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003249/*
3250 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003251 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003252 * Adjusts "end_leaderp" until it is at "start_leader".
3253 */
3254 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003255eval7_leader(
3256 typval_T *rettv,
3257 int numeric_only,
3258 char_u *start_leader,
3259 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003260{
3261 char_u *end_leader = *end_leaderp;
3262 int ret = OK;
3263 int error = FALSE;
3264 varnumber_T val = 0;
3265#ifdef FEAT_FLOAT
3266 float_T f = 0.0;
3267
3268 if (rettv->v_type == VAR_FLOAT)
3269 f = rettv->vval.v_float;
3270 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003271#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003272 val = tv_get_number_chk(rettv, &error);
3273 if (error)
3274 {
3275 clear_tv(rettv);
3276 ret = FAIL;
3277 }
3278 else
3279 {
3280 while (end_leader > start_leader)
3281 {
3282 --end_leader;
3283 if (*end_leader == '!')
3284 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003285 if (numeric_only)
3286 {
3287 ++end_leader;
3288 break;
3289 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003290#ifdef FEAT_FLOAT
3291 if (rettv->v_type == VAR_FLOAT)
3292 f = !f;
3293 else
3294#endif
3295 val = !val;
3296 }
3297 else if (*end_leader == '-')
3298 {
3299#ifdef FEAT_FLOAT
3300 if (rettv->v_type == VAR_FLOAT)
3301 f = -f;
3302 else
3303#endif
3304 val = -val;
3305 }
3306 }
3307#ifdef FEAT_FLOAT
3308 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003310 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003311 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003313 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003314#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003315 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003316 clear_tv(rettv);
3317 rettv->v_type = VAR_NUMBER;
3318 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003321 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 return ret;
3323}
3324
3325/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003326 * Call the function referred to in "rettv".
3327 */
3328 static int
3329call_func_rettv(
3330 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003331 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003332 typval_T *rettv,
3333 int evaluate,
3334 dict_T *selfdict,
3335 typval_T *basetv)
3336{
3337 partial_T *pt = NULL;
3338 funcexe_T funcexe;
3339 typval_T functv;
3340 char_u *s;
3341 int ret;
3342
3343 // need to copy the funcref so that we can clear rettv
3344 if (evaluate)
3345 {
3346 functv = *rettv;
3347 rettv->v_type = VAR_UNKNOWN;
3348
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003349 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003350 if (functv.v_type == VAR_PARTIAL)
3351 {
3352 pt = functv.vval.v_partial;
3353 s = partial_name(pt);
3354 }
3355 else
3356 s = functv.vval.v_string;
3357 }
3358 else
3359 s = (char_u *)"";
3360
Bram Moolenaara80faa82020-04-12 19:37:17 +02003361 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003362 funcexe.firstline = curwin->w_cursor.lnum;
3363 funcexe.lastline = curwin->w_cursor.lnum;
3364 funcexe.evaluate = evaluate;
3365 funcexe.partial = pt;
3366 funcexe.selfdict = selfdict;
3367 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003368 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003369
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003370 // Clear the funcref afterwards, so that deleting it while
3371 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003372 if (evaluate)
3373 clear_tv(&functv);
3374
3375 return ret;
3376}
3377
3378/*
3379 * Evaluate "->method()".
3380 * "*arg" points to the '-'.
3381 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3382 */
3383 static int
3384eval_lambda(
3385 char_u **arg,
3386 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003387 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003388 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003389{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003390 int evaluate = evalarg != NULL
3391 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003392 typval_T base = *rettv;
3393 int ret;
3394
3395 // Skip over the ->.
3396 *arg += 2;
3397 rettv->v_type = VAR_UNKNOWN;
3398
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003399 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003400 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003401 return FAIL;
3402 else if (**arg != '(')
3403 {
3404 if (verbose)
3405 {
3406 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003407 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003408 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003410 }
3411 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003412 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003413 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003414 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003415 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003416
3417 // Clear the funcref afterwards, so that deleting it while
3418 // evaluating the arguments is possible (see test55).
3419 if (evaluate)
3420 clear_tv(&base);
3421
3422 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003423}
3424
3425/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003426 * Evaluate "->method()".
3427 * "*arg" points to the '-'.
3428 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3429 */
3430 static int
3431eval_method(
3432 char_u **arg,
3433 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003434 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003435 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003436{
3437 char_u *name;
3438 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003439 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003440 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003441 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003442 int evaluate = evalarg != NULL
3443 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003444
3445 // Skip over the ->.
3446 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003447 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003448
Bram Moolenaarac92e252019-08-03 21:58:38 +02003449 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003450 len = get_name_len(arg, &alias, evaluate, TRUE);
3451 if (alias != NULL)
3452 name = alias;
3453
3454 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003455 {
3456 if (verbose)
3457 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003458 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003459 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003460 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003461 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003462 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003463 if (**arg != '(')
3464 {
3465 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003467 ret = FAIL;
3468 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003469 else if (VIM_ISWHITE((*arg)[-1]))
3470 {
3471 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003472 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003473 ret = FAIL;
3474 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003475 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003476 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003477 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003478 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003479
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003480 // Clear the funcref afterwards, so that deleting it while
3481 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003482 if (evaluate)
3483 clear_tv(&base);
3484
Bram Moolenaarac92e252019-08-03 21:58:38 +02003485 return ret;
3486}
3487
3488/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003489 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3490 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003491 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3492 */
3493 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003494eval_index(
3495 char_u **arg,
3496 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003497 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003498 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003499{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003500 int evaluate = evalarg != NULL
3501 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003502 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003503 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003504 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003505 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003506 long len = -1;
3507 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003508 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003509 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003510
Bram Moolenaara03f2332016-02-06 18:09:59 +01003511 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003512 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003513 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003514 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003515 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003516 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003517 return FAIL;
3518 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003519#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003520 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003521 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003522 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003523#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003524 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003525 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003526 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003527 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003528 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003529 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003530 return FAIL;
3531 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003532 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003534 if (evaluate)
3535 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003536 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003537
3538 case VAR_STRING:
3539 case VAR_NUMBER:
3540 case VAR_LIST:
3541 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003542 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003543 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003544 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003545
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003546 init_tv(&var1);
3547 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003548 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003549 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003550 /*
3551 * dict.name
3552 */
3553 key = *arg + 1;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003554 for (len = 0; eval_isdictc(key[len]); ++len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003555 ;
3556 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003557 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003558 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003559 }
3560 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003561 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003562 /*
3563 * something[idx]
3564 *
3565 * Get the (first) variable from inside the [].
3566 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003567 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003568 if (**arg == ':')
3569 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003570 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003571 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003572 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003573 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003574 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003575 clear_tv(&var1);
3576 return FAIL;
3577 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003578
3579 /*
3580 * Get the second variable from inside the [:].
3581 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003582 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003583 if (**arg == ':')
3584 {
3585 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003586 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003587 if (**arg == ']')
3588 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003589 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003591 if (!empty1)
3592 clear_tv(&var1);
3593 return FAIL;
3594 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003595 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003596 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003597 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003598 if (!empty1)
3599 clear_tv(&var1);
3600 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601 return FAIL;
3602 }
3603 }
3604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003605 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003606 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003607 if (**arg != ']')
3608 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003609 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003610 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 clear_tv(&var1);
3612 if (range)
3613 clear_tv(&var2);
3614 return FAIL;
3615 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003616 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003617 }
3618
3619 if (evaluate)
3620 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003621 n1 = 0;
3622 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003623 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003624 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003625 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003626 }
3627 if (range)
3628 {
3629 if (empty2)
3630 n2 = -1;
3631 else
3632 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003633 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003634 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003635 }
3636 }
3637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003638 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003639 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003640 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003641 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003643 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003644 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003645 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003646 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003647 case VAR_SPECIAL:
3648 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003649 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003650 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003651
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003652 case VAR_NUMBER:
3653 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003654 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003655 len = (long)STRLEN(s);
3656 if (range)
3657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003658 // The resulting variable is a substring. If the indexes
3659 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003660 if (n1 < 0)
3661 {
3662 n1 = len + n1;
3663 if (n1 < 0)
3664 n1 = 0;
3665 }
3666 if (n2 < 0)
3667 n2 = len + n2;
3668 else if (n2 >= len)
3669 n2 = len;
3670 if (n1 >= len || n2 < 0 || n1 > n2)
3671 s = NULL;
3672 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003673 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003674 }
3675 else
3676 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003677 // The resulting variable is a string of a single
3678 // character. If the index is too big or negative the
3679 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003680 if (n1 >= len || n1 < 0)
3681 s = NULL;
3682 else
3683 s = vim_strnsave(s + n1, 1);
3684 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003685 clear_tv(rettv);
3686 rettv->v_type = VAR_STRING;
3687 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003688 break;
3689
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003690 case VAR_BLOB:
3691 len = blob_len(rettv->vval.v_blob);
3692 if (range)
3693 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003694 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003695 // are out of range the result is empty.
3696 if (n1 < 0)
3697 {
3698 n1 = len + n1;
3699 if (n1 < 0)
3700 n1 = 0;
3701 }
3702 if (n2 < 0)
3703 n2 = len + n2;
3704 else if (n2 >= len)
3705 n2 = len - 1;
3706 if (n1 >= len || n2 < 0 || n1 > n2)
3707 {
3708 clear_tv(rettv);
3709 rettv->v_type = VAR_BLOB;
3710 rettv->vval.v_blob = NULL;
3711 }
3712 else
3713 {
3714 blob_T *blob = blob_alloc();
3715
3716 if (blob != NULL)
3717 {
3718 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3719 {
3720 blob_free(blob);
3721 return FAIL;
3722 }
3723 blob->bv_ga.ga_len = n2 - n1 + 1;
3724 for (i = n1; i <= n2; i++)
3725 blob_set(blob, i - n1,
3726 blob_get(rettv->vval.v_blob, i));
3727
3728 clear_tv(rettv);
3729 rettv_blob_set(rettv, blob);
3730 }
3731 }
3732 }
3733 else
3734 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003735 // The resulting variable is a byte value.
3736 // If the index is too big or negative that is an error.
3737 if (n1 < 0)
3738 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003739 if (n1 < len && n1 >= 0)
3740 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003741 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003742
3743 clear_tv(rettv);
3744 rettv->v_type = VAR_NUMBER;
3745 rettv->vval.v_number = v;
3746 }
3747 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003748 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003749 }
3750 break;
3751
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003752 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003753 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003754 if (n1 < 0)
3755 n1 = len + n1;
3756 if (!empty1 && (n1 < 0 || n1 >= len))
3757 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003758 // For a range we allow invalid values and return an empty
3759 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003760 if (!range)
3761 {
3762 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003763 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003764 return FAIL;
3765 }
3766 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003767 }
3768 if (range)
3769 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003771
3772 if (n2 < 0)
3773 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003774 else if (n2 >= len)
3775 n2 = len - 1;
3776 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003777 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003778 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003779 if (l == NULL)
3780 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003782 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003783 }
3784 else
3785 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003786 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003787 clear_tv(rettv);
3788 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003789 }
3790 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003791
3792 case VAR_DICT:
3793 if (range)
3794 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003795 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003796 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003797 if (len == -1)
3798 clear_tv(&var1);
3799 return FAIL;
3800 }
3801 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003802 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003803
3804 if (len == -1)
3805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003806 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003807 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003809 clear_tv(&var1);
3810 return FAIL;
3811 }
3812 }
3813
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003814 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003815
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003816 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003817 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003818 if (len == -1)
3819 clear_tv(&var1);
3820 if (item == NULL)
3821 return FAIL;
3822
3823 copy_tv(&item->di_tv, &var1);
3824 clear_tv(rettv);
3825 *rettv = var1;
3826 }
3827 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003828 }
3829 }
3830
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003831 return OK;
3832}
3833
3834/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003835 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003836 */
3837 char_u *
3838partial_name(partial_T *pt)
3839{
3840 if (pt->pt_name != NULL)
3841 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003842 if (pt->pt_func != NULL)
3843 return pt->pt_func->uf_name;
3844 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003845}
3846
Bram Moolenaarddecc252016-04-06 22:59:37 +02003847 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003848partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003849{
3850 int i;
3851
3852 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003853 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003854 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003855 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003856 if (pt->pt_name != NULL)
3857 {
3858 func_unref(pt->pt_name);
3859 vim_free(pt->pt_name);
3860 }
3861 else
3862 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003863
3864 if (pt->pt_funcstack != NULL)
3865 {
3866 // Decrease the reference count for the context of a closure. If down
3867 // to zero free it and clear the variables on the stack.
3868 if (--pt->pt_funcstack->fs_refcount == 0)
3869 {
3870 garray_T *gap = &pt->pt_funcstack->fs_ga;
3871 typval_T *stack = gap->ga_data;
3872
3873 for (i = 0; i < gap->ga_len; ++i)
3874 clear_tv(stack + i);
3875 ga_clear(gap);
3876 vim_free(pt->pt_funcstack);
3877 }
3878 pt->pt_funcstack = NULL;
3879 }
3880
Bram Moolenaarddecc252016-04-06 22:59:37 +02003881 vim_free(pt);
3882}
3883
3884/*
3885 * Unreference a closure: decrement the reference count and free it when it
3886 * becomes zero.
3887 */
3888 void
3889partial_unref(partial_T *pt)
3890{
3891 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003892 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003893}
3894
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003895/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003896 * Return the next (unique) copy ID.
3897 * Used for serializing nested structures.
3898 */
3899 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003900get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003901{
3902 current_copyID += COPYID_INC;
3903 return current_copyID;
3904}
3905
3906/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003907 * Garbage collection for lists and dictionaries.
3908 *
3909 * We use reference counts to be able to free most items right away when they
3910 * are no longer used. But for composite items it's possible that it becomes
3911 * unused while the reference count is > 0: When there is a recursive
3912 * reference. Example:
3913 * :let l = [1, 2, 3]
3914 * :let d = {9: l}
3915 * :let l[1] = d
3916 *
3917 * Since this is quite unusual we handle this with garbage collection: every
3918 * once in a while find out which lists and dicts are not referenced from any
3919 * variable.
3920 *
3921 * Here is a good reference text about garbage collection (refers to Python
3922 * but it applies to all reference-counting mechanisms):
3923 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003924 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003925
3926/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003927 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003928 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003929 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003930 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003931 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003932garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003933{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003934 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003935 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003936 buf_T *buf;
3937 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003938 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003939 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003940
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003941 if (!testing)
3942 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003943 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003944 want_garbage_collect = FALSE;
3945 may_garbage_collect = FALSE;
3946 garbage_collect_at_exit = FALSE;
3947 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003948
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003949 // The execution stack can grow big, limit the size.
3950 if (exestack.ga_maxlen - exestack.ga_len > 500)
3951 {
3952 size_t new_len;
3953 char_u *pp;
3954 int n;
3955
3956 // Keep 150% of the current size, with a minimum of the growth size.
3957 n = exestack.ga_len / 2;
3958 if (n < exestack.ga_growsize)
3959 n = exestack.ga_growsize;
3960
3961 // Don't make it bigger though.
3962 if (exestack.ga_len + n < exestack.ga_maxlen)
3963 {
3964 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3965 pp = vim_realloc(exestack.ga_data, new_len);
3966 if (pp == NULL)
3967 return FAIL;
3968 exestack.ga_maxlen = exestack.ga_len + n;
3969 exestack.ga_data = pp;
3970 }
3971 }
3972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003973 // We advance by two because we add one for items referenced through
3974 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003975 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003976
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003977 /*
3978 * 1. Go through all accessible variables and mark all lists and dicts
3979 * with copyID.
3980 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003981
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003982 // Don't free variables in the previous_funccal list unless they are only
3983 // referenced through previous_funccal. This must be first, because if
3984 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003985 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003986
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003987 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003988 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003990 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003991 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003992 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3993 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003995 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003996 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003997 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3998 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003999 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004000 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4001 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004002#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004003 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004004 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4005 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004006 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004007 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004008 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4009 NULL, NULL);
4010#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004012 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004013 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004014 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4015 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004016 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004017 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004019 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004020 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004021
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004022 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004023 abort = abort || set_ref_in_functions(copyID);
4024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004025 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004026 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004028 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004029 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004030
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004031 // callbacks in buffers
4032 abort = abort || set_ref_in_buffers(copyID);
4033
Bram Moolenaar1dced572012-04-05 16:54:08 +02004034#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004035 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004036#endif
4037
Bram Moolenaardb913952012-06-29 12:54:53 +02004038#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004039 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004040#endif
4041
4042#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004043 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004044#endif
4045
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004046#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004047 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004048 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004049#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004050#ifdef FEAT_NETBEANS_INTG
4051 abort = abort || set_ref_in_nb_channel(copyID);
4052#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004053
Bram Moolenaare3188e22016-05-31 21:13:04 +02004054#ifdef FEAT_TIMERS
4055 abort = abort || set_ref_in_timer(copyID);
4056#endif
4057
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004058#ifdef FEAT_QUICKFIX
4059 abort = abort || set_ref_in_quickfix(copyID);
4060#endif
4061
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004062#ifdef FEAT_TERMINAL
4063 abort = abort || set_ref_in_term(copyID);
4064#endif
4065
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004066#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004067 abort = abort || set_ref_in_popups(copyID);
4068#endif
4069
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004070 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004071 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004072 /*
4073 * 2. Free lists and dictionaries that are not referenced.
4074 */
4075 did_free = free_unref_items(copyID);
4076
4077 /*
4078 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004080 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004081 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004082 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004083 else if (p_verbose > 0)
4084 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004085 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004086 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004087
4088 return did_free;
4089}
4090
4091/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004092 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004093 */
4094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004095free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004096{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004097 int did_free = FALSE;
4098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004099 // Let all "free" functions know that we are here. This means no
4100 // dictionaries, lists, channels or jobs are to be freed, because we will
4101 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004102 in_free_unref_items = TRUE;
4103
4104 /*
4105 * PASS 1: free the contents of the items. We don't free the items
4106 * themselves yet, so that it is possible to decrement refcount counters
4107 */
4108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004109 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004110 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004111
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004112 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004113 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004114
4115#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004116 // Go through the list of jobs and free items without the copyID. This
4117 // must happen before doing channels, because jobs refer to channels, but
4118 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004119 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4120
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004121 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004122 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4123#endif
4124
4125 /*
4126 * PASS 2: free the items themselves.
4127 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004128 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004129 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004130
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004131#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004132 // Go through the list of jobs and free items without the copyID. This
4133 // must happen before doing channels, because jobs refer to channels, but
4134 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004135 free_unused_jobs(copyID, COPYID_MASK);
4136
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004137 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004138 free_unused_channels(copyID, COPYID_MASK);
4139#endif
4140
4141 in_free_unref_items = FALSE;
4142
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004143 return did_free;
4144}
4145
4146/*
4147 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004148 * "list_stack" is used to add lists to be marked. Can be NULL.
4149 *
4150 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004151 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004152 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004153set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004154{
4155 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004156 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004157 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004158 hashtab_T *cur_ht;
4159 ht_stack_T *ht_stack = NULL;
4160 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004161
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004162 cur_ht = ht;
4163 for (;;)
4164 {
4165 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004166 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004167 // Mark each item in the hashtab. If the item contains a hashtab
4168 // it is added to ht_stack, if it contains a list it is added to
4169 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004170 todo = (int)cur_ht->ht_used;
4171 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4172 if (!HASHITEM_EMPTY(hi))
4173 {
4174 --todo;
4175 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4176 &ht_stack, list_stack);
4177 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004178 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004179
4180 if (ht_stack == NULL)
4181 break;
4182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004183 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004184 cur_ht = ht_stack->ht;
4185 tempitem = ht_stack;
4186 ht_stack = ht_stack->prev;
4187 free(tempitem);
4188 }
4189
4190 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004191}
4192
4193/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004194 * Mark a dict and its items with "copyID".
4195 * Returns TRUE if setting references failed somehow.
4196 */
4197 int
4198set_ref_in_dict(dict_T *d, int copyID)
4199{
4200 if (d != NULL && d->dv_copyID != copyID)
4201 {
4202 d->dv_copyID = copyID;
4203 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4204 }
4205 return FALSE;
4206}
4207
4208/*
4209 * Mark a list and its items with "copyID".
4210 * Returns TRUE if setting references failed somehow.
4211 */
4212 int
4213set_ref_in_list(list_T *ll, int copyID)
4214{
4215 if (ll != NULL && ll->lv_copyID != copyID)
4216 {
4217 ll->lv_copyID = copyID;
4218 return set_ref_in_list_items(ll, copyID, NULL);
4219 }
4220 return FALSE;
4221}
4222
4223/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004224 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004225 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4226 *
4227 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004228 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004229 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004230set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004231{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004232 listitem_T *li;
4233 int abort = FALSE;
4234 list_T *cur_l;
4235 list_stack_T *list_stack = NULL;
4236 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004237
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004238 cur_l = l;
4239 for (;;)
4240 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004241 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004242 // Mark each item in the list. If the item contains a hashtab
4243 // it is added to ht_stack, if it contains a list it is added to
4244 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004245 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4246 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4247 ht_stack, &list_stack);
4248 if (list_stack == NULL)
4249 break;
4250
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004251 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004252 cur_l = list_stack->list;
4253 tempitem = list_stack;
4254 list_stack = list_stack->prev;
4255 free(tempitem);
4256 }
4257
4258 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004259}
4260
4261/*
4262 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004263 * "list_stack" is used to add lists to be marked. Can be NULL.
4264 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4265 *
4266 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004267 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004268 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004269set_ref_in_item(
4270 typval_T *tv,
4271 int copyID,
4272 ht_stack_T **ht_stack,
4273 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004274{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004275 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004276
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004277 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004278 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004279 dict_T *dd = tv->vval.v_dict;
4280
Bram Moolenaara03f2332016-02-06 18:09:59 +01004281 if (dd != NULL && dd->dv_copyID != copyID)
4282 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004283 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004284 dd->dv_copyID = copyID;
4285 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004286 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004287 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4288 }
4289 else
4290 {
4291 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4292 if (newitem == NULL)
4293 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004294 else
4295 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004296 newitem->ht = &dd->dv_hashtab;
4297 newitem->prev = *ht_stack;
4298 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004299 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004300 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004301 }
4302 }
4303 else if (tv->v_type == VAR_LIST)
4304 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004305 list_T *ll = tv->vval.v_list;
4306
Bram Moolenaara03f2332016-02-06 18:09:59 +01004307 if (ll != NULL && ll->lv_copyID != copyID)
4308 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004309 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004310 ll->lv_copyID = copyID;
4311 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004312 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004313 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004314 }
4315 else
4316 {
4317 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004318 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004319 if (newitem == NULL)
4320 abort = TRUE;
4321 else
4322 {
4323 newitem->list = ll;
4324 newitem->prev = *list_stack;
4325 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004326 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004327 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004328 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004329 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004330 else if (tv->v_type == VAR_FUNC)
4331 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004332 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004333 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004334 else if (tv->v_type == VAR_PARTIAL)
4335 {
4336 partial_T *pt = tv->vval.v_partial;
4337 int i;
4338
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004339 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004340 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004341 // Didn't see this partial yet.
4342 pt->pt_copyID = copyID;
4343
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004344 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004345
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004346 if (pt->pt_dict != NULL)
4347 {
4348 typval_T dtv;
4349
4350 dtv.v_type = VAR_DICT;
4351 dtv.vval.v_dict = pt->pt_dict;
4352 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4353 }
4354
4355 for (i = 0; i < pt->pt_argc; ++i)
4356 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4357 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004358 if (pt->pt_funcstack != NULL)
4359 {
4360 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4361
4362 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4363 abort = abort || set_ref_in_item(stack + i, copyID,
4364 ht_stack, list_stack);
4365 }
4366
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004367 }
4368 }
4369#ifdef FEAT_JOB_CHANNEL
4370 else if (tv->v_type == VAR_JOB)
4371 {
4372 job_T *job = tv->vval.v_job;
4373 typval_T dtv;
4374
4375 if (job != NULL && job->jv_copyID != copyID)
4376 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004377 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004378 if (job->jv_channel != NULL)
4379 {
4380 dtv.v_type = VAR_CHANNEL;
4381 dtv.vval.v_channel = job->jv_channel;
4382 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4383 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004384 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004385 {
4386 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004387 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004388 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4389 }
4390 }
4391 }
4392 else if (tv->v_type == VAR_CHANNEL)
4393 {
4394 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004395 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004396 typval_T dtv;
4397 jsonq_T *jq;
4398 cbq_T *cq;
4399
4400 if (ch != NULL && ch->ch_copyID != copyID)
4401 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004402 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004403 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004404 {
4405 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4406 jq = jq->jq_next)
4407 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4408 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4409 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004410 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004411 {
4412 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004413 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004414 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4415 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004416 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004417 {
4418 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004419 dtv.vval.v_partial =
4420 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004421 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4422 }
4423 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004424 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004425 {
4426 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004427 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004428 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4429 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004430 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004431 {
4432 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004433 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004434 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4435 }
4436 }
4437 }
4438#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004439 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004440}
4441
Bram Moolenaar8c711452005-01-14 21:53:12 +00004442/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004443 * Return a string with the string representation of a variable.
4444 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004445 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004446 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004447 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4448 * stings as "string()", otherwise does not put quotes around strings, as
4449 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004450 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4451 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004452 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004453 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004454 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004455echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004456 typval_T *tv,
4457 char_u **tofree,
4458 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004459 int copyID,
4460 int echo_style,
4461 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004462 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004463{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004464 static int recurse = 0;
4465 char_u *r = NULL;
4466
Bram Moolenaar33570922005-01-25 22:26:29 +00004467 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004468 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004469 if (!did_echo_string_emsg)
4470 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004471 // Only give this message once for a recursive call to avoid
4472 // flooding the user with errors. And stop iterating over lists
4473 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004474 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004475 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004476 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004477 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004478 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004479 }
4480 ++recurse;
4481
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004482 switch (tv->v_type)
4483 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004484 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004485 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004486 {
4487 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004488 r = tv->vval.v_string;
4489 if (r == NULL)
4490 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004491 }
4492 else
4493 {
4494 *tofree = string_quote(tv->vval.v_string, FALSE);
4495 r = *tofree;
4496 }
4497 break;
4498
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004499 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004500 if (echo_style)
4501 {
4502 *tofree = NULL;
4503 r = tv->vval.v_string;
4504 }
4505 else
4506 {
4507 *tofree = string_quote(tv->vval.v_string, TRUE);
4508 r = *tofree;
4509 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004510 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004511
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004512 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004513 {
4514 partial_T *pt = tv->vval.v_partial;
4515 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004516 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004517 garray_T ga;
4518 int i;
4519 char_u *tf;
4520
4521 ga_init2(&ga, 1, 100);
4522 ga_concat(&ga, (char_u *)"function(");
4523 if (fname != NULL)
4524 {
4525 ga_concat(&ga, fname);
4526 vim_free(fname);
4527 }
4528 if (pt != NULL && pt->pt_argc > 0)
4529 {
4530 ga_concat(&ga, (char_u *)", [");
4531 for (i = 0; i < pt->pt_argc; ++i)
4532 {
4533 if (i > 0)
4534 ga_concat(&ga, (char_u *)", ");
4535 ga_concat(&ga,
4536 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4537 vim_free(tf);
4538 }
4539 ga_concat(&ga, (char_u *)"]");
4540 }
4541 if (pt != NULL && pt->pt_dict != NULL)
4542 {
4543 typval_T dtv;
4544
4545 ga_concat(&ga, (char_u *)", ");
4546 dtv.v_type = VAR_DICT;
4547 dtv.vval.v_dict = pt->pt_dict;
4548 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4549 vim_free(tf);
4550 }
4551 ga_concat(&ga, (char_u *)")");
4552
4553 *tofree = ga.ga_data;
4554 r = *tofree;
4555 break;
4556 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004557
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004558 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004559 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004560 break;
4561
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004562 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004563 if (tv->vval.v_list == NULL)
4564 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004565 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004566 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004567 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004568 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004569 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4570 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004571 {
4572 *tofree = NULL;
4573 r = (char_u *)"[...]";
4574 }
4575 else
4576 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004577 int old_copyID = tv->vval.v_list->lv_copyID;
4578
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004579 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004580 *tofree = list2string(tv, copyID, restore_copyID);
4581 if (restore_copyID)
4582 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004583 r = *tofree;
4584 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004585 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004586
Bram Moolenaar8c711452005-01-14 21:53:12 +00004587 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004588 if (tv->vval.v_dict == NULL)
4589 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004590 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004591 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004592 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004593 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004594 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4595 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004596 {
4597 *tofree = NULL;
4598 r = (char_u *)"{...}";
4599 }
4600 else
4601 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004602 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004603
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004604 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004605 *tofree = dict2string(tv, copyID, restore_copyID);
4606 if (restore_copyID)
4607 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004608 r = *tofree;
4609 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004610 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004611
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004613 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004614 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004616 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004617 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004618 break;
4619
Bram Moolenaar835dc632016-02-07 14:27:38 +01004620 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004621 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004622 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004623 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004624 if (composite_val)
4625 {
4626 *tofree = string_quote(r, FALSE);
4627 r = *tofree;
4628 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004630
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004631 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004632#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004633 *tofree = NULL;
4634 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4635 r = numbuf;
4636 break;
4637#endif
4638
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004639 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004640 case VAR_SPECIAL:
4641 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004642 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004643 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004645
Bram Moolenaar8502c702014-06-17 12:51:16 +02004646 if (--recurse == 0)
4647 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004648 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004649}
4650
4651/*
4652 * Return a string with the string representation of a variable.
4653 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4654 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004655 * Does not put quotes around strings, as ":echo" displays values.
4656 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4657 * May return NULL.
4658 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004659 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004660echo_string(
4661 typval_T *tv,
4662 char_u **tofree,
4663 char_u *numbuf,
4664 int copyID)
4665{
4666 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4667}
4668
4669/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004670 * Return string "str" in ' quotes, doubling ' characters.
4671 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004672 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004673 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004674 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004675string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004676{
Bram Moolenaar33570922005-01-25 22:26:29 +00004677 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004678 char_u *p, *r, *s;
4679
Bram Moolenaar33570922005-01-25 22:26:29 +00004680 len = (function ? 13 : 3);
4681 if (str != NULL)
4682 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004683 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004684 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004685 if (*p == '\'')
4686 ++len;
4687 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004688 s = r = alloc(len);
4689 if (r != NULL)
4690 {
4691 if (function)
4692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004693 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004694 r += 10;
4695 }
4696 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004697 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004698 if (str != NULL)
4699 for (p = str; *p != NUL; )
4700 {
4701 if (*p == '\'')
4702 *r++ = '\'';
4703 MB_COPY_CHAR(p, r);
4704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004705 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004706 if (function)
4707 *r++ = ')';
4708 *r++ = NUL;
4709 }
4710 return s;
4711}
4712
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004713#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004714/*
4715 * Convert the string "text" to a floating point number.
4716 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4717 * this always uses a decimal point.
4718 * Returns the length of the text that was consumed.
4719 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004720 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004721string2float(
4722 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004723 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004724{
4725 char *s = (char *)text;
4726 float_T f;
4727
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004728 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004729 if (STRNICMP(text, "inf", 3) == 0)
4730 {
4731 *value = INFINITY;
4732 return 3;
4733 }
4734 if (STRNICMP(text, "-inf", 3) == 0)
4735 {
4736 *value = -INFINITY;
4737 return 4;
4738 }
4739 if (STRNICMP(text, "nan", 3) == 0)
4740 {
4741 *value = NAN;
4742 return 3;
4743 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004744 f = strtod(s, &s);
4745 *value = f;
4746 return (int)((char_u *)s - text);
4747}
4748#endif
4749
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004752 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004754 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004755var2fpos(
4756 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004757 int dollar_lnum, // TRUE when $ is last line
4758 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004760 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004762 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004764 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004765 if (varp->v_type == VAR_LIST)
4766 {
4767 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004768 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004769 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004770 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004771
4772 l = varp->vval.v_list;
4773 if (l == NULL)
4774 return NULL;
4775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004776 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004777 pos.lnum = list_find_nr(l, 0L, &error);
4778 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004779 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004781 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004782 pos.col = list_find_nr(l, 1L, &error);
4783 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004784 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004785 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004786
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004787 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004788 li = list_find(l, 1L);
4789 if (li != NULL && li->li_tv.v_type == VAR_STRING
4790 && li->li_tv.vval.v_string != NULL
4791 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4792 pos.col = len + 1;
4793
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004794 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004795 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004796 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004797 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004799 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004800 pos.coladd = list_find_nr(l, 2L, &error);
4801 if (error)
4802 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004803
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004804 return &pos;
4805 }
4806
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004807 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004808 if (name == NULL)
4809 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004810 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004812 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004813 {
4814 if (VIsual_active)
4815 return &VIsual;
4816 return &curwin->w_cursor;
4817 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004818 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004820 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4822 return NULL;
4823 return pp;
4824 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004825
Bram Moolenaara5525202006-03-02 22:52:09 +00004826 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004827
Bram Moolenaar477933c2007-07-17 14:32:23 +00004828 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004829 {
4830 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004831 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004832 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004833 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004834 // In silent Ex mode topline is zero, but that's not a valid line
4835 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004836 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004837 return &pos;
4838 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004839 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004840 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004841 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004842 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004843 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004844 return &pos;
4845 }
4846 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004847 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004849 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
4851 pos.lnum = curbuf->b_ml.ml_line_count;
4852 pos.col = 0;
4853 }
4854 else
4855 {
4856 pos.lnum = curwin->w_cursor.lnum;
4857 pos.col = (colnr_T)STRLEN(ml_get_curline());
4858 }
4859 return &pos;
4860 }
4861 return NULL;
4862}
4863
4864/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004865 * Convert list in "arg" into a position and optional file number.
4866 * When "fnump" is NULL there is no file number, only 3 items.
4867 * Note that the column is passed on as-is, the caller may want to decrement
4868 * it to use 1 for the first column.
4869 * Return FAIL when conversion is not possible, doesn't check the position for
4870 * validity.
4871 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004872 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004873list2fpos(
4874 typval_T *arg,
4875 pos_T *posp,
4876 int *fnump,
4877 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004878{
4879 list_T *l = arg->vval.v_list;
4880 long i = 0;
4881 long n;
4882
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004883 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4884 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004885 if (arg->v_type != VAR_LIST
4886 || l == NULL
4887 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004888 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004889 return FAIL;
4890
4891 if (fnump != NULL)
4892 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004893 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004894 if (n < 0)
4895 return FAIL;
4896 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004897 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004898 *fnump = n;
4899 }
4900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004901 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004902 if (n < 0)
4903 return FAIL;
4904 posp->lnum = n;
4905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004906 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004907 if (n < 0)
4908 return FAIL;
4909 posp->col = n;
4910
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004911 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004912 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004913 posp->coladd = 0;
4914 else
4915 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004916
Bram Moolenaar493c1782014-05-28 14:34:46 +02004917 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004918 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004919
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004920 return OK;
4921}
4922
4923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 * Get the length of an environment variable name.
4925 * Advance "arg" to the first character after the name.
4926 * Return 0 for error.
4927 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004928 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004929get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930{
4931 char_u *p;
4932 int len;
4933
4934 for (p = *arg; vim_isIDc(*p); ++p)
4935 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004936 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 return 0;
4938
4939 len = (int)(p - *arg);
4940 *arg = p;
4941 return len;
4942}
4943
4944/*
4945 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004946 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 * Return 0 if something is wrong.
4948 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004949 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004950get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
4952 char_u *p;
4953 int len;
4954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004955 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004957 {
4958 if (*p == ':')
4959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004960 // "s:" is start of "s:var", but "n:" is not and can be used in
4961 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004962 len = (int)(p - *arg);
4963 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4964 || len > 1)
4965 break;
4966 }
4967 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004968 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 return 0;
4970
4971 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004972 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974 return len;
4975}
4976
4977/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004978 * Get the length of the name of a variable or function.
4979 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004981 * Return -1 if curly braces expansion failed.
4982 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 * If the name contains 'magic' {}'s, expand them and return the
4984 * expanded name in an allocated string via 'alias' - caller must free.
4985 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004986 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004987get_name_len(
4988 char_u **arg,
4989 char_u **alias,
4990 int evaluate,
4991 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
4993 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 char_u *p;
4995 char_u *expr_start;
4996 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004998 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
5000 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5001 && (*arg)[2] == (int)KE_SNR)
5002 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005003 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 *arg += 3;
5005 return get_id_len(arg) + 3;
5006 }
5007 len = eval_fname_script(*arg);
5008 if (len > 0)
5009 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005010 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 *arg += len;
5012 }
5013
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005015 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005017 p = find_name_end(*arg, &expr_start, &expr_end,
5018 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (expr_start != NULL)
5020 {
5021 char_u *temp_string;
5022
5023 if (!evaluate)
5024 {
5025 len += (int)(p - *arg);
5026 *arg = skipwhite(p);
5027 return len;
5028 }
5029
5030 /*
5031 * Include any <SID> etc in the expanded string:
5032 * Thus the -len here.
5033 */
5034 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5035 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005036 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 *alias = temp_string;
5038 *arg = skipwhite(p);
5039 return (int)STRLEN(temp_string);
5040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
5042 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005043 // Only give an error when there is something, otherwise it will be
5044 // reported at a higher level.
5045 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005046 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 return len;
5049}
5050
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005051/*
5052 * Find the end of a variable or function name, taking care of magic braces.
5053 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5054 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005055 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005056 * Return a pointer to just after the name. Equal to "arg" if there is no
5057 * valid name.
5058 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005059 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005060find_name_end(
5061 char_u *arg,
5062 char_u **expr_start,
5063 char_u **expr_end,
5064 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 int mb_nest = 0;
5067 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005069 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005070 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 *expr_start = NULL;
5075 *expr_end = NULL;
5076 }
5077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005078 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005079 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5080 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005081 return arg;
5082
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 for (p = arg; *p != NUL
5084 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005085 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005086 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005087 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005088 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005089 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005091 if (*p == '\'')
5092 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005093 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005094 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005095 ;
5096 if (*p == NUL)
5097 break;
5098 }
5099 else if (*p == '"')
5100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005101 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005102 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005103 if (*p == '\\' && p[1] != NUL)
5104 ++p;
5105 if (*p == NUL)
5106 break;
5107 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005108 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5109 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005110 // "s:" is start of "s:var", but "n:" is not and can be used in
5111 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005112 len = (int)(p - arg);
5113 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005114 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005115 break;
5116 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005117
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005120 if (*p == '[')
5121 ++br_nest;
5122 else if (*p == ']')
5123 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005125
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005126 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005128 if (*p == '{')
5129 {
5130 mb_nest++;
5131 if (expr_start != NULL && *expr_start == NULL)
5132 *expr_start = p;
5133 }
5134 else if (*p == '}')
5135 {
5136 mb_nest--;
5137 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5138 *expr_end = p;
5139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142
5143 return p;
5144}
5145
5146/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005147 * Expands out the 'magic' {}'s in a variable/function name.
5148 * Note that this can call itself recursively, to deal with
5149 * constructs like foo{bar}{baz}{bam}
5150 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5151 * "in_start" ^
5152 * "expr_start" ^
5153 * "expr_end" ^
5154 * "in_end" ^
5155 *
5156 * Returns a new allocated string, which the caller must free.
5157 * Returns NULL for failure.
5158 */
5159 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005160make_expanded_name(
5161 char_u *in_start,
5162 char_u *expr_start,
5163 char_u *expr_end,
5164 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005165{
5166 char_u c1;
5167 char_u *retval = NULL;
5168 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005169
5170 if (expr_end == NULL || in_end == NULL)
5171 return NULL;
5172 *expr_start = NUL;
5173 *expr_end = NUL;
5174 c1 = *in_end;
5175 *in_end = NUL;
5176
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005177 temp_result = eval_to_string(expr_start + 1, FALSE);
5178 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005179 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005180 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5181 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005182 if (retval != NULL)
5183 {
5184 STRCPY(retval, in_start);
5185 STRCAT(retval, temp_result);
5186 STRCAT(retval, expr_end + 1);
5187 }
5188 }
5189 vim_free(temp_result);
5190
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005191 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005192 *expr_start = '{';
5193 *expr_end = '}';
5194
5195 if (retval != NULL)
5196 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005197 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005198 if (expr_start != NULL)
5199 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005200 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005201 temp_result = make_expanded_name(retval, expr_start,
5202 expr_end, temp_result);
5203 vim_free(retval);
5204 retval = temp_result;
5205 }
5206 }
5207
5208 return retval;
5209}
5210
5211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005213 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005215 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005216eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005218 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005219}
5220
5221/*
5222 * Return TRUE if character "c" can be used as the first character in a
5223 * variable or function name (excluding '{' and '}').
5224 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005225 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005226eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005227{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005228 return ASCII_ISALPHA(c) || c == '_';
5229}
5230
5231/*
5232 * Return TRUE if character "c" can be used as the first character of a
5233 * dictionary key.
5234 */
5235 int
5236eval_isdictc(int c)
5237{
5238 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239}
5240
5241/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005242 * Handle:
5243 * - expr[expr], expr[expr:expr] subscript
5244 * - ".name" lookup
5245 * - function call with Funcref variable: func(expr)
5246 * - method call: var->method()
5247 *
5248 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005249 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005250 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005251handle_subscript(
5252 char_u **arg,
5253 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005254 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005255 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005256{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005257 int evaluate = evalarg != NULL
5258 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005259 int ret = OK;
5260 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005261 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005262 int getnext;
5263 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005264
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005265 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005266 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005267 // When at the end of the line and ".name" or "->{" or "->X" follows in
5268 // the next line then consume the line break.
5269 p = eval_next_non_blank(*arg, evalarg, &getnext);
5270 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005271 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005272 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005273 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005274 {
5275 *arg = eval_next_line(evalarg);
5276 check_white = FALSE;
5277 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005279 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5280 || rettv->v_type == VAR_PARTIAL))
5281 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005282 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005283 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5284 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005285
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005286 // Stop the expression evaluation when immediately aborting on
5287 // error, or when an interrupt occurred or an exception was thrown
5288 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005289 if (aborting())
5290 {
5291 if (ret == OK)
5292 clear_tv(rettv);
5293 ret = FAIL;
5294 }
5295 dict_unref(selfdict);
5296 selfdict = NULL;
5297 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005298 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005299 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005300 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005301 if (ret == OK)
5302 {
5303 if ((*arg)[2] == '{')
5304 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005305 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005306 else
5307 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005308 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005309 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005310 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005311 // "." is ".name" lookup when we found a dict or when evaluating and
5312 // scriptversion is at least 2, where string concatenation is "..".
5313 else if (**arg == '['
5314 || (**arg == '.' && (rettv->v_type == VAR_DICT
5315 || (!evaluate
5316 && (*arg)[1] != '.'
5317 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005318 {
5319 dict_unref(selfdict);
5320 if (rettv->v_type == VAR_DICT)
5321 {
5322 selfdict = rettv->vval.v_dict;
5323 if (selfdict != NULL)
5324 ++selfdict->dv_refcount;
5325 }
5326 else
5327 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005328 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005329 {
5330 clear_tv(rettv);
5331 ret = FAIL;
5332 }
5333 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005334 else
5335 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005336 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005338 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5339 // Don't do this when "Func" is already a partial that was bound
5340 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005341 if (selfdict != NULL
5342 && (rettv->v_type == VAR_FUNC
5343 || (rettv->v_type == VAR_PARTIAL
5344 && (rettv->vval.v_partial->pt_auto
5345 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005346 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005347
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005348 dict_unref(selfdict);
5349 return ret;
5350}
5351
5352/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005353 * Make a copy of an item.
5354 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005355 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5356 * reference to an already copied list/dict can be used.
5357 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005358 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005359 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005360item_copy(
5361 typval_T *from,
5362 typval_T *to,
5363 int deep,
5364 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005365{
5366 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005367 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005368
Bram Moolenaar33570922005-01-25 22:26:29 +00005369 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005371 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005372 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005373 }
5374 ++recurse;
5375
5376 switch (from->v_type)
5377 {
5378 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005379 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005380 case VAR_STRING:
5381 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005382 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005383 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005384 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005385 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005386 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005387 copy_tv(from, to);
5388 break;
5389 case VAR_LIST:
5390 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005391 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005392 if (from->vval.v_list == NULL)
5393 to->vval.v_list = NULL;
5394 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5395 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005396 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005397 to->vval.v_list = from->vval.v_list->lv_copylist;
5398 ++to->vval.v_list->lv_refcount;
5399 }
5400 else
5401 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5402 if (to->vval.v_list == NULL)
5403 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005404 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005405 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005406 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005407 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005408 case VAR_DICT:
5409 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005410 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005411 if (from->vval.v_dict == NULL)
5412 to->vval.v_dict = NULL;
5413 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5414 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005415 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005416 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5417 ++to->vval.v_dict->dv_refcount;
5418 }
5419 else
5420 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5421 if (to->vval.v_dict == NULL)
5422 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005423 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005424 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005425 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005426 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005427 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005428 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005429 }
5430 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005431 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005432}
5433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434 void
5435echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5436{
5437 char_u *tofree;
5438 char_u numbuf[NUMBUFLEN];
5439 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5440
5441 if (*atstart)
5442 {
5443 *atstart = FALSE;
5444 // Call msg_start() after eval1(), evaluating the expression
5445 // may cause a message to appear.
5446 if (with_space)
5447 {
5448 // Mark the saved text as finishing the line, so that what
5449 // follows is displayed on a new line when scrolling back
5450 // at the more prompt.
5451 msg_sb_eol();
5452 msg_start();
5453 }
5454 }
5455 else if (with_space)
5456 msg_puts_attr(" ", echo_attr);
5457
5458 if (p != NULL)
5459 for ( ; *p != NUL && !got_int; ++p)
5460 {
5461 if (*p == '\n' || *p == '\r' || *p == TAB)
5462 {
5463 if (*p != TAB && *needclr)
5464 {
5465 // remove any text still there from the command
5466 msg_clr_eos();
5467 *needclr = FALSE;
5468 }
5469 msg_putchar_attr(*p, echo_attr);
5470 }
5471 else
5472 {
5473 if (has_mbyte)
5474 {
5475 int i = (*mb_ptr2len)(p);
5476
5477 (void)msg_outtrans_len_attr(p, i, echo_attr);
5478 p += i - 1;
5479 }
5480 else
5481 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5482 }
5483 }
5484 vim_free(tofree);
5485}
5486
Bram Moolenaare9a41262005-01-15 22:18:47 +00005487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 * ":echo expr1 ..." print each argument separated with a space, add a
5489 * newline at the end.
5490 * ":echon expr1 ..." print each argument plain.
5491 */
5492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005493ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005496 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 char_u *p;
5498 int needclr = TRUE;
5499 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005500 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005501 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005502 evalarg_T evalarg;
5503
Bram Moolenaare707c882020-07-01 13:07:37 +02005504 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
5506 if (eap->skip)
5507 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005508 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005510 // If eval1() causes an error message the text from the command may
5511 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005512 need_clr_eos = needclr;
5513
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005515 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 {
5517 /*
5518 * Report the invalid expression unless the expression evaluation
5519 * has been cancelled due to an aborting error, an interrupt, or an
5520 * exception.
5521 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005522 if (!aborting() && did_emsg == did_emsg_before
5523 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005524 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005525 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 break;
5527 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005528 need_clr_eos = FALSE;
5529
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 arg = skipwhite(arg);
5535 }
5536 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005537 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
5539 if (eap->skip)
5540 --emsg_skip;
5541 else
5542 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005543 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 if (needclr)
5545 msg_clr_eos();
5546 if (eap->cmdidx == CMD_echo)
5547 msg_end();
5548 }
5549}
5550
5551/*
5552 * ":echohl {name}".
5553 */
5554 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005555ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005557 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558}
5559
5560/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005561 * Returns the :echo attribute
5562 */
5563 int
5564get_echo_attr(void)
5565{
5566 return echo_attr;
5567}
5568
5569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 * ":execute expr1 ..." execute the result of an expression.
5571 * ":echomsg expr1 ..." Print a message
5572 * ":echoerr expr1 ..." Print an error
5573 * Each gets spaces around each argument and a newline at the end for
5574 * echo commands
5575 */
5576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005577ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005580 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 int ret = OK;
5582 char_u *p;
5583 garray_T ga;
5584 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
5586 ga_init2(&ga, 1, 80);
5587
5588 if (eap->skip)
5589 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005590 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005592 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005593 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595
5596 if (!eap->skip)
5597 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005598 char_u buf[NUMBUFLEN];
5599
5600 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005601 {
5602 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5603 {
5604 emsg(_(e_inval_string));
5605 p = NULL;
5606 }
5607 else
5608 p = tv_get_string_buf(&rettv, buf);
5609 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005610 else
5611 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005612 if (p == NULL)
5613 {
5614 clear_tv(&rettv);
5615 ret = FAIL;
5616 break;
5617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 len = (int)STRLEN(p);
5619 if (ga_grow(&ga, len + 2) == FAIL)
5620 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005621 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 ret = FAIL;
5623 break;
5624 }
5625 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 ga.ga_len += len;
5629 }
5630
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005631 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 arg = skipwhite(arg);
5633 }
5634
5635 if (ret != FAIL && ga.ga_data != NULL)
5636 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005637 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5638 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005639 // Mark the already saved text as finishing the line, so that what
5640 // follows is displayed on a new line when scrolling back at the
5641 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005642 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005643 }
5644
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005646 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005647 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005648 out_flush();
5649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 else if (eap->cmdidx == CMD_echoerr)
5651 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005652 int save_did_emsg = did_emsg;
5653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005654 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005655 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 if (!force_abort)
5657 did_emsg = save_did_emsg;
5658 }
5659 else if (eap->cmdidx == CMD_execute)
5660 do_cmdline((char_u *)ga.ga_data,
5661 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5662 }
5663
5664 ga_clear(&ga);
5665
5666 if (eap->skip)
5667 --emsg_skip;
5668
5669 eap->nextcmd = check_nextcmd(arg);
5670}
5671
5672/*
5673 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5674 * "arg" points to the "&" or '+' when called, to "option" when returning.
5675 * Returns NULL when no option name found. Otherwise pointer to the char
5676 * after the option name.
5677 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005678 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005679find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
5681 char_u *p = *arg;
5682
5683 ++p;
5684 if (*p == 'g' && p[1] == ':')
5685 {
5686 *opt_flags = OPT_GLOBAL;
5687 p += 2;
5688 }
5689 else if (*p == 'l' && p[1] == ':')
5690 {
5691 *opt_flags = OPT_LOCAL;
5692 p += 2;
5693 }
5694 else
5695 *opt_flags = 0;
5696
5697 if (!ASCII_ISALPHA(*p))
5698 return NULL;
5699 *arg = p;
5700
5701 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005702 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 else
5704 while (ASCII_ISALPHA(*p))
5705 ++p;
5706 return p;
5707}
5708
5709/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005710 * Display script name where an item was last set.
5711 * Should only be invoked when 'verbose' is non-zero.
5712 */
5713 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005714last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005715{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005716 char_u *p;
5717
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005718 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005719 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005720 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005721 if (p != NULL)
5722 {
5723 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005724 msg_puts(_("\n\tLast set from "));
5725 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005726 if (script_ctx.sc_lnum > 0)
5727 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005728 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005729 msg_outnum((long)script_ctx.sc_lnum);
5730 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005731 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005732 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005733 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005734 }
5735}
5736
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005737#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
5739/*
5740 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005741 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 * "flags" can be "g" to do a global substitute.
5743 * Returns an allocated string, NULL for error.
5744 */
5745 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005746do_string_sub(
5747 char_u *str,
5748 char_u *pat,
5749 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005750 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005751 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
5753 int sublen;
5754 regmatch_T regmatch;
5755 int i;
5756 int do_all;
5757 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005758 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 garray_T ga;
5760 char_u *ret;
5761 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005762 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005764 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005766 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 ga_init2(&ga, 1, 200);
5769
5770 do_all = (flags[0] == 'g');
5771
5772 regmatch.rm_ic = p_ic;
5773 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5774 if (regmatch.regprog != NULL)
5775 {
5776 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005777 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5779 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005780 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005781 if (regmatch.startp[0] == regmatch.endp[0])
5782 {
5783 if (zero_width == regmatch.startp[0])
5784 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005785 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005786 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005787 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5788 (size_t)i);
5789 ga.ga_len += i;
5790 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005791 continue;
5792 }
5793 zero_width = regmatch.startp[0];
5794 }
5795
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 /*
5797 * Get some space for a temporary buffer to do the substitution
5798 * into. It will contain:
5799 * - The text up to where the match is.
5800 * - The substituted text.
5801 * - The text after the match.
5802 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005803 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005804 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5806 {
5807 ga_clear(&ga);
5808 break;
5809 }
5810
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005811 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 i = (int)(regmatch.startp[0] - tail);
5813 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005814 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005815 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 + ga.ga_len + i, TRUE, TRUE, FALSE);
5817 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005818 tail = regmatch.endp[0];
5819 if (*tail == NUL)
5820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 if (!do_all)
5822 break;
5823 }
5824
5825 if (ga.ga_data != NULL)
5826 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5827
Bram Moolenaar473de612013-06-08 18:19:48 +02005828 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
5830
5831 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5832 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005833 if (p_cpo == empty_option)
5834 p_cpo = save_cpo;
5835 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005836 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005837 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
5839 return ret;
5840}