blob: 99554e7e263e2268aefd7992e49d1f278b47e451 [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 Moolenaar3e06a1e2020-08-10 21:57:54 +0200195 if (in_vim9script())
196 retval = tv2bool(&tv);
197 else
198 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000199 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 }
201 }
202 if (skip)
203 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200204 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200206 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207}
208
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100209/*
210 * Call eval1() and give an error message if not done at a lower level.
211 */
212 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200213eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100215 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 int ret;
217 int did_emsg_before = did_emsg;
218 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200219 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100220
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200221 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
222
223 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100224 if (ret == FAIL)
225 {
226 // Report the invalid expression unless the expression evaluation has
227 // been cancelled due to an aborting error, an interrupt, or an
228 // exception, or we already gave a more specific error.
229 // Also check called_emsg for when using assert_fails().
230 if (!aborting() && did_emsg == did_emsg_before
231 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100232 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100233 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200234 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100235 return ret;
236}
237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200239 * Return whether a typval is a valid expression to pass to eval_expr_typval()
240 * or eval_expr_to_bool(). An empty string returns FALSE;
241 */
242 int
243eval_expr_valid_arg(typval_T *tv)
244{
245 return tv->v_type != VAR_UNKNOWN
246 && (tv->v_type != VAR_STRING
247 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
248}
249
250/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251 * Evaluate an expression, which can be a function, partial or string.
252 * Pass arguments "argv[argc]".
253 * Return the result in "rettv" and OK or FAIL.
254 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200255 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100256eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
257{
258 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100259 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200260 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100261
262 if (expr->v_type == VAR_FUNC)
263 {
264 s = expr->vval.v_string;
265 if (s == NULL || *s == NUL)
266 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200267 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200268 funcexe.evaluate = TRUE;
269 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100270 return FAIL;
271 }
272 else if (expr->v_type == VAR_PARTIAL)
273 {
274 partial_T *partial = expr->vval.v_partial;
275
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200276 if (partial == NULL)
277 return FAIL;
278
Bram Moolenaar822ba242020-05-24 23:00:18 +0200279 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200280 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200282 if (call_def_function(partial->pt_func, argc, argv,
283 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284 return FAIL;
285 }
286 else
287 {
288 s = partial_name(partial);
289 if (s == NULL || *s == NUL)
290 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200291 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292 funcexe.evaluate = TRUE;
293 funcexe.partial = partial;
294 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
295 return FAIL;
296 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 }
298 else
299 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100300 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100301 if (s == NULL)
302 return FAIL;
303 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200304 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100305 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200306 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100307 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200308 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100309 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100310 return FAIL;
311 }
312 }
313 return OK;
314}
315
316/*
317 * Like eval_to_bool() but using a typval_T instead of a string.
318 * Works for string, funcref and partial.
319 */
320 int
321eval_expr_to_bool(typval_T *expr, int *error)
322{
323 typval_T rettv;
324 int res;
325
326 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
327 {
328 *error = TRUE;
329 return FALSE;
330 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100331 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100332 clear_tv(&rettv);
333 return res;
334}
335
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336/*
337 * Top level evaluation function, returning a string. If "skip" is TRUE,
338 * only parsing to "nextcmd" is done, without reporting errors. Return
339 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
340 */
341 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100342eval_to_string_skip(
343 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200344 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100345 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346{
Bram Moolenaar33570922005-01-25 22:26:29 +0000347 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200349 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
Bram Moolenaar37c83712020-06-30 21:18:36 +0200351 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 if (skip)
353 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200354 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 retval = NULL;
356 else
357 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100358 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000359 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 }
361 if (skip)
362 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200363 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364
365 return retval;
366}
367
368/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000369 * Skip over an expression at "*pp".
370 * Return FAIL for an error, OK otherwise.
371 */
372 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100373skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000374{
Bram Moolenaar33570922005-01-25 22:26:29 +0000375 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000376
377 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200378 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000379}
380
381/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200382 * Skip over an expression at "*pp".
383 * If in Vim9 script and line breaks are encountered, the lines are
384 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200385 * "arg" is advanced to just after the expression.
386 * "start" is set to the start of the expression, "end" to just after the end.
387 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200388 * Return FAIL for an error, OK otherwise.
389 */
390 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200391skip_expr_concatenate(
392 char_u **arg,
393 char_u **start,
394 char_u **end,
395 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200396{
397 typval_T rettv;
398 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200399 int vim9script = in_vim9script();
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200400 garray_T *gap = &evalarg->eval_ga;
401 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
402
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200403 if (vim9script
404 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 {
406 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200407 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 ++gap->ga_len;
410 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200411 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412
413 // Don't evaluate the expression.
414 if (evalarg != NULL)
415 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200416 *arg = skipwhite(*arg);
417 res = eval1(arg, &rettv, evalarg);
418 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 if (evalarg != NULL)
420 evalarg->eval_flags = save_flags;
421
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200422 if (vim9script
423 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200425 if (evalarg->eval_ga.ga_len == 1)
426 {
427 // just one line, no need to concatenate
428 ga_clear(gap);
429 gap->ga_itemsize = 0;
430 }
431 else
432 {
433 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200435
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 // Line breaks encountered, concatenate all the lines.
437 *((char_u **)gap->ga_data) = *start;
438 p = ga_concat_strings(gap, "");
439
440 // free the lines only when using getsourceline()
441 if (evalarg->eval_cookie != NULL)
442 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200444 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200445 // Do not free the last line, "arg" points into it, free it
446 // later.
447 vim_free(evalarg->eval_tofree);
448 evalarg->eval_tofree =
449 ((char_u **)gap->ga_data)[gap->ga_len - 1];
450 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear_strings(gap);
452 }
453 else
454 ga_clear(gap);
455 gap->ga_itemsize = 0;
456 if (p == NULL)
457 return FAIL;
458 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200459 vim_free(evalarg->eval_tofree_lambda);
460 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200461 // Compute "end" relative to the end.
462 *end = *start + STRLEN(*start) - endoff;
463 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200464 }
465
466 return res;
467}
468
469/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200470 * Top level evaluation function, returning a string. Does not handle line
471 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000472 * When "convert" is TRUE convert a List into a sequence of lines and convert
473 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 * Return pointer to allocated memory, or NULL for failure.
475 */
476 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100477eval_to_string(
478 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100479 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
Bram Moolenaar33570922005-01-25 22:26:29 +0000481 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000483 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000484#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000485 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200488 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 retval = NULL;
490 else
491 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000492 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000493 {
494 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000495 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200496 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200497 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200498 if (tv.vval.v_list->lv_len > 0)
499 ga_append(&ga, NL);
500 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000501 ga_append(&ga, NUL);
502 retval = (char_u *)ga.ga_data;
503 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000504#ifdef FEAT_FLOAT
505 else if (convert && tv.v_type == VAR_FLOAT)
506 {
507 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
508 retval = vim_strsave(numbuf);
509 }
510#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000511 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100512 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000513 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200515 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
517 return retval;
518}
519
520/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000521 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200522 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 */
524 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100525eval_to_string_safe(
526 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100527 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
529 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200530 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200532 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000533 if (use_sandbox)
534 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200535 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200536 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000537 if (use_sandbox)
538 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200539 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200540 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 return retval;
542}
543
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544/*
545 * Top level evaluation function, returning a number.
546 * Evaluates "expr" silently.
547 * Returns -1 for an error.
548 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200549 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100550eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551{
Bram Moolenaar33570922005-01-25 22:26:29 +0000552 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200553 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000554 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556 ++emsg_off;
557
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200558 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 retval = -1;
560 else
561 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100562 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000563 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 }
565 --emsg_off;
566
567 return retval;
568}
569
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000570/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000571 * Top level evaluation function.
572 * Returns an allocated typval_T with the result.
573 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000574 */
575 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200576eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000577{
578 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200579 evalarg_T evalarg;
580
581 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000582
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200583 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200584 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100585 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000586
Bram Moolenaar37c83712020-06-30 21:18:36 +0200587 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000588 return tv;
589}
590
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100592 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200593 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
594 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000595 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200597 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100598call_vim_function(
599 char_u *func,
600 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200601 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200602 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000604 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200605 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100607 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200608 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200609 funcexe.firstline = curwin->w_cursor.lnum;
610 funcexe.lastline = curwin->w_cursor.lnum;
611 funcexe.evaluate = TRUE;
612 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000613 if (ret == FAIL)
614 clear_tv(rettv);
615
616 return ret;
617}
618
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100619/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100620 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100621 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200622 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
623 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100624 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200625 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100626call_func_retnr(
627 char_u *func,
628 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200629 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100630{
631 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200632 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100633
Bram Moolenaarded27a12018-08-01 19:06:03 +0200634 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100635 return -1;
636
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100637 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100638 clear_tv(&rettv);
639 return retval;
640}
641
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000642/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100643 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000644 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200645 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
646 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000647 */
648 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100649call_func_retstr(
650 char_u *func,
651 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200652 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000653{
654 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000655 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000656
Bram Moolenaarded27a12018-08-01 19:06:03 +0200657 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000658 return NULL;
659
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100660 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000661 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 return retval;
663}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000664
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000665/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100666 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200667 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
668 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000669 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000670 */
671 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100672call_func_retlist(
673 char_u *func,
674 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200675 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000676{
677 typval_T rettv;
678
Bram Moolenaarded27a12018-08-01 19:06:03 +0200679 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000680 return NULL;
681
682 if (rettv.v_type != VAR_LIST)
683 {
684 clear_tv(&rettv);
685 return NULL;
686 }
687
688 return rettv.vval.v_list;
689}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691#ifdef FEAT_FOLDING
692/*
693 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
694 * it in "*cp". Doesn't give error messages.
695 */
696 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100697eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698{
Bram Moolenaar33570922005-01-25 22:26:29 +0000699 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200700 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000702 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
703 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
705 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000706 if (use_sandbox)
707 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200708 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200710 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 retval = 0;
712 else
713 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100714 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000715 if (tv.v_type == VAR_NUMBER)
716 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000717 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 retval = 0;
719 else
720 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100721 // If the result is a string, check if there is a non-digit before
722 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000723 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 if (!VIM_ISDIGIT(*s) && *s != '-')
725 *cp = *s++;
726 retval = atol((char *)s);
727 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000728 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 }
730 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000731 if (use_sandbox)
732 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200733 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200734 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200736 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737}
738#endif
739
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000741 * Get an lval: variable, Dict item or List item that can be assigned a value
742 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
743 * "name.key", "name.key[expr]" etc.
744 * Indexing only works if "name" is an existing List or Dictionary.
745 * "name" points to the start of the name.
746 * If "rettv" is not NULL it points to the value to be assigned.
747 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
748 * wrong; must end in space or cmd separator.
749 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100750 * flags:
751 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100752 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100753 * GLV_NO_AUTOLOAD: do not use script autoloading
754 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000755 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000756 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000757 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000758 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200759 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100760get_lval(
761 char_u *name,
762 typval_T *rettv,
763 lval_T *lp,
764 int unlet,
765 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100766 int flags, // GLV_ values
767 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000768{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000769 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000770 char_u *expr_start, *expr_end;
771 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000772 dictitem_T *v;
773 typval_T var1;
774 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000775 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000776 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000777 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000778 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000779 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100780 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000781
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100782 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200783 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000784
785 if (skip)
786 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100787 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200789 lp->ll_name_end = find_name_end(name, NULL, NULL,
790 FNE_INCL_BR | fne_flags);
791 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000792 }
793
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100794 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000795 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000797 if (expr_start != NULL)
798 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100799 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100800 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 && *p != '[' && *p != '.')
802 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200803 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000804 return NULL;
805 }
806
807 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
808 if (lp->ll_exp_name == NULL)
809 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100810 // Report an invalid expression in braces, unless the
811 // expression evaluation has been cancelled due to an
812 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000813 if (!aborting() && !quiet)
814 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000815 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100816 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000817 return NULL;
818 }
819 }
820 lp->ll_name = lp->ll_exp_name;
821 }
822 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000824 lp->ll_name = name;
825
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200826 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200828 // "a: type" is declaring variable "a" with a type, not "a:".
829 if (p == name + 2 && p[-1] == ':')
830 {
831 --p;
832 lp->ll_name_end = p;
833 }
834 if (*p == ':')
835 {
836 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
837 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200839 // parse the type after the name
840 lp->ll_type = parse_type(&tp, &si->sn_type_list);
841 lp->ll_name_end = tp;
842 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 }
844 }
845
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100846 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
848 return p;
849
850 cc = *p;
851 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 // Only pass &ht when we would write to the variable, it prevents autoload
853 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100854 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
855 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100857 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000859 if (v == NULL)
860 return NULL;
861
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 /*
863 * Loop until no more [idx] or .key is following.
864 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000865 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100866 var1.v_type = VAR_UNKNOWN;
867 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
871 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100872 && lp->ll_tv->vval.v_dict != NULL)
873 && !(lp->ll_tv->v_type == VAR_BLOB
874 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000875 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000876 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100877 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000879 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000881 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100883 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000884 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000885 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000886
Bram Moolenaar8c711452005-01-14 21:53:12 +0000887 len = -1;
888 if (*p == '.')
889 {
890 key = p + 1;
891 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
892 ;
893 if (len == 0)
894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000895 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100896 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000898 }
899 p = key + len;
900 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000901 else
902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100903 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000904 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000905 if (*p == ':')
906 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000907 else
908 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000909 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200910 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100912 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000913 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100914 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000915 clear_tv(&var1);
916 return NULL;
917 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200918 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000919 }
920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100921 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000922 if (*p == ':')
923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000924 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000925 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000926 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100927 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100928 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000929 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000930 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100931 if (rettv != NULL
932 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100933 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100934 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100935 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000936 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000937 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100938 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100939 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000941 }
942 p = skipwhite(p + 1);
943 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000944 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000945 else
946 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000947 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200948 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200949 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000950 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100951 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000953 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100954 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000955 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100956 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100957 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000958 clear_tv(&var2);
959 return NULL;
960 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000963 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000964 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000966
Bram Moolenaar8c711452005-01-14 21:53:12 +0000967 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000968 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100970 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100971 clear_tv(&var1);
972 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000974 }
975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100976 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000977 ++p;
978 }
979
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 {
982 if (len == -1)
983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100984 // "[key]": get key from "var1"
985 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200986 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000987 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000989 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 }
991 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000992 lp->ll_list = NULL;
993 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000994 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100996 // When assigning to a scope dictionary check that a function and
997 // variable name is valid (only variable name unless it is l: or
998 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200999 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001000 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001001 int prevval;
1002 int wrong;
1003
1004 if (len != -1)
1005 {
1006 prevval = key[len];
1007 key[len] = NUL;
1008 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001009 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001010 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001011 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1012 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001013 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001014 || !valid_varname(key);
1015 if (len != -1)
1016 key[len] = prevval;
1017 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001018 {
1019 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001020 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001021 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001022 }
1023
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001024 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001025 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001026 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001027 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001028 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001029 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001030 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001031 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001032 return NULL;
1033 }
1034
Bram Moolenaar31b81602019-02-10 22:14:27 +01001035 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001037 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001039 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001040 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001042 }
1043 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001047 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 p = NULL;
1050 break;
1051 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001052 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001053 else if ((flags & GLV_READ_ONLY) == 0
1054 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001055 {
1056 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001057 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001058 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001059
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001060 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001063 else if (lp->ll_tv->v_type == VAR_BLOB)
1064 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001065 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1066
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001067 /*
1068 * Get the number and item for the only or first index of the List.
1069 */
1070 if (empty1)
1071 lp->ll_n1 = 0;
1072 else
1073 // is number or string
1074 lp->ll_n1 = (long)tv_get_number(&var1);
1075 clear_tv(&var1);
1076
1077 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001078 || lp->ll_n1 > bloblen
1079 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001080 {
1081 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001082 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001083 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001084 return NULL;
1085 }
1086 if (lp->ll_range && !lp->ll_empty2)
1087 {
1088 lp->ll_n2 = (long)tv_get_number(&var2);
1089 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001090 if (lp->ll_n2 < 0
1091 || lp->ll_n2 >= bloblen
1092 || lp->ll_n2 < lp->ll_n1)
1093 {
1094 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001095 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001096 return NULL;
1097 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001098 }
1099 lp->ll_blob = lp->ll_tv->vval.v_blob;
1100 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001101 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001102 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001103 else
1104 {
1105 /*
1106 * Get the number and item for the only or first index of the List.
1107 */
1108 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001109 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001110 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001111 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001112 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001113 clear_tv(&var1);
1114
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001115 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001116 lp->ll_list = lp->ll_tv->vval.v_list;
1117 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1118 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001119 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001120 if (lp->ll_n1 < 0)
1121 {
1122 lp->ll_n1 = 0;
1123 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1124 }
1125 }
1126 if (lp->ll_li == NULL)
1127 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001128 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001129 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001130 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001131 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001132 }
1133
1134 /*
1135 * May need to find the item or absolute index for the second
1136 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 * When no index given: "lp->ll_empty2" is TRUE.
1138 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001139 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001142 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001143 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001144 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001146 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001147 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001148 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001149 {
1150 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001151 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001153 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001155 }
1156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001157 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158 if (lp->ll_n1 < 0)
1159 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1160 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001161 {
1162 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001163 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001164 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001165 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001166 }
1167
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001168 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001169 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001170 }
1171
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001172 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001174 return p;
1175}
1176
1177/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001178 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001179 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001181clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001182{
1183 vim_free(lp->ll_exp_name);
1184 vim_free(lp->ll_newkey);
1185}
1186
1187/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001188 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001189 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001190 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1191 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001192 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001194set_var_lval(
1195 lval_T *lp,
1196 char_u *endp,
1197 typval_T *rettv,
1198 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001200 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001201{
1202 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001203 listitem_T *ri;
1204 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205
1206 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001207 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001208 cc = *endp;
1209 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001210 if (lp->ll_blob != NULL)
1211 {
1212 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001213
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001214 if (op != NULL && *op != '=')
1215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001217 return;
1218 }
1219
1220 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1221 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001222 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001223
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001224 if (lp->ll_empty2)
1225 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1226
1227 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001229 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001230 return;
1231 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001232 if (lp->ll_empty2)
1233 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001234
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001235 ir = 0;
1236 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1237 blob_set(lp->ll_blob, il,
1238 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001239 }
1240 else
1241 {
1242 val = (int)tv_get_number_chk(rettv, &error);
1243 if (!error)
1244 {
1245 garray_T *gap = &lp->ll_blob->bv_ga;
1246
1247 // Allow for appending a byte. Setting a byte beyond
1248 // the end is an error otherwise.
1249 if (lp->ll_n1 < gap->ga_len
1250 || (lp->ll_n1 == gap->ga_len
1251 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1252 {
1253 blob_set(lp->ll_blob, lp->ll_n1, val);
1254 if (lp->ll_n1 == gap->ga_len)
1255 ++gap->ga_len;
1256 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001257 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001258 }
1259 }
1260 }
1261 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001262 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001263 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001266 {
1267 emsg(_(e_cannot_mod));
1268 *endp = cc;
1269 return;
1270 }
1271
Bram Moolenaarff697e62019-02-12 22:28:33 +01001272 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001273 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001274 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001275 &tv, &di, TRUE, FALSE) == OK)
1276 {
1277 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001278 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1279 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001280 && tv_op(&tv, rettv, op) == OK)
1281 set_var(lp->ll_name, &tv, FALSE);
1282 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001284 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001285 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001286 {
1287 if (lp->ll_type != NULL
1288 && check_typval_type(lp->ll_type, rettv) == FAIL)
1289 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001291 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001292 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001293 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001294 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001295 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001296 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001297 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 else if (lp->ll_range)
1299 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001300 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001301 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001304 {
1305 emsg(_("E996: Cannot lock a range"));
1306 return;
1307 }
1308
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001309 /*
1310 * Check whether any of the list items is locked
1311 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001312 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001313 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001314 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001315 return;
1316 ri = ri->li_next;
1317 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1318 break;
1319 ll_li = ll_li->li_next;
1320 ++ll_n1;
1321 }
1322
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001323 /*
1324 * Assign the List values to the list items.
1325 */
1326 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001327 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001328 if (op != NULL && *op != '=')
1329 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1330 else
1331 {
1332 clear_tv(&lp->ll_li->li_tv);
1333 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1334 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335 ri = ri->li_next;
1336 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1337 break;
1338 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001339 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001340 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001341 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001342 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001343 ri = NULL;
1344 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001345 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001346 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001347 lp->ll_li = lp->ll_li->li_next;
1348 ++lp->ll_n1;
1349 }
1350 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001351 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001352 else if (lp->ll_empty2
1353 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001354 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001355 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001356 }
1357 else
1358 {
1359 /*
1360 * Assign to a List or Dictionary item.
1361 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001363 {
1364 emsg(_("E996: Cannot lock a list or dict"));
1365 return;
1366 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001367 if (lp->ll_newkey != NULL)
1368 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001369 if (op != NULL && *op != '=')
1370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001371 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001372 return;
1373 }
1374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001375 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001376 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001377 if (di == NULL)
1378 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001379 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1380 {
1381 vim_free(di);
1382 return;
1383 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001384 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001385 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001386 else if (op != NULL && *op != '=')
1387 {
1388 tv_op(lp->ll_tv, rettv, op);
1389 return;
1390 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001392 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001393
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001394 /*
1395 * Assign the value to the variable or list item.
1396 */
1397 if (copy)
1398 copy_tv(rettv, lp->ll_tv);
1399 else
1400 {
1401 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001402 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001403 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001404 }
1405 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001406}
1407
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001409 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1410 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001411 * Returns OK or FAIL.
1412 */
1413 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001414tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001415{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001416 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001417 char_u numbuf[NUMBUFLEN];
1418 char_u *s;
1419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001420 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001421 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001422 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001423 {
1424 switch (tv1->v_type)
1425 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001426 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001427 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001429 case VAR_DICT:
1430 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001431 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001432 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001433 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001434 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001435 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001436 break;
1437
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001438 case VAR_BLOB:
1439 if (*op != '+' || tv2->v_type != VAR_BLOB)
1440 break;
1441 // BLOB += BLOB
1442 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1443 {
1444 blob_T *b1 = tv1->vval.v_blob;
1445 blob_T *b2 = tv2->vval.v_blob;
1446 int i, len = blob_len(b2);
1447 for (i = 0; i < len; i++)
1448 ga_append(&b1->bv_ga, blob_get(b2, i));
1449 }
1450 return OK;
1451
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001452 case VAR_LIST:
1453 if (*op != '+' || tv2->v_type != VAR_LIST)
1454 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001455 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001456 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1457 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1458 return OK;
1459
1460 case VAR_NUMBER:
1461 case VAR_STRING:
1462 if (tv2->v_type == VAR_LIST)
1463 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001464 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001466 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001467 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001468#ifdef FEAT_FLOAT
1469 if (tv2->v_type == VAR_FLOAT)
1470 {
1471 float_T f = n;
1472
Bram Moolenaarff697e62019-02-12 22:28:33 +01001473 if (*op == '%')
1474 break;
1475 switch (*op)
1476 {
1477 case '+': f += tv2->vval.v_float; break;
1478 case '-': f -= tv2->vval.v_float; break;
1479 case '*': f *= tv2->vval.v_float; break;
1480 case '/': f /= tv2->vval.v_float; break;
1481 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001482 clear_tv(tv1);
1483 tv1->v_type = VAR_FLOAT;
1484 tv1->vval.v_float = f;
1485 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001486 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001487#endif
1488 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001489 switch (*op)
1490 {
1491 case '+': n += tv_get_number(tv2); break;
1492 case '-': n -= tv_get_number(tv2); break;
1493 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001494 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1495 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001496 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001497 clear_tv(tv1);
1498 tv1->v_type = VAR_NUMBER;
1499 tv1->vval.v_number = n;
1500 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001501 }
1502 else
1503 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001504 if (tv2->v_type == VAR_FLOAT)
1505 break;
1506
Bram Moolenaarff697e62019-02-12 22:28:33 +01001507 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001508 s = tv_get_string(tv1);
1509 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001510 clear_tv(tv1);
1511 tv1->v_type = VAR_STRING;
1512 tv1->vval.v_string = s;
1513 }
1514 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001515
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001516 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001517#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001518 {
1519 float_T f;
1520
Bram Moolenaarff697e62019-02-12 22:28:33 +01001521 if (*op == '%' || *op == '.'
1522 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001523 && tv2->v_type != VAR_NUMBER
1524 && tv2->v_type != VAR_STRING))
1525 break;
1526 if (tv2->v_type == VAR_FLOAT)
1527 f = tv2->vval.v_float;
1528 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001529 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001530 switch (*op)
1531 {
1532 case '+': tv1->vval.v_float += f; break;
1533 case '-': tv1->vval.v_float -= f; break;
1534 case '*': tv1->vval.v_float *= f; break;
1535 case '/': tv1->vval.v_float /= f; break;
1536 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001537 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001538#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001539 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001540 }
1541 }
1542
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001543 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001544 return FAIL;
1545}
1546
1547/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001548 * Evaluate the expression used in a ":for var in expr" command.
1549 * "arg" points to "var".
1550 * Set "*errp" to TRUE for an error, FALSE otherwise;
1551 * Return a pointer that holds the info. Null when there is an error.
1552 */
1553 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001554eval_for_line(
1555 char_u *arg,
1556 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001557 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001558 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001559{
Bram Moolenaar33570922005-01-25 22:26:29 +00001560 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001562 typval_T tv;
1563 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001564 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001566 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001567
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001568 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001569 if (fi == NULL)
1570 return NULL;
1571
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001572 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001573 if (expr == NULL)
1574 return fi;
1575
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001576 expr = skipwhite_and_linebreak(expr, evalarg);
1577 if (expr[0] != 'i' || expr[1] != 'n'
1578 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001579 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001581 return fi;
1582 }
1583
1584 if (skip)
1585 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001586 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1587 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001588 {
1589 *errp = FALSE;
1590 if (!skip)
1591 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001592 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001593 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001594 l = tv.vval.v_list;
1595 if (l == NULL)
1596 {
1597 // a null list is like an empty list: do nothing
1598 clear_tv(&tv);
1599 }
1600 else
1601 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001603 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001605 // No need to increment the refcount, it's already set for
1606 // the list being used in "tv".
1607 fi->fi_list = l;
1608 list_add_watch(l, &fi->fi_lw);
1609 fi->fi_lw.lw_item = l->lv_first;
1610 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001611 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001612 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001613 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001614 fi->fi_bi = 0;
1615 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001616 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001617 typval_T btv;
1618
1619 // Make a copy, so that the iteration still works when the
1620 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001622 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001623 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001624 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001625 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001626 else
1627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001628 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001629 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630 }
1631 }
1632 }
1633 if (skip)
1634 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001635 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001636
1637 return fi;
1638}
1639
1640/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001641 * Used when looping over a :for line, skip the "in expr" part.
1642 */
1643 void
1644skip_for_lines(void *fi_void, evalarg_T *evalarg)
1645{
1646 forinfo_T *fi = (forinfo_T *)fi_void;
1647 int i;
1648
1649 for (i = 0; i < fi->fi_break_count; ++i)
1650 eval_next_line(evalarg);
1651}
1652
1653/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654 * Use the first item in a ":for" list. Advance to the next.
1655 * Assign the values to the variable (list). "arg" points to the first one.
1656 * Return TRUE when a valid item was found, FALSE when at end of list or
1657 * something wrong.
1658 */
1659 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001660next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001662 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663 int result;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001664 int flag = in_vim9script() ? LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001665 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001667 if (fi->fi_blob != NULL)
1668 {
1669 typval_T tv;
1670
1671 if (fi->fi_bi >= blob_len(fi->fi_blob))
1672 return FALSE;
1673 tv.v_type = VAR_NUMBER;
1674 tv.v_lock = VAR_FIXED;
1675 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1676 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001677 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001678 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001679 }
1680
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 item = fi->fi_lw.lw_item;
1682 if (item == NULL)
1683 result = FALSE;
1684 else
1685 {
1686 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001687 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001688 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 }
1690 return result;
1691}
1692
1693/*
1694 * Free the structure used to store info used by ":for".
1695 */
1696 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001697free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698{
Bram Moolenaar33570922005-01-25 22:26:29 +00001699 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001700
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001701 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001702 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001703 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001704 list_unref(fi->fi_list);
1705 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001706 if (fi != NULL && fi->fi_blob != NULL)
1707 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001708 vim_free(fi);
1709}
1710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001712set_context_for_expression(
1713 expand_T *xp,
1714 char_u *arg,
1715 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
1717 int got_eq = FALSE;
1718 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001721 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 {
1723 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001724 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001726 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001727 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001728 {
1729 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001730 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001731 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 break;
1733 }
1734 return;
1735 }
1736 }
1737 else
1738 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1739 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 while ((xp->xp_pattern = vim_strpbrk(arg,
1741 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1742 {
1743 c = *xp->xp_pattern;
1744 if (c == '&')
1745 {
1746 c = xp->xp_pattern[1];
1747 if (c == '&')
1748 {
1749 ++xp->xp_pattern;
1750 xp->xp_context = cmdidx != CMD_let || got_eq
1751 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1752 }
1753 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001754 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001756 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1757 xp->xp_pattern += 2;
1758
1759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761 else if (c == '$')
1762 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001763 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 xp->xp_context = EXPAND_ENV_VARS;
1765 }
1766 else if (c == '=')
1767 {
1768 got_eq = TRUE;
1769 xp->xp_context = EXPAND_EXPRESSION;
1770 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001771 else if (c == '#'
1772 && xp->xp_context == EXPAND_EXPRESSION)
1773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001774 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001775 break;
1776 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001777 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 && xp->xp_context == EXPAND_FUNCTIONS
1779 && vim_strchr(xp->xp_pattern, '(') == NULL)
1780 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001781 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 break;
1783 }
1784 else if (cmdidx != CMD_let || got_eq)
1785 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001786 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 {
1788 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1789 if (c == '\\' && xp->xp_pattern[1] != NUL)
1790 ++xp->xp_pattern;
1791 xp->xp_context = EXPAND_NOTHING;
1792 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001793 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001795 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1797 /* skip */ ;
1798 xp->xp_context = EXPAND_NOTHING;
1799 }
1800 else if (c == '|')
1801 {
1802 if (xp->xp_pattern[1] == '|')
1803 {
1804 ++xp->xp_pattern;
1805 xp->xp_context = EXPAND_EXPRESSION;
1806 }
1807 else
1808 xp->xp_context = EXPAND_COMMANDS;
1809 }
1810 else
1811 xp->xp_context = EXPAND_EXPRESSION;
1812 }
1813 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001814 // Doesn't look like something valid, expand as an expression
1815 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 arg = xp->xp_pattern;
1818 if (*arg != NUL)
1819 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1820 /* skip */ ;
1821 }
1822 xp->xp_pattern = arg;
1823}
1824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001826 * Return TRUE if "pat" matches "text".
1827 * Does not use 'cpo' and always uses 'magic'.
1828 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001829 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001830pattern_match(char_u *pat, char_u *text, int ic)
1831{
1832 int matches = FALSE;
1833 char_u *save_cpo;
1834 regmatch_T regmatch;
1835
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001836 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001837 save_cpo = p_cpo;
1838 p_cpo = (char_u *)"";
1839 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1840 if (regmatch.regprog != NULL)
1841 {
1842 regmatch.rm_ic = ic;
1843 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1844 vim_regfree(regmatch.regprog);
1845 }
1846 p_cpo = save_cpo;
1847 return matches;
1848}
1849
1850/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001851 * Handle a name followed by "(". Both for just "name(arg)" and for
1852 * "expr->name(arg)".
1853 * Returns OK or FAIL.
1854 */
1855 static int
1856eval_func(
1857 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001858 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001859 char_u *name,
1860 int name_len,
1861 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001862 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001863 typval_T *basetv) // "expr" for "expr->name(arg)"
1864{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001865 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001866 char_u *s = name;
1867 int len = name_len;
1868 partial_T *partial;
1869 int ret = OK;
1870
1871 if (!evaluate)
1872 check_vars(s, len);
1873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001874 // If "s" is the name of a variable of type VAR_FUNC
1875 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001876 s = deref_func_name(s, &len, &partial, !evaluate);
1877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001878 // Need to make a copy, in case evaluating the arguments makes
1879 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001880 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001881 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001882 ret = FAIL;
1883 else
1884 {
1885 funcexe_T funcexe;
1886
1887 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001888 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001889 funcexe.firstline = curwin->w_cursor.lnum;
1890 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001891 funcexe.evaluate = evaluate;
1892 funcexe.partial = partial;
1893 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001894 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001895 }
1896 vim_free(s);
1897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001898 // If evaluate is FALSE rettv->v_type was not set in
1899 // get_func_tv, but it's needed in handle_subscript() to parse
1900 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001901 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1902 {
1903 rettv->vval.v_string = NULL;
1904 rettv->v_type = VAR_FUNC;
1905 }
1906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001907 // Stop the expression evaluation when immediately
1908 // aborting on error, or when an interrupt occurred or
1909 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001910 if (evaluate && aborting())
1911 {
1912 if (ret == OK)
1913 clear_tv(rettv);
1914 ret = FAIL;
1915 }
1916 return ret;
1917}
1918
1919/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001920 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1921 * comment) and there is a next line, return the next line (skipping blanks)
1922 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001923 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1924 * "arg" must point somewhere inside a line, not at the start.
1925 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001926 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001927eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1928{
Bram Moolenaar9d489562020-07-30 20:08:50 +02001929 char_u *p = skipwhite(arg);
1930
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001931 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001932 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001933 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001934 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001935 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001936 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02001937 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001938
1939 if (evalarg->eval_cookie != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001940 next = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001941 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001942 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001943
Bram Moolenaar9d489562020-07-30 20:08:50 +02001944 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001945 {
1946 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001947 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001948 }
1949 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02001950 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001951}
1952
1953/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001954 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001955 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001956 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001957eval_next_line(evalarg_T *evalarg)
1958{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001959 garray_T *gap = &evalarg->eval_ga;
1960 char_u *line;
1961
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001962 if (evalarg->eval_cookie != NULL)
1963 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
1964 else
1965 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001966 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001967 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1968 {
1969 // Going to concatenate the lines after parsing.
1970 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1971 ++gap->ga_len;
1972 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001973 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001974 {
1975 vim_free(evalarg->eval_tofree);
1976 evalarg->eval_tofree = line;
1977 }
1978 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001979}
1980
Bram Moolenaare6b53242020-07-01 17:28:33 +02001981/*
1982 * Call eval_next_non_blank() and get the next line if needed.
1983 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001984 char_u *
1985skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1986{
1987 int getnext;
1988 char_u *p = skipwhite(arg);
1989
Bram Moolenaar962d7212020-07-04 14:15:00 +02001990 if (evalarg == NULL)
1991 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02001992 eval_next_non_blank(p, evalarg, &getnext);
1993 if (getnext)
1994 return eval_next_line(evalarg);
1995 return p;
1996}
1997
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001998/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001999 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002000 */
2001 void
2002clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2003{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002004 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002005 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002006 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002007 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002008 if (eap != NULL)
2009 {
2010 // We may need to keep the original command line, e.g. for
2011 // ":let" it has the variable names. But we may also need the
2012 // new one, "nextcmd" points into it. Keep both.
2013 vim_free(eap->cmdline_tofree);
2014 eap->cmdline_tofree = *eap->cmdlinep;
2015 *eap->cmdlinep = evalarg->eval_tofree;
2016 }
2017 else
2018 vim_free(evalarg->eval_tofree);
2019 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002020 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002021
2022 vim_free(evalarg->eval_tofree_lambda);
2023 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002024 }
2025}
2026
2027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002029 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2031 */
2032
2033/*
2034 * Handle zero level expression.
2035 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002037 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002038 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 * Return OK or FAIL.
2040 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002042eval0(
2043 char_u *arg,
2044 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002045 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002046 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047{
2048 int ret;
2049 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002050 int did_emsg_before = did_emsg;
2051 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002052 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002055 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002056 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002057
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002058 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 {
2060 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 /*
2063 * Report the invalid expression unless the expression evaluation has
2064 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002065 * exception, or we already gave a more specific error.
2066 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002068 if (!aborting()
2069 && did_emsg == did_emsg_before
2070 && called_emsg == called_emsg_before
2071 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002072 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 ret = FAIL;
2074 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002075
2076 if (eap != NULL)
2077 eap->nextcmd = check_nextcmd(p);
2078
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 return ret;
2080}
2081
2082/*
2083 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002084 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 *
2086 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002087 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002089 * Note: "rettv.v_lock" is not set.
2090 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 * Return OK or FAIL.
2092 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002093 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002094eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002096 char_u *p;
2097 int getnext;
2098
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 /*
2100 * Get the first variable.
2101 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002102 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 return FAIL;
2104
Bram Moolenaar793648f2020-06-26 21:28:25 +02002105 p = eval_next_non_blank(*arg, evalarg, &getnext);
2106 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002108 int result;
2109 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002110 evalarg_T *evalarg_used = evalarg;
2111 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002112 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002113 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002114
2115 if (evalarg == NULL)
2116 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002117 CLEAR_FIELD(local_evalarg);
2118 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002119 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002120 orig_flags = evalarg_used->eval_flags;
2121 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002122
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002123 if (getnext)
2124 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002125 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002126 {
2127 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2128 {
2129 error_white_both(p, 1);
2130 clear_tv(rettv);
2131 return FAIL;
2132 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002133 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002134 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002135
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002137 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002139 int error = FALSE;
2140
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002141 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002144 if (error)
2145 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 }
2147
2148 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002149 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002151 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2152 {
2153 error_white_both(p, 1);
2154 clear_tv(rettv);
2155 return FAIL;
2156 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002157 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2158 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002159 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002160 if (eval1(arg, rettv, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 return FAIL;
2162
2163 /*
2164 * Check for the ":".
2165 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002166 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar793648f2020-06-26 21:28:25 +02002167 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002171 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 return FAIL;
2173 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002174 if (getnext)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002175 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002176 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002177 {
2178 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2179 {
2180 error_white_both(p, 1);
2181 clear_tv(rettv);
2182 return FAIL;
2183 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002184 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
2187 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002188 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002190 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2191 {
2192 error_white_both(p, 1);
2193 clear_tv(rettv);
2194 return FAIL;
2195 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002196 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2197 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002198 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002199 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 {
2201 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 return FAIL;
2204 }
2205 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 *rettv = var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002207
2208 if (evalarg == NULL)
2209 clear_evalarg(&local_evalarg, NULL);
2210 else
2211 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 }
2213
2214 return OK;
2215}
2216
2217/*
2218 * Handle first level expression:
2219 * expr2 || expr2 || expr2 logical OR
2220 *
2221 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002222 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 *
2224 * Return OK or FAIL.
2225 */
2226 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002227eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002229 char_u *p;
2230 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
2232 /*
2233 * Get the first variable.
2234 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002235 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 return FAIL;
2237
2238 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002239 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002241 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002242 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002244 evalarg_T *evalarg_used = evalarg;
2245 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002246 int evaluate;
2247 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002248 long result = FALSE;
2249 typval_T var2;
2250 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002251 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002252
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002253 if (evalarg == NULL)
2254 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002255 CLEAR_FIELD(local_evalarg);
2256 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002257 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002258 orig_flags = evalarg_used->eval_flags;
2259 evaluate = orig_flags & EVAL_EVALUATE;
2260 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002261 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002262 if (vim9script)
2263 {
2264 result = tv2bool(rettv);
2265 }
2266 else
2267 {
2268 error = FALSE;
2269 if (tv_get_number_chk(rettv, &error) != 0)
2270 result = TRUE;
2271 clear_tv(rettv);
2272 if (error)
2273 return FAIL;
2274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 }
2276
2277 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002278 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002280 while (p[0] == '|' && p[1] == '|')
2281 {
2282 if (getnext)
2283 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002284 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002285 {
2286 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2287 {
2288 error_white_both(p, 2);
2289 clear_tv(rettv);
2290 return FAIL;
2291 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002292 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002293 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002294
2295 /*
2296 * Get the second variable.
2297 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002298 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2299 {
2300 error_white_both(p, 2);
2301 clear_tv(rettv);
2302 return FAIL;
2303 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002304 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2305 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002306 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002307 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002308 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002309
2310 /*
2311 * Compute the result.
2312 */
2313 if (evaluate && !result)
2314 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002315 if (vim9script)
2316 {
2317 clear_tv(rettv);
2318 *rettv = var2;
2319 result = tv2bool(rettv);
2320 }
2321 else
2322 {
2323 if (tv_get_number_chk(&var2, &error) != 0)
2324 result = TRUE;
2325 clear_tv(&var2);
2326 if (error)
2327 return FAIL;
2328 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002329 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002330 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002331 {
2332 rettv->v_type = VAR_NUMBER;
2333 rettv->vval.v_number = result;
2334 }
2335
2336 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002338
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002339 if (evalarg == NULL)
2340 clear_evalarg(&local_evalarg, NULL);
2341 else
2342 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
2344
2345 return OK;
2346}
2347
2348/*
2349 * Handle second level expression:
2350 * expr3 && expr3 && expr3 logical AND
2351 *
2352 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002353 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 *
2355 * Return OK or FAIL.
2356 */
2357 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002358eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002360 char_u *p;
2361 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
2363 /*
2364 * Get the first variable.
2365 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002366 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 return FAIL;
2368
2369 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002370 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002372 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002373 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002375 evalarg_T *evalarg_used = evalarg;
2376 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002377 int orig_flags;
2378 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002379 long result = TRUE;
2380 typval_T var2;
2381 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002382 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002383
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002384 if (evalarg == NULL)
2385 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002386 CLEAR_FIELD(local_evalarg);
2387 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002388 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002389 orig_flags = evalarg_used->eval_flags;
2390 evaluate = orig_flags & EVAL_EVALUATE;
2391 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002392 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002393 if (vim9script)
2394 {
2395 result = tv2bool(rettv);
2396 }
2397 else
2398 {
2399 error = FALSE;
2400 if (tv_get_number_chk(rettv, &error) == 0)
2401 result = FALSE;
2402 clear_tv(rettv);
2403 if (error)
2404 return FAIL;
2405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407
2408 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002409 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002411 while (p[0] == '&' && p[1] == '&')
2412 {
2413 if (getnext)
2414 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002415 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002416 {
2417 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2418 {
2419 error_white_both(p, 2);
2420 clear_tv(rettv);
2421 return FAIL;
2422 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002423 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002424 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002425
2426 /*
2427 * Get the second variable.
2428 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002429 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2430 {
2431 error_white_both(p, 2);
2432 clear_tv(rettv);
2433 return FAIL;
2434 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002435 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2436 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002437 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002440
2441 /*
2442 * Compute the result.
2443 */
2444 if (evaluate && result)
2445 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002446 if (vim9script)
2447 {
2448 clear_tv(rettv);
2449 *rettv = var2;
2450 result = tv2bool(rettv);
2451 }
2452 else
2453 {
2454 if (tv_get_number_chk(&var2, &error) == 0)
2455 result = FALSE;
2456 clear_tv(&var2);
2457 if (error)
2458 return FAIL;
2459 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002460 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002461 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002462 {
2463 rettv->v_type = VAR_NUMBER;
2464 rettv->vval.v_number = result;
2465 }
2466
2467 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002469
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002470 if (evalarg == NULL)
2471 clear_evalarg(&local_evalarg, NULL);
2472 else
2473 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 }
2475
2476 return OK;
2477}
2478
2479/*
2480 * Handle third level expression:
2481 * var1 == var2
2482 * var1 =~ var2
2483 * var1 != var2
2484 * var1 !~ var2
2485 * var1 > var2
2486 * var1 >= var2
2487 * var1 < var2
2488 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002489 * var1 is var2
2490 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 *
2492 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002493 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 *
2495 * Return OK or FAIL.
2496 */
2497 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002498eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002501 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002502 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002504 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
2506 /*
2507 * Get the first variable.
2508 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002509 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 return FAIL;
2511
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002512 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002513 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
2515 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002516 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002518 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002520 typval_T var2;
2521 int ic;
2522 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002523 int evaluate = evalarg == NULL
2524 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002525
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002526 if (getnext)
2527 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002528 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2529 {
2530 error_white_both(p, len);
2531 clear_tv(rettv);
2532 return FAIL;
2533 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002534
Bram Moolenaar696ba232020-07-29 21:20:41 +02002535 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2536 {
2537 semsg(_(e_invexpr2), p);
2538 clear_tv(rettv);
2539 return FAIL;
2540 }
2541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002542 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (p[len] == '?')
2544 {
2545 ic = TRUE;
2546 ++len;
2547 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002548 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 else if (p[len] == '#')
2550 {
2551 ic = FALSE;
2552 ++len;
2553 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002554 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002556 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 /*
2559 * Get the second variable.
2560 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002561 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2562 {
2563 error_white_both(p, 1);
2564 clear_tv(rettv);
2565 return FAIL;
2566 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002567 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002568 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 return FAIL;
2572 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002573 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002574 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002575 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002576
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002577 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002578 {
2579 ret = FAIL;
2580 clear_tv(rettv);
2581 }
2582 else
2583 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002584 clear_tv(&var2);
2585 return ret;
2586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588
2589 return OK;
2590}
2591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 void
2593eval_addblob(typval_T *tv1, typval_T *tv2)
2594{
2595 blob_T *b1 = tv1->vval.v_blob;
2596 blob_T *b2 = tv2->vval.v_blob;
2597 blob_T *b = blob_alloc();
2598 int i;
2599
2600 if (b != NULL)
2601 {
2602 for (i = 0; i < blob_len(b1); i++)
2603 ga_append(&b->bv_ga, blob_get(b1, i));
2604 for (i = 0; i < blob_len(b2); i++)
2605 ga_append(&b->bv_ga, blob_get(b2, i));
2606
2607 clear_tv(tv1);
2608 rettv_blob_set(tv1, b);
2609 }
2610}
2611
2612 int
2613eval_addlist(typval_T *tv1, typval_T *tv2)
2614{
2615 typval_T var3;
2616
2617 // concatenate Lists
2618 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2619 {
2620 clear_tv(tv1);
2621 clear_tv(tv2);
2622 return FAIL;
2623 }
2624 clear_tv(tv1);
2625 *tv1 = var3;
2626 return OK;
2627}
2628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629/*
2630 * Handle fourth level expression:
2631 * + number addition
2632 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002633 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002634 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 *
2636 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002637 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 *
2639 * Return OK or FAIL.
2640 */
2641 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002642eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 /*
2645 * Get the first variable.
2646 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002647 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 return FAIL;
2649
2650 /*
2651 * Repeat computing, until no '+', '-' or '.' is following.
2652 */
2653 for (;;)
2654 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002655 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002656 int getnext;
2657 char_u *p;
2658 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002659 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002660 int concat;
2661 typval_T var2;
2662
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002663 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002664 p = eval_next_non_blank(*arg, evalarg, &getnext);
2665 op = *p;
2666 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002667 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002669
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002670 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002671 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002672 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002673 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002674 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002675 {
2676 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2677 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002678 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002679 clear_tv(rettv);
2680 return FAIL;
2681 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002682 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002683 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002684 if ((op != '+' || (rettv->v_type != VAR_LIST
2685 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002686#ifdef FEAT_FLOAT
2687 && (op == '.' || rettv->v_type != VAR_FLOAT)
2688#endif
2689 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002690 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002691 // For "list + ...", an illegal use of the first operand as
2692 // a number cannot be determined before evaluating the 2nd
2693 // operand: if this is also a list, all is ok.
2694 // For "something . ...", "something - ..." or "non-list + ...",
2695 // we know that the first operand needs to be a string or number
2696 // without evaluating the 2nd operand. So check before to avoid
2697 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002698 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002699 {
2700 clear_tv(rettv);
2701 return FAIL;
2702 }
2703 }
2704
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 /*
2706 * Get the second variable.
2707 */
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002708 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[oplen]))
2709 {
2710 error_white_both(p, oplen);
2711 clear_tv(rettv);
2712 return FAIL;
2713 }
2714 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002715 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002717 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 return FAIL;
2719 }
2720
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002721 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 {
2723 /*
2724 * Compute the result.
2725 */
2726 if (op == '.')
2727 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002728 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2729 char_u *s1 = tv_get_string_buf(rettv, buf1);
2730 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2731
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002732 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002733 {
2734 clear_tv(rettv);
2735 clear_tv(&var2);
2736 return FAIL;
2737 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002738 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002739 clear_tv(rettv);
2740 rettv->v_type = VAR_STRING;
2741 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002743 else if (op == '+' && rettv->v_type == VAR_BLOB
2744 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002746 else if (op == '+' && rettv->v_type == VAR_LIST
2747 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002748 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002750 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 else
2753 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002754 int error = FALSE;
2755 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002756#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002757 float_T f1 = 0, f2 = 0;
2758
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002759 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002761 f1 = rettv->vval.v_float;
2762 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002763 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002764 else
2765#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002766 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002767 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002768 if (error)
2769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002770 // This can only happen for "list + non-list". For
2771 // "non-list + ..." or "something - ...", we returned
2772 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002773 clear_tv(rettv);
2774 return FAIL;
2775 }
2776#ifdef FEAT_FLOAT
2777 if (var2.v_type == VAR_FLOAT)
2778 f1 = n1;
2779#endif
2780 }
2781#ifdef FEAT_FLOAT
2782 if (var2.v_type == VAR_FLOAT)
2783 {
2784 f2 = var2.vval.v_float;
2785 n2 = 0;
2786 }
2787 else
2788#endif
2789 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002790 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002791 if (error)
2792 {
2793 clear_tv(rettv);
2794 clear_tv(&var2);
2795 return FAIL;
2796 }
2797#ifdef FEAT_FLOAT
2798 if (rettv->v_type == VAR_FLOAT)
2799 f2 = n2;
2800#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002801 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002802 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002803
2804#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002805 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002806 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2807 {
2808 if (op == '+')
2809 f1 = f1 + f2;
2810 else
2811 f1 = f1 - f2;
2812 rettv->v_type = VAR_FLOAT;
2813 rettv->vval.v_float = f1;
2814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002816#endif
2817 {
2818 if (op == '+')
2819 n1 = n1 + n2;
2820 else
2821 n1 = n1 - n2;
2822 rettv->v_type = VAR_NUMBER;
2823 rettv->vval.v_number = n1;
2824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002826 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
2828 }
2829 return OK;
2830}
2831
2832/*
2833 * Handle fifth level expression:
2834 * * number multiplication
2835 * / number division
2836 * % number modulo
2837 *
2838 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002839 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 *
2841 * Return OK or FAIL.
2842 */
2843 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002844eval6(
2845 char_u **arg,
2846 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002847 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002848 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002850#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002851 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
2854 /*
2855 * Get the first variable.
2856 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002857 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 return FAIL;
2859
2860 /*
2861 * Repeat computing, until no '*', '/' or '%' is following.
2862 */
2863 for (;;)
2864 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002865 int evaluate;
2866 int getnext;
2867 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002868 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002869 int op;
2870 varnumber_T n1, n2;
2871#ifdef FEAT_FLOAT
2872 float_T f1, f2;
2873#endif
2874 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002875
Bram Moolenaar9d489562020-07-30 20:08:50 +02002876 p = eval_next_non_blank(*arg, evalarg, &getnext);
2877 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002878 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002880
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002881 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002882 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002883 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002884 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002885 {
2886 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2887 {
2888 error_white_both(p, 1);
2889 clear_tv(rettv);
2890 return FAIL;
2891 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002892 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002895#ifdef FEAT_FLOAT
2896 f1 = 0;
2897 f2 = 0;
2898#endif
2899 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002900 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002902#ifdef FEAT_FLOAT
2903 if (rettv->v_type == VAR_FLOAT)
2904 {
2905 f1 = rettv->vval.v_float;
2906 use_float = TRUE;
2907 n1 = 0;
2908 }
2909 else
2910#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002911 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002913 if (error)
2914 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
2916 else
2917 n1 = 0;
2918
2919 /*
2920 * Get the second variable.
2921 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002922 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2923 {
2924 error_white_both(p, 1);
2925 clear_tv(rettv);
2926 return FAIL;
2927 }
2928 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002929 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 return FAIL;
2931
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002932 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002934#ifdef FEAT_FLOAT
2935 if (var2.v_type == VAR_FLOAT)
2936 {
2937 if (!use_float)
2938 {
2939 f1 = n1;
2940 use_float = TRUE;
2941 }
2942 f2 = var2.vval.v_float;
2943 n2 = 0;
2944 }
2945 else
2946#endif
2947 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002948 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002949 clear_tv(&var2);
2950 if (error)
2951 return FAIL;
2952#ifdef FEAT_FLOAT
2953 if (use_float)
2954 f2 = n2;
2955#endif
2956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 /*
2959 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002960 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002962#ifdef FEAT_FLOAT
2963 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002965 if (op == '*')
2966 f1 = f1 * f2;
2967 else if (op == '/')
2968 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002969# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002970 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002971 if (f2 == 0.0)
2972 {
2973 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002974 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002975 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002976 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002977 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002978 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002979 }
2980 else
2981 f1 = f1 / f2;
2982# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002983 // We rely on the floating point library to handle divide
2984 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002986# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002989 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002991 return FAIL;
2992 }
2993 rettv->v_type = VAR_FLOAT;
2994 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002999 if (op == '*')
3000 n1 = n1 * n2;
3001 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003002 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003004 n1 = num_modulus(n1, n2);
3005
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003006 rettv->v_type = VAR_NUMBER;
3007 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 }
3010 }
3011
3012 return OK;
3013}
3014
3015/*
3016 * Handle sixth level expression:
3017 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003018 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003019 * "string" string constant
3020 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 * &option-name option value
3022 * @r register contents
3023 * identifier variable value
3024 * function() function call
3025 * $VAR environment variable
3026 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003027 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003029 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003030 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 *
3032 * Also handle:
3033 * ! in front logical NOT
3034 * - in front unary minus
3035 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003036 * trailing [] subscript in String or List
3037 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003038 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 *
3040 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003041 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 *
3043 * Return OK or FAIL.
3044 */
3045 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003046eval7(
3047 char_u **arg,
3048 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003049 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003052 int evaluate = evalarg != NULL
3053 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 int len;
3055 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 char_u *start_leader, *end_leader;
3057 int ret = OK;
3058 char_u *alias;
3059
3060 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003062 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065
3066 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003067 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 */
3069 start_leader = *arg;
3070 while (**arg == '!' || **arg == '-' || **arg == '+')
3071 *arg = skipwhite(*arg + 1);
3072 end_leader = *arg;
3073
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003074 if (**arg == '.' && (!isdigit(*(*arg + 1))
3075#ifdef FEAT_FLOAT
3076 || current_sctx.sc_version < 2
3077#endif
3078 ))
3079 {
3080 semsg(_(e_invexpr2), *arg);
3081 ++*arg;
3082 return FAIL;
3083 }
3084
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 switch (**arg)
3086 {
3087 /*
3088 * Number constant.
3089 */
3090 case '0':
3091 case '1':
3092 case '2':
3093 case '3':
3094 case '4':
3095 case '5':
3096 case '6':
3097 case '7':
3098 case '8':
3099 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003100 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003101
3102 // Apply prefixed "-" and "+" now. Matters especially when
3103 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003104 if (ret == OK && evaluate && end_leader > start_leader
3105 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003106 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 break;
3108
3109 /*
3110 * String constant: "string".
3111 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003112 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 break;
3114
3115 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003116 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003118 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003119 break;
3120
3121 /*
3122 * List: [expr, expr]
3123 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003124 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 break;
3126
3127 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003128 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003129 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003130 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003131 {
3132 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003133 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003134 }
3135 else
3136 ret = NOTDONE;
3137 break;
3138
3139 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003140 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003141 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003142 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003143 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003144 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003145 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003146 break;
3147
3148 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003149 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003151 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 break;
3153
3154 /*
3155 * Environment variable: $VAR.
3156 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003157 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 break;
3159
3160 /*
3161 * Register contents: @r.
3162 */
3163 case '@': ++*arg;
3164 if (evaluate)
3165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003166 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003167 rettv->vval.v_string = get_reg_contents(**arg,
3168 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170 if (**arg != NUL)
3171 ++*arg;
3172 break;
3173
3174 /*
3175 * nested expression: (expression).
3176 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003177 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003178 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003179 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003180
Bram Moolenaar9215f012020-06-27 21:18:00 +02003181 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003182 if (**arg == ')')
3183 ++*arg;
3184 else if (ret == OK)
3185 {
3186 emsg(_(e_missing_close));
3187 clear_tv(rettv);
3188 ret = FAIL;
3189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 }
3191 break;
3192
Bram Moolenaar8c711452005-01-14 21:53:12 +00003193 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 break;
3195 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003196
3197 if (ret == NOTDONE)
3198 {
3199 /*
3200 * Must be a variable or function name.
3201 * Can also be a curly-braces kind of name: {expr}.
3202 */
3203 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003204 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003205 if (alias != NULL)
3206 s = alias;
3207
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003208 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003209 ret = FAIL;
3210 else
3211 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003212 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3213
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003214 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3215 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003216 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003217 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003218 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003219 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003220 else if (flags & EVAL_CONSTANT)
3221 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003222 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003223 {
3224 // get the value of "true", "false" or a variable
3225 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3226 {
3227 rettv->v_type = VAR_BOOL;
3228 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003229 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003230 }
3231 else if (len == 5 && in_vim9script()
3232 && STRNCMP(s, "false", 4) == 0)
3233 {
3234 rettv->v_type = VAR_BOOL;
3235 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003236 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003237 }
3238 else
3239 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3240 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003241 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003242 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003243 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003244 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003245 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003246 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003247 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003248 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003249 }
3250
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003251 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3252 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003253 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003254 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
3256 /*
3257 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3258 */
3259 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003260 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003261 return ret;
3262}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003263
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003264/*
3265 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003266 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003267 * Adjusts "end_leaderp" until it is at "start_leader".
3268 */
3269 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003270eval7_leader(
3271 typval_T *rettv,
3272 int numeric_only,
3273 char_u *start_leader,
3274 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003275{
3276 char_u *end_leader = *end_leaderp;
3277 int ret = OK;
3278 int error = FALSE;
3279 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003280 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003281#ifdef FEAT_FLOAT
3282 float_T f = 0.0;
3283
3284 if (rettv->v_type == VAR_FLOAT)
3285 f = rettv->vval.v_float;
3286 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003287#endif
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003288 if (in_vim9script() && end_leader[-1] == '!')
3289 val = tv2bool(rettv);
3290 else
3291 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003292 if (error)
3293 {
3294 clear_tv(rettv);
3295 ret = FAIL;
3296 }
3297 else
3298 {
3299 while (end_leader > start_leader)
3300 {
3301 --end_leader;
3302 if (*end_leader == '!')
3303 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003304 if (numeric_only)
3305 {
3306 ++end_leader;
3307 break;
3308 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003309#ifdef FEAT_FLOAT
3310 if (rettv->v_type == VAR_FLOAT)
3311 f = !f;
3312 else
3313#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003314 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003315 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003316 type = VAR_BOOL;
3317 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003318 }
3319 else if (*end_leader == '-')
3320 {
3321#ifdef FEAT_FLOAT
3322 if (rettv->v_type == VAR_FLOAT)
3323 f = -f;
3324 else
3325#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003326 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003327 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003328 type = VAR_NUMBER;
3329 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003330 }
3331 }
3332#ifdef FEAT_FLOAT
3333 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003335 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003336 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003338 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003339#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003340 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003341 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003342 if (in_vim9script())
3343 rettv->v_type = type;
3344 else
3345 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003346 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003349 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 return ret;
3351}
3352
3353/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003354 * Call the function referred to in "rettv".
3355 */
3356 static int
3357call_func_rettv(
3358 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003359 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003360 typval_T *rettv,
3361 int evaluate,
3362 dict_T *selfdict,
3363 typval_T *basetv)
3364{
3365 partial_T *pt = NULL;
3366 funcexe_T funcexe;
3367 typval_T functv;
3368 char_u *s;
3369 int ret;
3370
3371 // need to copy the funcref so that we can clear rettv
3372 if (evaluate)
3373 {
3374 functv = *rettv;
3375 rettv->v_type = VAR_UNKNOWN;
3376
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003377 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003378 if (functv.v_type == VAR_PARTIAL)
3379 {
3380 pt = functv.vval.v_partial;
3381 s = partial_name(pt);
3382 }
3383 else
3384 s = functv.vval.v_string;
3385 }
3386 else
3387 s = (char_u *)"";
3388
Bram Moolenaara80faa82020-04-12 19:37:17 +02003389 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003390 funcexe.firstline = curwin->w_cursor.lnum;
3391 funcexe.lastline = curwin->w_cursor.lnum;
3392 funcexe.evaluate = evaluate;
3393 funcexe.partial = pt;
3394 funcexe.selfdict = selfdict;
3395 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003396 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003397
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003398 // Clear the funcref afterwards, so that deleting it while
3399 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003400 if (evaluate)
3401 clear_tv(&functv);
3402
3403 return ret;
3404}
3405
3406/*
3407 * Evaluate "->method()".
3408 * "*arg" points to the '-'.
3409 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3410 */
3411 static int
3412eval_lambda(
3413 char_u **arg,
3414 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003415 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003416 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003417{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003418 int evaluate = evalarg != NULL
3419 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003420 typval_T base = *rettv;
3421 int ret;
3422
3423 // Skip over the ->.
3424 *arg += 2;
3425 rettv->v_type = VAR_UNKNOWN;
3426
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003427 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003428 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003429 return FAIL;
3430 else if (**arg != '(')
3431 {
3432 if (verbose)
3433 {
3434 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003435 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003436 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003438 }
3439 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003440 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003441 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003442 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003443 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003444
3445 // Clear the funcref afterwards, so that deleting it while
3446 // evaluating the arguments is possible (see test55).
3447 if (evaluate)
3448 clear_tv(&base);
3449
3450 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003451}
3452
3453/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003454 * Evaluate "->method()".
3455 * "*arg" points to the '-'.
3456 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3457 */
3458 static int
3459eval_method(
3460 char_u **arg,
3461 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003462 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003463 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003464{
3465 char_u *name;
3466 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003467 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003468 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003469 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003470 int evaluate = evalarg != NULL
3471 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003472
3473 // Skip over the ->.
3474 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003475 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003476
Bram Moolenaarac92e252019-08-03 21:58:38 +02003477 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003478 len = get_name_len(arg, &alias, evaluate, TRUE);
3479 if (alias != NULL)
3480 name = alias;
3481
3482 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003483 {
3484 if (verbose)
3485 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003486 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003487 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003488 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003489 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003490 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003491 if (**arg != '(')
3492 {
3493 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003495 ret = FAIL;
3496 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003497 else if (VIM_ISWHITE((*arg)[-1]))
3498 {
3499 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003500 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003501 ret = FAIL;
3502 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003503 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003504 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003505 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003506 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003507
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003508 // Clear the funcref afterwards, so that deleting it while
3509 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003510 if (evaluate)
3511 clear_tv(&base);
3512
Bram Moolenaarac92e252019-08-03 21:58:38 +02003513 return ret;
3514}
3515
3516/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003517 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3518 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003519 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3520 */
3521 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003522eval_index(
3523 char_u **arg,
3524 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003525 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003526 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003527{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003528 int evaluate = evalarg != NULL
3529 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003530 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003531 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003532 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003533 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003534 long len = -1;
3535 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003536 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003538
Bram Moolenaara03f2332016-02-06 18:09:59 +01003539 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003540 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003541 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003542 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003543 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003544 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003545 return FAIL;
3546 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003547#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003548 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003549 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003550 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003551#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003552 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003553 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003554 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003555 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003556 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003557 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003558 return FAIL;
3559 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003560 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003562 if (evaluate)
3563 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003564 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003565
3566 case VAR_STRING:
3567 case VAR_NUMBER:
3568 case VAR_LIST:
3569 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003570 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003571 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003572 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003573
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003574 init_tv(&var1);
3575 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003576 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003577 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003578 /*
3579 * dict.name
3580 */
3581 key = *arg + 1;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003582 for (len = 0; eval_isdictc(key[len]); ++len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003583 ;
3584 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003585 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003586 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003587 }
3588 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003589 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 /*
3591 * something[idx]
3592 *
3593 * Get the (first) variable from inside the [].
3594 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003595 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 if (**arg == ':')
3597 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003598 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003600 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003601 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003602 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003603 clear_tv(&var1);
3604 return FAIL;
3605 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606
3607 /*
3608 * Get the second variable from inside the [:].
3609 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003610 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 if (**arg == ':')
3612 {
3613 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003614 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 if (**arg == ']')
3616 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003617 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003619 if (!empty1)
3620 clear_tv(&var1);
3621 return FAIL;
3622 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003623 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003624 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003625 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003626 if (!empty1)
3627 clear_tv(&var1);
3628 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003629 return FAIL;
3630 }
3631 }
3632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003633 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003634 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003635 if (**arg != ']')
3636 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003637 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003638 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003639 clear_tv(&var1);
3640 if (range)
3641 clear_tv(&var2);
3642 return FAIL;
3643 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003644 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003645 }
3646
3647 if (evaluate)
3648 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003649 n1 = 0;
3650 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003651 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003652 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003653 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003654 }
3655 if (range)
3656 {
3657 if (empty2)
3658 n2 = -1;
3659 else
3660 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003661 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003662 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003663 }
3664 }
3665
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003666 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003667 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003668 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003669 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003671 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003672 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003673 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003674 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003675 case VAR_SPECIAL:
3676 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003677 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003678 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003679
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003680 case VAR_NUMBER:
3681 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003682 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003683 len = (long)STRLEN(s);
3684 if (range)
3685 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003686 // The resulting variable is a substring. If the indexes
3687 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003688 if (n1 < 0)
3689 {
3690 n1 = len + n1;
3691 if (n1 < 0)
3692 n1 = 0;
3693 }
3694 if (n2 < 0)
3695 n2 = len + n2;
3696 else if (n2 >= len)
3697 n2 = len;
3698 if (n1 >= len || n2 < 0 || n1 > n2)
3699 s = NULL;
3700 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003701 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003702 }
3703 else
3704 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003705 // The resulting variable is a string of a single
3706 // character. If the index is too big or negative the
3707 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003708 if (n1 >= len || n1 < 0)
3709 s = NULL;
3710 else
3711 s = vim_strnsave(s + n1, 1);
3712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003713 clear_tv(rettv);
3714 rettv->v_type = VAR_STRING;
3715 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003716 break;
3717
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003718 case VAR_BLOB:
3719 len = blob_len(rettv->vval.v_blob);
3720 if (range)
3721 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003722 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003723 // are out of range the result is empty.
3724 if (n1 < 0)
3725 {
3726 n1 = len + n1;
3727 if (n1 < 0)
3728 n1 = 0;
3729 }
3730 if (n2 < 0)
3731 n2 = len + n2;
3732 else if (n2 >= len)
3733 n2 = len - 1;
3734 if (n1 >= len || n2 < 0 || n1 > n2)
3735 {
3736 clear_tv(rettv);
3737 rettv->v_type = VAR_BLOB;
3738 rettv->vval.v_blob = NULL;
3739 }
3740 else
3741 {
3742 blob_T *blob = blob_alloc();
3743
3744 if (blob != NULL)
3745 {
3746 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3747 {
3748 blob_free(blob);
3749 return FAIL;
3750 }
3751 blob->bv_ga.ga_len = n2 - n1 + 1;
3752 for (i = n1; i <= n2; i++)
3753 blob_set(blob, i - n1,
3754 blob_get(rettv->vval.v_blob, i));
3755
3756 clear_tv(rettv);
3757 rettv_blob_set(rettv, blob);
3758 }
3759 }
3760 }
3761 else
3762 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003763 // The resulting variable is a byte value.
3764 // If the index is too big or negative that is an error.
3765 if (n1 < 0)
3766 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003767 if (n1 < len && n1 >= 0)
3768 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003769 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003770
3771 clear_tv(rettv);
3772 rettv->v_type = VAR_NUMBER;
3773 rettv->vval.v_number = v;
3774 }
3775 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003776 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003777 }
3778 break;
3779
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003780 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003782 if (n1 < 0)
3783 n1 = len + n1;
3784 if (!empty1 && (n1 < 0 || n1 >= len))
3785 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003786 // For a range we allow invalid values and return an empty
3787 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003788 if (!range)
3789 {
3790 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003791 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003792 return FAIL;
3793 }
3794 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003795 }
3796 if (range)
3797 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003798 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003799
3800 if (n2 < 0)
3801 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003802 else if (n2 >= len)
3803 n2 = len - 1;
3804 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003805 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003806 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003807 if (l == NULL)
3808 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003809 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003810 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003811 }
3812 else
3813 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003814 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003815 clear_tv(rettv);
3816 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003817 }
3818 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003819
3820 case VAR_DICT:
3821 if (range)
3822 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003823 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003824 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003825 if (len == -1)
3826 clear_tv(&var1);
3827 return FAIL;
3828 }
3829 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003830 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003831
3832 if (len == -1)
3833 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003834 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003835 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003836 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003837 clear_tv(&var1);
3838 return FAIL;
3839 }
3840 }
3841
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003842 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003843
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003844 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003845 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003846 if (len == -1)
3847 clear_tv(&var1);
3848 if (item == NULL)
3849 return FAIL;
3850
3851 copy_tv(&item->di_tv, &var1);
3852 clear_tv(rettv);
3853 *rettv = var1;
3854 }
3855 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003856 }
3857 }
3858
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003859 return OK;
3860}
3861
3862/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003863 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003864 */
3865 char_u *
3866partial_name(partial_T *pt)
3867{
3868 if (pt->pt_name != NULL)
3869 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003870 if (pt->pt_func != NULL)
3871 return pt->pt_func->uf_name;
3872 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003873}
3874
Bram Moolenaarddecc252016-04-06 22:59:37 +02003875 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003876partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003877{
3878 int i;
3879
3880 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003881 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003882 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003883 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003884 if (pt->pt_name != NULL)
3885 {
3886 func_unref(pt->pt_name);
3887 vim_free(pt->pt_name);
3888 }
3889 else
3890 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003891
3892 if (pt->pt_funcstack != NULL)
3893 {
3894 // Decrease the reference count for the context of a closure. If down
3895 // to zero free it and clear the variables on the stack.
3896 if (--pt->pt_funcstack->fs_refcount == 0)
3897 {
3898 garray_T *gap = &pt->pt_funcstack->fs_ga;
3899 typval_T *stack = gap->ga_data;
3900
3901 for (i = 0; i < gap->ga_len; ++i)
3902 clear_tv(stack + i);
3903 ga_clear(gap);
3904 vim_free(pt->pt_funcstack);
3905 }
3906 pt->pt_funcstack = NULL;
3907 }
3908
Bram Moolenaarddecc252016-04-06 22:59:37 +02003909 vim_free(pt);
3910}
3911
3912/*
3913 * Unreference a closure: decrement the reference count and free it when it
3914 * becomes zero.
3915 */
3916 void
3917partial_unref(partial_T *pt)
3918{
3919 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003920 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003921}
3922
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003923/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003924 * Return the next (unique) copy ID.
3925 * Used for serializing nested structures.
3926 */
3927 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003928get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003929{
3930 current_copyID += COPYID_INC;
3931 return current_copyID;
3932}
3933
3934/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003935 * Garbage collection for lists and dictionaries.
3936 *
3937 * We use reference counts to be able to free most items right away when they
3938 * are no longer used. But for composite items it's possible that it becomes
3939 * unused while the reference count is > 0: When there is a recursive
3940 * reference. Example:
3941 * :let l = [1, 2, 3]
3942 * :let d = {9: l}
3943 * :let l[1] = d
3944 *
3945 * Since this is quite unusual we handle this with garbage collection: every
3946 * once in a while find out which lists and dicts are not referenced from any
3947 * variable.
3948 *
3949 * Here is a good reference text about garbage collection (refers to Python
3950 * but it applies to all reference-counting mechanisms):
3951 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003952 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003953
3954/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003955 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003956 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003957 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003958 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003959 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003960garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003961{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003962 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003963 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003964 buf_T *buf;
3965 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003966 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003967 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003968
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003969 if (!testing)
3970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003971 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003972 want_garbage_collect = FALSE;
3973 may_garbage_collect = FALSE;
3974 garbage_collect_at_exit = FALSE;
3975 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003976
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003977 // The execution stack can grow big, limit the size.
3978 if (exestack.ga_maxlen - exestack.ga_len > 500)
3979 {
3980 size_t new_len;
3981 char_u *pp;
3982 int n;
3983
3984 // Keep 150% of the current size, with a minimum of the growth size.
3985 n = exestack.ga_len / 2;
3986 if (n < exestack.ga_growsize)
3987 n = exestack.ga_growsize;
3988
3989 // Don't make it bigger though.
3990 if (exestack.ga_len + n < exestack.ga_maxlen)
3991 {
3992 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3993 pp = vim_realloc(exestack.ga_data, new_len);
3994 if (pp == NULL)
3995 return FAIL;
3996 exestack.ga_maxlen = exestack.ga_len + n;
3997 exestack.ga_data = pp;
3998 }
3999 }
4000
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004001 // We advance by two because we add one for items referenced through
4002 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004003 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004004
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004005 /*
4006 * 1. Go through all accessible variables and mark all lists and dicts
4007 * with copyID.
4008 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004010 // Don't free variables in the previous_funccal list unless they are only
4011 // referenced through previous_funccal. This must be first, because if
4012 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004013 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004015 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004016 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004017
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004018 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004019 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004020 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4021 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004023 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004024 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004025 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4026 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004027 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004028 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4029 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004030#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004031 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004032 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4033 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004034 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004035 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004036 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4037 NULL, NULL);
4038#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004040 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004041 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004042 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4043 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004044 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004045 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004047 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004049
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004050 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004051 abort = abort || set_ref_in_functions(copyID);
4052
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004053 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004054 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004055
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004056 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004057 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004058
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004059 // callbacks in buffers
4060 abort = abort || set_ref_in_buffers(copyID);
4061
Bram Moolenaar1dced572012-04-05 16:54:08 +02004062#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004063 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004064#endif
4065
Bram Moolenaardb913952012-06-29 12:54:53 +02004066#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004067 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004068#endif
4069
4070#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004071 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004072#endif
4073
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004074#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004075 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004076 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004077#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004078#ifdef FEAT_NETBEANS_INTG
4079 abort = abort || set_ref_in_nb_channel(copyID);
4080#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004081
Bram Moolenaare3188e22016-05-31 21:13:04 +02004082#ifdef FEAT_TIMERS
4083 abort = abort || set_ref_in_timer(copyID);
4084#endif
4085
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004086#ifdef FEAT_QUICKFIX
4087 abort = abort || set_ref_in_quickfix(copyID);
4088#endif
4089
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004090#ifdef FEAT_TERMINAL
4091 abort = abort || set_ref_in_term(copyID);
4092#endif
4093
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004094#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004095 abort = abort || set_ref_in_popups(copyID);
4096#endif
4097
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004098 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004099 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004100 /*
4101 * 2. Free lists and dictionaries that are not referenced.
4102 */
4103 did_free = free_unref_items(copyID);
4104
4105 /*
4106 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004107 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004108 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004109 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004110 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004111 else if (p_verbose > 0)
4112 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004113 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004114 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004115
4116 return did_free;
4117}
4118
4119/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004120 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004121 */
4122 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004123free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004124{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004125 int did_free = FALSE;
4126
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004127 // Let all "free" functions know that we are here. This means no
4128 // dictionaries, lists, channels or jobs are to be freed, because we will
4129 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004130 in_free_unref_items = TRUE;
4131
4132 /*
4133 * PASS 1: free the contents of the items. We don't free the items
4134 * themselves yet, so that it is possible to decrement refcount counters
4135 */
4136
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004137 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004138 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004139
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004140 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004141 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004142
4143#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004144 // Go through the list of jobs and free items without the copyID. This
4145 // must happen before doing channels, because jobs refer to channels, but
4146 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004147 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4148
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004149 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004150 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4151#endif
4152
4153 /*
4154 * PASS 2: free the items themselves.
4155 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004156 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004157 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004158
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004159#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004160 // Go through the list of jobs and free items without the copyID. This
4161 // must happen before doing channels, because jobs refer to channels, but
4162 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004163 free_unused_jobs(copyID, COPYID_MASK);
4164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004165 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004166 free_unused_channels(copyID, COPYID_MASK);
4167#endif
4168
4169 in_free_unref_items = FALSE;
4170
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004171 return did_free;
4172}
4173
4174/*
4175 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004176 * "list_stack" is used to add lists to be marked. Can be NULL.
4177 *
4178 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004179 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004180 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004181set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004182{
4183 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004184 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004185 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004186 hashtab_T *cur_ht;
4187 ht_stack_T *ht_stack = NULL;
4188 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004189
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004190 cur_ht = ht;
4191 for (;;)
4192 {
4193 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004194 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004195 // Mark each item in the hashtab. If the item contains a hashtab
4196 // it is added to ht_stack, if it contains a list it is added to
4197 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004198 todo = (int)cur_ht->ht_used;
4199 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4200 if (!HASHITEM_EMPTY(hi))
4201 {
4202 --todo;
4203 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4204 &ht_stack, list_stack);
4205 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004206 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004207
4208 if (ht_stack == NULL)
4209 break;
4210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004211 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004212 cur_ht = ht_stack->ht;
4213 tempitem = ht_stack;
4214 ht_stack = ht_stack->prev;
4215 free(tempitem);
4216 }
4217
4218 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004219}
4220
4221/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004222 * Mark a dict and its items with "copyID".
4223 * Returns TRUE if setting references failed somehow.
4224 */
4225 int
4226set_ref_in_dict(dict_T *d, int copyID)
4227{
4228 if (d != NULL && d->dv_copyID != copyID)
4229 {
4230 d->dv_copyID = copyID;
4231 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4232 }
4233 return FALSE;
4234}
4235
4236/*
4237 * Mark a list and its items with "copyID".
4238 * Returns TRUE if setting references failed somehow.
4239 */
4240 int
4241set_ref_in_list(list_T *ll, int copyID)
4242{
4243 if (ll != NULL && ll->lv_copyID != copyID)
4244 {
4245 ll->lv_copyID = copyID;
4246 return set_ref_in_list_items(ll, copyID, NULL);
4247 }
4248 return FALSE;
4249}
4250
4251/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004252 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004253 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4254 *
4255 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004256 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004257 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004258set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004259{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004260 listitem_T *li;
4261 int abort = FALSE;
4262 list_T *cur_l;
4263 list_stack_T *list_stack = NULL;
4264 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004265
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004266 cur_l = l;
4267 for (;;)
4268 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004269 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004270 // Mark each item in the list. If the item contains a hashtab
4271 // it is added to ht_stack, if it contains a list it is added to
4272 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004273 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4274 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4275 ht_stack, &list_stack);
4276 if (list_stack == NULL)
4277 break;
4278
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004279 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004280 cur_l = list_stack->list;
4281 tempitem = list_stack;
4282 list_stack = list_stack->prev;
4283 free(tempitem);
4284 }
4285
4286 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004287}
4288
4289/*
4290 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004291 * "list_stack" is used to add lists to be marked. Can be NULL.
4292 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4293 *
4294 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004295 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004296 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004297set_ref_in_item(
4298 typval_T *tv,
4299 int copyID,
4300 ht_stack_T **ht_stack,
4301 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004302{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004303 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004304
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004305 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004306 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004307 dict_T *dd = tv->vval.v_dict;
4308
Bram Moolenaara03f2332016-02-06 18:09:59 +01004309 if (dd != NULL && dd->dv_copyID != copyID)
4310 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004311 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004312 dd->dv_copyID = copyID;
4313 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004314 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004315 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4316 }
4317 else
4318 {
4319 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4320 if (newitem == NULL)
4321 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004322 else
4323 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004324 newitem->ht = &dd->dv_hashtab;
4325 newitem->prev = *ht_stack;
4326 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004327 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004328 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004329 }
4330 }
4331 else if (tv->v_type == VAR_LIST)
4332 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004333 list_T *ll = tv->vval.v_list;
4334
Bram Moolenaara03f2332016-02-06 18:09:59 +01004335 if (ll != NULL && ll->lv_copyID != copyID)
4336 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004337 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004338 ll->lv_copyID = copyID;
4339 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004340 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004341 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004342 }
4343 else
4344 {
4345 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004346 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004347 if (newitem == NULL)
4348 abort = TRUE;
4349 else
4350 {
4351 newitem->list = ll;
4352 newitem->prev = *list_stack;
4353 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004354 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004355 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004356 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004357 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004358 else if (tv->v_type == VAR_FUNC)
4359 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004360 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004361 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004362 else if (tv->v_type == VAR_PARTIAL)
4363 {
4364 partial_T *pt = tv->vval.v_partial;
4365 int i;
4366
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004367 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004368 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004369 // Didn't see this partial yet.
4370 pt->pt_copyID = copyID;
4371
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004372 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004373
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004374 if (pt->pt_dict != NULL)
4375 {
4376 typval_T dtv;
4377
4378 dtv.v_type = VAR_DICT;
4379 dtv.vval.v_dict = pt->pt_dict;
4380 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4381 }
4382
4383 for (i = 0; i < pt->pt_argc; ++i)
4384 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4385 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004386 if (pt->pt_funcstack != NULL)
4387 {
4388 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4389
4390 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4391 abort = abort || set_ref_in_item(stack + i, copyID,
4392 ht_stack, list_stack);
4393 }
4394
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004395 }
4396 }
4397#ifdef FEAT_JOB_CHANNEL
4398 else if (tv->v_type == VAR_JOB)
4399 {
4400 job_T *job = tv->vval.v_job;
4401 typval_T dtv;
4402
4403 if (job != NULL && job->jv_copyID != copyID)
4404 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004405 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004406 if (job->jv_channel != NULL)
4407 {
4408 dtv.v_type = VAR_CHANNEL;
4409 dtv.vval.v_channel = job->jv_channel;
4410 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4411 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004412 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004413 {
4414 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004415 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004416 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4417 }
4418 }
4419 }
4420 else if (tv->v_type == VAR_CHANNEL)
4421 {
4422 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004423 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004424 typval_T dtv;
4425 jsonq_T *jq;
4426 cbq_T *cq;
4427
4428 if (ch != NULL && ch->ch_copyID != copyID)
4429 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004430 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004431 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004432 {
4433 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4434 jq = jq->jq_next)
4435 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4436 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4437 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004438 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004439 {
4440 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004441 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004442 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4443 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004444 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004445 {
4446 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004447 dtv.vval.v_partial =
4448 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004449 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4450 }
4451 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004452 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004453 {
4454 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004455 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004456 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4457 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004458 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004459 {
4460 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004461 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004462 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4463 }
4464 }
4465 }
4466#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004467 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004468}
4469
Bram Moolenaar8c711452005-01-14 21:53:12 +00004470/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004471 * Return a string with the string representation of a variable.
4472 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004473 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004474 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004475 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4476 * stings as "string()", otherwise does not put quotes around strings, as
4477 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004478 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4479 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004480 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004481 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004482 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004483echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004484 typval_T *tv,
4485 char_u **tofree,
4486 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004487 int copyID,
4488 int echo_style,
4489 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004490 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004491{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004492 static int recurse = 0;
4493 char_u *r = NULL;
4494
Bram Moolenaar33570922005-01-25 22:26:29 +00004495 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004496 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004497 if (!did_echo_string_emsg)
4498 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004499 // Only give this message once for a recursive call to avoid
4500 // flooding the user with errors. And stop iterating over lists
4501 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004502 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004503 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004504 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004505 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004506 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004507 }
4508 ++recurse;
4509
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004510 switch (tv->v_type)
4511 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004512 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004513 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004514 {
4515 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004516 r = tv->vval.v_string;
4517 if (r == NULL)
4518 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004519 }
4520 else
4521 {
4522 *tofree = string_quote(tv->vval.v_string, FALSE);
4523 r = *tofree;
4524 }
4525 break;
4526
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004527 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004528 if (echo_style)
4529 {
4530 *tofree = NULL;
4531 r = tv->vval.v_string;
4532 }
4533 else
4534 {
4535 *tofree = string_quote(tv->vval.v_string, TRUE);
4536 r = *tofree;
4537 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004538 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004539
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004540 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004541 {
4542 partial_T *pt = tv->vval.v_partial;
4543 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004544 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004545 garray_T ga;
4546 int i;
4547 char_u *tf;
4548
4549 ga_init2(&ga, 1, 100);
4550 ga_concat(&ga, (char_u *)"function(");
4551 if (fname != NULL)
4552 {
4553 ga_concat(&ga, fname);
4554 vim_free(fname);
4555 }
4556 if (pt != NULL && pt->pt_argc > 0)
4557 {
4558 ga_concat(&ga, (char_u *)", [");
4559 for (i = 0; i < pt->pt_argc; ++i)
4560 {
4561 if (i > 0)
4562 ga_concat(&ga, (char_u *)", ");
4563 ga_concat(&ga,
4564 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4565 vim_free(tf);
4566 }
4567 ga_concat(&ga, (char_u *)"]");
4568 }
4569 if (pt != NULL && pt->pt_dict != NULL)
4570 {
4571 typval_T dtv;
4572
4573 ga_concat(&ga, (char_u *)", ");
4574 dtv.v_type = VAR_DICT;
4575 dtv.vval.v_dict = pt->pt_dict;
4576 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4577 vim_free(tf);
4578 }
4579 ga_concat(&ga, (char_u *)")");
4580
4581 *tofree = ga.ga_data;
4582 r = *tofree;
4583 break;
4584 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004585
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004586 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004587 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004588 break;
4589
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004591 if (tv->vval.v_list == NULL)
4592 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004593 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004594 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004595 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004596 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004597 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4598 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004599 {
4600 *tofree = NULL;
4601 r = (char_u *)"[...]";
4602 }
4603 else
4604 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004605 int old_copyID = tv->vval.v_list->lv_copyID;
4606
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004607 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004608 *tofree = list2string(tv, copyID, restore_copyID);
4609 if (restore_copyID)
4610 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004611 r = *tofree;
4612 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004613 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004614
Bram Moolenaar8c711452005-01-14 21:53:12 +00004615 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004616 if (tv->vval.v_dict == NULL)
4617 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004618 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004619 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004620 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004621 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004622 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4623 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004624 {
4625 *tofree = NULL;
4626 r = (char_u *)"{...}";
4627 }
4628 else
4629 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004630 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004631
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004632 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004633 *tofree = dict2string(tv, copyID, restore_copyID);
4634 if (restore_copyID)
4635 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004636 r = *tofree;
4637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004638 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004639
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004641 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004642 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004644 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004645 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004646 break;
4647
Bram Moolenaar835dc632016-02-07 14:27:38 +01004648 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004649 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004650 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004651 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004652 if (composite_val)
4653 {
4654 *tofree = string_quote(r, FALSE);
4655 r = *tofree;
4656 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004658
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004659 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004660#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004661 *tofree = NULL;
4662 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4663 r = numbuf;
4664 break;
4665#endif
4666
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004667 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004668 case VAR_SPECIAL:
4669 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004670 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004671 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004672 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004673
Bram Moolenaar8502c702014-06-17 12:51:16 +02004674 if (--recurse == 0)
4675 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004676 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004677}
4678
4679/*
4680 * Return a string with the string representation of a variable.
4681 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4682 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004683 * Does not put quotes around strings, as ":echo" displays values.
4684 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4685 * May return NULL.
4686 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004687 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004688echo_string(
4689 typval_T *tv,
4690 char_u **tofree,
4691 char_u *numbuf,
4692 int copyID)
4693{
4694 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4695}
4696
4697/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004698 * Return string "str" in ' quotes, doubling ' characters.
4699 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004700 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004701 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004702 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004703string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004704{
Bram Moolenaar33570922005-01-25 22:26:29 +00004705 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004706 char_u *p, *r, *s;
4707
Bram Moolenaar33570922005-01-25 22:26:29 +00004708 len = (function ? 13 : 3);
4709 if (str != NULL)
4710 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004711 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004712 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004713 if (*p == '\'')
4714 ++len;
4715 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004716 s = r = alloc(len);
4717 if (r != NULL)
4718 {
4719 if (function)
4720 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004721 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004722 r += 10;
4723 }
4724 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004725 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004726 if (str != NULL)
4727 for (p = str; *p != NUL; )
4728 {
4729 if (*p == '\'')
4730 *r++ = '\'';
4731 MB_COPY_CHAR(p, r);
4732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004733 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004734 if (function)
4735 *r++ = ')';
4736 *r++ = NUL;
4737 }
4738 return s;
4739}
4740
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004741#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004742/*
4743 * Convert the string "text" to a floating point number.
4744 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4745 * this always uses a decimal point.
4746 * Returns the length of the text that was consumed.
4747 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004748 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004749string2float(
4750 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004751 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004752{
4753 char *s = (char *)text;
4754 float_T f;
4755
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004756 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004757 if (STRNICMP(text, "inf", 3) == 0)
4758 {
4759 *value = INFINITY;
4760 return 3;
4761 }
4762 if (STRNICMP(text, "-inf", 3) == 0)
4763 {
4764 *value = -INFINITY;
4765 return 4;
4766 }
4767 if (STRNICMP(text, "nan", 3) == 0)
4768 {
4769 *value = NAN;
4770 return 3;
4771 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004772 f = strtod(s, &s);
4773 *value = f;
4774 return (int)((char_u *)s - text);
4775}
4776#endif
4777
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004780 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004782 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004783var2fpos(
4784 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004785 int dollar_lnum, // TRUE when $ is last line
4786 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004788 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004790 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004792 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004793 if (varp->v_type == VAR_LIST)
4794 {
4795 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004796 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004797 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004798 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004799
4800 l = varp->vval.v_list;
4801 if (l == NULL)
4802 return NULL;
4803
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004804 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004805 pos.lnum = list_find_nr(l, 0L, &error);
4806 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004807 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004809 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004810 pos.col = list_find_nr(l, 1L, &error);
4811 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004812 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004813 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004815 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004816 li = list_find(l, 1L);
4817 if (li != NULL && li->li_tv.v_type == VAR_STRING
4818 && li->li_tv.vval.v_string != NULL
4819 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4820 pos.col = len + 1;
4821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004822 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004823 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004824 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004825 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004827 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004828 pos.coladd = list_find_nr(l, 2L, &error);
4829 if (error)
4830 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004831
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004832 return &pos;
4833 }
4834
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004835 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004836 if (name == NULL)
4837 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004838 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004840 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004841 {
4842 if (VIsual_active)
4843 return &VIsual;
4844 return &curwin->w_cursor;
4845 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004846 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004848 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4850 return NULL;
4851 return pp;
4852 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004853
Bram Moolenaara5525202006-03-02 22:52:09 +00004854 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004855
Bram Moolenaar477933c2007-07-17 14:32:23 +00004856 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004857 {
4858 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004859 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004860 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004861 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004862 // In silent Ex mode topline is zero, but that's not a valid line
4863 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004864 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004865 return &pos;
4866 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004867 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004868 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004869 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004870 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004871 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004872 return &pos;
4873 }
4874 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004875 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004877 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
4879 pos.lnum = curbuf->b_ml.ml_line_count;
4880 pos.col = 0;
4881 }
4882 else
4883 {
4884 pos.lnum = curwin->w_cursor.lnum;
4885 pos.col = (colnr_T)STRLEN(ml_get_curline());
4886 }
4887 return &pos;
4888 }
4889 return NULL;
4890}
4891
4892/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004893 * Convert list in "arg" into a position and optional file number.
4894 * When "fnump" is NULL there is no file number, only 3 items.
4895 * Note that the column is passed on as-is, the caller may want to decrement
4896 * it to use 1 for the first column.
4897 * Return FAIL when conversion is not possible, doesn't check the position for
4898 * validity.
4899 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004900 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004901list2fpos(
4902 typval_T *arg,
4903 pos_T *posp,
4904 int *fnump,
4905 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004906{
4907 list_T *l = arg->vval.v_list;
4908 long i = 0;
4909 long n;
4910
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004911 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4912 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004913 if (arg->v_type != VAR_LIST
4914 || l == NULL
4915 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004916 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004917 return FAIL;
4918
4919 if (fnump != NULL)
4920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004922 if (n < 0)
4923 return FAIL;
4924 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004925 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004926 *fnump = n;
4927 }
4928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004929 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004930 if (n < 0)
4931 return FAIL;
4932 posp->lnum = n;
4933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004934 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004935 if (n < 0)
4936 return FAIL;
4937 posp->col = n;
4938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004939 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004940 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004941 posp->coladd = 0;
4942 else
4943 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004944
Bram Moolenaar493c1782014-05-28 14:34:46 +02004945 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004946 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004947
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004948 return OK;
4949}
4950
4951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 * Get the length of an environment variable name.
4953 * Advance "arg" to the first character after the name.
4954 * Return 0 for error.
4955 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004957get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
4959 char_u *p;
4960 int len;
4961
4962 for (p = *arg; vim_isIDc(*p); ++p)
4963 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004964 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 return 0;
4966
4967 len = (int)(p - *arg);
4968 *arg = p;
4969 return len;
4970}
4971
4972/*
4973 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004974 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 * Return 0 if something is wrong.
4976 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004977 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004978get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979{
4980 char_u *p;
4981 int len;
4982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004983 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004985 {
4986 if (*p == ':')
4987 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004988 // "s:" is start of "s:var", but "n:" is not and can be used in
4989 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004990 len = (int)(p - *arg);
4991 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4992 || len > 1)
4993 break;
4994 }
4995 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004996 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 return 0;
4998
4999 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005000 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
5002 return len;
5003}
5004
5005/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005006 * Get the length of the name of a variable or function.
5007 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005009 * Return -1 if curly braces expansion failed.
5010 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 * If the name contains 'magic' {}'s, expand them and return the
5012 * expanded name in an allocated string via 'alias' - caller must free.
5013 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005014 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005015get_name_len(
5016 char_u **arg,
5017 char_u **alias,
5018 int evaluate,
5019 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020{
5021 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 char_u *p;
5023 char_u *expr_start;
5024 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005026 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027
5028 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5029 && (*arg)[2] == (int)KE_SNR)
5030 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005031 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 *arg += 3;
5033 return get_id_len(arg) + 3;
5034 }
5035 len = eval_fname_script(*arg);
5036 if (len > 0)
5037 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005038 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 *arg += len;
5040 }
5041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005043 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005045 p = find_name_end(*arg, &expr_start, &expr_end,
5046 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (expr_start != NULL)
5048 {
5049 char_u *temp_string;
5050
5051 if (!evaluate)
5052 {
5053 len += (int)(p - *arg);
5054 *arg = skipwhite(p);
5055 return len;
5056 }
5057
5058 /*
5059 * Include any <SID> etc in the expanded string:
5060 * Thus the -len here.
5061 */
5062 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5063 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005064 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 *alias = temp_string;
5066 *arg = skipwhite(p);
5067 return (int)STRLEN(temp_string);
5068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005071 // Only give an error when there is something, otherwise it will be
5072 // reported at a higher level.
5073 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005074 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075
5076 return len;
5077}
5078
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079/*
5080 * Find the end of a variable or function name, taking care of magic braces.
5081 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5082 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005083 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 * Return a pointer to just after the name. Equal to "arg" if there is no
5085 * valid name.
5086 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005087 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005088find_name_end(
5089 char_u *arg,
5090 char_u **expr_start,
5091 char_u **expr_end,
5092 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 int mb_nest = 0;
5095 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005097 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005098 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005102 *expr_start = NULL;
5103 *expr_end = NULL;
5104 }
5105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005106 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005107 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5108 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005109 return arg;
5110
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005111 for (p = arg; *p != NUL
5112 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005113 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005114 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005115 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005117 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005119 if (*p == '\'')
5120 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005121 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005122 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005123 ;
5124 if (*p == NUL)
5125 break;
5126 }
5127 else if (*p == '"')
5128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005129 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005130 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005131 if (*p == '\\' && p[1] != NUL)
5132 ++p;
5133 if (*p == NUL)
5134 break;
5135 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005136 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5137 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005138 // "s:" is start of "s:var", but "n:" is not and can be used in
5139 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005140 len = (int)(p - arg);
5141 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005142 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005143 break;
5144 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005145
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005146 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 if (*p == '[')
5149 ++br_nest;
5150 else if (*p == ']')
5151 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005153
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005154 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005156 if (*p == '{')
5157 {
5158 mb_nest++;
5159 if (expr_start != NULL && *expr_start == NULL)
5160 *expr_start = p;
5161 }
5162 else if (*p == '}')
5163 {
5164 mb_nest--;
5165 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5166 *expr_end = p;
5167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170
5171 return p;
5172}
5173
5174/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005175 * Expands out the 'magic' {}'s in a variable/function name.
5176 * Note that this can call itself recursively, to deal with
5177 * constructs like foo{bar}{baz}{bam}
5178 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5179 * "in_start" ^
5180 * "expr_start" ^
5181 * "expr_end" ^
5182 * "in_end" ^
5183 *
5184 * Returns a new allocated string, which the caller must free.
5185 * Returns NULL for failure.
5186 */
5187 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005188make_expanded_name(
5189 char_u *in_start,
5190 char_u *expr_start,
5191 char_u *expr_end,
5192 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005193{
5194 char_u c1;
5195 char_u *retval = NULL;
5196 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005197
5198 if (expr_end == NULL || in_end == NULL)
5199 return NULL;
5200 *expr_start = NUL;
5201 *expr_end = NUL;
5202 c1 = *in_end;
5203 *in_end = NUL;
5204
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005205 temp_result = eval_to_string(expr_start + 1, FALSE);
5206 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005207 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005208 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5209 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005210 if (retval != NULL)
5211 {
5212 STRCPY(retval, in_start);
5213 STRCAT(retval, temp_result);
5214 STRCAT(retval, expr_end + 1);
5215 }
5216 }
5217 vim_free(temp_result);
5218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005219 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005220 *expr_start = '{';
5221 *expr_end = '}';
5222
5223 if (retval != NULL)
5224 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005225 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005226 if (expr_start != NULL)
5227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005228 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005229 temp_result = make_expanded_name(retval, expr_start,
5230 expr_end, temp_result);
5231 vim_free(retval);
5232 retval = temp_result;
5233 }
5234 }
5235
5236 return retval;
5237}
5238
5239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005241 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005243 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005244eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005246 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005247}
5248
5249/*
5250 * Return TRUE if character "c" can be used as the first character in a
5251 * variable or function name (excluding '{' and '}').
5252 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005253 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005254eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005255{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005256 return ASCII_ISALPHA(c) || c == '_';
5257}
5258
5259/*
5260 * Return TRUE if character "c" can be used as the first character of a
5261 * dictionary key.
5262 */
5263 int
5264eval_isdictc(int c)
5265{
5266 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267}
5268
5269/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005270 * Handle:
5271 * - expr[expr], expr[expr:expr] subscript
5272 * - ".name" lookup
5273 * - function call with Funcref variable: func(expr)
5274 * - method call: var->method()
5275 *
5276 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005278 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005279handle_subscript(
5280 char_u **arg,
5281 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005282 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005283 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005284{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005285 int evaluate = evalarg != NULL
5286 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005287 int ret = OK;
5288 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005289 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005290 int getnext;
5291 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005292
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005293 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005294 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005295 // When at the end of the line and ".name" or "->{" or "->X" follows in
5296 // the next line then consume the line break.
5297 p = eval_next_non_blank(*arg, evalarg, &getnext);
5298 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005299 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005300 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005301 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005302 {
5303 *arg = eval_next_line(evalarg);
5304 check_white = FALSE;
5305 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005306
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005307 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5308 || rettv->v_type == VAR_PARTIAL))
5309 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005310 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005311 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5312 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005313
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005314 // Stop the expression evaluation when immediately aborting on
5315 // error, or when an interrupt occurred or an exception was thrown
5316 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005317 if (aborting())
5318 {
5319 if (ret == OK)
5320 clear_tv(rettv);
5321 ret = FAIL;
5322 }
5323 dict_unref(selfdict);
5324 selfdict = NULL;
5325 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005326 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005327 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005328 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005329 if (ret == OK)
5330 {
5331 if ((*arg)[2] == '{')
5332 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005333 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005334 else
5335 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005336 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005337 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005338 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005339 // "." is ".name" lookup when we found a dict or when evaluating and
5340 // scriptversion is at least 2, where string concatenation is "..".
5341 else if (**arg == '['
5342 || (**arg == '.' && (rettv->v_type == VAR_DICT
5343 || (!evaluate
5344 && (*arg)[1] != '.'
5345 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005346 {
5347 dict_unref(selfdict);
5348 if (rettv->v_type == VAR_DICT)
5349 {
5350 selfdict = rettv->vval.v_dict;
5351 if (selfdict != NULL)
5352 ++selfdict->dv_refcount;
5353 }
5354 else
5355 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005356 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005357 {
5358 clear_tv(rettv);
5359 ret = FAIL;
5360 }
5361 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005362 else
5363 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005364 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005365
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005366 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5367 // Don't do this when "Func" is already a partial that was bound
5368 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005369 if (selfdict != NULL
5370 && (rettv->v_type == VAR_FUNC
5371 || (rettv->v_type == VAR_PARTIAL
5372 && (rettv->vval.v_partial->pt_auto
5373 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005374 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005375
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005376 dict_unref(selfdict);
5377 return ret;
5378}
5379
5380/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005381 * Make a copy of an item.
5382 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005383 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5384 * reference to an already copied list/dict can be used.
5385 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005386 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005387 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005388item_copy(
5389 typval_T *from,
5390 typval_T *to,
5391 int deep,
5392 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005393{
5394 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005395 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005396
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005398 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005399 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005400 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005401 }
5402 ++recurse;
5403
5404 switch (from->v_type)
5405 {
5406 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005407 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005408 case VAR_STRING:
5409 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005410 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005411 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005412 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005413 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005414 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005415 copy_tv(from, to);
5416 break;
5417 case VAR_LIST:
5418 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005419 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005420 if (from->vval.v_list == NULL)
5421 to->vval.v_list = NULL;
5422 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5423 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005424 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005425 to->vval.v_list = from->vval.v_list->lv_copylist;
5426 ++to->vval.v_list->lv_refcount;
5427 }
5428 else
5429 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5430 if (to->vval.v_list == NULL)
5431 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005432 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005433 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005435 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005436 case VAR_DICT:
5437 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005438 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005439 if (from->vval.v_dict == NULL)
5440 to->vval.v_dict = NULL;
5441 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5442 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005443 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005444 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5445 ++to->vval.v_dict->dv_refcount;
5446 }
5447 else
5448 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5449 if (to->vval.v_dict == NULL)
5450 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005451 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005452 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005453 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005454 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005455 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005456 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005457 }
5458 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005459 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005460}
5461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462 void
5463echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5464{
5465 char_u *tofree;
5466 char_u numbuf[NUMBUFLEN];
5467 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5468
5469 if (*atstart)
5470 {
5471 *atstart = FALSE;
5472 // Call msg_start() after eval1(), evaluating the expression
5473 // may cause a message to appear.
5474 if (with_space)
5475 {
5476 // Mark the saved text as finishing the line, so that what
5477 // follows is displayed on a new line when scrolling back
5478 // at the more prompt.
5479 msg_sb_eol();
5480 msg_start();
5481 }
5482 }
5483 else if (with_space)
5484 msg_puts_attr(" ", echo_attr);
5485
5486 if (p != NULL)
5487 for ( ; *p != NUL && !got_int; ++p)
5488 {
5489 if (*p == '\n' || *p == '\r' || *p == TAB)
5490 {
5491 if (*p != TAB && *needclr)
5492 {
5493 // remove any text still there from the command
5494 msg_clr_eos();
5495 *needclr = FALSE;
5496 }
5497 msg_putchar_attr(*p, echo_attr);
5498 }
5499 else
5500 {
5501 if (has_mbyte)
5502 {
5503 int i = (*mb_ptr2len)(p);
5504
5505 (void)msg_outtrans_len_attr(p, i, echo_attr);
5506 p += i - 1;
5507 }
5508 else
5509 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5510 }
5511 }
5512 vim_free(tofree);
5513}
5514
Bram Moolenaare9a41262005-01-15 22:18:47 +00005515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 * ":echo expr1 ..." print each argument separated with a space, add a
5517 * newline at the end.
5518 * ":echon expr1 ..." print each argument plain.
5519 */
5520 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005521ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
5523 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005524 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 char_u *p;
5526 int needclr = TRUE;
5527 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005528 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005529 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005530 evalarg_T evalarg;
5531
Bram Moolenaare707c882020-07-01 13:07:37 +02005532 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
5534 if (eap->skip)
5535 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005536 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005538 // If eval1() causes an error message the text from the command may
5539 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005540 need_clr_eos = needclr;
5541
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005543 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
5545 /*
5546 * Report the invalid expression unless the expression evaluation
5547 * has been cancelled due to an aborting error, an interrupt, or an
5548 * exception.
5549 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005550 if (!aborting() && did_emsg == did_emsg_before
5551 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005552 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005553 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 break;
5555 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005556 need_clr_eos = FALSE;
5557
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005559 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005560
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 arg = skipwhite(arg);
5563 }
5564 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005565 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
5567 if (eap->skip)
5568 --emsg_skip;
5569 else
5570 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005571 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 if (needclr)
5573 msg_clr_eos();
5574 if (eap->cmdidx == CMD_echo)
5575 msg_end();
5576 }
5577}
5578
5579/*
5580 * ":echohl {name}".
5581 */
5582 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005583ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005585 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586}
5587
5588/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005589 * Returns the :echo attribute
5590 */
5591 int
5592get_echo_attr(void)
5593{
5594 return echo_attr;
5595}
5596
5597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 * ":execute expr1 ..." execute the result of an expression.
5599 * ":echomsg expr1 ..." Print a message
5600 * ":echoerr expr1 ..." Print an error
5601 * Each gets spaces around each argument and a newline at the end for
5602 * echo commands
5603 */
5604 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005605ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606{
5607 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005608 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 int ret = OK;
5610 char_u *p;
5611 garray_T ga;
5612 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614 ga_init2(&ga, 1, 80);
5615
5616 if (eap->skip)
5617 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005618 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005620 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005621 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624 if (!eap->skip)
5625 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005626 char_u buf[NUMBUFLEN];
5627
5628 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005629 {
5630 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5631 {
5632 emsg(_(e_inval_string));
5633 p = NULL;
5634 }
5635 else
5636 p = tv_get_string_buf(&rettv, buf);
5637 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005638 else
5639 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005640 if (p == NULL)
5641 {
5642 clear_tv(&rettv);
5643 ret = FAIL;
5644 break;
5645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 len = (int)STRLEN(p);
5647 if (ga_grow(&ga, len + 2) == FAIL)
5648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 ret = FAIL;
5651 break;
5652 }
5653 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 ga.ga_len += len;
5657 }
5658
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 arg = skipwhite(arg);
5661 }
5662
5663 if (ret != FAIL && ga.ga_data != NULL)
5664 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005665 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5666 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005667 // Mark the already saved text as finishing the line, so that what
5668 // follows is displayed on a new line when scrolling back at the
5669 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005670 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005671 }
5672
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005674 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005675 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005676 out_flush();
5677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 else if (eap->cmdidx == CMD_echoerr)
5679 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005680 int save_did_emsg = did_emsg;
5681
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005682 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005683 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 if (!force_abort)
5685 did_emsg = save_did_emsg;
5686 }
5687 else if (eap->cmdidx == CMD_execute)
5688 do_cmdline((char_u *)ga.ga_data,
5689 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5690 }
5691
5692 ga_clear(&ga);
5693
5694 if (eap->skip)
5695 --emsg_skip;
5696
5697 eap->nextcmd = check_nextcmd(arg);
5698}
5699
5700/*
5701 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5702 * "arg" points to the "&" or '+' when called, to "option" when returning.
5703 * Returns NULL when no option name found. Otherwise pointer to the char
5704 * after the option name.
5705 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005706 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005707find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 char_u *p = *arg;
5710
5711 ++p;
5712 if (*p == 'g' && p[1] == ':')
5713 {
5714 *opt_flags = OPT_GLOBAL;
5715 p += 2;
5716 }
5717 else if (*p == 'l' && p[1] == ':')
5718 {
5719 *opt_flags = OPT_LOCAL;
5720 p += 2;
5721 }
5722 else
5723 *opt_flags = 0;
5724
5725 if (!ASCII_ISALPHA(*p))
5726 return NULL;
5727 *arg = p;
5728
5729 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005730 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 else
5732 while (ASCII_ISALPHA(*p))
5733 ++p;
5734 return p;
5735}
5736
5737/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005738 * Display script name where an item was last set.
5739 * Should only be invoked when 'verbose' is non-zero.
5740 */
5741 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005742last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005743{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005744 char_u *p;
5745
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005746 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005747 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005748 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005749 if (p != NULL)
5750 {
5751 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005752 msg_puts(_("\n\tLast set from "));
5753 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005754 if (script_ctx.sc_lnum > 0)
5755 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005756 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005757 msg_outnum((long)script_ctx.sc_lnum);
5758 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005759 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005760 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005761 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005762 }
5763}
5764
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005765#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767/*
5768 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005769 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 * "flags" can be "g" to do a global substitute.
5771 * Returns an allocated string, NULL for error.
5772 */
5773 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005774do_string_sub(
5775 char_u *str,
5776 char_u *pat,
5777 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005778 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005779 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 int sublen;
5782 regmatch_T regmatch;
5783 int i;
5784 int do_all;
5785 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005786 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 garray_T ga;
5788 char_u *ret;
5789 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005790 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005792 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005794 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
5796 ga_init2(&ga, 1, 200);
5797
5798 do_all = (flags[0] == 'g');
5799
5800 regmatch.rm_ic = p_ic;
5801 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5802 if (regmatch.regprog != NULL)
5803 {
5804 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005805 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5807 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005808 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005809 if (regmatch.startp[0] == regmatch.endp[0])
5810 {
5811 if (zero_width == regmatch.startp[0])
5812 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005813 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005814 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005815 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5816 (size_t)i);
5817 ga.ga_len += i;
5818 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005819 continue;
5820 }
5821 zero_width = regmatch.startp[0];
5822 }
5823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 /*
5825 * Get some space for a temporary buffer to do the substitution
5826 * into. It will contain:
5827 * - The text up to where the match is.
5828 * - The substituted text.
5829 * - The text after the match.
5830 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005831 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005832 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5834 {
5835 ga_clear(&ga);
5836 break;
5837 }
5838
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005839 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 i = (int)(regmatch.startp[0] - tail);
5841 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005842 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005843 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 + ga.ga_len + i, TRUE, TRUE, FALSE);
5845 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005846 tail = regmatch.endp[0];
5847 if (*tail == NUL)
5848 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 if (!do_all)
5850 break;
5851 }
5852
5853 if (ga.ga_data != NULL)
5854 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5855
Bram Moolenaar473de612013-06-08 18:19:48 +02005856 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 }
5858
5859 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5860 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005861 if (p_cpo == empty_option)
5862 p_cpo = save_cpo;
5863 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005864 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005865 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
5867 return ret;
5868}