blob: abdf076d2649880945d7a8b9841ca1300cf327a1 [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 Moolenaar95dd9f22020-08-07 19:28:08 +0200823 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200825 // "a: type" is declaring variable "a" with a type, not "a:".
826 if (p == name + 2 && p[-1] == ':')
827 {
828 --p;
829 lp->ll_name_end = p;
830 }
831 if (*p == ':')
832 {
833 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
834 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200836 // parse the type after the name
837 lp->ll_type = parse_type(&tp, &si->sn_type_list);
838 lp->ll_name_end = tp;
839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 }
841 }
842
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100843 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
845 return p;
846
847 cc = *p;
848 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100849 // Only pass &ht when we would write to the variable, it prevents autoload
850 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100851 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
852 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000853 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100854 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000855 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000856 if (v == NULL)
857 return NULL;
858
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 /*
860 * Loop until no more [idx] or .key is following.
861 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100863 var1.v_type = VAR_UNKNOWN;
864 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000866 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000867 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
868 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100869 && lp->ll_tv->vval.v_dict != NULL)
870 && !(lp->ll_tv->v_type == VAR_BLOB
871 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100874 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000876 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100880 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000882 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000883
Bram Moolenaar8c711452005-01-14 21:53:12 +0000884 len = -1;
885 if (*p == '.')
886 {
887 key = p + 1;
888 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
889 ;
890 if (len == 0)
891 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000892 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100893 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000895 }
896 p = key + len;
897 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000898 else
899 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100900 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000901 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 if (*p == ':')
903 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000904 else
905 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000906 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200907 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100909 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000910 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100911 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000912 clear_tv(&var1);
913 return NULL;
914 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200915 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000916 }
917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100918 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000919 if (*p == ':')
920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000921 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000923 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100924 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100925 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000926 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000927 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100928 if (rettv != NULL
929 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100930 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100931 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100932 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000934 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100935 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100936 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000937 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000938 }
939 p = skipwhite(p + 1);
940 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000942 else
943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000944 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200945 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200946 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000947 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100948 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000950 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100951 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000952 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100953 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100954 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000955 clear_tv(&var2);
956 return NULL;
957 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000958 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000960 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000961 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000963
Bram Moolenaar8c711452005-01-14 21:53:12 +0000964 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000965 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100967 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100968 clear_tv(&var1);
969 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000970 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000971 }
972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100973 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000974 ++p;
975 }
976
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 {
979 if (len == -1)
980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100981 // "[key]": get key from "var1"
982 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200983 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000984 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000985 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000987 }
988 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000989 lp->ll_list = NULL;
990 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000991 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100993 // When assigning to a scope dictionary check that a function and
994 // variable name is valid (only variable name unless it is l: or
995 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200996 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200997 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200998 int prevval;
999 int wrong;
1000
1001 if (len != -1)
1002 {
1003 prevval = key[len];
1004 key[len] = NUL;
1005 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001006 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001007 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001008 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1009 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001010 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001011 || !valid_varname(key);
1012 if (len != -1)
1013 key[len] = prevval;
1014 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001015 {
1016 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001017 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001018 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001019 }
1020
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001022 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001023 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001024 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001025 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001027 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001028 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001029 return NULL;
1030 }
1031
Bram Moolenaar31b81602019-02-10 22:14:27 +01001032 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001036 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001037 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001039 }
1040 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001042 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001043 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001044 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001046 p = NULL;
1047 break;
1048 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001049 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001050 else if ((flags & GLV_READ_ONLY) == 0
1051 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001052 {
1053 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001054 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001055 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001056
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001057 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001059 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001060 else if (lp->ll_tv->v_type == VAR_BLOB)
1061 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001062 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1063
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001064 /*
1065 * Get the number and item for the only or first index of the List.
1066 */
1067 if (empty1)
1068 lp->ll_n1 = 0;
1069 else
1070 // is number or string
1071 lp->ll_n1 = (long)tv_get_number(&var1);
1072 clear_tv(&var1);
1073
1074 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001075 || lp->ll_n1 > bloblen
1076 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001077 {
1078 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001079 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001080 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001081 return NULL;
1082 }
1083 if (lp->ll_range && !lp->ll_empty2)
1084 {
1085 lp->ll_n2 = (long)tv_get_number(&var2);
1086 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001087 if (lp->ll_n2 < 0
1088 || lp->ll_n2 >= bloblen
1089 || lp->ll_n2 < lp->ll_n1)
1090 {
1091 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001092 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001093 return NULL;
1094 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001095 }
1096 lp->ll_blob = lp->ll_tv->vval.v_blob;
1097 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001098 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001099 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001100 else
1101 {
1102 /*
1103 * Get the number and item for the only or first index of the List.
1104 */
1105 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001106 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001107 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001108 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001109 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001110 clear_tv(&var1);
1111
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001112 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 lp->ll_list = lp->ll_tv->vval.v_list;
1114 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1115 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001116 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001117 if (lp->ll_n1 < 0)
1118 {
1119 lp->ll_n1 = 0;
1120 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1121 }
1122 }
1123 if (lp->ll_li == NULL)
1124 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001125 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001126 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001127 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001128 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001129 }
1130
1131 /*
1132 * May need to find the item or absolute index for the second
1133 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 * When no index given: "lp->ll_empty2" is TRUE.
1135 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001136 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001138 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001139 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001140 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001145 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001146 {
1147 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001148 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001150 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001152 }
1153
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001154 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001155 if (lp->ll_n1 < 0)
1156 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1157 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001158 {
1159 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001160 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001162 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001163 }
1164
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001165 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001166 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001167 }
1168
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001169 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001171 return p;
1172}
1173
1174/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001175 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001178clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001179{
1180 vim_free(lp->ll_exp_name);
1181 vim_free(lp->ll_newkey);
1182}
1183
1184/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001185 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001186 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001187 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1188 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001189 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001191set_var_lval(
1192 lval_T *lp,
1193 char_u *endp,
1194 typval_T *rettv,
1195 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001197 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198{
1199 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001200 listitem_T *ri;
1201 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202
1203 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001204 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001205 cc = *endp;
1206 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001207 if (lp->ll_blob != NULL)
1208 {
1209 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001210
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001211 if (op != NULL && *op != '=')
1212 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001213 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001214 return;
1215 }
1216
1217 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1218 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001219 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001220
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001221 if (lp->ll_empty2)
1222 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1223
1224 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001227 return;
1228 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001229 if (lp->ll_empty2)
1230 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001231
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001232 ir = 0;
1233 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1234 blob_set(lp->ll_blob, il,
1235 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001236 }
1237 else
1238 {
1239 val = (int)tv_get_number_chk(rettv, &error);
1240 if (!error)
1241 {
1242 garray_T *gap = &lp->ll_blob->bv_ga;
1243
1244 // Allow for appending a byte. Setting a byte beyond
1245 // the end is an error otherwise.
1246 if (lp->ll_n1 < gap->ga_len
1247 || (lp->ll_n1 == gap->ga_len
1248 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1249 {
1250 blob_set(lp->ll_blob, lp->ll_n1, val);
1251 if (lp->ll_n1 == gap->ga_len)
1252 ++gap->ga_len;
1253 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001254 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001255 }
1256 }
1257 }
1258 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001259 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001260 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001263 {
1264 emsg(_(e_cannot_mod));
1265 *endp = cc;
1266 return;
1267 }
1268
Bram Moolenaarff697e62019-02-12 22:28:33 +01001269 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001270 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001271 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001272 &tv, &di, TRUE, FALSE) == OK)
1273 {
1274 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001275 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1276 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001277 && tv_op(&tv, rettv, op) == OK)
1278 set_var(lp->ll_name, &tv, FALSE);
1279 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001281 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001282 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001283 {
1284 if (lp->ll_type != NULL
1285 && check_typval_type(lp->ll_type, rettv) == FAIL)
1286 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001288 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001289 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001291 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001292 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001293 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001294 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 else if (lp->ll_range)
1296 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001297 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001298 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001301 {
1302 emsg(_("E996: Cannot lock a range"));
1303 return;
1304 }
1305
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001306 /*
1307 * Check whether any of the list items is locked
1308 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001309 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001310 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001311 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001312 return;
1313 ri = ri->li_next;
1314 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1315 break;
1316 ll_li = ll_li->li_next;
1317 ++ll_n1;
1318 }
1319
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001320 /*
1321 * Assign the List values to the list items.
1322 */
1323 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001325 if (op != NULL && *op != '=')
1326 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1327 else
1328 {
1329 clear_tv(&lp->ll_li->li_tv);
1330 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1331 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 ri = ri->li_next;
1333 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1334 break;
1335 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001336 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001337 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001338 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001339 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 ri = NULL;
1341 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001342 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001343 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001344 lp->ll_li = lp->ll_li->li_next;
1345 ++lp->ll_n1;
1346 }
1347 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001348 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001349 else if (lp->ll_empty2
1350 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001351 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001352 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001353 }
1354 else
1355 {
1356 /*
1357 * Assign to a List or Dictionary item.
1358 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001360 {
1361 emsg(_("E996: Cannot lock a list or dict"));
1362 return;
1363 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001364 if (lp->ll_newkey != NULL)
1365 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001366 if (op != NULL && *op != '=')
1367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001368 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001369 return;
1370 }
1371
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001372 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001373 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001374 if (di == NULL)
1375 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001376 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1377 {
1378 vim_free(di);
1379 return;
1380 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001381 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001382 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001383 else if (op != NULL && *op != '=')
1384 {
1385 tv_op(lp->ll_tv, rettv, op);
1386 return;
1387 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001388 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001389 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001390
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001391 /*
1392 * Assign the value to the variable or list item.
1393 */
1394 if (copy)
1395 copy_tv(rettv, lp->ll_tv);
1396 else
1397 {
1398 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001399 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001400 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 }
1402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001403}
1404
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001405/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001406 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1407 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001408 * Returns OK or FAIL.
1409 */
1410 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001411tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001412{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001413 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001414 char_u numbuf[NUMBUFLEN];
1415 char_u *s;
1416
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001417 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001418 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001419 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001420 {
1421 switch (tv1->v_type)
1422 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001423 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001424 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001426 case VAR_DICT:
1427 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001428 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001429 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001430 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001431 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001432 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 break;
1434
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001435 case VAR_BLOB:
1436 if (*op != '+' || tv2->v_type != VAR_BLOB)
1437 break;
1438 // BLOB += BLOB
1439 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1440 {
1441 blob_T *b1 = tv1->vval.v_blob;
1442 blob_T *b2 = tv2->vval.v_blob;
1443 int i, len = blob_len(b2);
1444 for (i = 0; i < len; i++)
1445 ga_append(&b1->bv_ga, blob_get(b2, i));
1446 }
1447 return OK;
1448
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 case VAR_LIST:
1450 if (*op != '+' || tv2->v_type != VAR_LIST)
1451 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001452 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1454 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1455 return OK;
1456
1457 case VAR_NUMBER:
1458 case VAR_STRING:
1459 if (tv2->v_type == VAR_LIST)
1460 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001461 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001462 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001463 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001464 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001465#ifdef FEAT_FLOAT
1466 if (tv2->v_type == VAR_FLOAT)
1467 {
1468 float_T f = n;
1469
Bram Moolenaarff697e62019-02-12 22:28:33 +01001470 if (*op == '%')
1471 break;
1472 switch (*op)
1473 {
1474 case '+': f += tv2->vval.v_float; break;
1475 case '-': f -= tv2->vval.v_float; break;
1476 case '*': f *= tv2->vval.v_float; break;
1477 case '/': f /= tv2->vval.v_float; break;
1478 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001479 clear_tv(tv1);
1480 tv1->v_type = VAR_FLOAT;
1481 tv1->vval.v_float = f;
1482 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001483 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001484#endif
1485 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001486 switch (*op)
1487 {
1488 case '+': n += tv_get_number(tv2); break;
1489 case '-': n -= tv_get_number(tv2); break;
1490 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001491 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1492 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001493 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001494 clear_tv(tv1);
1495 tv1->v_type = VAR_NUMBER;
1496 tv1->vval.v_number = n;
1497 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001498 }
1499 else
1500 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001501 if (tv2->v_type == VAR_FLOAT)
1502 break;
1503
Bram Moolenaarff697e62019-02-12 22:28:33 +01001504 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001505 s = tv_get_string(tv1);
1506 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001507 clear_tv(tv1);
1508 tv1->v_type = VAR_STRING;
1509 tv1->vval.v_string = s;
1510 }
1511 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001512
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001513 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001514#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001515 {
1516 float_T f;
1517
Bram Moolenaarff697e62019-02-12 22:28:33 +01001518 if (*op == '%' || *op == '.'
1519 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001520 && tv2->v_type != VAR_NUMBER
1521 && tv2->v_type != VAR_STRING))
1522 break;
1523 if (tv2->v_type == VAR_FLOAT)
1524 f = tv2->vval.v_float;
1525 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001526 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001527 switch (*op)
1528 {
1529 case '+': tv1->vval.v_float += f; break;
1530 case '-': tv1->vval.v_float -= f; break;
1531 case '*': tv1->vval.v_float *= f; break;
1532 case '/': tv1->vval.v_float /= f; break;
1533 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001534 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001535#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001536 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001537 }
1538 }
1539
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001540 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541 return FAIL;
1542}
1543
1544/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001545 * Evaluate the expression used in a ":for var in expr" command.
1546 * "arg" points to "var".
1547 * Set "*errp" to TRUE for an error, FALSE otherwise;
1548 * Return a pointer that holds the info. Null when there is an error.
1549 */
1550 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001551eval_for_line(
1552 char_u *arg,
1553 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001554 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001555 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001556{
Bram Moolenaar33570922005-01-25 22:26:29 +00001557 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001558 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001559 typval_T tv;
1560 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001561 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001563 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001564
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001565 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001566 if (fi == NULL)
1567 return NULL;
1568
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001569 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001570 if (expr == NULL)
1571 return fi;
1572
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001573 expr = skipwhite_and_linebreak(expr, evalarg);
1574 if (expr[0] != 'i' || expr[1] != 'n'
1575 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001576 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001578 return fi;
1579 }
1580
1581 if (skip)
1582 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001583 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1584 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001585 {
1586 *errp = FALSE;
1587 if (!skip)
1588 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001589 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001590 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001591 l = tv.vval.v_list;
1592 if (l == NULL)
1593 {
1594 // a null list is like an empty list: do nothing
1595 clear_tv(&tv);
1596 }
1597 else
1598 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001600 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001602 // No need to increment the refcount, it's already set for
1603 // the list being used in "tv".
1604 fi->fi_list = l;
1605 list_add_watch(l, &fi->fi_lw);
1606 fi->fi_lw.lw_item = l->lv_first;
1607 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001608 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001609 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001610 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001611 fi->fi_bi = 0;
1612 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001613 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001614 typval_T btv;
1615
1616 // Make a copy, so that the iteration still works when the
1617 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001619 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001620 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001621 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001622 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623 else
1624 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001625 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001626 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001627 }
1628 }
1629 }
1630 if (skip)
1631 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001632 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001633
1634 return fi;
1635}
1636
1637/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001638 * Used when looping over a :for line, skip the "in expr" part.
1639 */
1640 void
1641skip_for_lines(void *fi_void, evalarg_T *evalarg)
1642{
1643 forinfo_T *fi = (forinfo_T *)fi_void;
1644 int i;
1645
1646 for (i = 0; i < fi->fi_break_count; ++i)
1647 eval_next_line(evalarg);
1648}
1649
1650/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 * Use the first item in a ":for" list. Advance to the next.
1652 * Assign the values to the variable (list). "arg" points to the first one.
1653 * Return TRUE when a valid item was found, FALSE when at end of list or
1654 * something wrong.
1655 */
1656 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001657next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001659 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 int result;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001661 int flag = in_vim9script() ? LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001662 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001664 if (fi->fi_blob != NULL)
1665 {
1666 typval_T tv;
1667
1668 if (fi->fi_bi >= blob_len(fi->fi_blob))
1669 return FALSE;
1670 tv.v_type = VAR_NUMBER;
1671 tv.v_lock = VAR_FIXED;
1672 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1673 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001674 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001675 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001676 }
1677
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 item = fi->fi_lw.lw_item;
1679 if (item == NULL)
1680 result = FALSE;
1681 else
1682 {
1683 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001684 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001685 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 }
1687 return result;
1688}
1689
1690/*
1691 * Free the structure used to store info used by ":for".
1692 */
1693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001694free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695{
Bram Moolenaar33570922005-01-25 22:26:29 +00001696 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001698 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001699 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001701 list_unref(fi->fi_list);
1702 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001703 if (fi != NULL && fi->fi_blob != NULL)
1704 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 vim_free(fi);
1706}
1707
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001709set_context_for_expression(
1710 expand_T *xp,
1711 char_u *arg,
1712 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 int got_eq = FALSE;
1715 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001718 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 {
1720 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001721 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001723 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001724 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 {
1726 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001727 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001728 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 break;
1730 }
1731 return;
1732 }
1733 }
1734 else
1735 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1736 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 while ((xp->xp_pattern = vim_strpbrk(arg,
1738 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1739 {
1740 c = *xp->xp_pattern;
1741 if (c == '&')
1742 {
1743 c = xp->xp_pattern[1];
1744 if (c == '&')
1745 {
1746 ++xp->xp_pattern;
1747 xp->xp_context = cmdidx != CMD_let || got_eq
1748 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1749 }
1750 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001753 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1754 xp->xp_pattern += 2;
1755
1756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
1758 else if (c == '$')
1759 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001760 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 xp->xp_context = EXPAND_ENV_VARS;
1762 }
1763 else if (c == '=')
1764 {
1765 got_eq = TRUE;
1766 xp->xp_context = EXPAND_EXPRESSION;
1767 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001768 else if (c == '#'
1769 && xp->xp_context == EXPAND_EXPRESSION)
1770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001771 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001772 break;
1773 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001774 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 && xp->xp_context == EXPAND_FUNCTIONS
1776 && vim_strchr(xp->xp_pattern, '(') == NULL)
1777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001778 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 break;
1780 }
1781 else if (cmdidx != CMD_let || got_eq)
1782 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001783 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 {
1785 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1786 if (c == '\\' && xp->xp_pattern[1] != NUL)
1787 ++xp->xp_pattern;
1788 xp->xp_context = EXPAND_NOTHING;
1789 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001790 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001792 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1794 /* skip */ ;
1795 xp->xp_context = EXPAND_NOTHING;
1796 }
1797 else if (c == '|')
1798 {
1799 if (xp->xp_pattern[1] == '|')
1800 {
1801 ++xp->xp_pattern;
1802 xp->xp_context = EXPAND_EXPRESSION;
1803 }
1804 else
1805 xp->xp_context = EXPAND_COMMANDS;
1806 }
1807 else
1808 xp->xp_context = EXPAND_EXPRESSION;
1809 }
1810 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001811 // Doesn't look like something valid, expand as an expression
1812 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 arg = xp->xp_pattern;
1815 if (*arg != NUL)
1816 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1817 /* skip */ ;
1818 }
1819 xp->xp_pattern = arg;
1820}
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001823 * Return TRUE if "pat" matches "text".
1824 * Does not use 'cpo' and always uses 'magic'.
1825 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001826 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001827pattern_match(char_u *pat, char_u *text, int ic)
1828{
1829 int matches = FALSE;
1830 char_u *save_cpo;
1831 regmatch_T regmatch;
1832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001833 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001834 save_cpo = p_cpo;
1835 p_cpo = (char_u *)"";
1836 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1837 if (regmatch.regprog != NULL)
1838 {
1839 regmatch.rm_ic = ic;
1840 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1841 vim_regfree(regmatch.regprog);
1842 }
1843 p_cpo = save_cpo;
1844 return matches;
1845}
1846
1847/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001848 * Handle a name followed by "(". Both for just "name(arg)" and for
1849 * "expr->name(arg)".
1850 * Returns OK or FAIL.
1851 */
1852 static int
1853eval_func(
1854 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001855 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001856 char_u *name,
1857 int name_len,
1858 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001859 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001860 typval_T *basetv) // "expr" for "expr->name(arg)"
1861{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001862 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001863 char_u *s = name;
1864 int len = name_len;
1865 partial_T *partial;
1866 int ret = OK;
1867
1868 if (!evaluate)
1869 check_vars(s, len);
1870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001871 // If "s" is the name of a variable of type VAR_FUNC
1872 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001873 s = deref_func_name(s, &len, &partial, !evaluate);
1874
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001875 // Need to make a copy, in case evaluating the arguments makes
1876 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001877 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001878 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001879 ret = FAIL;
1880 else
1881 {
1882 funcexe_T funcexe;
1883
1884 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001885 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001886 funcexe.firstline = curwin->w_cursor.lnum;
1887 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001888 funcexe.evaluate = evaluate;
1889 funcexe.partial = partial;
1890 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001891 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001892 }
1893 vim_free(s);
1894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001895 // If evaluate is FALSE rettv->v_type was not set in
1896 // get_func_tv, but it's needed in handle_subscript() to parse
1897 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001898 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1899 {
1900 rettv->vval.v_string = NULL;
1901 rettv->v_type = VAR_FUNC;
1902 }
1903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001904 // Stop the expression evaluation when immediately
1905 // aborting on error, or when an interrupt occurred or
1906 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001907 if (evaluate && aborting())
1908 {
1909 if (ret == OK)
1910 clear_tv(rettv);
1911 ret = FAIL;
1912 }
1913 return ret;
1914}
1915
1916/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001917 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1918 * comment) and there is a next line, return the next line (skipping blanks)
1919 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001920 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1921 * "arg" must point somewhere inside a line, not at the start.
1922 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001923 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001924eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1925{
Bram Moolenaar9d489562020-07-30 20:08:50 +02001926 char_u *p = skipwhite(arg);
1927
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001928 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001929 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001930 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001931 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001932 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001933 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02001934 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001935
1936 if (evalarg->eval_cookie != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001937 next = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001938 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001939 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001940
Bram Moolenaar9d489562020-07-30 20:08:50 +02001941 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001942 {
1943 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001944 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001945 }
1946 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02001947 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001948}
1949
1950/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001951 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001952 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001953 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001954eval_next_line(evalarg_T *evalarg)
1955{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001956 garray_T *gap = &evalarg->eval_ga;
1957 char_u *line;
1958
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001959 if (evalarg->eval_cookie != NULL)
1960 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
1961 else
1962 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001963 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001964 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1965 {
1966 // Going to concatenate the lines after parsing.
1967 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1968 ++gap->ga_len;
1969 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001970 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001971 {
1972 vim_free(evalarg->eval_tofree);
1973 evalarg->eval_tofree = line;
1974 }
1975 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001976}
1977
Bram Moolenaare6b53242020-07-01 17:28:33 +02001978/*
1979 * Call eval_next_non_blank() and get the next line if needed.
1980 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001981 char_u *
1982skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1983{
1984 int getnext;
1985 char_u *p = skipwhite(arg);
1986
Bram Moolenaar962d7212020-07-04 14:15:00 +02001987 if (evalarg == NULL)
1988 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02001989 eval_next_non_blank(p, evalarg, &getnext);
1990 if (getnext)
1991 return eval_next_line(evalarg);
1992 return p;
1993}
1994
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001995/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001996 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001997 */
1998 void
1999clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2000{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002001 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002002 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002003 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002004 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002005 if (eap != NULL)
2006 {
2007 // We may need to keep the original command line, e.g. for
2008 // ":let" it has the variable names. But we may also need the
2009 // new one, "nextcmd" points into it. Keep both.
2010 vim_free(eap->cmdline_tofree);
2011 eap->cmdline_tofree = *eap->cmdlinep;
2012 *eap->cmdlinep = evalarg->eval_tofree;
2013 }
2014 else
2015 vim_free(evalarg->eval_tofree);
2016 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002017 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002018
2019 vim_free(evalarg->eval_tofree_lambda);
2020 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002021 }
2022}
2023
2024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2028 */
2029
2030/*
2031 * Handle zero level expression.
2032 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002034 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002035 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 * Return OK or FAIL.
2037 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002038 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002039eval0(
2040 char_u *arg,
2041 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002042 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002043 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
2045 int ret;
2046 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002047 int did_emsg_before = did_emsg;
2048 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002049 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002052 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002053 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002054
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002055 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
2057 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002058 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 /*
2060 * Report the invalid expression unless the expression evaluation has
2061 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002062 * exception, or we already gave a more specific error.
2063 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002065 if (!aborting()
2066 && did_emsg == did_emsg_before
2067 && called_emsg == called_emsg_before
2068 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002069 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 ret = FAIL;
2071 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002072
2073 if (eap != NULL)
2074 eap->nextcmd = check_nextcmd(p);
2075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 return ret;
2077}
2078
2079/*
2080 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002081 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 *
2083 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002084 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002086 * Note: "rettv.v_lock" is not set.
2087 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 * Return OK or FAIL.
2089 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002090 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002091eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002093 char_u *p;
2094 int getnext;
2095
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 /*
2097 * Get the first variable.
2098 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002099 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 return FAIL;
2101
Bram Moolenaar793648f2020-06-26 21:28:25 +02002102 p = eval_next_non_blank(*arg, evalarg, &getnext);
2103 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002105 int result;
2106 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002107 evalarg_T *evalarg_used = evalarg;
2108 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002109 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002110 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002111
2112 if (evalarg == NULL)
2113 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002114 CLEAR_FIELD(local_evalarg);
2115 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002117 orig_flags = evalarg_used->eval_flags;
2118 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002119
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002120 if (getnext)
2121 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002122 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002123 {
2124 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2125 {
2126 error_white_both(p, 1);
2127 clear_tv(rettv);
2128 return FAIL;
2129 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002130 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002131 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002132
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002134 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002136 int error = FALSE;
2137
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002138 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002141 if (error)
2142 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
2144
2145 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002146 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002148 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2149 {
2150 error_white_both(p, 1);
2151 clear_tv(rettv);
2152 return FAIL;
2153 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002154 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2155 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002156 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002157 if (eval1(arg, rettv, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 return FAIL;
2159
2160 /*
2161 * Check for the ":".
2162 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002163 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar793648f2020-06-26 21:28:25 +02002164 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 return FAIL;
2170 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002171 if (getnext)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002172 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002173 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002174 {
2175 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2176 {
2177 error_white_both(p, 1);
2178 clear_tv(rettv);
2179 return FAIL;
2180 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002181 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183
2184 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002185 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002187 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2188 {
2189 error_white_both(p, 1);
2190 clear_tv(rettv);
2191 return FAIL;
2192 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002193 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2194 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002195 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002196 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
2198 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 return FAIL;
2201 }
2202 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 *rettv = var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002204
2205 if (evalarg == NULL)
2206 clear_evalarg(&local_evalarg, NULL);
2207 else
2208 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
2210
2211 return OK;
2212}
2213
2214/*
2215 * Handle first level expression:
2216 * expr2 || expr2 || expr2 logical OR
2217 *
2218 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002219 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 *
2221 * Return OK or FAIL.
2222 */
2223 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002224eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002226 char_u *p;
2227 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
2229 /*
2230 * Get the first variable.
2231 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002232 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 return FAIL;
2234
2235 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002236 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002238 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002239 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002241 evalarg_T *evalarg_used = evalarg;
2242 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002243 int evaluate;
2244 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002245 long result = FALSE;
2246 typval_T var2;
2247 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002248 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002249
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002250 if (evalarg == NULL)
2251 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002252 CLEAR_FIELD(local_evalarg);
2253 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002254 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002255 orig_flags = evalarg_used->eval_flags;
2256 evaluate = orig_flags & EVAL_EVALUATE;
2257 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002258 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002259 if (vim9script)
2260 {
2261 result = tv2bool(rettv);
2262 }
2263 else
2264 {
2265 error = FALSE;
2266 if (tv_get_number_chk(rettv, &error) != 0)
2267 result = TRUE;
2268 clear_tv(rettv);
2269 if (error)
2270 return FAIL;
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
2273
2274 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002275 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002277 while (p[0] == '|' && p[1] == '|')
2278 {
2279 if (getnext)
2280 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002281 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002282 {
2283 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2284 {
2285 error_white_both(p, 2);
2286 clear_tv(rettv);
2287 return FAIL;
2288 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002289 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002290 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002291
2292 /*
2293 * Get the second variable.
2294 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002295 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2296 {
2297 error_white_both(p, 2);
2298 clear_tv(rettv);
2299 return FAIL;
2300 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002301 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2302 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002303 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002304 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002305 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002306
2307 /*
2308 * Compute the result.
2309 */
2310 if (evaluate && !result)
2311 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002312 if (vim9script)
2313 {
2314 clear_tv(rettv);
2315 *rettv = var2;
2316 result = tv2bool(rettv);
2317 }
2318 else
2319 {
2320 if (tv_get_number_chk(&var2, &error) != 0)
2321 result = TRUE;
2322 clear_tv(&var2);
2323 if (error)
2324 return FAIL;
2325 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002326 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002327 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002328 {
2329 rettv->v_type = VAR_NUMBER;
2330 rettv->vval.v_number = result;
2331 }
2332
2333 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002335
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002336 if (evalarg == NULL)
2337 clear_evalarg(&local_evalarg, NULL);
2338 else
2339 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341
2342 return OK;
2343}
2344
2345/*
2346 * Handle second level expression:
2347 * expr3 && expr3 && expr3 logical AND
2348 *
2349 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002350 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 *
2352 * Return OK or FAIL.
2353 */
2354 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002355eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002357 char_u *p;
2358 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360 /*
2361 * Get the first variable.
2362 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002363 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 return FAIL;
2365
2366 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002367 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002369 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002370 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002372 evalarg_T *evalarg_used = evalarg;
2373 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002374 int orig_flags;
2375 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002376 long result = TRUE;
2377 typval_T var2;
2378 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002379 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002380
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002381 if (evalarg == NULL)
2382 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002383 CLEAR_FIELD(local_evalarg);
2384 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002385 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002386 orig_flags = evalarg_used->eval_flags;
2387 evaluate = orig_flags & EVAL_EVALUATE;
2388 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002389 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002390 if (vim9script)
2391 {
2392 result = tv2bool(rettv);
2393 }
2394 else
2395 {
2396 error = FALSE;
2397 if (tv_get_number_chk(rettv, &error) == 0)
2398 result = FALSE;
2399 clear_tv(rettv);
2400 if (error)
2401 return FAIL;
2402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404
2405 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002406 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002408 while (p[0] == '&' && p[1] == '&')
2409 {
2410 if (getnext)
2411 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002412 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002413 {
2414 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2415 {
2416 error_white_both(p, 2);
2417 clear_tv(rettv);
2418 return FAIL;
2419 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002420 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002421 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002422
2423 /*
2424 * Get the second variable.
2425 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002426 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2427 {
2428 error_white_both(p, 2);
2429 clear_tv(rettv);
2430 return FAIL;
2431 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002432 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2433 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002434 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002435 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002436 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002437
2438 /*
2439 * Compute the result.
2440 */
2441 if (evaluate && result)
2442 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002443 if (vim9script)
2444 {
2445 clear_tv(rettv);
2446 *rettv = var2;
2447 result = tv2bool(rettv);
2448 }
2449 else
2450 {
2451 if (tv_get_number_chk(&var2, &error) == 0)
2452 result = FALSE;
2453 clear_tv(&var2);
2454 if (error)
2455 return FAIL;
2456 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002457 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002458 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002459 {
2460 rettv->v_type = VAR_NUMBER;
2461 rettv->vval.v_number = result;
2462 }
2463
2464 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002466
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002467 if (evalarg == NULL)
2468 clear_evalarg(&local_evalarg, NULL);
2469 else
2470 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472
2473 return OK;
2474}
2475
2476/*
2477 * Handle third level expression:
2478 * var1 == var2
2479 * var1 =~ var2
2480 * var1 != var2
2481 * var1 !~ var2
2482 * var1 > var2
2483 * var1 >= var2
2484 * var1 < var2
2485 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002486 * var1 is var2
2487 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 *
2489 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002490 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 *
2492 * Return OK or FAIL.
2493 */
2494 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002495eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002498 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002499 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002501 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 /*
2504 * Get the first variable.
2505 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002506 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 return FAIL;
2508
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002509 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002510 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002513 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002515 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002517 typval_T var2;
2518 int ic;
2519 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002520 int evaluate = evalarg == NULL
2521 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002522
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002523 if (getnext)
2524 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002525 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2526 {
2527 error_white_both(p, len);
2528 clear_tv(rettv);
2529 return FAIL;
2530 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002531
Bram Moolenaar696ba232020-07-29 21:20:41 +02002532 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2533 {
2534 semsg(_(e_invexpr2), p);
2535 clear_tv(rettv);
2536 return FAIL;
2537 }
2538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002539 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (p[len] == '?')
2541 {
2542 ic = TRUE;
2543 ++len;
2544 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002545 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 else if (p[len] == '#')
2547 {
2548 ic = FALSE;
2549 ++len;
2550 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002551 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002553 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
2555 /*
2556 * Get the second variable.
2557 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002558 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2559 {
2560 error_white_both(p, 1);
2561 clear_tv(rettv);
2562 return FAIL;
2563 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002564 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002565 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 return FAIL;
2569 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002570 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002571 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002572 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002573
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002574 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002575 {
2576 ret = FAIL;
2577 clear_tv(rettv);
2578 }
2579 else
2580 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002581 clear_tv(&var2);
2582 return ret;
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585
2586 return OK;
2587}
2588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 void
2590eval_addblob(typval_T *tv1, typval_T *tv2)
2591{
2592 blob_T *b1 = tv1->vval.v_blob;
2593 blob_T *b2 = tv2->vval.v_blob;
2594 blob_T *b = blob_alloc();
2595 int i;
2596
2597 if (b != NULL)
2598 {
2599 for (i = 0; i < blob_len(b1); i++)
2600 ga_append(&b->bv_ga, blob_get(b1, i));
2601 for (i = 0; i < blob_len(b2); i++)
2602 ga_append(&b->bv_ga, blob_get(b2, i));
2603
2604 clear_tv(tv1);
2605 rettv_blob_set(tv1, b);
2606 }
2607}
2608
2609 int
2610eval_addlist(typval_T *tv1, typval_T *tv2)
2611{
2612 typval_T var3;
2613
2614 // concatenate Lists
2615 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2616 {
2617 clear_tv(tv1);
2618 clear_tv(tv2);
2619 return FAIL;
2620 }
2621 clear_tv(tv1);
2622 *tv1 = var3;
2623 return OK;
2624}
2625
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626/*
2627 * Handle fourth level expression:
2628 * + number addition
2629 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002630 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002631 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 *
2633 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002634 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 *
2636 * Return OK or FAIL.
2637 */
2638 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002639eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 /*
2642 * Get the first variable.
2643 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 return FAIL;
2646
2647 /*
2648 * Repeat computing, until no '+', '-' or '.' is following.
2649 */
2650 for (;;)
2651 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002652 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002653 int getnext;
2654 char_u *p;
2655 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002656 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002657 int concat;
2658 typval_T var2;
2659
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002660 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002661 p = eval_next_non_blank(*arg, evalarg, &getnext);
2662 op = *p;
2663 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002664 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002666
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002667 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002668 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002669 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002670 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002671 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002672 {
2673 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2674 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002675 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002676 clear_tv(rettv);
2677 return FAIL;
2678 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002679 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002680 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002681 if ((op != '+' || (rettv->v_type != VAR_LIST
2682 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002683#ifdef FEAT_FLOAT
2684 && (op == '.' || rettv->v_type != VAR_FLOAT)
2685#endif
2686 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002687 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002688 // For "list + ...", an illegal use of the first operand as
2689 // a number cannot be determined before evaluating the 2nd
2690 // operand: if this is also a list, all is ok.
2691 // For "something . ...", "something - ..." or "non-list + ...",
2692 // we know that the first operand needs to be a string or number
2693 // without evaluating the 2nd operand. So check before to avoid
2694 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002695 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002696 {
2697 clear_tv(rettv);
2698 return FAIL;
2699 }
2700 }
2701
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 /*
2703 * Get the second variable.
2704 */
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002705 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[oplen]))
2706 {
2707 error_white_both(p, oplen);
2708 clear_tv(rettv);
2709 return FAIL;
2710 }
2711 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002712 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002714 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 return FAIL;
2716 }
2717
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002718 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
2720 /*
2721 * Compute the result.
2722 */
2723 if (op == '.')
2724 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002725 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2726 char_u *s1 = tv_get_string_buf(rettv, buf1);
2727 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002729 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002730 {
2731 clear_tv(rettv);
2732 clear_tv(&var2);
2733 return FAIL;
2734 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002735 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002736 clear_tv(rettv);
2737 rettv->v_type = VAR_STRING;
2738 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002740 else if (op == '+' && rettv->v_type == VAR_BLOB
2741 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002743 else if (op == '+' && rettv->v_type == VAR_LIST
2744 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002745 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002747 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 else
2750 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002751 int error = FALSE;
2752 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002753#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002754 float_T f1 = 0, f2 = 0;
2755
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002756 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002757 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002758 f1 = rettv->vval.v_float;
2759 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002761 else
2762#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002763 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002764 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002765 if (error)
2766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002767 // This can only happen for "list + non-list". For
2768 // "non-list + ..." or "something - ...", we returned
2769 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002770 clear_tv(rettv);
2771 return FAIL;
2772 }
2773#ifdef FEAT_FLOAT
2774 if (var2.v_type == VAR_FLOAT)
2775 f1 = n1;
2776#endif
2777 }
2778#ifdef FEAT_FLOAT
2779 if (var2.v_type == VAR_FLOAT)
2780 {
2781 f2 = var2.vval.v_float;
2782 n2 = 0;
2783 }
2784 else
2785#endif
2786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002787 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002788 if (error)
2789 {
2790 clear_tv(rettv);
2791 clear_tv(&var2);
2792 return FAIL;
2793 }
2794#ifdef FEAT_FLOAT
2795 if (rettv->v_type == VAR_FLOAT)
2796 f2 = n2;
2797#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002799 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002800
2801#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002802 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002803 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2804 {
2805 if (op == '+')
2806 f1 = f1 + f2;
2807 else
2808 f1 = f1 - f2;
2809 rettv->v_type = VAR_FLOAT;
2810 rettv->vval.v_float = f1;
2811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002813#endif
2814 {
2815 if (op == '+')
2816 n1 = n1 + n2;
2817 else
2818 n1 = n1 - n2;
2819 rettv->v_type = VAR_NUMBER;
2820 rettv->vval.v_number = n1;
2821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002823 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825 }
2826 return OK;
2827}
2828
2829/*
2830 * Handle fifth level expression:
2831 * * number multiplication
2832 * / number division
2833 * % number modulo
2834 *
2835 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002836 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 *
2838 * Return OK or FAIL.
2839 */
2840 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002841eval6(
2842 char_u **arg,
2843 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002844 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002845 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002847#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002848 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
2851 /*
2852 * Get the first variable.
2853 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002854 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 return FAIL;
2856
2857 /*
2858 * Repeat computing, until no '*', '/' or '%' is following.
2859 */
2860 for (;;)
2861 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002862 int evaluate;
2863 int getnext;
2864 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002865 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002866 int op;
2867 varnumber_T n1, n2;
2868#ifdef FEAT_FLOAT
2869 float_T f1, f2;
2870#endif
2871 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002872
Bram Moolenaar9d489562020-07-30 20:08:50 +02002873 p = eval_next_non_blank(*arg, evalarg, &getnext);
2874 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002875 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002877
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002878 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002879 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002880 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002881 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002882 {
2883 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2884 {
2885 error_white_both(p, 1);
2886 clear_tv(rettv);
2887 return FAIL;
2888 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002889 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002892#ifdef FEAT_FLOAT
2893 f1 = 0;
2894 f2 = 0;
2895#endif
2896 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002897 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002899#ifdef FEAT_FLOAT
2900 if (rettv->v_type == VAR_FLOAT)
2901 {
2902 f1 = rettv->vval.v_float;
2903 use_float = TRUE;
2904 n1 = 0;
2905 }
2906 else
2907#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002908 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002910 if (error)
2911 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
2913 else
2914 n1 = 0;
2915
2916 /*
2917 * Get the second variable.
2918 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002919 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2920 {
2921 error_white_both(p, 1);
2922 clear_tv(rettv);
2923 return FAIL;
2924 }
2925 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002926 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 return FAIL;
2928
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002929 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002931#ifdef FEAT_FLOAT
2932 if (var2.v_type == VAR_FLOAT)
2933 {
2934 if (!use_float)
2935 {
2936 f1 = n1;
2937 use_float = TRUE;
2938 }
2939 f2 = var2.vval.v_float;
2940 n2 = 0;
2941 }
2942 else
2943#endif
2944 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002945 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002946 clear_tv(&var2);
2947 if (error)
2948 return FAIL;
2949#ifdef FEAT_FLOAT
2950 if (use_float)
2951 f2 = n2;
2952#endif
2953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
2955 /*
2956 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002957 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002959#ifdef FEAT_FLOAT
2960 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002962 if (op == '*')
2963 f1 = f1 * f2;
2964 else if (op == '/')
2965 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002966# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002967 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002968 if (f2 == 0.0)
2969 {
2970 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002971 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002972 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002973 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002974 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002975 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002976 }
2977 else
2978 f1 = f1 / f2;
2979# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002980 // We rely on the floating point library to handle divide
2981 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002982 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002983# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002986 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002988 return FAIL;
2989 }
2990 rettv->v_type = VAR_FLOAT;
2991 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
2993 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002996 if (op == '*')
2997 n1 = n1 * n2;
2998 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002999 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003001 n1 = num_modulus(n1, n2);
3002
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003003 rettv->v_type = VAR_NUMBER;
3004 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
3007 }
3008
3009 return OK;
3010}
3011
3012/*
3013 * Handle sixth level expression:
3014 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003015 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003016 * "string" string constant
3017 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 * &option-name option value
3019 * @r register contents
3020 * identifier variable value
3021 * function() function call
3022 * $VAR environment variable
3023 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003024 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003026 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003027 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 *
3029 * Also handle:
3030 * ! in front logical NOT
3031 * - in front unary minus
3032 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003033 * trailing [] subscript in String or List
3034 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003035 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 *
3037 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003038 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 *
3040 * Return OK or FAIL.
3041 */
3042 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003043eval7(
3044 char_u **arg,
3045 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003046 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003049 int evaluate = evalarg != NULL
3050 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 int len;
3052 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 char_u *start_leader, *end_leader;
3054 int ret = OK;
3055 char_u *alias;
3056
3057 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003058 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003059 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
3063 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003064 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 */
3066 start_leader = *arg;
3067 while (**arg == '!' || **arg == '-' || **arg == '+')
3068 *arg = skipwhite(*arg + 1);
3069 end_leader = *arg;
3070
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003071 if (**arg == '.' && (!isdigit(*(*arg + 1))
3072#ifdef FEAT_FLOAT
3073 || current_sctx.sc_version < 2
3074#endif
3075 ))
3076 {
3077 semsg(_(e_invexpr2), *arg);
3078 ++*arg;
3079 return FAIL;
3080 }
3081
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 switch (**arg)
3083 {
3084 /*
3085 * Number constant.
3086 */
3087 case '0':
3088 case '1':
3089 case '2':
3090 case '3':
3091 case '4':
3092 case '5':
3093 case '6':
3094 case '7':
3095 case '8':
3096 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003097 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003098
3099 // Apply prefixed "-" and "+" now. Matters especially when
3100 // "->" follows.
3101 if (ret == OK && evaluate && end_leader > start_leader)
3102 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 break;
3104
3105 /*
3106 * String constant: "string".
3107 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003108 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 break;
3110
3111 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003112 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003114 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003115 break;
3116
3117 /*
3118 * List: [expr, expr]
3119 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003120 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 break;
3122
3123 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003124 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003125 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003126 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003127 {
3128 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003129 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003130 }
3131 else
3132 ret = NOTDONE;
3133 break;
3134
3135 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003136 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003137 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003138 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003139 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003140 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003141 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003142 break;
3143
3144 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003145 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003147 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 break;
3149
3150 /*
3151 * Environment variable: $VAR.
3152 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003153 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 break;
3155
3156 /*
3157 * Register contents: @r.
3158 */
3159 case '@': ++*arg;
3160 if (evaluate)
3161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003162 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003163 rettv->vval.v_string = get_reg_contents(**arg,
3164 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 }
3166 if (**arg != NUL)
3167 ++*arg;
3168 break;
3169
3170 /*
3171 * nested expression: (expression).
3172 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003173 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003174 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003175 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003176
Bram Moolenaar9215f012020-06-27 21:18:00 +02003177 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003178 if (**arg == ')')
3179 ++*arg;
3180 else if (ret == OK)
3181 {
3182 emsg(_(e_missing_close));
3183 clear_tv(rettv);
3184 ret = FAIL;
3185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187 break;
3188
Bram Moolenaar8c711452005-01-14 21:53:12 +00003189 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 break;
3191 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003192
3193 if (ret == NOTDONE)
3194 {
3195 /*
3196 * Must be a variable or function name.
3197 * Can also be a curly-braces kind of name: {expr}.
3198 */
3199 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003200 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003201 if (alias != NULL)
3202 s = alias;
3203
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003204 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003205 ret = FAIL;
3206 else
3207 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003208 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3209
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003210 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3211 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003212 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003213 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003214 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003215 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003216 else if (flags & EVAL_CONSTANT)
3217 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003218 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003219 {
3220 // get the value of "true", "false" or a variable
3221 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3222 {
3223 rettv->v_type = VAR_BOOL;
3224 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003225 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003226 }
3227 else if (len == 5 && in_vim9script()
3228 && STRNCMP(s, "false", 4) == 0)
3229 {
3230 rettv->v_type = VAR_BOOL;
3231 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003232 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003233 }
3234 else
3235 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3236 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003237 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003238 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003239 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003240 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003241 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003242 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003243 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003244 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003245 }
3246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003247 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3248 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003249 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003250 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
3252 /*
3253 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3254 */
3255 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003256 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003257 return ret;
3258}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003259
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003260/*
3261 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003262 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003263 * Adjusts "end_leaderp" until it is at "start_leader".
3264 */
3265 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003266eval7_leader(
3267 typval_T *rettv,
3268 int numeric_only,
3269 char_u *start_leader,
3270 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003271{
3272 char_u *end_leader = *end_leaderp;
3273 int ret = OK;
3274 int error = FALSE;
3275 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003276 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003277#ifdef FEAT_FLOAT
3278 float_T f = 0.0;
3279
3280 if (rettv->v_type == VAR_FLOAT)
3281 f = rettv->vval.v_float;
3282 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003283#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003284 val = tv_get_number_chk(rettv, &error);
3285 if (error)
3286 {
3287 clear_tv(rettv);
3288 ret = FAIL;
3289 }
3290 else
3291 {
3292 while (end_leader > start_leader)
3293 {
3294 --end_leader;
3295 if (*end_leader == '!')
3296 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003297 if (numeric_only)
3298 {
3299 ++end_leader;
3300 break;
3301 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003302#ifdef FEAT_FLOAT
3303 if (rettv->v_type == VAR_FLOAT)
3304 f = !f;
3305 else
3306#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003307 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003308 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003309 type = VAR_BOOL;
3310 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003311 }
3312 else if (*end_leader == '-')
3313 {
3314#ifdef FEAT_FLOAT
3315 if (rettv->v_type == VAR_FLOAT)
3316 f = -f;
3317 else
3318#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003319 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003320 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003321 type = VAR_NUMBER;
3322 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003323 }
3324 }
3325#ifdef FEAT_FLOAT
3326 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003328 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003329 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003331 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003332#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003333 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003334 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003335 if (in_vim9script())
3336 rettv->v_type = type;
3337 else
3338 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003339 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003342 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 return ret;
3344}
3345
3346/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003347 * Call the function referred to in "rettv".
3348 */
3349 static int
3350call_func_rettv(
3351 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003352 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003353 typval_T *rettv,
3354 int evaluate,
3355 dict_T *selfdict,
3356 typval_T *basetv)
3357{
3358 partial_T *pt = NULL;
3359 funcexe_T funcexe;
3360 typval_T functv;
3361 char_u *s;
3362 int ret;
3363
3364 // need to copy the funcref so that we can clear rettv
3365 if (evaluate)
3366 {
3367 functv = *rettv;
3368 rettv->v_type = VAR_UNKNOWN;
3369
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003370 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003371 if (functv.v_type == VAR_PARTIAL)
3372 {
3373 pt = functv.vval.v_partial;
3374 s = partial_name(pt);
3375 }
3376 else
3377 s = functv.vval.v_string;
3378 }
3379 else
3380 s = (char_u *)"";
3381
Bram Moolenaara80faa82020-04-12 19:37:17 +02003382 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003383 funcexe.firstline = curwin->w_cursor.lnum;
3384 funcexe.lastline = curwin->w_cursor.lnum;
3385 funcexe.evaluate = evaluate;
3386 funcexe.partial = pt;
3387 funcexe.selfdict = selfdict;
3388 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003389 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003390
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003391 // Clear the funcref afterwards, so that deleting it while
3392 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003393 if (evaluate)
3394 clear_tv(&functv);
3395
3396 return ret;
3397}
3398
3399/*
3400 * Evaluate "->method()".
3401 * "*arg" points to the '-'.
3402 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3403 */
3404 static int
3405eval_lambda(
3406 char_u **arg,
3407 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003408 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003409 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003410{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003411 int evaluate = evalarg != NULL
3412 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003413 typval_T base = *rettv;
3414 int ret;
3415
3416 // Skip over the ->.
3417 *arg += 2;
3418 rettv->v_type = VAR_UNKNOWN;
3419
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003420 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003421 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003422 return FAIL;
3423 else if (**arg != '(')
3424 {
3425 if (verbose)
3426 {
3427 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003428 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003429 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003431 }
3432 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003433 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003434 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003435 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003436 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003437
3438 // Clear the funcref afterwards, so that deleting it while
3439 // evaluating the arguments is possible (see test55).
3440 if (evaluate)
3441 clear_tv(&base);
3442
3443 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003444}
3445
3446/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003447 * Evaluate "->method()".
3448 * "*arg" points to the '-'.
3449 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3450 */
3451 static int
3452eval_method(
3453 char_u **arg,
3454 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003455 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003456 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003457{
3458 char_u *name;
3459 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003460 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003461 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003462 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003463 int evaluate = evalarg != NULL
3464 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003465
3466 // Skip over the ->.
3467 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003468 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003469
Bram Moolenaarac92e252019-08-03 21:58:38 +02003470 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003471 len = get_name_len(arg, &alias, evaluate, TRUE);
3472 if (alias != NULL)
3473 name = alias;
3474
3475 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003476 {
3477 if (verbose)
3478 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003479 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003480 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003481 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003482 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003483 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003484 if (**arg != '(')
3485 {
3486 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003488 ret = FAIL;
3489 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003490 else if (VIM_ISWHITE((*arg)[-1]))
3491 {
3492 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003493 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003494 ret = FAIL;
3495 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003496 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003497 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003498 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003499 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003500
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003501 // Clear the funcref afterwards, so that deleting it while
3502 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003503 if (evaluate)
3504 clear_tv(&base);
3505
Bram Moolenaarac92e252019-08-03 21:58:38 +02003506 return ret;
3507}
3508
3509/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003510 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3511 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003512 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3513 */
3514 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003515eval_index(
3516 char_u **arg,
3517 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003518 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003519 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003520{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003521 int evaluate = evalarg != NULL
3522 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003523 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003524 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003525 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003526 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003527 long len = -1;
3528 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003529 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003530 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003531
Bram Moolenaara03f2332016-02-06 18:09:59 +01003532 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003533 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003534 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003535 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003536 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003537 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003538 return FAIL;
3539 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003540#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003541 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003542 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003543 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003544#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003545 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003546 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003547 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003548 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003549 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003550 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003551 return FAIL;
3552 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003553 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003555 if (evaluate)
3556 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003557 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003558
3559 case VAR_STRING:
3560 case VAR_NUMBER:
3561 case VAR_LIST:
3562 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003563 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003564 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003565 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003566
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003567 init_tv(&var1);
3568 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003569 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003570 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003571 /*
3572 * dict.name
3573 */
3574 key = *arg + 1;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003575 for (len = 0; eval_isdictc(key[len]); ++len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003576 ;
3577 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003578 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003579 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003580 }
3581 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003582 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003583 /*
3584 * something[idx]
3585 *
3586 * Get the (first) variable from inside the [].
3587 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003588 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589 if (**arg == ':')
3590 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003591 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003592 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003593 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003594 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003595 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003596 clear_tv(&var1);
3597 return FAIL;
3598 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599
3600 /*
3601 * Get the second variable from inside the [:].
3602 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003603 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 if (**arg == ':')
3605 {
3606 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003607 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003608 if (**arg == ']')
3609 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003610 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003612 if (!empty1)
3613 clear_tv(&var1);
3614 return FAIL;
3615 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003616 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003617 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003618 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003619 if (!empty1)
3620 clear_tv(&var1);
3621 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 return FAIL;
3623 }
3624 }
3625
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003626 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003627 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003628 if (**arg != ']')
3629 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003630 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003631 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 clear_tv(&var1);
3633 if (range)
3634 clear_tv(&var2);
3635 return FAIL;
3636 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003637 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003638 }
3639
3640 if (evaluate)
3641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003642 n1 = 0;
3643 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003644 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003645 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003646 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003647 }
3648 if (range)
3649 {
3650 if (empty2)
3651 n2 = -1;
3652 else
3653 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003654 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003655 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003656 }
3657 }
3658
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003659 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003660 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003661 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003662 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003664 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003665 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003666 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003667 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003668 case VAR_SPECIAL:
3669 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003670 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003671 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003672
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003673 case VAR_NUMBER:
3674 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003675 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003676 len = (long)STRLEN(s);
3677 if (range)
3678 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003679 // The resulting variable is a substring. If the indexes
3680 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003681 if (n1 < 0)
3682 {
3683 n1 = len + n1;
3684 if (n1 < 0)
3685 n1 = 0;
3686 }
3687 if (n2 < 0)
3688 n2 = len + n2;
3689 else if (n2 >= len)
3690 n2 = len;
3691 if (n1 >= len || n2 < 0 || n1 > n2)
3692 s = NULL;
3693 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003694 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003695 }
3696 else
3697 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003698 // The resulting variable is a string of a single
3699 // character. If the index is too big or negative the
3700 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003701 if (n1 >= len || n1 < 0)
3702 s = NULL;
3703 else
3704 s = vim_strnsave(s + n1, 1);
3705 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003706 clear_tv(rettv);
3707 rettv->v_type = VAR_STRING;
3708 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003709 break;
3710
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003711 case VAR_BLOB:
3712 len = blob_len(rettv->vval.v_blob);
3713 if (range)
3714 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003715 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003716 // are out of range the result is empty.
3717 if (n1 < 0)
3718 {
3719 n1 = len + n1;
3720 if (n1 < 0)
3721 n1 = 0;
3722 }
3723 if (n2 < 0)
3724 n2 = len + n2;
3725 else if (n2 >= len)
3726 n2 = len - 1;
3727 if (n1 >= len || n2 < 0 || n1 > n2)
3728 {
3729 clear_tv(rettv);
3730 rettv->v_type = VAR_BLOB;
3731 rettv->vval.v_blob = NULL;
3732 }
3733 else
3734 {
3735 blob_T *blob = blob_alloc();
3736
3737 if (blob != NULL)
3738 {
3739 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3740 {
3741 blob_free(blob);
3742 return FAIL;
3743 }
3744 blob->bv_ga.ga_len = n2 - n1 + 1;
3745 for (i = n1; i <= n2; i++)
3746 blob_set(blob, i - n1,
3747 blob_get(rettv->vval.v_blob, i));
3748
3749 clear_tv(rettv);
3750 rettv_blob_set(rettv, blob);
3751 }
3752 }
3753 }
3754 else
3755 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003756 // The resulting variable is a byte value.
3757 // If the index is too big or negative that is an error.
3758 if (n1 < 0)
3759 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003760 if (n1 < len && n1 >= 0)
3761 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003762 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003763
3764 clear_tv(rettv);
3765 rettv->v_type = VAR_NUMBER;
3766 rettv->vval.v_number = v;
3767 }
3768 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003769 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003770 }
3771 break;
3772
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003773 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003774 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003775 if (n1 < 0)
3776 n1 = len + n1;
3777 if (!empty1 && (n1 < 0 || n1 >= len))
3778 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003779 // For a range we allow invalid values and return an empty
3780 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003781 if (!range)
3782 {
3783 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003784 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003785 return FAIL;
3786 }
3787 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003788 }
3789 if (range)
3790 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003791 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003792
3793 if (n2 < 0)
3794 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003795 else if (n2 >= len)
3796 n2 = len - 1;
3797 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003798 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003799 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003800 if (l == NULL)
3801 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003802 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003803 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003804 }
3805 else
3806 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003807 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003808 clear_tv(rettv);
3809 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003810 }
3811 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003812
3813 case VAR_DICT:
3814 if (range)
3815 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003816 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003817 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003818 if (len == -1)
3819 clear_tv(&var1);
3820 return FAIL;
3821 }
3822 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003823 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003824
3825 if (len == -1)
3826 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003827 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003828 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003829 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003830 clear_tv(&var1);
3831 return FAIL;
3832 }
3833 }
3834
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003835 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003836
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003837 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003838 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003839 if (len == -1)
3840 clear_tv(&var1);
3841 if (item == NULL)
3842 return FAIL;
3843
3844 copy_tv(&item->di_tv, &var1);
3845 clear_tv(rettv);
3846 *rettv = var1;
3847 }
3848 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003849 }
3850 }
3851
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003852 return OK;
3853}
3854
3855/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003856 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003857 */
3858 char_u *
3859partial_name(partial_T *pt)
3860{
3861 if (pt->pt_name != NULL)
3862 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003863 if (pt->pt_func != NULL)
3864 return pt->pt_func->uf_name;
3865 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003866}
3867
Bram Moolenaarddecc252016-04-06 22:59:37 +02003868 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003869partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003870{
3871 int i;
3872
3873 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003874 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003875 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003876 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003877 if (pt->pt_name != NULL)
3878 {
3879 func_unref(pt->pt_name);
3880 vim_free(pt->pt_name);
3881 }
3882 else
3883 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003884
3885 if (pt->pt_funcstack != NULL)
3886 {
3887 // Decrease the reference count for the context of a closure. If down
3888 // to zero free it and clear the variables on the stack.
3889 if (--pt->pt_funcstack->fs_refcount == 0)
3890 {
3891 garray_T *gap = &pt->pt_funcstack->fs_ga;
3892 typval_T *stack = gap->ga_data;
3893
3894 for (i = 0; i < gap->ga_len; ++i)
3895 clear_tv(stack + i);
3896 ga_clear(gap);
3897 vim_free(pt->pt_funcstack);
3898 }
3899 pt->pt_funcstack = NULL;
3900 }
3901
Bram Moolenaarddecc252016-04-06 22:59:37 +02003902 vim_free(pt);
3903}
3904
3905/*
3906 * Unreference a closure: decrement the reference count and free it when it
3907 * becomes zero.
3908 */
3909 void
3910partial_unref(partial_T *pt)
3911{
3912 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003913 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003914}
3915
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003916/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003917 * Return the next (unique) copy ID.
3918 * Used for serializing nested structures.
3919 */
3920 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003921get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003922{
3923 current_copyID += COPYID_INC;
3924 return current_copyID;
3925}
3926
3927/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003928 * Garbage collection for lists and dictionaries.
3929 *
3930 * We use reference counts to be able to free most items right away when they
3931 * are no longer used. But for composite items it's possible that it becomes
3932 * unused while the reference count is > 0: When there is a recursive
3933 * reference. Example:
3934 * :let l = [1, 2, 3]
3935 * :let d = {9: l}
3936 * :let l[1] = d
3937 *
3938 * Since this is quite unusual we handle this with garbage collection: every
3939 * once in a while find out which lists and dicts are not referenced from any
3940 * variable.
3941 *
3942 * Here is a good reference text about garbage collection (refers to Python
3943 * but it applies to all reference-counting mechanisms):
3944 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003945 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003946
3947/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003948 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003949 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003950 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003951 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003952 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003953garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003954{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003955 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003956 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003957 buf_T *buf;
3958 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003959 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003960 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003961
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003962 if (!testing)
3963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003964 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003965 want_garbage_collect = FALSE;
3966 may_garbage_collect = FALSE;
3967 garbage_collect_at_exit = FALSE;
3968 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003969
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003970 // The execution stack can grow big, limit the size.
3971 if (exestack.ga_maxlen - exestack.ga_len > 500)
3972 {
3973 size_t new_len;
3974 char_u *pp;
3975 int n;
3976
3977 // Keep 150% of the current size, with a minimum of the growth size.
3978 n = exestack.ga_len / 2;
3979 if (n < exestack.ga_growsize)
3980 n = exestack.ga_growsize;
3981
3982 // Don't make it bigger though.
3983 if (exestack.ga_len + n < exestack.ga_maxlen)
3984 {
3985 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3986 pp = vim_realloc(exestack.ga_data, new_len);
3987 if (pp == NULL)
3988 return FAIL;
3989 exestack.ga_maxlen = exestack.ga_len + n;
3990 exestack.ga_data = pp;
3991 }
3992 }
3993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003994 // We advance by two because we add one for items referenced through
3995 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003996 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003997
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003998 /*
3999 * 1. Go through all accessible variables and mark all lists and dicts
4000 * with copyID.
4001 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004003 // Don't free variables in the previous_funccal list unless they are only
4004 // referenced through previous_funccal. This must be first, because if
4005 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004006 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004008 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004009 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004010
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004011 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004012 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004013 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4014 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004016 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004017 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004018 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4019 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004020 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004021 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4022 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004023#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004024 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004025 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4026 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004027 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004028 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004029 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4030 NULL, NULL);
4031#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004032
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004033 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004034 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004035 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4036 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004037 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004038 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004040 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004041 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004043 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004044 abort = abort || set_ref_in_functions(copyID);
4045
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004046 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004047 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004049 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004050 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004051
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004052 // callbacks in buffers
4053 abort = abort || set_ref_in_buffers(copyID);
4054
Bram Moolenaar1dced572012-04-05 16:54:08 +02004055#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004056 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004057#endif
4058
Bram Moolenaardb913952012-06-29 12:54:53 +02004059#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004060 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004061#endif
4062
4063#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004064 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004065#endif
4066
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004067#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004068 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004069 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004070#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004071#ifdef FEAT_NETBEANS_INTG
4072 abort = abort || set_ref_in_nb_channel(copyID);
4073#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004074
Bram Moolenaare3188e22016-05-31 21:13:04 +02004075#ifdef FEAT_TIMERS
4076 abort = abort || set_ref_in_timer(copyID);
4077#endif
4078
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004079#ifdef FEAT_QUICKFIX
4080 abort = abort || set_ref_in_quickfix(copyID);
4081#endif
4082
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004083#ifdef FEAT_TERMINAL
4084 abort = abort || set_ref_in_term(copyID);
4085#endif
4086
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004087#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004088 abort = abort || set_ref_in_popups(copyID);
4089#endif
4090
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004091 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004092 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004093 /*
4094 * 2. Free lists and dictionaries that are not referenced.
4095 */
4096 did_free = free_unref_items(copyID);
4097
4098 /*
4099 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004100 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004101 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004102 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004103 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004104 else if (p_verbose > 0)
4105 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004106 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004107 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004108
4109 return did_free;
4110}
4111
4112/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004113 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004114 */
4115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004116free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004117{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004118 int did_free = FALSE;
4119
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004120 // Let all "free" functions know that we are here. This means no
4121 // dictionaries, lists, channels or jobs are to be freed, because we will
4122 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004123 in_free_unref_items = TRUE;
4124
4125 /*
4126 * PASS 1: free the contents of the items. We don't free the items
4127 * themselves yet, so that it is possible to decrement refcount counters
4128 */
4129
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004130 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004131 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004132
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004133 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004134 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004135
4136#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004137 // Go through the list of jobs and free items without the copyID. This
4138 // must happen before doing channels, because jobs refer to channels, but
4139 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004140 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4141
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004142 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004143 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4144#endif
4145
4146 /*
4147 * PASS 2: free the items themselves.
4148 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004149 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004150 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004151
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004152#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004153 // Go through the list of jobs and free items without the copyID. This
4154 // must happen before doing channels, because jobs refer to channels, but
4155 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004156 free_unused_jobs(copyID, COPYID_MASK);
4157
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004158 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004159 free_unused_channels(copyID, COPYID_MASK);
4160#endif
4161
4162 in_free_unref_items = FALSE;
4163
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004164 return did_free;
4165}
4166
4167/*
4168 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004169 * "list_stack" is used to add lists to be marked. Can be NULL.
4170 *
4171 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004172 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004174set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004175{
4176 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004177 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004178 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004179 hashtab_T *cur_ht;
4180 ht_stack_T *ht_stack = NULL;
4181 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004182
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004183 cur_ht = ht;
4184 for (;;)
4185 {
4186 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004187 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004188 // Mark each item in the hashtab. If the item contains a hashtab
4189 // it is added to ht_stack, if it contains a list it is added to
4190 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004191 todo = (int)cur_ht->ht_used;
4192 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4193 if (!HASHITEM_EMPTY(hi))
4194 {
4195 --todo;
4196 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4197 &ht_stack, list_stack);
4198 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004199 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004200
4201 if (ht_stack == NULL)
4202 break;
4203
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004204 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004205 cur_ht = ht_stack->ht;
4206 tempitem = ht_stack;
4207 ht_stack = ht_stack->prev;
4208 free(tempitem);
4209 }
4210
4211 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004212}
4213
4214/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004215 * Mark a dict and its items with "copyID".
4216 * Returns TRUE if setting references failed somehow.
4217 */
4218 int
4219set_ref_in_dict(dict_T *d, int copyID)
4220{
4221 if (d != NULL && d->dv_copyID != copyID)
4222 {
4223 d->dv_copyID = copyID;
4224 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4225 }
4226 return FALSE;
4227}
4228
4229/*
4230 * Mark a list and its items with "copyID".
4231 * Returns TRUE if setting references failed somehow.
4232 */
4233 int
4234set_ref_in_list(list_T *ll, int copyID)
4235{
4236 if (ll != NULL && ll->lv_copyID != copyID)
4237 {
4238 ll->lv_copyID = copyID;
4239 return set_ref_in_list_items(ll, copyID, NULL);
4240 }
4241 return FALSE;
4242}
4243
4244/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004245 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004246 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4247 *
4248 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004249 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004250 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004251set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004252{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004253 listitem_T *li;
4254 int abort = FALSE;
4255 list_T *cur_l;
4256 list_stack_T *list_stack = NULL;
4257 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004258
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004259 cur_l = l;
4260 for (;;)
4261 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004262 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004263 // Mark each item in the list. If the item contains a hashtab
4264 // it is added to ht_stack, if it contains a list it is added to
4265 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004266 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4267 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4268 ht_stack, &list_stack);
4269 if (list_stack == NULL)
4270 break;
4271
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004272 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004273 cur_l = list_stack->list;
4274 tempitem = list_stack;
4275 list_stack = list_stack->prev;
4276 free(tempitem);
4277 }
4278
4279 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004280}
4281
4282/*
4283 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004284 * "list_stack" is used to add lists to be marked. Can be NULL.
4285 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4286 *
4287 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004288 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004289 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004290set_ref_in_item(
4291 typval_T *tv,
4292 int copyID,
4293 ht_stack_T **ht_stack,
4294 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004295{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004296 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004297
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004298 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004299 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004300 dict_T *dd = tv->vval.v_dict;
4301
Bram Moolenaara03f2332016-02-06 18:09:59 +01004302 if (dd != NULL && dd->dv_copyID != copyID)
4303 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004304 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004305 dd->dv_copyID = copyID;
4306 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004307 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004308 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4309 }
4310 else
4311 {
4312 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4313 if (newitem == NULL)
4314 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004315 else
4316 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004317 newitem->ht = &dd->dv_hashtab;
4318 newitem->prev = *ht_stack;
4319 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004320 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004321 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004322 }
4323 }
4324 else if (tv->v_type == VAR_LIST)
4325 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004326 list_T *ll = tv->vval.v_list;
4327
Bram Moolenaara03f2332016-02-06 18:09:59 +01004328 if (ll != NULL && ll->lv_copyID != copyID)
4329 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004330 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004331 ll->lv_copyID = copyID;
4332 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004333 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004334 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004335 }
4336 else
4337 {
4338 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004339 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004340 if (newitem == NULL)
4341 abort = TRUE;
4342 else
4343 {
4344 newitem->list = ll;
4345 newitem->prev = *list_stack;
4346 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004347 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004348 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004349 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004350 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004351 else if (tv->v_type == VAR_FUNC)
4352 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004353 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004354 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004355 else if (tv->v_type == VAR_PARTIAL)
4356 {
4357 partial_T *pt = tv->vval.v_partial;
4358 int i;
4359
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004360 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004361 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004362 // Didn't see this partial yet.
4363 pt->pt_copyID = copyID;
4364
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004365 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004366
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004367 if (pt->pt_dict != NULL)
4368 {
4369 typval_T dtv;
4370
4371 dtv.v_type = VAR_DICT;
4372 dtv.vval.v_dict = pt->pt_dict;
4373 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4374 }
4375
4376 for (i = 0; i < pt->pt_argc; ++i)
4377 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4378 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004379 if (pt->pt_funcstack != NULL)
4380 {
4381 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4382
4383 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4384 abort = abort || set_ref_in_item(stack + i, copyID,
4385 ht_stack, list_stack);
4386 }
4387
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004388 }
4389 }
4390#ifdef FEAT_JOB_CHANNEL
4391 else if (tv->v_type == VAR_JOB)
4392 {
4393 job_T *job = tv->vval.v_job;
4394 typval_T dtv;
4395
4396 if (job != NULL && job->jv_copyID != copyID)
4397 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004398 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004399 if (job->jv_channel != NULL)
4400 {
4401 dtv.v_type = VAR_CHANNEL;
4402 dtv.vval.v_channel = job->jv_channel;
4403 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4404 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004405 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004406 {
4407 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004408 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004409 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4410 }
4411 }
4412 }
4413 else if (tv->v_type == VAR_CHANNEL)
4414 {
4415 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004416 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004417 typval_T dtv;
4418 jsonq_T *jq;
4419 cbq_T *cq;
4420
4421 if (ch != NULL && ch->ch_copyID != copyID)
4422 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004423 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004424 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004425 {
4426 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4427 jq = jq->jq_next)
4428 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4429 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4430 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004431 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004432 {
4433 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004434 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004435 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4436 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004437 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004438 {
4439 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004440 dtv.vval.v_partial =
4441 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004442 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4443 }
4444 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004445 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004446 {
4447 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004448 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004449 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4450 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004451 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004452 {
4453 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004454 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004455 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4456 }
4457 }
4458 }
4459#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004460 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004461}
4462
Bram Moolenaar8c711452005-01-14 21:53:12 +00004463/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004464 * Return a string with the string representation of a variable.
4465 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004466 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004467 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004468 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4469 * stings as "string()", otherwise does not put quotes around strings, as
4470 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004471 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4472 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004473 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004474 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004475 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004476echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004477 typval_T *tv,
4478 char_u **tofree,
4479 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004480 int copyID,
4481 int echo_style,
4482 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004483 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004484{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004485 static int recurse = 0;
4486 char_u *r = NULL;
4487
Bram Moolenaar33570922005-01-25 22:26:29 +00004488 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004489 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004490 if (!did_echo_string_emsg)
4491 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004492 // Only give this message once for a recursive call to avoid
4493 // flooding the user with errors. And stop iterating over lists
4494 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004495 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004496 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004497 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004498 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004499 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004500 }
4501 ++recurse;
4502
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004503 switch (tv->v_type)
4504 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004505 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004506 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004507 {
4508 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004509 r = tv->vval.v_string;
4510 if (r == NULL)
4511 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004512 }
4513 else
4514 {
4515 *tofree = string_quote(tv->vval.v_string, FALSE);
4516 r = *tofree;
4517 }
4518 break;
4519
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004520 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004521 if (echo_style)
4522 {
4523 *tofree = NULL;
4524 r = tv->vval.v_string;
4525 }
4526 else
4527 {
4528 *tofree = string_quote(tv->vval.v_string, TRUE);
4529 r = *tofree;
4530 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004531 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004532
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004533 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004534 {
4535 partial_T *pt = tv->vval.v_partial;
4536 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004537 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004538 garray_T ga;
4539 int i;
4540 char_u *tf;
4541
4542 ga_init2(&ga, 1, 100);
4543 ga_concat(&ga, (char_u *)"function(");
4544 if (fname != NULL)
4545 {
4546 ga_concat(&ga, fname);
4547 vim_free(fname);
4548 }
4549 if (pt != NULL && pt->pt_argc > 0)
4550 {
4551 ga_concat(&ga, (char_u *)", [");
4552 for (i = 0; i < pt->pt_argc; ++i)
4553 {
4554 if (i > 0)
4555 ga_concat(&ga, (char_u *)", ");
4556 ga_concat(&ga,
4557 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4558 vim_free(tf);
4559 }
4560 ga_concat(&ga, (char_u *)"]");
4561 }
4562 if (pt != NULL && pt->pt_dict != NULL)
4563 {
4564 typval_T dtv;
4565
4566 ga_concat(&ga, (char_u *)", ");
4567 dtv.v_type = VAR_DICT;
4568 dtv.vval.v_dict = pt->pt_dict;
4569 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4570 vim_free(tf);
4571 }
4572 ga_concat(&ga, (char_u *)")");
4573
4574 *tofree = ga.ga_data;
4575 r = *tofree;
4576 break;
4577 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004578
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004579 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004580 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004581 break;
4582
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004583 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004584 if (tv->vval.v_list == NULL)
4585 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004586 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004587 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004588 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004589 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004590 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4591 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004592 {
4593 *tofree = NULL;
4594 r = (char_u *)"[...]";
4595 }
4596 else
4597 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004598 int old_copyID = tv->vval.v_list->lv_copyID;
4599
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004600 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004601 *tofree = list2string(tv, copyID, restore_copyID);
4602 if (restore_copyID)
4603 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004604 r = *tofree;
4605 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004606 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004607
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004609 if (tv->vval.v_dict == NULL)
4610 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004611 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004612 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004613 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004614 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004615 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4616 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004617 {
4618 *tofree = NULL;
4619 r = (char_u *)"{...}";
4620 }
4621 else
4622 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004623 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004624
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004625 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004626 *tofree = dict2string(tv, copyID, restore_copyID);
4627 if (restore_copyID)
4628 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004629 r = *tofree;
4630 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004631 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004632
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004633 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004634 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004635 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004637 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004638 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004639 break;
4640
Bram Moolenaar835dc632016-02-07 14:27:38 +01004641 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004642 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004643 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004644 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004645 if (composite_val)
4646 {
4647 *tofree = string_quote(r, FALSE);
4648 r = *tofree;
4649 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004650 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004651
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004652 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004653#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004654 *tofree = NULL;
4655 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4656 r = numbuf;
4657 break;
4658#endif
4659
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004660 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004661 case VAR_SPECIAL:
4662 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004663 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004664 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004665 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004666
Bram Moolenaar8502c702014-06-17 12:51:16 +02004667 if (--recurse == 0)
4668 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004669 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004670}
4671
4672/*
4673 * Return a string with the string representation of a variable.
4674 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4675 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004676 * Does not put quotes around strings, as ":echo" displays values.
4677 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4678 * May return NULL.
4679 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004680 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004681echo_string(
4682 typval_T *tv,
4683 char_u **tofree,
4684 char_u *numbuf,
4685 int copyID)
4686{
4687 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4688}
4689
4690/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004691 * Return string "str" in ' quotes, doubling ' characters.
4692 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004693 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004694 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004695 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004696string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004697{
Bram Moolenaar33570922005-01-25 22:26:29 +00004698 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004699 char_u *p, *r, *s;
4700
Bram Moolenaar33570922005-01-25 22:26:29 +00004701 len = (function ? 13 : 3);
4702 if (str != NULL)
4703 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004704 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004705 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004706 if (*p == '\'')
4707 ++len;
4708 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004709 s = r = alloc(len);
4710 if (r != NULL)
4711 {
4712 if (function)
4713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004714 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004715 r += 10;
4716 }
4717 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004718 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004719 if (str != NULL)
4720 for (p = str; *p != NUL; )
4721 {
4722 if (*p == '\'')
4723 *r++ = '\'';
4724 MB_COPY_CHAR(p, r);
4725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004726 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004727 if (function)
4728 *r++ = ')';
4729 *r++ = NUL;
4730 }
4731 return s;
4732}
4733
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004734#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004735/*
4736 * Convert the string "text" to a floating point number.
4737 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4738 * this always uses a decimal point.
4739 * Returns the length of the text that was consumed.
4740 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004742string2float(
4743 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004744 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004745{
4746 char *s = (char *)text;
4747 float_T f;
4748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004749 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004750 if (STRNICMP(text, "inf", 3) == 0)
4751 {
4752 *value = INFINITY;
4753 return 3;
4754 }
4755 if (STRNICMP(text, "-inf", 3) == 0)
4756 {
4757 *value = -INFINITY;
4758 return 4;
4759 }
4760 if (STRNICMP(text, "nan", 3) == 0)
4761 {
4762 *value = NAN;
4763 return 3;
4764 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004765 f = strtod(s, &s);
4766 *value = f;
4767 return (int)((char_u *)s - text);
4768}
4769#endif
4770
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004773 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004775 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004776var2fpos(
4777 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004778 int dollar_lnum, // TRUE when $ is last line
4779 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004781 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004783 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004785 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004786 if (varp->v_type == VAR_LIST)
4787 {
4788 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004789 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004790 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004791 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004792
4793 l = varp->vval.v_list;
4794 if (l == NULL)
4795 return NULL;
4796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004797 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004798 pos.lnum = list_find_nr(l, 0L, &error);
4799 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004800 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004801
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004802 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004803 pos.col = list_find_nr(l, 1L, &error);
4804 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004805 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004806 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004807
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004808 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004809 li = list_find(l, 1L);
4810 if (li != NULL && li->li_tv.v_type == VAR_STRING
4811 && li->li_tv.vval.v_string != NULL
4812 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4813 pos.col = len + 1;
4814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004815 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004816 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004817 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004818 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004819
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004820 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004821 pos.coladd = list_find_nr(l, 2L, &error);
4822 if (error)
4823 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004824
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004825 return &pos;
4826 }
4827
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004828 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 if (name == NULL)
4830 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004831 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004833 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004834 {
4835 if (VIsual_active)
4836 return &VIsual;
4837 return &curwin->w_cursor;
4838 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004839 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004841 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4843 return NULL;
4844 return pp;
4845 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004846
Bram Moolenaara5525202006-03-02 22:52:09 +00004847 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004848
Bram Moolenaar477933c2007-07-17 14:32:23 +00004849 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004850 {
4851 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004852 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004853 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004854 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004855 // In silent Ex mode topline is zero, but that's not a valid line
4856 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004857 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004858 return &pos;
4859 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004860 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004861 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004862 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004863 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004864 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004865 return &pos;
4866 }
4867 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004868 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004870 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 {
4872 pos.lnum = curbuf->b_ml.ml_line_count;
4873 pos.col = 0;
4874 }
4875 else
4876 {
4877 pos.lnum = curwin->w_cursor.lnum;
4878 pos.col = (colnr_T)STRLEN(ml_get_curline());
4879 }
4880 return &pos;
4881 }
4882 return NULL;
4883}
4884
4885/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004886 * Convert list in "arg" into a position and optional file number.
4887 * When "fnump" is NULL there is no file number, only 3 items.
4888 * Note that the column is passed on as-is, the caller may want to decrement
4889 * it to use 1 for the first column.
4890 * Return FAIL when conversion is not possible, doesn't check the position for
4891 * validity.
4892 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004893 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004894list2fpos(
4895 typval_T *arg,
4896 pos_T *posp,
4897 int *fnump,
4898 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004899{
4900 list_T *l = arg->vval.v_list;
4901 long i = 0;
4902 long n;
4903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004904 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4905 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004906 if (arg->v_type != VAR_LIST
4907 || l == NULL
4908 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004909 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004910 return FAIL;
4911
4912 if (fnump != NULL)
4913 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004914 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004915 if (n < 0)
4916 return FAIL;
4917 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004918 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004919 *fnump = n;
4920 }
4921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004922 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004923 if (n < 0)
4924 return FAIL;
4925 posp->lnum = n;
4926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004927 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004928 if (n < 0)
4929 return FAIL;
4930 posp->col = n;
4931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004932 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004933 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004934 posp->coladd = 0;
4935 else
4936 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004937
Bram Moolenaar493c1782014-05-28 14:34:46 +02004938 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004939 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004940
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004941 return OK;
4942}
4943
4944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 * Get the length of an environment variable name.
4946 * Advance "arg" to the first character after the name.
4947 * Return 0 for error.
4948 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004949 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004950get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
4952 char_u *p;
4953 int len;
4954
4955 for (p = *arg; vim_isIDc(*p); ++p)
4956 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004957 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 return 0;
4959
4960 len = (int)(p - *arg);
4961 *arg = p;
4962 return len;
4963}
4964
4965/*
4966 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004967 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 * Return 0 if something is wrong.
4969 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004970 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004971get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972{
4973 char_u *p;
4974 int len;
4975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004976 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004978 {
4979 if (*p == ':')
4980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004981 // "s:" is start of "s:var", but "n:" is not and can be used in
4982 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004983 len = (int)(p - *arg);
4984 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4985 || len > 1)
4986 break;
4987 }
4988 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004989 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 return 0;
4991
4992 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004993 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
4995 return len;
4996}
4997
4998/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004999 * Get the length of the name of a variable or function.
5000 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005002 * Return -1 if curly braces expansion failed.
5003 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 * If the name contains 'magic' {}'s, expand them and return the
5005 * expanded name in an allocated string via 'alias' - caller must free.
5006 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005007 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005008get_name_len(
5009 char_u **arg,
5010 char_u **alias,
5011 int evaluate,
5012 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013{
5014 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 char_u *p;
5016 char_u *expr_start;
5017 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005019 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020
5021 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5022 && (*arg)[2] == (int)KE_SNR)
5023 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005024 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 *arg += 3;
5026 return get_id_len(arg) + 3;
5027 }
5028 len = eval_fname_script(*arg);
5029 if (len > 0)
5030 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005031 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 *arg += len;
5033 }
5034
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005036 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005038 p = find_name_end(*arg, &expr_start, &expr_end,
5039 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 if (expr_start != NULL)
5041 {
5042 char_u *temp_string;
5043
5044 if (!evaluate)
5045 {
5046 len += (int)(p - *arg);
5047 *arg = skipwhite(p);
5048 return len;
5049 }
5050
5051 /*
5052 * Include any <SID> etc in the expanded string:
5053 * Thus the -len here.
5054 */
5055 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5056 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005057 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 *alias = temp_string;
5059 *arg = skipwhite(p);
5060 return (int)STRLEN(temp_string);
5061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
5063 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005064 // Only give an error when there is something, otherwise it will be
5065 // reported at a higher level.
5066 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005067 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
5069 return len;
5070}
5071
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072/*
5073 * Find the end of a variable or function name, taking care of magic braces.
5074 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5075 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005076 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 * Return a pointer to just after the name. Equal to "arg" if there is no
5078 * valid name.
5079 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005080 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005081find_name_end(
5082 char_u *arg,
5083 char_u **expr_start,
5084 char_u **expr_end,
5085 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 int mb_nest = 0;
5088 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005090 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005091 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005095 *expr_start = NULL;
5096 *expr_end = NULL;
5097 }
5098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005099 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005100 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5101 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005102 return arg;
5103
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005104 for (p = arg; *p != NUL
5105 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005106 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005107 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005108 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005109 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005110 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005111 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005112 if (*p == '\'')
5113 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005114 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005115 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005116 ;
5117 if (*p == NUL)
5118 break;
5119 }
5120 else if (*p == '"')
5121 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005122 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005123 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005124 if (*p == '\\' && p[1] != NUL)
5125 ++p;
5126 if (*p == NUL)
5127 break;
5128 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005129 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5130 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005131 // "s:" is start of "s:var", but "n:" is not and can be used in
5132 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005133 len = (int)(p - arg);
5134 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005135 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005136 break;
5137 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005138
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141 if (*p == '[')
5142 ++br_nest;
5143 else if (*p == ']')
5144 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005146
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005147 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149 if (*p == '{')
5150 {
5151 mb_nest++;
5152 if (expr_start != NULL && *expr_start == NULL)
5153 *expr_start = p;
5154 }
5155 else if (*p == '}')
5156 {
5157 mb_nest--;
5158 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5159 *expr_end = p;
5160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163
5164 return p;
5165}
5166
5167/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005168 * Expands out the 'magic' {}'s in a variable/function name.
5169 * Note that this can call itself recursively, to deal with
5170 * constructs like foo{bar}{baz}{bam}
5171 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5172 * "in_start" ^
5173 * "expr_start" ^
5174 * "expr_end" ^
5175 * "in_end" ^
5176 *
5177 * Returns a new allocated string, which the caller must free.
5178 * Returns NULL for failure.
5179 */
5180 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005181make_expanded_name(
5182 char_u *in_start,
5183 char_u *expr_start,
5184 char_u *expr_end,
5185 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005186{
5187 char_u c1;
5188 char_u *retval = NULL;
5189 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005190
5191 if (expr_end == NULL || in_end == NULL)
5192 return NULL;
5193 *expr_start = NUL;
5194 *expr_end = NUL;
5195 c1 = *in_end;
5196 *in_end = NUL;
5197
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005198 temp_result = eval_to_string(expr_start + 1, FALSE);
5199 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005200 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005201 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5202 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005203 if (retval != NULL)
5204 {
5205 STRCPY(retval, in_start);
5206 STRCAT(retval, temp_result);
5207 STRCAT(retval, expr_end + 1);
5208 }
5209 }
5210 vim_free(temp_result);
5211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005212 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005213 *expr_start = '{';
5214 *expr_end = '}';
5215
5216 if (retval != NULL)
5217 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005218 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005219 if (expr_start != NULL)
5220 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005221 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005222 temp_result = make_expanded_name(retval, expr_start,
5223 expr_end, temp_result);
5224 vim_free(retval);
5225 retval = temp_result;
5226 }
5227 }
5228
5229 return retval;
5230}
5231
5232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005234 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005237eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005239 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005240}
5241
5242/*
5243 * Return TRUE if character "c" can be used as the first character in a
5244 * variable or function name (excluding '{' and '}').
5245 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005246 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005247eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005248{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005249 return ASCII_ISALPHA(c) || c == '_';
5250}
5251
5252/*
5253 * Return TRUE if character "c" can be used as the first character of a
5254 * dictionary key.
5255 */
5256 int
5257eval_isdictc(int c)
5258{
5259 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260}
5261
5262/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005263 * Handle:
5264 * - expr[expr], expr[expr:expr] subscript
5265 * - ".name" lookup
5266 * - function call with Funcref variable: func(expr)
5267 * - method call: var->method()
5268 *
5269 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005270 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005271 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005272handle_subscript(
5273 char_u **arg,
5274 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005275 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005276 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005278 int evaluate = evalarg != NULL
5279 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005280 int ret = OK;
5281 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005282 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005283 int getnext;
5284 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005285
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005286 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005287 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005288 // When at the end of the line and ".name" or "->{" or "->X" follows in
5289 // the next line then consume the line break.
5290 p = eval_next_non_blank(*arg, evalarg, &getnext);
5291 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005292 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005293 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005294 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005295 {
5296 *arg = eval_next_line(evalarg);
5297 check_white = FALSE;
5298 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005299
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005300 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5301 || rettv->v_type == VAR_PARTIAL))
5302 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005303 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005304 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5305 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005306
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005307 // Stop the expression evaluation when immediately aborting on
5308 // error, or when an interrupt occurred or an exception was thrown
5309 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005310 if (aborting())
5311 {
5312 if (ret == OK)
5313 clear_tv(rettv);
5314 ret = FAIL;
5315 }
5316 dict_unref(selfdict);
5317 selfdict = NULL;
5318 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005319 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005320 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005321 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005322 if (ret == OK)
5323 {
5324 if ((*arg)[2] == '{')
5325 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005326 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005327 else
5328 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005329 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005330 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005331 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005332 // "." is ".name" lookup when we found a dict or when evaluating and
5333 // scriptversion is at least 2, where string concatenation is "..".
5334 else if (**arg == '['
5335 || (**arg == '.' && (rettv->v_type == VAR_DICT
5336 || (!evaluate
5337 && (*arg)[1] != '.'
5338 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005339 {
5340 dict_unref(selfdict);
5341 if (rettv->v_type == VAR_DICT)
5342 {
5343 selfdict = rettv->vval.v_dict;
5344 if (selfdict != NULL)
5345 ++selfdict->dv_refcount;
5346 }
5347 else
5348 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005349 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005350 {
5351 clear_tv(rettv);
5352 ret = FAIL;
5353 }
5354 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005355 else
5356 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005357 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005358
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005359 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5360 // Don't do this when "Func" is already a partial that was bound
5361 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005362 if (selfdict != NULL
5363 && (rettv->v_type == VAR_FUNC
5364 || (rettv->v_type == VAR_PARTIAL
5365 && (rettv->vval.v_partial->pt_auto
5366 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005367 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005368
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005369 dict_unref(selfdict);
5370 return ret;
5371}
5372
5373/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005374 * Make a copy of an item.
5375 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005376 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5377 * reference to an already copied list/dict can be used.
5378 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005379 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005380 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005381item_copy(
5382 typval_T *from,
5383 typval_T *to,
5384 int deep,
5385 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005386{
5387 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005388 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005389
Bram Moolenaar33570922005-01-25 22:26:29 +00005390 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005391 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005392 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005393 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005394 }
5395 ++recurse;
5396
5397 switch (from->v_type)
5398 {
5399 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005400 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005401 case VAR_STRING:
5402 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005403 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005404 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005405 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005406 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005407 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005408 copy_tv(from, to);
5409 break;
5410 case VAR_LIST:
5411 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005412 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005413 if (from->vval.v_list == NULL)
5414 to->vval.v_list = NULL;
5415 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5416 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005417 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005418 to->vval.v_list = from->vval.v_list->lv_copylist;
5419 ++to->vval.v_list->lv_refcount;
5420 }
5421 else
5422 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5423 if (to->vval.v_list == NULL)
5424 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005425 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005426 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005428 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005429 case VAR_DICT:
5430 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005431 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005432 if (from->vval.v_dict == NULL)
5433 to->vval.v_dict = NULL;
5434 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5435 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005436 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005437 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5438 ++to->vval.v_dict->dv_refcount;
5439 }
5440 else
5441 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5442 if (to->vval.v_dict == NULL)
5443 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005444 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005445 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005446 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005448 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005449 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005450 }
5451 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005452 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005453}
5454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005455 void
5456echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5457{
5458 char_u *tofree;
5459 char_u numbuf[NUMBUFLEN];
5460 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5461
5462 if (*atstart)
5463 {
5464 *atstart = FALSE;
5465 // Call msg_start() after eval1(), evaluating the expression
5466 // may cause a message to appear.
5467 if (with_space)
5468 {
5469 // Mark the saved text as finishing the line, so that what
5470 // follows is displayed on a new line when scrolling back
5471 // at the more prompt.
5472 msg_sb_eol();
5473 msg_start();
5474 }
5475 }
5476 else if (with_space)
5477 msg_puts_attr(" ", echo_attr);
5478
5479 if (p != NULL)
5480 for ( ; *p != NUL && !got_int; ++p)
5481 {
5482 if (*p == '\n' || *p == '\r' || *p == TAB)
5483 {
5484 if (*p != TAB && *needclr)
5485 {
5486 // remove any text still there from the command
5487 msg_clr_eos();
5488 *needclr = FALSE;
5489 }
5490 msg_putchar_attr(*p, echo_attr);
5491 }
5492 else
5493 {
5494 if (has_mbyte)
5495 {
5496 int i = (*mb_ptr2len)(p);
5497
5498 (void)msg_outtrans_len_attr(p, i, echo_attr);
5499 p += i - 1;
5500 }
5501 else
5502 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5503 }
5504 }
5505 vim_free(tofree);
5506}
5507
Bram Moolenaare9a41262005-01-15 22:18:47 +00005508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 * ":echo expr1 ..." print each argument separated with a space, add a
5510 * newline at the end.
5511 * ":echon expr1 ..." print each argument plain.
5512 */
5513 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005514ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
5516 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005517 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 char_u *p;
5519 int needclr = TRUE;
5520 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005521 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005522 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005523 evalarg_T evalarg;
5524
Bram Moolenaare707c882020-07-01 13:07:37 +02005525 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
5527 if (eap->skip)
5528 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005529 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005531 // If eval1() causes an error message the text from the command may
5532 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005533 need_clr_eos = needclr;
5534
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005536 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
5538 /*
5539 * Report the invalid expression unless the expression evaluation
5540 * has been cancelled due to an aborting error, an interrupt, or an
5541 * exception.
5542 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005543 if (!aborting() && did_emsg == did_emsg_before
5544 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005545 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005546 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 break;
5548 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005549 need_clr_eos = FALSE;
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005552 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005553
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 arg = skipwhite(arg);
5556 }
5557 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005558 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
5560 if (eap->skip)
5561 --emsg_skip;
5562 else
5563 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005564 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 if (needclr)
5566 msg_clr_eos();
5567 if (eap->cmdidx == CMD_echo)
5568 msg_end();
5569 }
5570}
5571
5572/*
5573 * ":echohl {name}".
5574 */
5575 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005576ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005578 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579}
5580
5581/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005582 * Returns the :echo attribute
5583 */
5584 int
5585get_echo_attr(void)
5586{
5587 return echo_attr;
5588}
5589
5590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 * ":execute expr1 ..." execute the result of an expression.
5592 * ":echomsg expr1 ..." Print a message
5593 * ":echoerr expr1 ..." Print an error
5594 * Each gets spaces around each argument and a newline at the end for
5595 * echo commands
5596 */
5597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005598ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599{
5600 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005601 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 int ret = OK;
5603 char_u *p;
5604 garray_T ga;
5605 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
5607 ga_init2(&ga, 1, 80);
5608
5609 if (eap->skip)
5610 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005611 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005613 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005614 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616
5617 if (!eap->skip)
5618 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005619 char_u buf[NUMBUFLEN];
5620
5621 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005622 {
5623 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5624 {
5625 emsg(_(e_inval_string));
5626 p = NULL;
5627 }
5628 else
5629 p = tv_get_string_buf(&rettv, buf);
5630 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005631 else
5632 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005633 if (p == NULL)
5634 {
5635 clear_tv(&rettv);
5636 ret = FAIL;
5637 break;
5638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 len = (int)STRLEN(p);
5640 if (ga_grow(&ga, len + 2) == FAIL)
5641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005642 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 ret = FAIL;
5644 break;
5645 }
5646 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 ga.ga_len += len;
5650 }
5651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 arg = skipwhite(arg);
5654 }
5655
5656 if (ret != FAIL && ga.ga_data != NULL)
5657 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005658 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5659 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005660 // Mark the already saved text as finishing the line, so that what
5661 // follows is displayed on a new line when scrolling back at the
5662 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005663 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005664 }
5665
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005667 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005668 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005669 out_flush();
5670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 else if (eap->cmdidx == CMD_echoerr)
5672 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005673 int save_did_emsg = did_emsg;
5674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005675 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005676 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 if (!force_abort)
5678 did_emsg = save_did_emsg;
5679 }
5680 else if (eap->cmdidx == CMD_execute)
5681 do_cmdline((char_u *)ga.ga_data,
5682 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5683 }
5684
5685 ga_clear(&ga);
5686
5687 if (eap->skip)
5688 --emsg_skip;
5689
5690 eap->nextcmd = check_nextcmd(arg);
5691}
5692
5693/*
5694 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5695 * "arg" points to the "&" or '+' when called, to "option" when returning.
5696 * Returns NULL when no option name found. Otherwise pointer to the char
5697 * after the option name.
5698 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005699 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005700find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
5702 char_u *p = *arg;
5703
5704 ++p;
5705 if (*p == 'g' && p[1] == ':')
5706 {
5707 *opt_flags = OPT_GLOBAL;
5708 p += 2;
5709 }
5710 else if (*p == 'l' && p[1] == ':')
5711 {
5712 *opt_flags = OPT_LOCAL;
5713 p += 2;
5714 }
5715 else
5716 *opt_flags = 0;
5717
5718 if (!ASCII_ISALPHA(*p))
5719 return NULL;
5720 *arg = p;
5721
5722 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005723 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 else
5725 while (ASCII_ISALPHA(*p))
5726 ++p;
5727 return p;
5728}
5729
5730/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005731 * Display script name where an item was last set.
5732 * Should only be invoked when 'verbose' is non-zero.
5733 */
5734 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005735last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005736{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005737 char_u *p;
5738
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005739 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005740 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005741 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005742 if (p != NULL)
5743 {
5744 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005745 msg_puts(_("\n\tLast set from "));
5746 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005747 if (script_ctx.sc_lnum > 0)
5748 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005749 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005750 msg_outnum((long)script_ctx.sc_lnum);
5751 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005752 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005753 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005754 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005755 }
5756}
5757
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005758#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759
5760/*
5761 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005762 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 * "flags" can be "g" to do a global substitute.
5764 * Returns an allocated string, NULL for error.
5765 */
5766 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005767do_string_sub(
5768 char_u *str,
5769 char_u *pat,
5770 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005771 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005772 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773{
5774 int sublen;
5775 regmatch_T regmatch;
5776 int i;
5777 int do_all;
5778 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005779 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 garray_T ga;
5781 char_u *ret;
5782 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005783 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005785 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005787 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788
5789 ga_init2(&ga, 1, 200);
5790
5791 do_all = (flags[0] == 'g');
5792
5793 regmatch.rm_ic = p_ic;
5794 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5795 if (regmatch.regprog != NULL)
5796 {
5797 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005798 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005801 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005802 if (regmatch.startp[0] == regmatch.endp[0])
5803 {
5804 if (zero_width == regmatch.startp[0])
5805 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005806 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005807 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005808 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5809 (size_t)i);
5810 ga.ga_len += i;
5811 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005812 continue;
5813 }
5814 zero_width = regmatch.startp[0];
5815 }
5816
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 /*
5818 * Get some space for a temporary buffer to do the substitution
5819 * into. It will contain:
5820 * - The text up to where the match is.
5821 * - The substituted text.
5822 * - The text after the match.
5823 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005824 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005825 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5827 {
5828 ga_clear(&ga);
5829 break;
5830 }
5831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005832 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 i = (int)(regmatch.startp[0] - tail);
5834 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005835 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005836 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 + ga.ga_len + i, TRUE, TRUE, FALSE);
5838 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005839 tail = regmatch.endp[0];
5840 if (*tail == NUL)
5841 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 if (!do_all)
5843 break;
5844 }
5845
5846 if (ga.ga_data != NULL)
5847 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5848
Bram Moolenaar473de612013-06-08 18:19:48 +02005849 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
5851
5852 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5853 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005854 if (p_cpo == empty_option)
5855 p_cpo = save_cpo;
5856 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005857 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005858 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
5860 return ret;
5861}