blob: 07b30c723a996059bb26725849270ae196ce1b51 [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 Moolenaar418f1df2020-08-12 21:34:49 +02002715 if (eval6(arg, &var2, evalarg, !in_vim9script() && 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);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002730 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002731
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002732 if (in_vim9script() && (var2.v_type == VAR_VOID
2733 || var2.v_type == VAR_CHANNEL
2734 || var2.v_type == VAR_JOB))
2735 emsg(_(e_inval_string));
2736#ifdef FEAT_FLOAT
2737 else if (var2.v_type == VAR_FLOAT)
2738 {
2739 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2740 var2.vval.v_float);
2741 s2 = buf2;
2742 }
2743#endif
2744 else
2745 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002746 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002747 {
2748 clear_tv(rettv);
2749 clear_tv(&var2);
2750 return FAIL;
2751 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002752 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002753 clear_tv(rettv);
2754 rettv->v_type = VAR_STRING;
2755 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002757 else if (op == '+' && rettv->v_type == VAR_BLOB
2758 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002760 else if (op == '+' && rettv->v_type == VAR_LIST
2761 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002762 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002764 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 else
2767 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002768 int error = FALSE;
2769 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002770#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002771 float_T f1 = 0, f2 = 0;
2772
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002773 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002774 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002775 f1 = rettv->vval.v_float;
2776 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002777 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002778 else
2779#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002781 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002782 if (error)
2783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002784 // This can only happen for "list + non-list". For
2785 // "non-list + ..." or "something - ...", we returned
2786 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002787 clear_tv(rettv);
2788 return FAIL;
2789 }
2790#ifdef FEAT_FLOAT
2791 if (var2.v_type == VAR_FLOAT)
2792 f1 = n1;
2793#endif
2794 }
2795#ifdef FEAT_FLOAT
2796 if (var2.v_type == VAR_FLOAT)
2797 {
2798 f2 = var2.vval.v_float;
2799 n2 = 0;
2800 }
2801 else
2802#endif
2803 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002804 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002805 if (error)
2806 {
2807 clear_tv(rettv);
2808 clear_tv(&var2);
2809 return FAIL;
2810 }
2811#ifdef FEAT_FLOAT
2812 if (rettv->v_type == VAR_FLOAT)
2813 f2 = n2;
2814#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002816 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002817
2818#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002819 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002820 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2821 {
2822 if (op == '+')
2823 f1 = f1 + f2;
2824 else
2825 f1 = f1 - f2;
2826 rettv->v_type = VAR_FLOAT;
2827 rettv->vval.v_float = f1;
2828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002830#endif
2831 {
2832 if (op == '+')
2833 n1 = n1 + n2;
2834 else
2835 n1 = n1 - n2;
2836 rettv->v_type = VAR_NUMBER;
2837 rettv->vval.v_number = n1;
2838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002840 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 }
2842 }
2843 return OK;
2844}
2845
2846/*
2847 * Handle fifth level expression:
2848 * * number multiplication
2849 * / number division
2850 * % number modulo
2851 *
2852 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002853 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 *
2855 * Return OK or FAIL.
2856 */
2857 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002858eval6(
2859 char_u **arg,
2860 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002861 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002862 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002864#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002865 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
2868 /*
2869 * Get the first variable.
2870 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002871 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 return FAIL;
2873
2874 /*
2875 * Repeat computing, until no '*', '/' or '%' is following.
2876 */
2877 for (;;)
2878 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002879 int evaluate;
2880 int getnext;
2881 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002882 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002883 int op;
2884 varnumber_T n1, n2;
2885#ifdef FEAT_FLOAT
2886 float_T f1, f2;
2887#endif
2888 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002889
Bram Moolenaar9d489562020-07-30 20:08:50 +02002890 p = eval_next_non_blank(*arg, evalarg, &getnext);
2891 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002892 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002894
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002895 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002896 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002897 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002898 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002899 {
2900 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2901 {
2902 error_white_both(p, 1);
2903 clear_tv(rettv);
2904 return FAIL;
2905 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002906 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002909#ifdef FEAT_FLOAT
2910 f1 = 0;
2911 f2 = 0;
2912#endif
2913 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002914 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002916#ifdef FEAT_FLOAT
2917 if (rettv->v_type == VAR_FLOAT)
2918 {
2919 f1 = rettv->vval.v_float;
2920 use_float = TRUE;
2921 n1 = 0;
2922 }
2923 else
2924#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002925 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002926 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002927 if (error)
2928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 }
2930 else
2931 n1 = 0;
2932
2933 /*
2934 * Get the second variable.
2935 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002936 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2937 {
2938 error_white_both(p, 1);
2939 clear_tv(rettv);
2940 return FAIL;
2941 }
2942 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002943 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 return FAIL;
2945
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002946 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002948#ifdef FEAT_FLOAT
2949 if (var2.v_type == VAR_FLOAT)
2950 {
2951 if (!use_float)
2952 {
2953 f1 = n1;
2954 use_float = TRUE;
2955 }
2956 f2 = var2.vval.v_float;
2957 n2 = 0;
2958 }
2959 else
2960#endif
2961 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002962 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002963 clear_tv(&var2);
2964 if (error)
2965 return FAIL;
2966#ifdef FEAT_FLOAT
2967 if (use_float)
2968 f2 = n2;
2969#endif
2970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 /*
2973 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002974 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002976#ifdef FEAT_FLOAT
2977 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002979 if (op == '*')
2980 f1 = f1 * f2;
2981 else if (op == '/')
2982 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002983# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002984 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002985 if (f2 == 0.0)
2986 {
2987 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002988 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002989 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002990 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002991 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002992 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002993 }
2994 else
2995 f1 = f1 / f2;
2996# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002997 // We rely on the floating point library to handle divide
2998 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002999 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003000# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003003 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003005 return FAIL;
3006 }
3007 rettv->v_type = VAR_FLOAT;
3008 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 }
3010 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003013 if (op == '*')
3014 n1 = n1 * n2;
3015 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003016 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003018 n1 = num_modulus(n1, n2);
3019
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003020 rettv->v_type = VAR_NUMBER;
3021 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 }
3024 }
3025
3026 return OK;
3027}
3028
3029/*
3030 * Handle sixth level expression:
3031 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003032 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003033 * "string" string constant
3034 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 * &option-name option value
3036 * @r register contents
3037 * identifier variable value
3038 * function() function call
3039 * $VAR environment variable
3040 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003041 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003043 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003044 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 *
3046 * Also handle:
3047 * ! in front logical NOT
3048 * - in front unary minus
3049 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003050 * trailing [] subscript in String or List
3051 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003052 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 *
3054 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003055 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 *
3057 * Return OK or FAIL.
3058 */
3059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003060eval7(
3061 char_u **arg,
3062 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003063 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003066 int evaluate = evalarg != NULL
3067 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 int len;
3069 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 char_u *start_leader, *end_leader;
3071 int ret = OK;
3072 char_u *alias;
3073
3074 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003075 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003076 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003078 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
3080 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003081 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 */
3083 start_leader = *arg;
3084 while (**arg == '!' || **arg == '-' || **arg == '+')
3085 *arg = skipwhite(*arg + 1);
3086 end_leader = *arg;
3087
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003088 if (**arg == '.' && (!isdigit(*(*arg + 1))
3089#ifdef FEAT_FLOAT
3090 || current_sctx.sc_version < 2
3091#endif
3092 ))
3093 {
3094 semsg(_(e_invexpr2), *arg);
3095 ++*arg;
3096 return FAIL;
3097 }
3098
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 switch (**arg)
3100 {
3101 /*
3102 * Number constant.
3103 */
3104 case '0':
3105 case '1':
3106 case '2':
3107 case '3':
3108 case '4':
3109 case '5':
3110 case '6':
3111 case '7':
3112 case '8':
3113 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003114 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003115
3116 // Apply prefixed "-" and "+" now. Matters especially when
3117 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003118 if (ret == OK && evaluate && end_leader > start_leader
3119 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003120 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 break;
3122
3123 /*
3124 * String constant: "string".
3125 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003126 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 break;
3128
3129 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003130 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003132 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003133 break;
3134
3135 /*
3136 * List: [expr, expr]
3137 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003138 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 break;
3140
3141 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003142 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003143 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003144 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003145 {
3146 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003147 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003148 }
3149 else
3150 ret = NOTDONE;
3151 break;
3152
3153 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003154 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003155 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003156 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003157 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003158 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003159 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003160 break;
3161
3162 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003163 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003165 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 break;
3167
3168 /*
3169 * Environment variable: $VAR.
3170 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003171 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 break;
3173
3174 /*
3175 * Register contents: @r.
3176 */
3177 case '@': ++*arg;
3178 if (evaluate)
3179 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003180 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003181 rettv->vval.v_string = get_reg_contents(**arg,
3182 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 }
3184 if (**arg != NUL)
3185 ++*arg;
3186 break;
3187
3188 /*
3189 * nested expression: (expression).
3190 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003191 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003192 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003193 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003194
Bram Moolenaar9215f012020-06-27 21:18:00 +02003195 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003196 if (**arg == ')')
3197 ++*arg;
3198 else if (ret == OK)
3199 {
3200 emsg(_(e_missing_close));
3201 clear_tv(rettv);
3202 ret = FAIL;
3203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205 break;
3206
Bram Moolenaar8c711452005-01-14 21:53:12 +00003207 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 break;
3209 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003210
3211 if (ret == NOTDONE)
3212 {
3213 /*
3214 * Must be a variable or function name.
3215 * Can also be a curly-braces kind of name: {expr}.
3216 */
3217 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003218 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003219 if (alias != NULL)
3220 s = alias;
3221
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003222 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003223 ret = FAIL;
3224 else
3225 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003226 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3227
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003228 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3229 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003230 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003231 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003232 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003233 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003234 else if (flags & EVAL_CONSTANT)
3235 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003236 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003237 {
3238 // get the value of "true", "false" or a variable
3239 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3240 {
3241 rettv->v_type = VAR_BOOL;
3242 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003243 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003244 }
3245 else if (len == 5 && in_vim9script()
3246 && STRNCMP(s, "false", 4) == 0)
3247 {
3248 rettv->v_type = VAR_BOOL;
3249 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003250 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003251 }
3252 else
3253 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3254 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003255 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003256 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003257 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003258 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003259 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003260 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003261 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003262 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003263 }
3264
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003265 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3266 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003267 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003268 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
3270 /*
3271 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3272 */
3273 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003274 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003275 return ret;
3276}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003277
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003278/*
3279 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003280 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003281 * Adjusts "end_leaderp" until it is at "start_leader".
3282 */
3283 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003284eval7_leader(
3285 typval_T *rettv,
3286 int numeric_only,
3287 char_u *start_leader,
3288 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003289{
3290 char_u *end_leader = *end_leaderp;
3291 int ret = OK;
3292 int error = FALSE;
3293 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003294 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003295#ifdef FEAT_FLOAT
3296 float_T f = 0.0;
3297
3298 if (rettv->v_type == VAR_FLOAT)
3299 f = rettv->vval.v_float;
3300 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003301#endif
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003302 if (in_vim9script() && end_leader[-1] == '!')
3303 val = tv2bool(rettv);
3304 else
3305 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003306 if (error)
3307 {
3308 clear_tv(rettv);
3309 ret = FAIL;
3310 }
3311 else
3312 {
3313 while (end_leader > start_leader)
3314 {
3315 --end_leader;
3316 if (*end_leader == '!')
3317 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003318 if (numeric_only)
3319 {
3320 ++end_leader;
3321 break;
3322 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003323#ifdef FEAT_FLOAT
3324 if (rettv->v_type == VAR_FLOAT)
3325 f = !f;
3326 else
3327#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003328 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003329 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003330 type = VAR_BOOL;
3331 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003332 }
3333 else if (*end_leader == '-')
3334 {
3335#ifdef FEAT_FLOAT
3336 if (rettv->v_type == VAR_FLOAT)
3337 f = -f;
3338 else
3339#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003340 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003341 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003342 type = VAR_NUMBER;
3343 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003344 }
3345 }
3346#ifdef FEAT_FLOAT
3347 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003349 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003350 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003352 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003353#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003354 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003355 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003356 if (in_vim9script())
3357 rettv->v_type = type;
3358 else
3359 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003360 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003363 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 return ret;
3365}
3366
3367/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003368 * Call the function referred to in "rettv".
3369 */
3370 static int
3371call_func_rettv(
3372 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003373 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003374 typval_T *rettv,
3375 int evaluate,
3376 dict_T *selfdict,
3377 typval_T *basetv)
3378{
3379 partial_T *pt = NULL;
3380 funcexe_T funcexe;
3381 typval_T functv;
3382 char_u *s;
3383 int ret;
3384
3385 // need to copy the funcref so that we can clear rettv
3386 if (evaluate)
3387 {
3388 functv = *rettv;
3389 rettv->v_type = VAR_UNKNOWN;
3390
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003391 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003392 if (functv.v_type == VAR_PARTIAL)
3393 {
3394 pt = functv.vval.v_partial;
3395 s = partial_name(pt);
3396 }
3397 else
3398 s = functv.vval.v_string;
3399 }
3400 else
3401 s = (char_u *)"";
3402
Bram Moolenaara80faa82020-04-12 19:37:17 +02003403 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003404 funcexe.firstline = curwin->w_cursor.lnum;
3405 funcexe.lastline = curwin->w_cursor.lnum;
3406 funcexe.evaluate = evaluate;
3407 funcexe.partial = pt;
3408 funcexe.selfdict = selfdict;
3409 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003410 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003411
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003412 // Clear the funcref afterwards, so that deleting it while
3413 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003414 if (evaluate)
3415 clear_tv(&functv);
3416
3417 return ret;
3418}
3419
3420/*
3421 * Evaluate "->method()".
3422 * "*arg" points to the '-'.
3423 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3424 */
3425 static int
3426eval_lambda(
3427 char_u **arg,
3428 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003429 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003430 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003431{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003432 int evaluate = evalarg != NULL
3433 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003434 typval_T base = *rettv;
3435 int ret;
3436
3437 // Skip over the ->.
3438 *arg += 2;
3439 rettv->v_type = VAR_UNKNOWN;
3440
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003441 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003442 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003443 return FAIL;
3444 else if (**arg != '(')
3445 {
3446 if (verbose)
3447 {
3448 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003449 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003450 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003452 }
3453 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003454 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003455 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003456 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003457 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003458
3459 // Clear the funcref afterwards, so that deleting it while
3460 // evaluating the arguments is possible (see test55).
3461 if (evaluate)
3462 clear_tv(&base);
3463
3464 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003465}
3466
3467/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003468 * Evaluate "->method()".
3469 * "*arg" points to the '-'.
3470 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3471 */
3472 static int
3473eval_method(
3474 char_u **arg,
3475 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003476 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003477 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003478{
3479 char_u *name;
3480 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003481 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003482 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003483 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003484 int evaluate = evalarg != NULL
3485 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003486
3487 // Skip over the ->.
3488 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003489 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003490
Bram Moolenaarac92e252019-08-03 21:58:38 +02003491 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003492 len = get_name_len(arg, &alias, evaluate, TRUE);
3493 if (alias != NULL)
3494 name = alias;
3495
3496 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003497 {
3498 if (verbose)
3499 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003500 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003501 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003502 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003503 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003504 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003505 if (**arg != '(')
3506 {
3507 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003509 ret = FAIL;
3510 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003511 else if (VIM_ISWHITE((*arg)[-1]))
3512 {
3513 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003514 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003515 ret = FAIL;
3516 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003517 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003518 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003519 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003520 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003521
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003522 // Clear the funcref afterwards, so that deleting it while
3523 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003524 if (evaluate)
3525 clear_tv(&base);
3526
Bram Moolenaarac92e252019-08-03 21:58:38 +02003527 return ret;
3528}
3529
3530/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003531 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3532 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003533 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3534 */
3535 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003536eval_index(
3537 char_u **arg,
3538 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003539 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003540 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003541{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003542 int evaluate = evalarg != NULL
3543 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003545 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003546 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003548 long len = -1;
3549 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003550 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003551 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003552
Bram Moolenaara03f2332016-02-06 18:09:59 +01003553 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003554 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003555 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003556 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003557 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003558 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003559 return FAIL;
3560 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003561#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003562 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003563 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003564 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003565#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003566 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003567 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003568 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003569 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003570 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003571 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003572 return FAIL;
3573 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003574 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003576 if (evaluate)
3577 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003578 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003579
3580 case VAR_STRING:
3581 case VAR_NUMBER:
3582 case VAR_LIST:
3583 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003584 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003585 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003586 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003587
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003588 init_tv(&var1);
3589 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003591 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003592 /*
3593 * dict.name
3594 */
3595 key = *arg + 1;
Bram Moolenaarb13ab992020-07-27 21:43:28 +02003596 for (len = 0; eval_isdictc(key[len]); ++len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003597 ;
3598 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003599 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003601 }
3602 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003603 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 /*
3605 * something[idx]
3606 *
3607 * Get the (first) variable from inside the [].
3608 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003609 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 if (**arg == ':')
3611 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003612 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003613 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003614 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003615 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003616 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003617 clear_tv(&var1);
3618 return FAIL;
3619 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620
3621 /*
3622 * Get the second variable from inside the [:].
3623 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003624 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003625 if (**arg == ':')
3626 {
3627 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003628 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003629 if (**arg == ']')
3630 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003631 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003633 if (!empty1)
3634 clear_tv(&var1);
3635 return FAIL;
3636 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003637 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003638 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003639 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003640 if (!empty1)
3641 clear_tv(&var1);
3642 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003643 return FAIL;
3644 }
3645 }
3646
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003647 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003648 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003649 if (**arg != ']')
3650 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003651 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003652 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 clear_tv(&var1);
3654 if (range)
3655 clear_tv(&var2);
3656 return FAIL;
3657 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003658 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003659 }
3660
3661 if (evaluate)
3662 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 n1 = 0;
3664 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003665 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003666 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003667 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003668 }
3669 if (range)
3670 {
3671 if (empty2)
3672 n2 = -1;
3673 else
3674 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003675 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003676 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003677 }
3678 }
3679
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003680 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003681 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003682 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003683 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003685 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003686 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003687 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003688 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003689 case VAR_SPECIAL:
3690 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003691 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003692 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003693
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003694 case VAR_NUMBER:
3695 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003696 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003697 len = (long)STRLEN(s);
3698 if (range)
3699 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003700 // The resulting variable is a substring. If the indexes
3701 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003702 if (n1 < 0)
3703 {
3704 n1 = len + n1;
3705 if (n1 < 0)
3706 n1 = 0;
3707 }
3708 if (n2 < 0)
3709 n2 = len + n2;
3710 else if (n2 >= len)
3711 n2 = len;
3712 if (n1 >= len || n2 < 0 || n1 > n2)
3713 s = NULL;
3714 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003715 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003716 }
3717 else
3718 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003719 // The resulting variable is a string of a single
3720 // character. If the index is too big or negative the
3721 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003722 if (n1 >= len || n1 < 0)
3723 s = NULL;
3724 else
3725 s = vim_strnsave(s + n1, 1);
3726 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003727 clear_tv(rettv);
3728 rettv->v_type = VAR_STRING;
3729 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003730 break;
3731
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003732 case VAR_BLOB:
3733 len = blob_len(rettv->vval.v_blob);
3734 if (range)
3735 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003736 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003737 // are out of range the result is empty.
3738 if (n1 < 0)
3739 {
3740 n1 = len + n1;
3741 if (n1 < 0)
3742 n1 = 0;
3743 }
3744 if (n2 < 0)
3745 n2 = len + n2;
3746 else if (n2 >= len)
3747 n2 = len - 1;
3748 if (n1 >= len || n2 < 0 || n1 > n2)
3749 {
3750 clear_tv(rettv);
3751 rettv->v_type = VAR_BLOB;
3752 rettv->vval.v_blob = NULL;
3753 }
3754 else
3755 {
3756 blob_T *blob = blob_alloc();
3757
3758 if (blob != NULL)
3759 {
3760 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3761 {
3762 blob_free(blob);
3763 return FAIL;
3764 }
3765 blob->bv_ga.ga_len = n2 - n1 + 1;
3766 for (i = n1; i <= n2; i++)
3767 blob_set(blob, i - n1,
3768 blob_get(rettv->vval.v_blob, i));
3769
3770 clear_tv(rettv);
3771 rettv_blob_set(rettv, blob);
3772 }
3773 }
3774 }
3775 else
3776 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003777 // The resulting variable is a byte value.
3778 // If the index is too big or negative that is an error.
3779 if (n1 < 0)
3780 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003781 if (n1 < len && n1 >= 0)
3782 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003783 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003784
3785 clear_tv(rettv);
3786 rettv->v_type = VAR_NUMBER;
3787 rettv->vval.v_number = v;
3788 }
3789 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003790 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003791 }
3792 break;
3793
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003794 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003795 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003796 if (n1 < 0)
3797 n1 = len + n1;
3798 if (!empty1 && (n1 < 0 || n1 >= len))
3799 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003800 // For a range we allow invalid values and return an empty
3801 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003802 if (!range)
3803 {
3804 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003805 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003806 return FAIL;
3807 }
3808 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003809 }
3810 if (range)
3811 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003812 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003813
3814 if (n2 < 0)
3815 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003816 else if (n2 >= len)
3817 n2 = len - 1;
3818 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003819 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003820 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003821 if (l == NULL)
3822 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003823 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003824 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003825 }
3826 else
3827 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003828 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003829 clear_tv(rettv);
3830 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003831 }
3832 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003833
3834 case VAR_DICT:
3835 if (range)
3836 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003837 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003838 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003839 if (len == -1)
3840 clear_tv(&var1);
3841 return FAIL;
3842 }
3843 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003844 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003845
3846 if (len == -1)
3847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003848 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003849 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003850 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003851 clear_tv(&var1);
3852 return FAIL;
3853 }
3854 }
3855
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003856 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003857
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003858 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003859 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003860 if (len == -1)
3861 clear_tv(&var1);
3862 if (item == NULL)
3863 return FAIL;
3864
3865 copy_tv(&item->di_tv, &var1);
3866 clear_tv(rettv);
3867 *rettv = var1;
3868 }
3869 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003870 }
3871 }
3872
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003873 return OK;
3874}
3875
3876/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003877 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003878 */
3879 char_u *
3880partial_name(partial_T *pt)
3881{
3882 if (pt->pt_name != NULL)
3883 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003884 if (pt->pt_func != NULL)
3885 return pt->pt_func->uf_name;
3886 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003887}
3888
Bram Moolenaarddecc252016-04-06 22:59:37 +02003889 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003890partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003891{
3892 int i;
3893
3894 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003895 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003896 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003897 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003898 if (pt->pt_name != NULL)
3899 {
3900 func_unref(pt->pt_name);
3901 vim_free(pt->pt_name);
3902 }
3903 else
3904 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003905
3906 if (pt->pt_funcstack != NULL)
3907 {
3908 // Decrease the reference count for the context of a closure. If down
3909 // to zero free it and clear the variables on the stack.
3910 if (--pt->pt_funcstack->fs_refcount == 0)
3911 {
3912 garray_T *gap = &pt->pt_funcstack->fs_ga;
3913 typval_T *stack = gap->ga_data;
3914
3915 for (i = 0; i < gap->ga_len; ++i)
3916 clear_tv(stack + i);
3917 ga_clear(gap);
3918 vim_free(pt->pt_funcstack);
3919 }
3920 pt->pt_funcstack = NULL;
3921 }
3922
Bram Moolenaarddecc252016-04-06 22:59:37 +02003923 vim_free(pt);
3924}
3925
3926/*
3927 * Unreference a closure: decrement the reference count and free it when it
3928 * becomes zero.
3929 */
3930 void
3931partial_unref(partial_T *pt)
3932{
3933 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003934 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003935}
3936
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003937/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003938 * Return the next (unique) copy ID.
3939 * Used for serializing nested structures.
3940 */
3941 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003942get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003943{
3944 current_copyID += COPYID_INC;
3945 return current_copyID;
3946}
3947
3948/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003949 * Garbage collection for lists and dictionaries.
3950 *
3951 * We use reference counts to be able to free most items right away when they
3952 * are no longer used. But for composite items it's possible that it becomes
3953 * unused while the reference count is > 0: When there is a recursive
3954 * reference. Example:
3955 * :let l = [1, 2, 3]
3956 * :let d = {9: l}
3957 * :let l[1] = d
3958 *
3959 * Since this is quite unusual we handle this with garbage collection: every
3960 * once in a while find out which lists and dicts are not referenced from any
3961 * variable.
3962 *
3963 * Here is a good reference text about garbage collection (refers to Python
3964 * but it applies to all reference-counting mechanisms):
3965 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003966 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003967
3968/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003969 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003970 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003971 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003972 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003973 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003974garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003975{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003976 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003977 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003978 buf_T *buf;
3979 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003980 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003981 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003982
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003983 if (!testing)
3984 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003985 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003986 want_garbage_collect = FALSE;
3987 may_garbage_collect = FALSE;
3988 garbage_collect_at_exit = FALSE;
3989 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003990
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003991 // The execution stack can grow big, limit the size.
3992 if (exestack.ga_maxlen - exestack.ga_len > 500)
3993 {
3994 size_t new_len;
3995 char_u *pp;
3996 int n;
3997
3998 // Keep 150% of the current size, with a minimum of the growth size.
3999 n = exestack.ga_len / 2;
4000 if (n < exestack.ga_growsize)
4001 n = exestack.ga_growsize;
4002
4003 // Don't make it bigger though.
4004 if (exestack.ga_len + n < exestack.ga_maxlen)
4005 {
4006 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4007 pp = vim_realloc(exestack.ga_data, new_len);
4008 if (pp == NULL)
4009 return FAIL;
4010 exestack.ga_maxlen = exestack.ga_len + n;
4011 exestack.ga_data = pp;
4012 }
4013 }
4014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004015 // We advance by two because we add one for items referenced through
4016 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004017 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004018
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004019 /*
4020 * 1. Go through all accessible variables and mark all lists and dicts
4021 * with copyID.
4022 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004024 // Don't free variables in the previous_funccal list unless they are only
4025 // referenced through previous_funccal. This must be first, because if
4026 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004027 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004029 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004030 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004032 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004033 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004034 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4035 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004037 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004038 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004039 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4040 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004041 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004042 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4043 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004044#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004045 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004046 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4047 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004048 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004049 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004050 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4051 NULL, NULL);
4052#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004054 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004055 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004056 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4057 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004058 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004059 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004060
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004061 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004062 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004063
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004064 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004065 abort = abort || set_ref_in_functions(copyID);
4066
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004067 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004068 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004069
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004070 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004071 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004072
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004073 // callbacks in buffers
4074 abort = abort || set_ref_in_buffers(copyID);
4075
Bram Moolenaar1dced572012-04-05 16:54:08 +02004076#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004077 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004078#endif
4079
Bram Moolenaardb913952012-06-29 12:54:53 +02004080#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004081 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004082#endif
4083
4084#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004085 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004086#endif
4087
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004088#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004089 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004090 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004091#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004092#ifdef FEAT_NETBEANS_INTG
4093 abort = abort || set_ref_in_nb_channel(copyID);
4094#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004095
Bram Moolenaare3188e22016-05-31 21:13:04 +02004096#ifdef FEAT_TIMERS
4097 abort = abort || set_ref_in_timer(copyID);
4098#endif
4099
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004100#ifdef FEAT_QUICKFIX
4101 abort = abort || set_ref_in_quickfix(copyID);
4102#endif
4103
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004104#ifdef FEAT_TERMINAL
4105 abort = abort || set_ref_in_term(copyID);
4106#endif
4107
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004108#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004109 abort = abort || set_ref_in_popups(copyID);
4110#endif
4111
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004112 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004113 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004114 /*
4115 * 2. Free lists and dictionaries that are not referenced.
4116 */
4117 did_free = free_unref_items(copyID);
4118
4119 /*
4120 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004121 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004122 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004123 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004124 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004125 else if (p_verbose > 0)
4126 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004127 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004128 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004129
4130 return did_free;
4131}
4132
4133/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004134 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004135 */
4136 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004137free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004138{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004139 int did_free = FALSE;
4140
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004141 // Let all "free" functions know that we are here. This means no
4142 // dictionaries, lists, channels or jobs are to be freed, because we will
4143 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004144 in_free_unref_items = TRUE;
4145
4146 /*
4147 * PASS 1: free the contents of the items. We don't free the items
4148 * themselves yet, so that it is possible to decrement refcount counters
4149 */
4150
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004151 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004152 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004153
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004154 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004155 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004156
4157#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004158 // Go through the list of jobs and free items without the copyID. This
4159 // must happen before doing channels, because jobs refer to channels, but
4160 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004161 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4162
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004163 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004164 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4165#endif
4166
4167 /*
4168 * PASS 2: free the items themselves.
4169 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004170 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004171 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004172
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004173#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004174 // Go through the list of jobs and free items without the copyID. This
4175 // must happen before doing channels, because jobs refer to channels, but
4176 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004177 free_unused_jobs(copyID, COPYID_MASK);
4178
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004179 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004180 free_unused_channels(copyID, COPYID_MASK);
4181#endif
4182
4183 in_free_unref_items = FALSE;
4184
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004185 return did_free;
4186}
4187
4188/*
4189 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004190 * "list_stack" is used to add lists to be marked. Can be NULL.
4191 *
4192 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004193 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004194 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004195set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004196{
4197 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004198 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004199 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004200 hashtab_T *cur_ht;
4201 ht_stack_T *ht_stack = NULL;
4202 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004203
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004204 cur_ht = ht;
4205 for (;;)
4206 {
4207 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004208 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004209 // Mark each item in the hashtab. If the item contains a hashtab
4210 // it is added to ht_stack, if it contains a list it is added to
4211 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004212 todo = (int)cur_ht->ht_used;
4213 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4214 if (!HASHITEM_EMPTY(hi))
4215 {
4216 --todo;
4217 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4218 &ht_stack, list_stack);
4219 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004220 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004221
4222 if (ht_stack == NULL)
4223 break;
4224
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004225 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004226 cur_ht = ht_stack->ht;
4227 tempitem = ht_stack;
4228 ht_stack = ht_stack->prev;
4229 free(tempitem);
4230 }
4231
4232 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004233}
4234
4235/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004236 * Mark a dict and its items with "copyID".
4237 * Returns TRUE if setting references failed somehow.
4238 */
4239 int
4240set_ref_in_dict(dict_T *d, int copyID)
4241{
4242 if (d != NULL && d->dv_copyID != copyID)
4243 {
4244 d->dv_copyID = copyID;
4245 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4246 }
4247 return FALSE;
4248}
4249
4250/*
4251 * Mark a list and its items with "copyID".
4252 * Returns TRUE if setting references failed somehow.
4253 */
4254 int
4255set_ref_in_list(list_T *ll, int copyID)
4256{
4257 if (ll != NULL && ll->lv_copyID != copyID)
4258 {
4259 ll->lv_copyID = copyID;
4260 return set_ref_in_list_items(ll, copyID, NULL);
4261 }
4262 return FALSE;
4263}
4264
4265/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004266 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004267 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4268 *
4269 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004270 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004271 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004272set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004273{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004274 listitem_T *li;
4275 int abort = FALSE;
4276 list_T *cur_l;
4277 list_stack_T *list_stack = NULL;
4278 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004279
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004280 cur_l = l;
4281 for (;;)
4282 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004283 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004284 // Mark each item in the list. If the item contains a hashtab
4285 // it is added to ht_stack, if it contains a list it is added to
4286 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004287 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4288 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4289 ht_stack, &list_stack);
4290 if (list_stack == NULL)
4291 break;
4292
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004293 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004294 cur_l = list_stack->list;
4295 tempitem = list_stack;
4296 list_stack = list_stack->prev;
4297 free(tempitem);
4298 }
4299
4300 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004301}
4302
4303/*
4304 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004305 * "list_stack" is used to add lists to be marked. Can be NULL.
4306 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4307 *
4308 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004309 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004310 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004311set_ref_in_item(
4312 typval_T *tv,
4313 int copyID,
4314 ht_stack_T **ht_stack,
4315 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004316{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004317 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004318
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004319 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004320 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004321 dict_T *dd = tv->vval.v_dict;
4322
Bram Moolenaara03f2332016-02-06 18:09:59 +01004323 if (dd != NULL && dd->dv_copyID != copyID)
4324 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004325 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004326 dd->dv_copyID = copyID;
4327 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004328 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004329 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4330 }
4331 else
4332 {
4333 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4334 if (newitem == NULL)
4335 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004336 else
4337 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004338 newitem->ht = &dd->dv_hashtab;
4339 newitem->prev = *ht_stack;
4340 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004341 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004342 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004343 }
4344 }
4345 else if (tv->v_type == VAR_LIST)
4346 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004347 list_T *ll = tv->vval.v_list;
4348
Bram Moolenaara03f2332016-02-06 18:09:59 +01004349 if (ll != NULL && ll->lv_copyID != copyID)
4350 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004351 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004352 ll->lv_copyID = copyID;
4353 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004354 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004355 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004356 }
4357 else
4358 {
4359 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004360 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004361 if (newitem == NULL)
4362 abort = TRUE;
4363 else
4364 {
4365 newitem->list = ll;
4366 newitem->prev = *list_stack;
4367 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004368 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004369 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004370 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004371 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004372 else if (tv->v_type == VAR_FUNC)
4373 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004374 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004375 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004376 else if (tv->v_type == VAR_PARTIAL)
4377 {
4378 partial_T *pt = tv->vval.v_partial;
4379 int i;
4380
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004381 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004382 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004383 // Didn't see this partial yet.
4384 pt->pt_copyID = copyID;
4385
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004386 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004387
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004388 if (pt->pt_dict != NULL)
4389 {
4390 typval_T dtv;
4391
4392 dtv.v_type = VAR_DICT;
4393 dtv.vval.v_dict = pt->pt_dict;
4394 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4395 }
4396
4397 for (i = 0; i < pt->pt_argc; ++i)
4398 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4399 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004400 if (pt->pt_funcstack != NULL)
4401 {
4402 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4403
4404 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4405 abort = abort || set_ref_in_item(stack + i, copyID,
4406 ht_stack, list_stack);
4407 }
4408
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004409 }
4410 }
4411#ifdef FEAT_JOB_CHANNEL
4412 else if (tv->v_type == VAR_JOB)
4413 {
4414 job_T *job = tv->vval.v_job;
4415 typval_T dtv;
4416
4417 if (job != NULL && job->jv_copyID != copyID)
4418 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004419 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004420 if (job->jv_channel != NULL)
4421 {
4422 dtv.v_type = VAR_CHANNEL;
4423 dtv.vval.v_channel = job->jv_channel;
4424 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4425 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004426 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004427 {
4428 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004429 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004430 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4431 }
4432 }
4433 }
4434 else if (tv->v_type == VAR_CHANNEL)
4435 {
4436 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004437 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004438 typval_T dtv;
4439 jsonq_T *jq;
4440 cbq_T *cq;
4441
4442 if (ch != NULL && ch->ch_copyID != copyID)
4443 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004444 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004445 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004446 {
4447 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4448 jq = jq->jq_next)
4449 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4450 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4451 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004452 if (cq->cq_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 = cq->cq_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_part[part].ch_callback.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 =
4462 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004463 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4464 }
4465 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004466 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004467 {
4468 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004469 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004470 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4471 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004472 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004473 {
4474 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004475 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004476 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4477 }
4478 }
4479 }
4480#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004481 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004482}
4483
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004485 * Return a string with the string representation of a variable.
4486 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004487 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004488 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004489 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4490 * stings as "string()", otherwise does not put quotes around strings, as
4491 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004492 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4493 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004494 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004495 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004496 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004497echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004498 typval_T *tv,
4499 char_u **tofree,
4500 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004501 int copyID,
4502 int echo_style,
4503 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004504 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004505{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004506 static int recurse = 0;
4507 char_u *r = NULL;
4508
Bram Moolenaar33570922005-01-25 22:26:29 +00004509 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004510 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004511 if (!did_echo_string_emsg)
4512 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004513 // Only give this message once for a recursive call to avoid
4514 // flooding the user with errors. And stop iterating over lists
4515 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004516 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004517 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004518 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004519 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004520 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004521 }
4522 ++recurse;
4523
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004524 switch (tv->v_type)
4525 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004526 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004527 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004528 {
4529 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004530 r = tv->vval.v_string;
4531 if (r == NULL)
4532 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004533 }
4534 else
4535 {
4536 *tofree = string_quote(tv->vval.v_string, FALSE);
4537 r = *tofree;
4538 }
4539 break;
4540
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004541 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004542 if (echo_style)
4543 {
4544 *tofree = NULL;
4545 r = tv->vval.v_string;
4546 }
4547 else
4548 {
4549 *tofree = string_quote(tv->vval.v_string, TRUE);
4550 r = *tofree;
4551 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004552 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004553
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004554 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004555 {
4556 partial_T *pt = tv->vval.v_partial;
4557 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004558 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004559 garray_T ga;
4560 int i;
4561 char_u *tf;
4562
4563 ga_init2(&ga, 1, 100);
4564 ga_concat(&ga, (char_u *)"function(");
4565 if (fname != NULL)
4566 {
4567 ga_concat(&ga, fname);
4568 vim_free(fname);
4569 }
4570 if (pt != NULL && pt->pt_argc > 0)
4571 {
4572 ga_concat(&ga, (char_u *)", [");
4573 for (i = 0; i < pt->pt_argc; ++i)
4574 {
4575 if (i > 0)
4576 ga_concat(&ga, (char_u *)", ");
4577 ga_concat(&ga,
4578 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4579 vim_free(tf);
4580 }
4581 ga_concat(&ga, (char_u *)"]");
4582 }
4583 if (pt != NULL && pt->pt_dict != NULL)
4584 {
4585 typval_T dtv;
4586
4587 ga_concat(&ga, (char_u *)", ");
4588 dtv.v_type = VAR_DICT;
4589 dtv.vval.v_dict = pt->pt_dict;
4590 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4591 vim_free(tf);
4592 }
4593 ga_concat(&ga, (char_u *)")");
4594
4595 *tofree = ga.ga_data;
4596 r = *tofree;
4597 break;
4598 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004599
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004600 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004601 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004602 break;
4603
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004604 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004605 if (tv->vval.v_list == NULL)
4606 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004607 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004608 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004609 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004610 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004611 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4612 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004613 {
4614 *tofree = NULL;
4615 r = (char_u *)"[...]";
4616 }
4617 else
4618 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004619 int old_copyID = tv->vval.v_list->lv_copyID;
4620
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004621 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004622 *tofree = list2string(tv, copyID, restore_copyID);
4623 if (restore_copyID)
4624 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004625 r = *tofree;
4626 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004627 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004628
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004630 if (tv->vval.v_dict == NULL)
4631 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004632 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004633 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004634 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004635 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004636 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4637 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004638 {
4639 *tofree = NULL;
4640 r = (char_u *)"{...}";
4641 }
4642 else
4643 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004644 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004645
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004646 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004647 *tofree = dict2string(tv, copyID, restore_copyID);
4648 if (restore_copyID)
4649 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004650 r = *tofree;
4651 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004652 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004653
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004654 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004655 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004656 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004658 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004659 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004660 break;
4661
Bram Moolenaar835dc632016-02-07 14:27:38 +01004662 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004663 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004664 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004665 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004666 if (composite_val)
4667 {
4668 *tofree = string_quote(r, FALSE);
4669 r = *tofree;
4670 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004671 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004672
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004673 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004674#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004675 *tofree = NULL;
4676 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4677 r = numbuf;
4678 break;
4679#endif
4680
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004681 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004682 case VAR_SPECIAL:
4683 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004684 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004685 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004687
Bram Moolenaar8502c702014-06-17 12:51:16 +02004688 if (--recurse == 0)
4689 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004690 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004691}
4692
4693/*
4694 * Return a string with the string representation of a variable.
4695 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4696 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004697 * Does not put quotes around strings, as ":echo" displays values.
4698 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4699 * May return NULL.
4700 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004701 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004702echo_string(
4703 typval_T *tv,
4704 char_u **tofree,
4705 char_u *numbuf,
4706 int copyID)
4707{
4708 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4709}
4710
4711/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004712 * Return string "str" in ' quotes, doubling ' characters.
4713 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004714 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004715 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004716 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004717string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004718{
Bram Moolenaar33570922005-01-25 22:26:29 +00004719 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004720 char_u *p, *r, *s;
4721
Bram Moolenaar33570922005-01-25 22:26:29 +00004722 len = (function ? 13 : 3);
4723 if (str != NULL)
4724 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004725 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004726 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004727 if (*p == '\'')
4728 ++len;
4729 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004730 s = r = alloc(len);
4731 if (r != NULL)
4732 {
4733 if (function)
4734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004735 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004736 r += 10;
4737 }
4738 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004739 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004740 if (str != NULL)
4741 for (p = str; *p != NUL; )
4742 {
4743 if (*p == '\'')
4744 *r++ = '\'';
4745 MB_COPY_CHAR(p, r);
4746 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004747 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004748 if (function)
4749 *r++ = ')';
4750 *r++ = NUL;
4751 }
4752 return s;
4753}
4754
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004755#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004756/*
4757 * Convert the string "text" to a floating point number.
4758 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4759 * this always uses a decimal point.
4760 * Returns the length of the text that was consumed.
4761 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004762 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004763string2float(
4764 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004765 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004766{
4767 char *s = (char *)text;
4768 float_T f;
4769
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004770 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004771 if (STRNICMP(text, "inf", 3) == 0)
4772 {
4773 *value = INFINITY;
4774 return 3;
4775 }
4776 if (STRNICMP(text, "-inf", 3) == 0)
4777 {
4778 *value = -INFINITY;
4779 return 4;
4780 }
4781 if (STRNICMP(text, "nan", 3) == 0)
4782 {
4783 *value = NAN;
4784 return 3;
4785 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 f = strtod(s, &s);
4787 *value = f;
4788 return (int)((char_u *)s - text);
4789}
4790#endif
4791
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004794 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004796 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004797var2fpos(
4798 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004799 int dollar_lnum, // TRUE when $ is last line
4800 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004802 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004804 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004806 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004807 if (varp->v_type == VAR_LIST)
4808 {
4809 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004810 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004811 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004812 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004813
4814 l = varp->vval.v_list;
4815 if (l == NULL)
4816 return NULL;
4817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004818 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004819 pos.lnum = list_find_nr(l, 0L, &error);
4820 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004821 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004823 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004824 pos.col = list_find_nr(l, 1L, &error);
4825 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004826 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004827 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004829 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004830 li = list_find(l, 1L);
4831 if (li != NULL && li->li_tv.v_type == VAR_STRING
4832 && li->li_tv.vval.v_string != NULL
4833 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4834 pos.col = len + 1;
4835
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004836 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004837 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004838 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004839 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004840
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004841 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004842 pos.coladd = list_find_nr(l, 2L, &error);
4843 if (error)
4844 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004845
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004846 return &pos;
4847 }
4848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004849 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004850 if (name == NULL)
4851 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004852 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004854 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004855 {
4856 if (VIsual_active)
4857 return &VIsual;
4858 return &curwin->w_cursor;
4859 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004860 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004862 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4864 return NULL;
4865 return pp;
4866 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004867
Bram Moolenaara5525202006-03-02 22:52:09 +00004868 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004869
Bram Moolenaar477933c2007-07-17 14:32:23 +00004870 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004871 {
4872 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004873 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004874 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004875 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004876 // In silent Ex mode topline is zero, but that's not a valid line
4877 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004878 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004879 return &pos;
4880 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004881 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004882 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004883 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004884 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004885 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004886 return &pos;
4887 }
4888 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004889 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004891 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 {
4893 pos.lnum = curbuf->b_ml.ml_line_count;
4894 pos.col = 0;
4895 }
4896 else
4897 {
4898 pos.lnum = curwin->w_cursor.lnum;
4899 pos.col = (colnr_T)STRLEN(ml_get_curline());
4900 }
4901 return &pos;
4902 }
4903 return NULL;
4904}
4905
4906/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004907 * Convert list in "arg" into a position and optional file number.
4908 * When "fnump" is NULL there is no file number, only 3 items.
4909 * Note that the column is passed on as-is, the caller may want to decrement
4910 * it to use 1 for the first column.
4911 * Return FAIL when conversion is not possible, doesn't check the position for
4912 * validity.
4913 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004914 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004915list2fpos(
4916 typval_T *arg,
4917 pos_T *posp,
4918 int *fnump,
4919 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004920{
4921 list_T *l = arg->vval.v_list;
4922 long i = 0;
4923 long n;
4924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004925 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4926 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004927 if (arg->v_type != VAR_LIST
4928 || l == NULL
4929 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004930 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004931 return FAIL;
4932
4933 if (fnump != NULL)
4934 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004935 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004936 if (n < 0)
4937 return FAIL;
4938 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004939 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004940 *fnump = n;
4941 }
4942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004943 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004944 if (n < 0)
4945 return FAIL;
4946 posp->lnum = n;
4947
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004948 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004949 if (n < 0)
4950 return FAIL;
4951 posp->col = n;
4952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004953 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004954 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004955 posp->coladd = 0;
4956 else
4957 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004958
Bram Moolenaar493c1782014-05-28 14:34:46 +02004959 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004960 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004961
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004962 return OK;
4963}
4964
4965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 * Get the length of an environment variable name.
4967 * Advance "arg" to the first character after the name.
4968 * Return 0 for error.
4969 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004970 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004971get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972{
4973 char_u *p;
4974 int len;
4975
4976 for (p = *arg; vim_isIDc(*p); ++p)
4977 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004978 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 return 0;
4980
4981 len = (int)(p - *arg);
4982 *arg = p;
4983 return len;
4984}
4985
4986/*
4987 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004988 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 * Return 0 if something is wrong.
4990 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004991 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004992get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993{
4994 char_u *p;
4995 int len;
4996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004997 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004999 {
5000 if (*p == ':')
5001 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005002 // "s:" is start of "s:var", but "n:" is not and can be used in
5003 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005004 len = (int)(p - *arg);
5005 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5006 || len > 1)
5007 break;
5008 }
5009 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005010 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 return 0;
5012
5013 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005014 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
5016 return len;
5017}
5018
5019/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005020 * Get the length of the name of a variable or function.
5021 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005023 * Return -1 if curly braces expansion failed.
5024 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 * If the name contains 'magic' {}'s, expand them and return the
5026 * expanded name in an allocated string via 'alias' - caller must free.
5027 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005028 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005029get_name_len(
5030 char_u **arg,
5031 char_u **alias,
5032 int evaluate,
5033 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034{
5035 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 char_u *p;
5037 char_u *expr_start;
5038 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005040 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
5042 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5043 && (*arg)[2] == (int)KE_SNR)
5044 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005045 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 *arg += 3;
5047 return get_id_len(arg) + 3;
5048 }
5049 len = eval_fname_script(*arg);
5050 if (len > 0)
5051 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005052 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 *arg += len;
5054 }
5055
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005057 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005059 p = find_name_end(*arg, &expr_start, &expr_end,
5060 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 if (expr_start != NULL)
5062 {
5063 char_u *temp_string;
5064
5065 if (!evaluate)
5066 {
5067 len += (int)(p - *arg);
5068 *arg = skipwhite(p);
5069 return len;
5070 }
5071
5072 /*
5073 * Include any <SID> etc in the expanded string:
5074 * Thus the -len here.
5075 */
5076 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5077 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005078 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 *alias = temp_string;
5080 *arg = skipwhite(p);
5081 return (int)STRLEN(temp_string);
5082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
5084 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005085 // Only give an error when there is something, otherwise it will be
5086 // reported at a higher level.
5087 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005088 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089
5090 return len;
5091}
5092
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093/*
5094 * Find the end of a variable or function name, taking care of magic braces.
5095 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5096 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005097 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005098 * Return a pointer to just after the name. Equal to "arg" if there is no
5099 * valid name.
5100 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005101 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005102find_name_end(
5103 char_u *arg,
5104 char_u **expr_start,
5105 char_u **expr_end,
5106 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005108 int mb_nest = 0;
5109 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005111 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005112 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005114 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 *expr_start = NULL;
5117 *expr_end = NULL;
5118 }
5119
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005120 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005121 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5122 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005123 return arg;
5124
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005125 for (p = arg; *p != NUL
5126 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005127 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005128 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005129 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005130 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005131 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005132 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005133 if (*p == '\'')
5134 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005135 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005136 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005137 ;
5138 if (*p == NUL)
5139 break;
5140 }
5141 else if (*p == '"')
5142 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005143 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005144 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005145 if (*p == '\\' && p[1] != NUL)
5146 ++p;
5147 if (*p == NUL)
5148 break;
5149 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005150 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005152 // "s:" is start of "s:var", but "n:" is not and can be used in
5153 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005154 len = (int)(p - arg);
5155 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005156 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005157 break;
5158 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005159
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 if (*p == '[')
5163 ++br_nest;
5164 else if (*p == ']')
5165 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005167
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005168 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005170 if (*p == '{')
5171 {
5172 mb_nest++;
5173 if (expr_start != NULL && *expr_start == NULL)
5174 *expr_start = p;
5175 }
5176 else if (*p == '}')
5177 {
5178 mb_nest--;
5179 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5180 *expr_end = p;
5181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
5184
5185 return p;
5186}
5187
5188/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005189 * Expands out the 'magic' {}'s in a variable/function name.
5190 * Note that this can call itself recursively, to deal with
5191 * constructs like foo{bar}{baz}{bam}
5192 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5193 * "in_start" ^
5194 * "expr_start" ^
5195 * "expr_end" ^
5196 * "in_end" ^
5197 *
5198 * Returns a new allocated string, which the caller must free.
5199 * Returns NULL for failure.
5200 */
5201 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005202make_expanded_name(
5203 char_u *in_start,
5204 char_u *expr_start,
5205 char_u *expr_end,
5206 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005207{
5208 char_u c1;
5209 char_u *retval = NULL;
5210 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005211
5212 if (expr_end == NULL || in_end == NULL)
5213 return NULL;
5214 *expr_start = NUL;
5215 *expr_end = NUL;
5216 c1 = *in_end;
5217 *in_end = NUL;
5218
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005219 temp_result = eval_to_string(expr_start + 1, FALSE);
5220 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005221 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005222 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5223 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005224 if (retval != NULL)
5225 {
5226 STRCPY(retval, in_start);
5227 STRCAT(retval, temp_result);
5228 STRCAT(retval, expr_end + 1);
5229 }
5230 }
5231 vim_free(temp_result);
5232
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005233 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005234 *expr_start = '{';
5235 *expr_end = '}';
5236
5237 if (retval != NULL)
5238 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005239 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005240 if (expr_start != NULL)
5241 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005242 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005243 temp_result = make_expanded_name(retval, expr_start,
5244 expr_end, temp_result);
5245 vim_free(retval);
5246 retval = temp_result;
5247 }
5248 }
5249
5250 return retval;
5251}
5252
5253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005255 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005257 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005258eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005260 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005261}
5262
5263/*
5264 * Return TRUE if character "c" can be used as the first character in a
5265 * variable or function name (excluding '{' and '}').
5266 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005268eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005269{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005270 return ASCII_ISALPHA(c) || c == '_';
5271}
5272
5273/*
5274 * Return TRUE if character "c" can be used as the first character of a
5275 * dictionary key.
5276 */
5277 int
5278eval_isdictc(int c)
5279{
5280 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281}
5282
5283/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005284 * Handle:
5285 * - expr[expr], expr[expr:expr] subscript
5286 * - ".name" lookup
5287 * - function call with Funcref variable: func(expr)
5288 * - method call: var->method()
5289 *
5290 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005291 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005293handle_subscript(
5294 char_u **arg,
5295 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005296 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005297 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005298{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005299 int evaluate = evalarg != NULL
5300 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005301 int ret = OK;
5302 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005303 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005304 int getnext;
5305 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005306
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005307 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005308 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005309 // When at the end of the line and ".name" or "->{" or "->X" follows in
5310 // the next line then consume the line break.
5311 p = eval_next_non_blank(*arg, evalarg, &getnext);
5312 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005313 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005314 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005315 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005316 {
5317 *arg = eval_next_line(evalarg);
5318 check_white = FALSE;
5319 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005320
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005321 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5322 || rettv->v_type == VAR_PARTIAL))
5323 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005324 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005325 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5326 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005327
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005328 // Stop the expression evaluation when immediately aborting on
5329 // error, or when an interrupt occurred or an exception was thrown
5330 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005331 if (aborting())
5332 {
5333 if (ret == OK)
5334 clear_tv(rettv);
5335 ret = FAIL;
5336 }
5337 dict_unref(selfdict);
5338 selfdict = NULL;
5339 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005340 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005341 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005342 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005343 if (ret == OK)
5344 {
5345 if ((*arg)[2] == '{')
5346 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005347 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005348 else
5349 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005350 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005351 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005352 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005353 // "." is ".name" lookup when we found a dict or when evaluating and
5354 // scriptversion is at least 2, where string concatenation is "..".
5355 else if (**arg == '['
5356 || (**arg == '.' && (rettv->v_type == VAR_DICT
5357 || (!evaluate
5358 && (*arg)[1] != '.'
5359 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005360 {
5361 dict_unref(selfdict);
5362 if (rettv->v_type == VAR_DICT)
5363 {
5364 selfdict = rettv->vval.v_dict;
5365 if (selfdict != NULL)
5366 ++selfdict->dv_refcount;
5367 }
5368 else
5369 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005370 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005371 {
5372 clear_tv(rettv);
5373 ret = FAIL;
5374 }
5375 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005376 else
5377 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005378 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005379
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005380 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5381 // Don't do this when "Func" is already a partial that was bound
5382 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005383 if (selfdict != NULL
5384 && (rettv->v_type == VAR_FUNC
5385 || (rettv->v_type == VAR_PARTIAL
5386 && (rettv->vval.v_partial->pt_auto
5387 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005388 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005389
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005390 dict_unref(selfdict);
5391 return ret;
5392}
5393
5394/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005395 * Make a copy of an item.
5396 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005397 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5398 * reference to an already copied list/dict can be used.
5399 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005400 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005402item_copy(
5403 typval_T *from,
5404 typval_T *to,
5405 int deep,
5406 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005407{
5408 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005409 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005410
Bram Moolenaar33570922005-01-25 22:26:29 +00005411 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005413 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005414 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005415 }
5416 ++recurse;
5417
5418 switch (from->v_type)
5419 {
5420 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005421 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005422 case VAR_STRING:
5423 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005424 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005425 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005426 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005427 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005428 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005429 copy_tv(from, to);
5430 break;
5431 case VAR_LIST:
5432 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005433 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005434 if (from->vval.v_list == NULL)
5435 to->vval.v_list = NULL;
5436 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5437 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005438 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005439 to->vval.v_list = from->vval.v_list->lv_copylist;
5440 ++to->vval.v_list->lv_refcount;
5441 }
5442 else
5443 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5444 if (to->vval.v_list == NULL)
5445 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005446 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005447 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005449 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005450 case VAR_DICT:
5451 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005452 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005453 if (from->vval.v_dict == NULL)
5454 to->vval.v_dict = NULL;
5455 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5456 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005457 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005458 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5459 ++to->vval.v_dict->dv_refcount;
5460 }
5461 else
5462 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5463 if (to->vval.v_dict == NULL)
5464 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005465 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005466 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005467 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005469 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005470 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005471 }
5472 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005473 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005474}
5475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005476 void
5477echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5478{
5479 char_u *tofree;
5480 char_u numbuf[NUMBUFLEN];
5481 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5482
5483 if (*atstart)
5484 {
5485 *atstart = FALSE;
5486 // Call msg_start() after eval1(), evaluating the expression
5487 // may cause a message to appear.
5488 if (with_space)
5489 {
5490 // Mark the saved text as finishing the line, so that what
5491 // follows is displayed on a new line when scrolling back
5492 // at the more prompt.
5493 msg_sb_eol();
5494 msg_start();
5495 }
5496 }
5497 else if (with_space)
5498 msg_puts_attr(" ", echo_attr);
5499
5500 if (p != NULL)
5501 for ( ; *p != NUL && !got_int; ++p)
5502 {
5503 if (*p == '\n' || *p == '\r' || *p == TAB)
5504 {
5505 if (*p != TAB && *needclr)
5506 {
5507 // remove any text still there from the command
5508 msg_clr_eos();
5509 *needclr = FALSE;
5510 }
5511 msg_putchar_attr(*p, echo_attr);
5512 }
5513 else
5514 {
5515 if (has_mbyte)
5516 {
5517 int i = (*mb_ptr2len)(p);
5518
5519 (void)msg_outtrans_len_attr(p, i, echo_attr);
5520 p += i - 1;
5521 }
5522 else
5523 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5524 }
5525 }
5526 vim_free(tofree);
5527}
5528
Bram Moolenaare9a41262005-01-15 22:18:47 +00005529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 * ":echo expr1 ..." print each argument separated with a space, add a
5531 * newline at the end.
5532 * ":echon expr1 ..." print each argument plain.
5533 */
5534 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005535ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005538 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 char_u *p;
5540 int needclr = TRUE;
5541 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005542 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005543 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005544 evalarg_T evalarg;
5545
Bram Moolenaare707c882020-07-01 13:07:37 +02005546 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548 if (eap->skip)
5549 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005550 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005552 // If eval1() causes an error message the text from the command may
5553 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005554 need_clr_eos = needclr;
5555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005557 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 {
5559 /*
5560 * Report the invalid expression unless the expression evaluation
5561 * has been cancelled due to an aborting error, an interrupt, or an
5562 * exception.
5563 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005564 if (!aborting() && did_emsg == did_emsg_before
5565 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005566 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005567 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 break;
5569 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005570 need_clr_eos = FALSE;
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005573 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005574
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 arg = skipwhite(arg);
5577 }
5578 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005579 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580
5581 if (eap->skip)
5582 --emsg_skip;
5583 else
5584 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005585 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 if (needclr)
5587 msg_clr_eos();
5588 if (eap->cmdidx == CMD_echo)
5589 msg_end();
5590 }
5591}
5592
5593/*
5594 * ":echohl {name}".
5595 */
5596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005597ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005599 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600}
5601
5602/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005603 * Returns the :echo attribute
5604 */
5605 int
5606get_echo_attr(void)
5607{
5608 return echo_attr;
5609}
5610
5611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 * ":execute expr1 ..." execute the result of an expression.
5613 * ":echomsg expr1 ..." Print a message
5614 * ":echoerr expr1 ..." Print an error
5615 * Each gets spaces around each argument and a newline at the end for
5616 * echo commands
5617 */
5618 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005619ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620{
5621 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 int ret = OK;
5624 char_u *p;
5625 garray_T ga;
5626 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
5628 ga_init2(&ga, 1, 80);
5629
5630 if (eap->skip)
5631 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005632 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005634 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005635 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
5638 if (!eap->skip)
5639 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005640 char_u buf[NUMBUFLEN];
5641
5642 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005643 {
5644 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5645 {
5646 emsg(_(e_inval_string));
5647 p = NULL;
5648 }
5649 else
5650 p = tv_get_string_buf(&rettv, buf);
5651 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005652 else
5653 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005654 if (p == NULL)
5655 {
5656 clear_tv(&rettv);
5657 ret = FAIL;
5658 break;
5659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 len = (int)STRLEN(p);
5661 if (ga_grow(&ga, len + 2) == FAIL)
5662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 ret = FAIL;
5665 break;
5666 }
5667 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 ga.ga_len += len;
5671 }
5672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 arg = skipwhite(arg);
5675 }
5676
5677 if (ret != FAIL && ga.ga_data != NULL)
5678 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005679 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5680 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005681 // Mark the already saved text as finishing the line, so that what
5682 // follows is displayed on a new line when scrolling back at the
5683 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005684 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005685 }
5686
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005688 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005689 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005690 out_flush();
5691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 else if (eap->cmdidx == CMD_echoerr)
5693 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005694 int save_did_emsg = did_emsg;
5695
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005696 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005697 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 if (!force_abort)
5699 did_emsg = save_did_emsg;
5700 }
5701 else if (eap->cmdidx == CMD_execute)
5702 do_cmdline((char_u *)ga.ga_data,
5703 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5704 }
5705
5706 ga_clear(&ga);
5707
5708 if (eap->skip)
5709 --emsg_skip;
5710
5711 eap->nextcmd = check_nextcmd(arg);
5712}
5713
5714/*
5715 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5716 * "arg" points to the "&" or '+' when called, to "option" when returning.
5717 * Returns NULL when no option name found. Otherwise pointer to the char
5718 * after the option name.
5719 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005720 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005721find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 char_u *p = *arg;
5724
5725 ++p;
5726 if (*p == 'g' && p[1] == ':')
5727 {
5728 *opt_flags = OPT_GLOBAL;
5729 p += 2;
5730 }
5731 else if (*p == 'l' && p[1] == ':')
5732 {
5733 *opt_flags = OPT_LOCAL;
5734 p += 2;
5735 }
5736 else
5737 *opt_flags = 0;
5738
5739 if (!ASCII_ISALPHA(*p))
5740 return NULL;
5741 *arg = p;
5742
5743 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005744 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 else
5746 while (ASCII_ISALPHA(*p))
5747 ++p;
5748 return p;
5749}
5750
5751/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005752 * Display script name where an item was last set.
5753 * Should only be invoked when 'verbose' is non-zero.
5754 */
5755 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005756last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005757{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005758 char_u *p;
5759
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005760 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005761 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005762 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005763 if (p != NULL)
5764 {
5765 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005766 msg_puts(_("\n\tLast set from "));
5767 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005768 if (script_ctx.sc_lnum > 0)
5769 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005770 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005771 msg_outnum((long)script_ctx.sc_lnum);
5772 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005773 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005774 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005775 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005776 }
5777}
5778
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005779#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
5781/*
5782 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005783 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 * "flags" can be "g" to do a global substitute.
5785 * Returns an allocated string, NULL for error.
5786 */
5787 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005788do_string_sub(
5789 char_u *str,
5790 char_u *pat,
5791 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005792 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005793 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 int sublen;
5796 regmatch_T regmatch;
5797 int i;
5798 int do_all;
5799 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005800 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 garray_T ga;
5802 char_u *ret;
5803 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005804 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005806 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005808 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
5810 ga_init2(&ga, 1, 200);
5811
5812 do_all = (flags[0] == 'g');
5813
5814 regmatch.rm_ic = p_ic;
5815 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5816 if (regmatch.regprog != NULL)
5817 {
5818 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005819 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5821 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005822 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005823 if (regmatch.startp[0] == regmatch.endp[0])
5824 {
5825 if (zero_width == regmatch.startp[0])
5826 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005827 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005828 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005829 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5830 (size_t)i);
5831 ga.ga_len += i;
5832 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005833 continue;
5834 }
5835 zero_width = regmatch.startp[0];
5836 }
5837
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 /*
5839 * Get some space for a temporary buffer to do the substitution
5840 * into. It will contain:
5841 * - The text up to where the match is.
5842 * - The substituted text.
5843 * - The text after the match.
5844 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005845 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005846 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5848 {
5849 ga_clear(&ga);
5850 break;
5851 }
5852
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005853 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 i = (int)(regmatch.startp[0] - tail);
5855 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005856 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005857 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 + ga.ga_len + i, TRUE, TRUE, FALSE);
5859 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005860 tail = regmatch.endp[0];
5861 if (*tail == NUL)
5862 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 if (!do_all)
5864 break;
5865 }
5866
5867 if (ga.ga_data != NULL)
5868 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5869
Bram Moolenaar473de612013-06-08 18:19:48 +02005870 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 }
5872
5873 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5874 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005875 if (p_cpo == empty_option)
5876 p_cpo = save_cpo;
5877 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005878 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005879 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880
5881 return ret;
5882}