blob: 060fd0c7d804d158f904d5c9adb237752da0715d [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 Moolenaar37c83712020-06-30 21:18:36 +0200156 static void
157fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
158{
159 CLEAR_FIELD(*evalarg);
160 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
161 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
162 {
163 evalarg->eval_getline = eap->getline;
164 evalarg->eval_cookie = eap->cookie;
165 }
166}
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
169 * Top level evaluation function, returning a boolean.
170 * Sets "error" to TRUE if there was an error.
171 * Return TRUE or FALSE.
172 */
173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100174eval_to_bool(
175 char_u *arg,
176 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200177 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100178 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179{
Bram Moolenaar33570922005-01-25 22:26:29 +0000180 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200181 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200182 evalarg_T evalarg;
183
Bram Moolenaar37c83712020-06-30 21:18:36 +0200184 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186 if (skip)
187 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200188 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 else
191 {
192 *error = FALSE;
193 if (!skip)
194 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100195 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000196 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 }
198 }
199 if (skip)
200 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200201 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200203 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204}
205
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206/*
207 * Call eval1() and give an error message if not done at a lower level.
208 */
209 static int
210eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
211{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100212 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100213 int ret;
214 int did_emsg_before = did_emsg;
215 int called_emsg_before = called_emsg;
216
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200217 ret = eval1(arg, rettv, evaluate ? &EVALARG_EVALUATE : NULL);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218 if (ret == FAIL)
219 {
220 // Report the invalid expression unless the expression evaluation has
221 // been cancelled due to an aborting error, an interrupt, or an
222 // exception, or we already gave a more specific error.
223 // Also check called_emsg for when using assert_fails().
224 if (!aborting() && did_emsg == did_emsg_before
225 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100226 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100227 }
228 return ret;
229}
230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100231/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200232 * Return whether a typval is a valid expression to pass to eval_expr_typval()
233 * or eval_expr_to_bool(). An empty string returns FALSE;
234 */
235 int
236eval_expr_valid_arg(typval_T *tv)
237{
238 return tv->v_type != VAR_UNKNOWN
239 && (tv->v_type != VAR_STRING
240 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
241}
242
243/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244 * Evaluate an expression, which can be a function, partial or string.
245 * Pass arguments "argv[argc]".
246 * Return the result in "rettv" and OK or FAIL.
247 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200248 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100249eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
250{
251 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100252 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200253 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100254
255 if (expr->v_type == VAR_FUNC)
256 {
257 s = expr->vval.v_string;
258 if (s == NULL || *s == NUL)
259 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200260 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200261 funcexe.evaluate = TRUE;
262 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100263 return FAIL;
264 }
265 else if (expr->v_type == VAR_PARTIAL)
266 {
267 partial_T *partial = expr->vval.v_partial;
268
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200269 if (partial == NULL)
270 return FAIL;
271
Bram Moolenaar822ba242020-05-24 23:00:18 +0200272 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200273 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200275 if (call_def_function(partial->pt_func, argc, argv,
276 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 return FAIL;
278 }
279 else
280 {
281 s = partial_name(partial);
282 if (s == NULL || *s == NUL)
283 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200284 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 funcexe.evaluate = TRUE;
286 funcexe.partial = partial;
287 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
288 return FAIL;
289 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100290 }
291 else
292 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100293 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 if (s == NULL)
295 return FAIL;
296 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100297 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100299 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100300 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200301 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100302 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100303 return FAIL;
304 }
305 }
306 return OK;
307}
308
309/*
310 * Like eval_to_bool() but using a typval_T instead of a string.
311 * Works for string, funcref and partial.
312 */
313 int
314eval_expr_to_bool(typval_T *expr, int *error)
315{
316 typval_T rettv;
317 int res;
318
319 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
320 {
321 *error = TRUE;
322 return FALSE;
323 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100324 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100325 clear_tv(&rettv);
326 return res;
327}
328
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329/*
330 * Top level evaluation function, returning a string. If "skip" is TRUE,
331 * only parsing to "nextcmd" is done, without reporting errors. Return
332 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
333 */
334 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100335eval_to_string_skip(
336 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200337 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100338 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
Bram Moolenaar33570922005-01-25 22:26:29 +0000340 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200342 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
Bram Moolenaar37c83712020-06-30 21:18:36 +0200344 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 if (skip)
346 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200347 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 retval = NULL;
349 else
350 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100351 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000352 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 }
354 if (skip)
355 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200356 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
358 return retval;
359}
360
361/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000362 * Skip over an expression at "*pp".
363 * Return FAIL for an error, OK otherwise.
364 */
365 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100366skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367{
Bram Moolenaar33570922005-01-25 22:26:29 +0000368 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000369
370 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200371 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000372}
373
374/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200375 * Skip over an expression at "*pp".
376 * If in Vim9 script and line breaks are encountered, the lines are
377 * concatenated. "evalarg->eval_tofree" will be set accordingly.
378 * Return FAIL for an error, OK otherwise.
379 */
380 int
381skip_expr_concatenate(char_u **start, char_u **end, evalarg_T *evalarg)
382{
383 typval_T rettv;
384 int res;
385 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
386 garray_T *gap = &evalarg->eval_ga;
387 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
388
389 if (vim9script && evalarg->eval_cookie != NULL)
390 {
391 ga_init2(gap, sizeof(char_u *), 10);
392 if (ga_grow(gap, 1) == OK)
393 // leave room for "start"
394 ++gap->ga_len;
395 }
396
397 // Don't evaluate the expression.
398 if (evalarg != NULL)
399 evalarg->eval_flags &= ~EVAL_EVALUATE;
400 *end = skipwhite(*end);
401 res = eval1(end, &rettv, evalarg);
402 if (evalarg != NULL)
403 evalarg->eval_flags = save_flags;
404
405 if (vim9script && evalarg->eval_cookie != NULL
406 && evalarg->eval_ga.ga_len > 1)
407 {
408 char_u *p;
409 size_t endoff = STRLEN(*end);
410
411 // Line breaks encountered, concatenate all the lines.
412 *((char_u **)gap->ga_data) = *start;
413 p = ga_concat_strings(gap, "");
414 *((char_u **)gap->ga_data) = NULL;
415 ga_clear_strings(gap);
416 gap->ga_itemsize = 0;
417 if (p == NULL)
418 return FAIL;
419 *start = p;
420 vim_free(evalarg->eval_tofree);
421 evalarg->eval_tofree = p;
422 // Compute "end" relative to the end.
423 *end = *start + STRLEN(*start) - endoff;
424 }
425
426 return res;
427}
428
429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000431 * When "convert" is TRUE convert a List into a sequence of lines and convert
432 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 * Return pointer to allocated memory, or NULL for failure.
434 */
435 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100436eval_to_string(
437 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100438 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439{
Bram Moolenaar33570922005-01-25 22:26:29 +0000440 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000442 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000443#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000444 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200447 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 retval = NULL;
449 else
450 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000451 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000452 {
453 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000454 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200455 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200456 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200457 if (tv.vval.v_list->lv_len > 0)
458 ga_append(&ga, NL);
459 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000460 ga_append(&ga, NUL);
461 retval = (char_u *)ga.ga_data;
462 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000463#ifdef FEAT_FLOAT
464 else if (convert && tv.v_type == VAR_FLOAT)
465 {
466 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
467 retval = vim_strsave(numbuf);
468 }
469#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000470 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100471 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000472 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200474 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475
476 return retval;
477}
478
479/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000480 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200481 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 */
483 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100484eval_to_string_safe(
485 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100486 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
488 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200489 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200491 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000492 if (use_sandbox)
493 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200494 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200495 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000496 if (use_sandbox)
497 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200498 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200499 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 return retval;
501}
502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503/*
504 * Top level evaluation function, returning a number.
505 * Evaluates "expr" silently.
506 * Returns -1 for an error.
507 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200508 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100509eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510{
Bram Moolenaar33570922005-01-25 22:26:29 +0000511 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200512 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000513 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514
515 ++emsg_off;
516
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200517 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 retval = -1;
519 else
520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100521 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000522 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 }
524 --emsg_off;
525
526 return retval;
527}
528
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000529/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000530 * Top level evaluation function.
531 * Returns an allocated typval_T with the result.
532 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000533 */
534 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200535eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000536{
537 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200538 evalarg_T evalarg;
539
540 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000541
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200542 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200543 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100544 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000545
Bram Moolenaar37c83712020-06-30 21:18:36 +0200546 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000547 return tv;
548}
549
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100551 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200552 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
553 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000554 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200556 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100557call_vim_function(
558 char_u *func,
559 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200560 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200561 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000563 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200564 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100566 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200567 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200568 funcexe.firstline = curwin->w_cursor.lnum;
569 funcexe.lastline = curwin->w_cursor.lnum;
570 funcexe.evaluate = TRUE;
571 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000572 if (ret == FAIL)
573 clear_tv(rettv);
574
575 return ret;
576}
577
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100578/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100579 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100580 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200581 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
582 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100583 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200584 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100585call_func_retnr(
586 char_u *func,
587 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200588 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100589{
590 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200591 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100592
Bram Moolenaarded27a12018-08-01 19:06:03 +0200593 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100594 return -1;
595
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100596 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100597 clear_tv(&rettv);
598 return retval;
599}
600
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000601/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100602 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000603 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200604 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
605 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000606 */
607 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100608call_func_retstr(
609 char_u *func,
610 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200611 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000612{
613 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000614 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000615
Bram Moolenaarded27a12018-08-01 19:06:03 +0200616 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000617 return NULL;
618
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100619 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000620 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 return retval;
622}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000623
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000624/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100625 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200626 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
627 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000628 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000629 */
630 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631call_func_retlist(
632 char_u *func,
633 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200634 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000635{
636 typval_T rettv;
637
Bram Moolenaarded27a12018-08-01 19:06:03 +0200638 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000639 return NULL;
640
641 if (rettv.v_type != VAR_LIST)
642 {
643 clear_tv(&rettv);
644 return NULL;
645 }
646
647 return rettv.vval.v_list;
648}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650#ifdef FEAT_FOLDING
651/*
652 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
653 * it in "*cp". Doesn't give error messages.
654 */
655 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100656eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657{
Bram Moolenaar33570922005-01-25 22:26:29 +0000658 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200659 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000661 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
662 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000665 if (use_sandbox)
666 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200667 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200669 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 retval = 0;
671 else
672 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100673 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000674 if (tv.v_type == VAR_NUMBER)
675 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000676 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 retval = 0;
678 else
679 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100680 // If the result is a string, check if there is a non-digit before
681 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000682 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 if (!VIM_ISDIGIT(*s) && *s != '-')
684 *cp = *s++;
685 retval = atol((char *)s);
686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000687 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
689 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000690 if (use_sandbox)
691 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200692 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200693 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200695 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696}
697#endif
698
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000700 * Get an lval: variable, Dict item or List item that can be assigned a value
701 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
702 * "name.key", "name.key[expr]" etc.
703 * Indexing only works if "name" is an existing List or Dictionary.
704 * "name" points to the start of the name.
705 * If "rettv" is not NULL it points to the value to be assigned.
706 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
707 * wrong; must end in space or cmd separator.
708 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100709 * flags:
710 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100711 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100712 * GLV_NO_AUTOLOAD: do not use script autoloading
713 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000714 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000715 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000716 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000717 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200718 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100719get_lval(
720 char_u *name,
721 typval_T *rettv,
722 lval_T *lp,
723 int unlet,
724 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100725 int flags, // GLV_ values
726 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000727{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000728 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000729 char_u *expr_start, *expr_end;
730 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000731 dictitem_T *v;
732 typval_T var1;
733 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000734 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000735 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000736 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000737 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000738 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100739 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100741 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200742 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000743
744 if (skip)
745 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100746 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000747 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200748 lp->ll_name_end = find_name_end(name, NULL, NULL,
749 FNE_INCL_BR | fne_flags);
750 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000751 }
752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100753 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000754 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000756 if (expr_start != NULL)
757 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100758 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100759 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000760 && *p != '[' && *p != '.')
761 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100762 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000763 return NULL;
764 }
765
766 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
767 if (lp->ll_exp_name == NULL)
768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100769 // Report an invalid expression in braces, unless the
770 // expression evaluation has been cancelled due to an
771 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 if (!aborting() && !quiet)
773 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000774 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100775 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000776 return NULL;
777 }
778 }
779 lp->ll_name = lp->ll_exp_name;
780 }
781 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000783 lp->ll_name = name;
784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
786 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100787 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788 char_u *tp = skipwhite(p + 1);
789
790 // parse the type after the name
791 lp->ll_type = parse_type(&tp, &si->sn_type_list);
792 lp->ll_name_end = tp;
793 }
794 }
795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100796 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000797 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
798 return p;
799
800 cc = *p;
801 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100802 // Only pass &ht when we would write to the variable, it prevents autoload
803 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100804 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
805 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100807 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000809 if (v == NULL)
810 return NULL;
811
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000812 /*
813 * Loop until no more [idx] or .key is following.
814 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000815 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100816 var1.v_type = VAR_UNKNOWN;
817 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000818 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000819 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000820 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
821 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100822 && lp->ll_tv->vval.v_dict != NULL)
823 && !(lp->ll_tv->v_type == VAR_BLOB
824 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000826 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100827 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000830 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000831 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100833 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000834 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000835 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000836
Bram Moolenaar8c711452005-01-14 21:53:12 +0000837 len = -1;
838 if (*p == '.')
839 {
840 key = p + 1;
841 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
842 ;
843 if (len == 0)
844 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000845 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000848 }
849 p = key + len;
850 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000851 else
852 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100853 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000854 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000855 if (*p == ':')
856 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000857 else
858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000859 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200860 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000861 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100862 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100864 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000865 clear_tv(&var1);
866 return NULL;
867 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000868 }
869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100870 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000871 if (*p == ':')
872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100876 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100877 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000879 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100880 if (rettv != NULL
881 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100882 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100883 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100884 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100887 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100888 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000889 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000890 }
891 p = skipwhite(p + 1);
892 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000894 else
895 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000896 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200897 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200898 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000899 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100900 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100903 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100906 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000907 clear_tv(&var2);
908 return NULL;
909 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000910 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000912 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000913 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000914 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000915
Bram Moolenaar8c711452005-01-14 21:53:12 +0000916 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000918 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100919 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100920 clear_tv(&var1);
921 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000923 }
924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100925 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000926 ++p;
927 }
928
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000929 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000930 {
931 if (len == -1)
932 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100933 // "[key]": get key from "var1"
934 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200935 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000936 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000937 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000938 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000939 }
940 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000941 lp->ll_list = NULL;
942 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000943 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100945 // When assigning to a scope dictionary check that a function and
946 // variable name is valid (only variable name unless it is l: or
947 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200948 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200949 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200950 int prevval;
951 int wrong;
952
953 if (len != -1)
954 {
955 prevval = key[len];
956 key[len] = NUL;
957 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200958 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100959 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200960 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
961 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200962 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200963 || !valid_varname(key);
964 if (len != -1)
965 key[len] = prevval;
966 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200967 {
968 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200969 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200970 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200971 }
972
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000974 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100975 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200976 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100977 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200978 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100979 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100980 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200981 return NULL;
982 }
983
Bram Moolenaar31b81602019-02-10 22:14:27 +0100984 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000985 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000986 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100988 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100989 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 }
992 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000994 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000995 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100996 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 p = NULL;
999 break;
1000 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001001 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001002 else if ((flags & GLV_READ_ONLY) == 0
1003 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001004 {
1005 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001006 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001007 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001008
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001009 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001011 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001012 else if (lp->ll_tv->v_type == VAR_BLOB)
1013 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001014 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1015
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001016 /*
1017 * Get the number and item for the only or first index of the List.
1018 */
1019 if (empty1)
1020 lp->ll_n1 = 0;
1021 else
1022 // is number or string
1023 lp->ll_n1 = (long)tv_get_number(&var1);
1024 clear_tv(&var1);
1025
1026 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001027 || lp->ll_n1 > bloblen
1028 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001029 {
1030 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001031 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001032 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001033 return NULL;
1034 }
1035 if (lp->ll_range && !lp->ll_empty2)
1036 {
1037 lp->ll_n2 = (long)tv_get_number(&var2);
1038 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001039 if (lp->ll_n2 < 0
1040 || lp->ll_n2 >= bloblen
1041 || lp->ll_n2 < lp->ll_n1)
1042 {
1043 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001044 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001045 return NULL;
1046 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001047 }
1048 lp->ll_blob = lp->ll_tv->vval.v_blob;
1049 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001050 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001052 else
1053 {
1054 /*
1055 * Get the number and item for the only or first index of the List.
1056 */
1057 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001059 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001060 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001061 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001062 clear_tv(&var1);
1063
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001064 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001065 lp->ll_list = lp->ll_tv->vval.v_list;
1066 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1067 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001069 if (lp->ll_n1 < 0)
1070 {
1071 lp->ll_n1 = 0;
1072 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1073 }
1074 }
1075 if (lp->ll_li == NULL)
1076 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001077 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001078 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001079 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001080 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001081 }
1082
1083 /*
1084 * May need to find the item or absolute index for the second
1085 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001086 * When no index given: "lp->ll_empty2" is TRUE.
1087 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001088 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001089 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001090 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001091 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001093 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001094 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001095 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001096 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001098 {
1099 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001100 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001101 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001102 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001103 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001104 }
1105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001106 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001107 if (lp->ll_n1 < 0)
1108 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1109 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001110 {
1111 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001112 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001114 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001115 }
1116
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001117 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001119 }
1120
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001121 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001123 return p;
1124}
1125
1126/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001127 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001128 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001129 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001130clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001131{
1132 vim_free(lp->ll_exp_name);
1133 vim_free(lp->ll_newkey);
1134}
1135
1136/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001137 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001139 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1140 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001142 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001143set_var_lval(
1144 lval_T *lp,
1145 char_u *endp,
1146 typval_T *rettv,
1147 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001149 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150{
1151 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001152 listitem_T *ri;
1153 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154
1155 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001156 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001157 cc = *endp;
1158 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001159 if (lp->ll_blob != NULL)
1160 {
1161 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001162
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001163 if (op != NULL && *op != '=')
1164 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001165 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001166 return;
1167 }
1168
1169 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1170 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001171 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001172
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001173 if (lp->ll_empty2)
1174 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1175
1176 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001178 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 return;
1180 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001181 if (lp->ll_empty2)
1182 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001183
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001184 ir = 0;
1185 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1186 blob_set(lp->ll_blob, il,
1187 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001188 }
1189 else
1190 {
1191 val = (int)tv_get_number_chk(rettv, &error);
1192 if (!error)
1193 {
1194 garray_T *gap = &lp->ll_blob->bv_ga;
1195
1196 // Allow for appending a byte. Setting a byte beyond
1197 // the end is an error otherwise.
1198 if (lp->ll_n1 < gap->ga_len
1199 || (lp->ll_n1 == gap->ga_len
1200 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1201 {
1202 blob_set(lp->ll_blob, lp->ll_n1, val);
1203 if (lp->ll_n1 == gap->ga_len)
1204 ++gap->ga_len;
1205 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001206 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001207 }
1208 }
1209 }
1210 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001212 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001215 {
1216 emsg(_(e_cannot_mod));
1217 *endp = cc;
1218 return;
1219 }
1220
Bram Moolenaarff697e62019-02-12 22:28:33 +01001221 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001222 di = NULL;
1223 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1224 &tv, &di, TRUE, FALSE) == OK)
1225 {
1226 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001227 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1228 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001229 && tv_op(&tv, rettv, op) == OK)
1230 set_var(lp->ll_name, &tv, FALSE);
1231 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001234 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001236 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001238 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001239 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001240 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001241 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 else if (lp->ll_range)
1243 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001244 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001245 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001248 {
1249 emsg(_("E996: Cannot lock a range"));
1250 return;
1251 }
1252
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001253 /*
1254 * Check whether any of the list items is locked
1255 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001256 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001257 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001258 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001259 return;
1260 ri = ri->li_next;
1261 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1262 break;
1263 ll_li = ll_li->li_next;
1264 ++ll_n1;
1265 }
1266
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 /*
1268 * Assign the List values to the list items.
1269 */
1270 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001271 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001272 if (op != NULL && *op != '=')
1273 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1274 else
1275 {
1276 clear_tv(&lp->ll_li->li_tv);
1277 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1278 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001279 ri = ri->li_next;
1280 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1281 break;
1282 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001283 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001284 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001285 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001286 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 ri = NULL;
1288 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001289 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001290 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001291 lp->ll_li = lp->ll_li->li_next;
1292 ++lp->ll_n1;
1293 }
1294 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001295 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001296 else if (lp->ll_empty2
1297 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001299 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300 }
1301 else
1302 {
1303 /*
1304 * Assign to a List or Dictionary item.
1305 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001307 {
1308 emsg(_("E996: Cannot lock a list or dict"));
1309 return;
1310 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001311 if (lp->ll_newkey != NULL)
1312 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001313 if (op != NULL && *op != '=')
1314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001315 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001316 return;
1317 }
1318
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001319 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001320 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001321 if (di == NULL)
1322 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001323 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1324 {
1325 vim_free(di);
1326 return;
1327 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001328 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001329 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001330 else if (op != NULL && *op != '=')
1331 {
1332 tv_op(lp->ll_tv, rettv, op);
1333 return;
1334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001336 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001337
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 /*
1339 * Assign the value to the variable or list item.
1340 */
1341 if (copy)
1342 copy_tv(rettv, lp->ll_tv);
1343 else
1344 {
1345 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001346 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001347 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 }
1349 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001350}
1351
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001352/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001353 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1354 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001355 * Returns OK or FAIL.
1356 */
1357 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001358tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001360 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001361 char_u numbuf[NUMBUFLEN];
1362 char_u *s;
1363
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001364 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001365 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001366 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001367 {
1368 switch (tv1->v_type)
1369 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001370 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001371 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001373 case VAR_DICT:
1374 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001375 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001376 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001377 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001378 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001379 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001380 break;
1381
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001382 case VAR_BLOB:
1383 if (*op != '+' || tv2->v_type != VAR_BLOB)
1384 break;
1385 // BLOB += BLOB
1386 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1387 {
1388 blob_T *b1 = tv1->vval.v_blob;
1389 blob_T *b2 = tv2->vval.v_blob;
1390 int i, len = blob_len(b2);
1391 for (i = 0; i < len; i++)
1392 ga_append(&b1->bv_ga, blob_get(b2, i));
1393 }
1394 return OK;
1395
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001396 case VAR_LIST:
1397 if (*op != '+' || tv2->v_type != VAR_LIST)
1398 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001399 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001400 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1401 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1402 return OK;
1403
1404 case VAR_NUMBER:
1405 case VAR_STRING:
1406 if (tv2->v_type == VAR_LIST)
1407 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001408 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001409 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001410 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001411 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001412#ifdef FEAT_FLOAT
1413 if (tv2->v_type == VAR_FLOAT)
1414 {
1415 float_T f = n;
1416
Bram Moolenaarff697e62019-02-12 22:28:33 +01001417 if (*op == '%')
1418 break;
1419 switch (*op)
1420 {
1421 case '+': f += tv2->vval.v_float; break;
1422 case '-': f -= tv2->vval.v_float; break;
1423 case '*': f *= tv2->vval.v_float; break;
1424 case '/': f /= tv2->vval.v_float; break;
1425 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001426 clear_tv(tv1);
1427 tv1->v_type = VAR_FLOAT;
1428 tv1->vval.v_float = f;
1429 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001430 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001431#endif
1432 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001433 switch (*op)
1434 {
1435 case '+': n += tv_get_number(tv2); break;
1436 case '-': n -= tv_get_number(tv2); break;
1437 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001438 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1439 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001440 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001441 clear_tv(tv1);
1442 tv1->v_type = VAR_NUMBER;
1443 tv1->vval.v_number = n;
1444 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001445 }
1446 else
1447 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001448 if (tv2->v_type == VAR_FLOAT)
1449 break;
1450
Bram Moolenaarff697e62019-02-12 22:28:33 +01001451 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001452 s = tv_get_string(tv1);
1453 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001454 clear_tv(tv1);
1455 tv1->v_type = VAR_STRING;
1456 tv1->vval.v_string = s;
1457 }
1458 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001459
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001460 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001461#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001462 {
1463 float_T f;
1464
Bram Moolenaarff697e62019-02-12 22:28:33 +01001465 if (*op == '%' || *op == '.'
1466 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001467 && tv2->v_type != VAR_NUMBER
1468 && tv2->v_type != VAR_STRING))
1469 break;
1470 if (tv2->v_type == VAR_FLOAT)
1471 f = tv2->vval.v_float;
1472 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001473 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001474 switch (*op)
1475 {
1476 case '+': tv1->vval.v_float += f; break;
1477 case '-': tv1->vval.v_float -= f; break;
1478 case '*': tv1->vval.v_float *= f; break;
1479 case '/': tv1->vval.v_float /= f; break;
1480 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001481 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001482#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001483 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 }
1485 }
1486
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001487 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001488 return FAIL;
1489}
1490
1491/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001492 * Evaluate the expression used in a ":for var in expr" command.
1493 * "arg" points to "var".
1494 * Set "*errp" to TRUE for an error, FALSE otherwise;
1495 * Return a pointer that holds the info. Null when there is an error.
1496 */
1497 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001498eval_for_line(
1499 char_u *arg,
1500 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001501 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001502 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001503{
Bram Moolenaar33570922005-01-25 22:26:29 +00001504 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001505 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001506 typval_T tv;
1507 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001508 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001510 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001511
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001512 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001513 if (fi == NULL)
1514 return NULL;
1515
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001516 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517 if (expr == NULL)
1518 return fi;
1519
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001520 expr = skipwhite_and_linebreak(expr, evalarg);
1521 if (expr[0] != 'i' || expr[1] != 'n'
1522 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001523 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001524 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001525 return fi;
1526 }
1527
1528 if (skip)
1529 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001530 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1531 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001532 {
1533 *errp = FALSE;
1534 if (!skip)
1535 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001536 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001537 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001538 l = tv.vval.v_list;
1539 if (l == NULL)
1540 {
1541 // a null list is like an empty list: do nothing
1542 clear_tv(&tv);
1543 }
1544 else
1545 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001547 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001549 // No need to increment the refcount, it's already set for
1550 // the list being used in "tv".
1551 fi->fi_list = l;
1552 list_add_watch(l, &fi->fi_lw);
1553 fi->fi_lw.lw_item = l->lv_first;
1554 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001555 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001556 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001557 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001558 fi->fi_bi = 0;
1559 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001560 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001561 typval_T btv;
1562
1563 // Make a copy, so that the iteration still works when the
1564 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001566 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001567 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001568 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001569 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001570 else
1571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001572 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001573 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001574 }
1575 }
1576 }
1577 if (skip)
1578 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001579 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001580
1581 return fi;
1582}
1583
1584/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001585 * Used when looping over a :for line, skip the "in expr" part.
1586 */
1587 void
1588skip_for_lines(void *fi_void, evalarg_T *evalarg)
1589{
1590 forinfo_T *fi = (forinfo_T *)fi_void;
1591 int i;
1592
1593 for (i = 0; i < fi->fi_break_count; ++i)
1594 eval_next_line(evalarg);
1595}
1596
1597/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001598 * Use the first item in a ":for" list. Advance to the next.
1599 * Assign the values to the variable (list). "arg" points to the first one.
1600 * Return TRUE when a valid item was found, FALSE when at end of list or
1601 * something wrong.
1602 */
1603 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001604next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001605{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001606 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001607 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001608 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1609 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001610 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001611
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001612 if (fi->fi_blob != NULL)
1613 {
1614 typval_T tv;
1615
1616 if (fi->fi_bi >= blob_len(fi->fi_blob))
1617 return FALSE;
1618 tv.v_type = VAR_NUMBER;
1619 tv.v_lock = VAR_FIXED;
1620 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1621 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001622 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001623 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001624 }
1625
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001626 item = fi->fi_lw.lw_item;
1627 if (item == NULL)
1628 result = FALSE;
1629 else
1630 {
1631 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001632 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001633 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001634 }
1635 return result;
1636}
1637
1638/*
1639 * Free the structure used to store info used by ":for".
1640 */
1641 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001642free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643{
Bram Moolenaar33570922005-01-25 22:26:29 +00001644 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001645
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001646 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001647 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001649 list_unref(fi->fi_list);
1650 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001651 if (fi != NULL && fi->fi_blob != NULL)
1652 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653 vim_free(fi);
1654}
1655
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001657set_context_for_expression(
1658 expand_T *xp,
1659 char_u *arg,
1660 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661{
1662 int got_eq = FALSE;
1663 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001666 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 {
1668 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001669 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001671 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001672 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 {
1674 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001675 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001676 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 break;
1678 }
1679 return;
1680 }
1681 }
1682 else
1683 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1684 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 while ((xp->xp_pattern = vim_strpbrk(arg,
1686 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1687 {
1688 c = *xp->xp_pattern;
1689 if (c == '&')
1690 {
1691 c = xp->xp_pattern[1];
1692 if (c == '&')
1693 {
1694 ++xp->xp_pattern;
1695 xp->xp_context = cmdidx != CMD_let || got_eq
1696 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1697 }
1698 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001699 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001701 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1702 xp->xp_pattern += 2;
1703
1704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
1706 else if (c == '$')
1707 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001708 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 xp->xp_context = EXPAND_ENV_VARS;
1710 }
1711 else if (c == '=')
1712 {
1713 got_eq = TRUE;
1714 xp->xp_context = EXPAND_EXPRESSION;
1715 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001716 else if (c == '#'
1717 && xp->xp_context == EXPAND_EXPRESSION)
1718 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001719 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001720 break;
1721 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001722 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 && xp->xp_context == EXPAND_FUNCTIONS
1724 && vim_strchr(xp->xp_pattern, '(') == NULL)
1725 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001726 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 break;
1728 }
1729 else if (cmdidx != CMD_let || got_eq)
1730 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001731 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 {
1733 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1734 if (c == '\\' && xp->xp_pattern[1] != NUL)
1735 ++xp->xp_pattern;
1736 xp->xp_context = EXPAND_NOTHING;
1737 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001738 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001740 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1742 /* skip */ ;
1743 xp->xp_context = EXPAND_NOTHING;
1744 }
1745 else if (c == '|')
1746 {
1747 if (xp->xp_pattern[1] == '|')
1748 {
1749 ++xp->xp_pattern;
1750 xp->xp_context = EXPAND_EXPRESSION;
1751 }
1752 else
1753 xp->xp_context = EXPAND_COMMANDS;
1754 }
1755 else
1756 xp->xp_context = EXPAND_EXPRESSION;
1757 }
1758 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001759 // Doesn't look like something valid, expand as an expression
1760 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 arg = xp->xp_pattern;
1763 if (*arg != NUL)
1764 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1765 /* skip */ ;
1766 }
1767 xp->xp_pattern = arg;
1768}
1769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001771 * Return TRUE if "pat" matches "text".
1772 * Does not use 'cpo' and always uses 'magic'.
1773 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001774 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001775pattern_match(char_u *pat, char_u *text, int ic)
1776{
1777 int matches = FALSE;
1778 char_u *save_cpo;
1779 regmatch_T regmatch;
1780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001781 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001782 save_cpo = p_cpo;
1783 p_cpo = (char_u *)"";
1784 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1785 if (regmatch.regprog != NULL)
1786 {
1787 regmatch.rm_ic = ic;
1788 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1789 vim_regfree(regmatch.regprog);
1790 }
1791 p_cpo = save_cpo;
1792 return matches;
1793}
1794
1795/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001796 * Handle a name followed by "(". Both for just "name(arg)" and for
1797 * "expr->name(arg)".
1798 * Returns OK or FAIL.
1799 */
1800 static int
1801eval_func(
1802 char_u **arg, // points to "(", will be advanced
1803 char_u *name,
1804 int name_len,
1805 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001806 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001807 typval_T *basetv) // "expr" for "expr->name(arg)"
1808{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001809 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001810 char_u *s = name;
1811 int len = name_len;
1812 partial_T *partial;
1813 int ret = OK;
1814
1815 if (!evaluate)
1816 check_vars(s, len);
1817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001818 // If "s" is the name of a variable of type VAR_FUNC
1819 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001820 s = deref_func_name(s, &len, &partial, !evaluate);
1821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001822 // Need to make a copy, in case evaluating the arguments makes
1823 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001824 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001825 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001826 ret = FAIL;
1827 else
1828 {
1829 funcexe_T funcexe;
1830
1831 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001832 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001833 funcexe.firstline = curwin->w_cursor.lnum;
1834 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001835 funcexe.evaluate = evaluate;
1836 funcexe.partial = partial;
1837 funcexe.basetv = basetv;
1838 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1839 }
1840 vim_free(s);
1841
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001842 // If evaluate is FALSE rettv->v_type was not set in
1843 // get_func_tv, but it's needed in handle_subscript() to parse
1844 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001845 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1846 {
1847 rettv->vval.v_string = NULL;
1848 rettv->v_type = VAR_FUNC;
1849 }
1850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001851 // Stop the expression evaluation when immediately
1852 // aborting on error, or when an interrupt occurred or
1853 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001854 if (evaluate && aborting())
1855 {
1856 if (ret == OK)
1857 clear_tv(rettv);
1858 ret = FAIL;
1859 }
1860 return ret;
1861}
1862
1863/*
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001864 * If inside Vim9 script, "arg" points to the end of a line (ignoring comments)
1865 * and there is a next line, return the next line (skipping blanks) and set
1866 * "getnext".
1867 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1868 * "arg" must point somewhere inside a line, not at the start.
1869 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001870 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001871eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1872{
1873 *getnext = FALSE;
1874 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1875 && evalarg != NULL
1876 && evalarg->eval_cookie != NULL
1877 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
Bram Moolenaard5053d02020-06-28 15:51:16 +02001878 && (*arg == '"' || *arg == '#'))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001879 {
Bram Moolenaard5053d02020-06-28 15:51:16 +02001880 char_u *p = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001881
1882 if (p != NULL)
1883 {
1884 *getnext = TRUE;
1885 return skipwhite(p);
1886 }
1887 }
1888 return arg;
1889}
1890
1891/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001892 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001893 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001894 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001895eval_next_line(evalarg_T *evalarg)
1896{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001897 garray_T *gap = &evalarg->eval_ga;
1898 char_u *line;
1899
Bram Moolenaard5053d02020-06-28 15:51:16 +02001900 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001901 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001902 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1903 {
1904 // Going to concatenate the lines after parsing.
1905 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1906 ++gap->ga_len;
1907 }
1908 else
1909 {
1910 vim_free(evalarg->eval_tofree);
1911 evalarg->eval_tofree = line;
1912 }
1913 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001914}
1915
Bram Moolenaar9215f012020-06-27 21:18:00 +02001916 char_u *
1917skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1918{
1919 int getnext;
1920 char_u *p = skipwhite(arg);
1921
1922 eval_next_non_blank(p, evalarg, &getnext);
1923 if (getnext)
1924 return eval_next_line(evalarg);
1925 return p;
1926}
1927
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001928/*
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001929 * After using "evalarg" filled from "eap" free the memory.
1930 */
1931 void
1932clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1933{
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001934 if (evalarg != NULL && evalarg->eval_tofree != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001935 {
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001936 if (eap != NULL)
1937 {
1938 // We may need to keep the original command line, e.g. for
1939 // ":let" it has the variable names. But we may also need the
1940 // new one, "nextcmd" points into it. Keep both.
1941 vim_free(eap->cmdline_tofree);
1942 eap->cmdline_tofree = *eap->cmdlinep;
1943 *eap->cmdlinep = evalarg->eval_tofree;
1944 }
1945 else
1946 vim_free(evalarg->eval_tofree);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001947 evalarg->eval_tofree = NULL;
1948 }
1949}
1950
1951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1955 */
1956
1957/*
1958 * Handle zero level expression.
1959 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001961 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001962 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 * Return OK or FAIL.
1964 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001965 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001966eval0(
1967 char_u *arg,
1968 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001969 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001970 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971{
1972 int ret;
1973 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001974 int did_emsg_before = did_emsg;
1975 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001976 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
1978 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001979 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001980
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001981 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
1983 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 /*
1986 * Report the invalid expression unless the expression evaluation has
1987 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001988 * exception, or we already gave a more specific error.
1989 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001991 if (!aborting()
1992 && did_emsg == did_emsg_before
1993 && called_emsg == called_emsg_before
1994 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001995 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 ret = FAIL;
1997 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001998
1999 if (eap != NULL)
2000 eap->nextcmd = check_nextcmd(p);
2001
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 return ret;
2003}
2004
2005/*
2006 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002007 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 *
2009 * "arg" must point to the first non-white of the expression.
2010 * "arg" is advanced to the next non-white after the recognized expression.
2011 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002012 * Note: "rettv.v_lock" is not set.
2013 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 * Return OK or FAIL.
2015 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002016 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002017eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002019 char_u *p;
2020 int getnext;
2021
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 /*
2023 * Get the first variable.
2024 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002025 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 return FAIL;
2027
Bram Moolenaar793648f2020-06-26 21:28:25 +02002028 p = eval_next_non_blank(*arg, evalarg, &getnext);
2029 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002031 int result;
2032 typval_T var2;
2033 evalarg_T nested_evalarg;
2034 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002035 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002036
Bram Moolenaar793648f2020-06-26 21:28:25 +02002037 if (getnext)
2038 *arg = eval_next_line(evalarg);
2039
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002040 if (evalarg == NULL)
2041 {
2042 CLEAR_FIELD(nested_evalarg);
2043 orig_flags = 0;
2044 }
2045 else
2046 {
2047 nested_evalarg = *evalarg;
2048 orig_flags = evalarg->eval_flags;
2049 }
2050
Bram Moolenaar7acde512020-06-24 23:02:40 +02002051 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002053 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002055 int error = FALSE;
2056
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002057 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002059 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002060 if (error)
2061 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
2063
2064 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002065 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002067 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002068 nested_evalarg.eval_flags = result ? orig_flags
2069 : orig_flags & ~EVAL_EVALUATE;
2070 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 return FAIL;
2072
2073 /*
2074 * Check for the ":".
2075 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002076 p = eval_next_non_blank(*arg, evalarg, &getnext);
2077 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 return FAIL;
2083 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002084 if (getnext)
2085 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
2087 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002088 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002090 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002091 nested_evalarg.eval_flags = !result ? orig_flags
2092 : orig_flags & ~EVAL_EVALUATE;
2093 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
2095 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002096 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 return FAIL;
2098 }
2099 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102
2103 return OK;
2104}
2105
2106/*
2107 * Handle first level expression:
2108 * expr2 || expr2 || expr2 logical OR
2109 *
2110 * "arg" must point to the first non-white of the expression.
2111 * "arg" is advanced to the next non-white after the recognized expression.
2112 *
2113 * Return OK or FAIL.
2114 */
2115 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002118 char_u *p;
2119 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002120 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 long result;
2122 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002123 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
2125 /*
2126 * Get the first variable.
2127 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002128 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 return FAIL;
2130
2131 /*
2132 * Repeat until there is no following "||".
2133 */
2134 first = TRUE;
2135 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002136 p = eval_next_non_blank(*arg, evalarg, &getnext);
2137 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002139 evalarg_T nested_evalarg;
2140 int evaluate;
2141 int orig_flags;
2142
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002143 if (getnext)
2144 *arg = eval_next_line(evalarg);
2145
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002146 if (evalarg == NULL)
2147 {
2148 CLEAR_FIELD(nested_evalarg);
2149 orig_flags = 0;
2150 evaluate = FALSE;
2151 }
2152 else
2153 {
2154 nested_evalarg = *evalarg;
2155 orig_flags = evalarg->eval_flags;
2156 evaluate = orig_flags & EVAL_EVALUATE;
2157 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002158
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 if (evaluate && first)
2160 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002161 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002164 if (error)
2165 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 first = FALSE;
2167 }
2168
2169 /*
2170 * Get the second variable.
2171 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002172 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002173 nested_evalarg.eval_flags = !result ? orig_flags
2174 : orig_flags & ~EVAL_EVALUATE;
2175 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 return FAIL;
2177
2178 /*
2179 * Compute the result.
2180 */
2181 if (evaluate && !result)
2182 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002183 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002186 if (error)
2187 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 }
2189 if (evaluate)
2190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 rettv->v_type = VAR_NUMBER;
2192 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002194
2195 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 }
2197
2198 return OK;
2199}
2200
2201/*
2202 * Handle second level expression:
2203 * expr3 && expr3 && expr3 logical AND
2204 *
2205 * "arg" must point to the first non-white of the expression.
2206 * "arg" is advanced to the next non-white after the recognized expression.
2207 *
2208 * Return OK or FAIL.
2209 */
2210 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002211eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002213 char_u *p;
2214 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002215 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 long result;
2217 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002218 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 /*
2221 * Get the first variable.
2222 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002223 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 return FAIL;
2225
2226 /*
2227 * Repeat until there is no following "&&".
2228 */
2229 first = TRUE;
2230 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002231 p = eval_next_non_blank(*arg, evalarg, &getnext);
2232 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002234 evalarg_T nested_evalarg;
2235 int orig_flags;
2236 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002237
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002238 if (getnext)
2239 *arg = eval_next_line(evalarg);
2240
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002241 if (evalarg == NULL)
2242 {
2243 CLEAR_FIELD(nested_evalarg);
2244 orig_flags = 0;
2245 evaluate = FALSE;
2246 }
2247 else
2248 {
2249 nested_evalarg = *evalarg;
2250 orig_flags = evalarg->eval_flags;
2251 evaluate = orig_flags & EVAL_EVALUATE;
2252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 if (evaluate && first)
2254 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002255 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002258 if (error)
2259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 first = FALSE;
2261 }
2262
2263 /*
2264 * Get the second variable.
2265 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002266 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002267 nested_evalarg.eval_flags = result ? orig_flags
2268 : orig_flags & ~EVAL_EVALUATE;
2269 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 return FAIL;
2271
2272 /*
2273 * Compute the result.
2274 */
2275 if (evaluate && result)
2276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002277 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002280 if (error)
2281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
2283 if (evaluate)
2284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 rettv->v_type = VAR_NUMBER;
2286 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002288
2289 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 }
2291
2292 return OK;
2293}
2294
2295/*
2296 * Handle third level expression:
2297 * var1 == var2
2298 * var1 =~ var2
2299 * var1 != var2
2300 * var1 !~ var2
2301 * var1 > var2
2302 * var1 >= var2
2303 * var1 < var2
2304 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002305 * var1 is var2
2306 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 *
2308 * "arg" must point to the first non-white of the expression.
2309 * "arg" is advanced to the next non-white after the recognized expression.
2310 *
2311 * Return OK or FAIL.
2312 */
2313 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002314eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315{
Bram Moolenaar33570922005-01-25 22:26:29 +00002316 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002318 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002320 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
2324 /*
2325 * Get the first variable.
2326 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002327 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 return FAIL;
2329
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002330 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 switch (p[0])
2332 {
2333 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002334 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002336 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 break;
2338 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002339 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002341 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 break;
2343 case '>': if (p[1] != '=')
2344 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002345 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 len = 1;
2347 }
2348 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002349 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 break;
2351 case '<': if (p[1] != '=')
2352 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002353 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 len = 1;
2355 }
2356 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002357 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002359 case 'i': if (p[1] == 's')
2360 {
2361 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2362 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002363 i = p[len];
2364 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002365 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002366 }
2367 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369
2370 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002371 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002373 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002375 if (getnext)
2376 *arg = eval_next_line(evalarg);
2377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002378 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 if (p[len] == '?')
2380 {
2381 ic = TRUE;
2382 ++len;
2383 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002384 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 else if (p[len] == '#')
2386 {
2387 ic = FALSE;
2388 ++len;
2389 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002390 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 else
2392 ic = p_ic;
2393
2394 /*
2395 * Get the second variable.
2396 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002397 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002398 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 return FAIL;
2402 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002403 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002404 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002405 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002406
2407 clear_tv(&var2);
2408 return ret;
2409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
2411
2412 return OK;
2413}
2414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 void
2416eval_addblob(typval_T *tv1, typval_T *tv2)
2417{
2418 blob_T *b1 = tv1->vval.v_blob;
2419 blob_T *b2 = tv2->vval.v_blob;
2420 blob_T *b = blob_alloc();
2421 int i;
2422
2423 if (b != NULL)
2424 {
2425 for (i = 0; i < blob_len(b1); i++)
2426 ga_append(&b->bv_ga, blob_get(b1, i));
2427 for (i = 0; i < blob_len(b2); i++)
2428 ga_append(&b->bv_ga, blob_get(b2, i));
2429
2430 clear_tv(tv1);
2431 rettv_blob_set(tv1, b);
2432 }
2433}
2434
2435 int
2436eval_addlist(typval_T *tv1, typval_T *tv2)
2437{
2438 typval_T var3;
2439
2440 // concatenate Lists
2441 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2442 {
2443 clear_tv(tv1);
2444 clear_tv(tv2);
2445 return FAIL;
2446 }
2447 clear_tv(tv1);
2448 *tv1 = var3;
2449 return OK;
2450}
2451
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452/*
2453 * Handle fourth level expression:
2454 * + number addition
2455 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002456 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002457 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 *
2459 * "arg" must point to the first non-white of the expression.
2460 * "arg" is advanced to the next non-white after the recognized expression.
2461 *
2462 * Return OK or FAIL.
2463 */
2464 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002467 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
2469 /*
2470 * Get the first variable.
2471 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002472 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 return FAIL;
2474
2475 /*
2476 * Repeat computing, until no '+', '-' or '.' is following.
2477 */
2478 for (;;)
2479 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002480 int getnext;
2481 char_u *p;
2482 int op;
2483 int concat;
2484 typval_T var2;
2485
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002486 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002487 p = eval_next_non_blank(*arg, evalarg, &getnext);
2488 op = *p;
2489 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002490 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002492 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002493 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002495 if ((op != '+' || (rettv->v_type != VAR_LIST
2496 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002497#ifdef FEAT_FLOAT
2498 && (op == '.' || rettv->v_type != VAR_FLOAT)
2499#endif
2500 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002502 // For "list + ...", an illegal use of the first operand as
2503 // a number cannot be determined before evaluating the 2nd
2504 // operand: if this is also a list, all is ok.
2505 // For "something . ...", "something - ..." or "non-list + ...",
2506 // we know that the first operand needs to be a string or number
2507 // without evaluating the 2nd operand. So check before to avoid
2508 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002509 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002510 {
2511 clear_tv(rettv);
2512 return FAIL;
2513 }
2514 }
2515
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 /*
2517 * Get the second variable.
2518 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002519 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2520 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002521 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002522 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 return FAIL;
2526 }
2527
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 /*
2531 * Compute the result.
2532 */
2533 if (op == '.')
2534 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002535 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2536 char_u *s1 = tv_get_string_buf(rettv, buf1);
2537 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002539 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002540 {
2541 clear_tv(rettv);
2542 clear_tv(&var2);
2543 return FAIL;
2544 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 clear_tv(rettv);
2547 rettv->v_type = VAR_STRING;
2548 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002550 else if (op == '+' && rettv->v_type == VAR_BLOB
2551 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002553 else if (op == '+' && rettv->v_type == VAR_LIST
2554 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002555 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002557 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 else
2560 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002561 int error = FALSE;
2562 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002563#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002564 float_T f1 = 0, f2 = 0;
2565
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002566 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002567 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002568 f1 = rettv->vval.v_float;
2569 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002570 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002571 else
2572#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002573 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002574 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002575 if (error)
2576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002577 // This can only happen for "list + non-list". For
2578 // "non-list + ..." or "something - ...", we returned
2579 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002580 clear_tv(rettv);
2581 return FAIL;
2582 }
2583#ifdef FEAT_FLOAT
2584 if (var2.v_type == VAR_FLOAT)
2585 f1 = n1;
2586#endif
2587 }
2588#ifdef FEAT_FLOAT
2589 if (var2.v_type == VAR_FLOAT)
2590 {
2591 f2 = var2.vval.v_float;
2592 n2 = 0;
2593 }
2594 else
2595#endif
2596 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002597 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002598 if (error)
2599 {
2600 clear_tv(rettv);
2601 clear_tv(&var2);
2602 return FAIL;
2603 }
2604#ifdef FEAT_FLOAT
2605 if (rettv->v_type == VAR_FLOAT)
2606 f2 = n2;
2607#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002608 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002610
2611#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002612 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002613 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2614 {
2615 if (op == '+')
2616 f1 = f1 + f2;
2617 else
2618 f1 = f1 - f2;
2619 rettv->v_type = VAR_FLOAT;
2620 rettv->vval.v_float = f1;
2621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002623#endif
2624 {
2625 if (op == '+')
2626 n1 = n1 + n2;
2627 else
2628 n1 = n1 - n2;
2629 rettv->v_type = VAR_NUMBER;
2630 rettv->vval.v_number = n1;
2631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
2635 }
2636 return OK;
2637}
2638
2639/*
2640 * Handle fifth level expression:
2641 * * number multiplication
2642 * / number division
2643 * % number modulo
2644 *
2645 * "arg" must point to the first non-white of the expression.
2646 * "arg" is advanced to the next non-white after the recognized expression.
2647 *
2648 * Return OK or FAIL.
2649 */
2650 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002651eval6(
2652 char_u **arg,
2653 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002654 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002655 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
Bram Moolenaar33570922005-01-25 22:26:29 +00002657 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002659 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002660#ifdef FEAT_FLOAT
2661 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002662 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002663#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002664 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 /*
2667 * Get the first variable.
2668 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002669 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 return FAIL;
2671
2672 /*
2673 * Repeat computing, until no '*', '/' or '%' is following.
2674 */
2675 for (;;)
2676 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002677 int evaluate = evalarg == NULL ? 0
2678 : (evalarg->eval_flags & EVAL_EVALUATE);
2679 int getnext;
2680
2681 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002682 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002684 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002685 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002687 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002689#ifdef FEAT_FLOAT
2690 if (rettv->v_type == VAR_FLOAT)
2691 {
2692 f1 = rettv->vval.v_float;
2693 use_float = TRUE;
2694 n1 = 0;
2695 }
2696 else
2697#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002698 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002700 if (error)
2701 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
2703 else
2704 n1 = 0;
2705
2706 /*
2707 * Get the second variable.
2708 */
2709 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002710 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 return FAIL;
2712
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002713 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002715#ifdef FEAT_FLOAT
2716 if (var2.v_type == VAR_FLOAT)
2717 {
2718 if (!use_float)
2719 {
2720 f1 = n1;
2721 use_float = TRUE;
2722 }
2723 f2 = var2.vval.v_float;
2724 n2 = 0;
2725 }
2726 else
2727#endif
2728 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002729 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002730 clear_tv(&var2);
2731 if (error)
2732 return FAIL;
2733#ifdef FEAT_FLOAT
2734 if (use_float)
2735 f2 = n2;
2736#endif
2737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 /*
2740 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002741 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002743#ifdef FEAT_FLOAT
2744 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002746 if (op == '*')
2747 f1 = f1 * f2;
2748 else if (op == '/')
2749 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002750# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002751 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002752 if (f2 == 0.0)
2753 {
2754 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002755 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002756 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002757 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002758 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002759 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002760 }
2761 else
2762 f1 = f1 / f2;
2763# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002764 // We rely on the floating point library to handle divide
2765 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002766 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002767# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002770 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002772 return FAIL;
2773 }
2774 rettv->v_type = VAR_FLOAT;
2775 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002780 if (op == '*')
2781 n1 = n1 * n2;
2782 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002783 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002785 n1 = num_modulus(n1, n2);
2786
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002787 rettv->v_type = VAR_NUMBER;
2788 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
2791 }
2792
2793 return OK;
2794}
2795
2796/*
2797 * Handle sixth level expression:
2798 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002799 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002800 * "string" string constant
2801 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 * &option-name option value
2803 * @r register contents
2804 * identifier variable value
2805 * function() function call
2806 * $VAR environment variable
2807 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002808 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002810 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002811 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 *
2813 * Also handle:
2814 * ! in front logical NOT
2815 * - in front unary minus
2816 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 * trailing [] subscript in String or List
2818 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002819 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 *
2821 * "arg" must point to the first non-white of the expression.
2822 * "arg" is advanced to the next non-white after the recognized expression.
2823 *
2824 * Return OK or FAIL.
2825 */
2826 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002827eval7(
2828 char_u **arg,
2829 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002830 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002833 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2834 int evaluate = evalarg != NULL
2835 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 int len;
2837 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 char_u *start_leader, *end_leader;
2839 int ret = OK;
2840 char_u *alias;
2841
2842 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002843 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002844 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002846 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847
2848 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002849 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 */
2851 start_leader = *arg;
2852 while (**arg == '!' || **arg == '-' || **arg == '+')
2853 *arg = skipwhite(*arg + 1);
2854 end_leader = *arg;
2855
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002856 if (**arg == '.' && (!isdigit(*(*arg + 1))
2857#ifdef FEAT_FLOAT
2858 || current_sctx.sc_version < 2
2859#endif
2860 ))
2861 {
2862 semsg(_(e_invexpr2), *arg);
2863 ++*arg;
2864 return FAIL;
2865 }
2866
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 switch (**arg)
2868 {
2869 /*
2870 * Number constant.
2871 */
2872 case '0':
2873 case '1':
2874 case '2':
2875 case '3':
2876 case '4':
2877 case '5':
2878 case '6':
2879 case '7':
2880 case '8':
2881 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002883
2884 // Apply prefixed "-" and "+" now. Matters especially when
2885 // "->" follows.
2886 if (ret == OK && evaluate && end_leader > start_leader)
2887 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 break;
2889
2890 /*
2891 * String constant: "string".
2892 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 break;
2895
2896 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002897 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002899 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002900 break;
2901
2902 /*
2903 * List: [expr, expr]
2904 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002905 case '[': ret = get_list_tv(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 break;
2907
2908 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002909 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002910 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002911 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002912 {
2913 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002914 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002915 }
2916 else
2917 ret = NOTDONE;
2918 break;
2919
2920 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002921 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002922 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002923 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002924 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002925 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002926 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002927 break;
2928
2929 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002930 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002932 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 break;
2934
2935 /*
2936 * Environment variable: $VAR.
2937 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002938 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 break;
2940
2941 /*
2942 * Register contents: @r.
2943 */
2944 case '@': ++*arg;
2945 if (evaluate)
2946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002947 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002948 rettv->vval.v_string = get_reg_contents(**arg,
2949 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 }
2951 if (**arg != NUL)
2952 ++*arg;
2953 break;
2954
2955 /*
2956 * nested expression: (expression).
2957 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002958 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02002959 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002960 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002961
Bram Moolenaar9215f012020-06-27 21:18:00 +02002962 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002963 if (**arg == ')')
2964 ++*arg;
2965 else if (ret == OK)
2966 {
2967 emsg(_(e_missing_close));
2968 clear_tv(rettv);
2969 ret = FAIL;
2970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
2972 break;
2973
Bram Moolenaar8c711452005-01-14 21:53:12 +00002974 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 break;
2976 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002977
2978 if (ret == NOTDONE)
2979 {
2980 /*
2981 * Must be a variable or function name.
2982 * Can also be a curly-braces kind of name: {expr}.
2983 */
2984 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002985 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002986 if (alias != NULL)
2987 s = alias;
2988
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002989 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002990 ret = FAIL;
2991 else
2992 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002993 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002994 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002995 else if (flags & EVAL_CONSTANT)
2996 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002997 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002998 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003000 {
3001 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003002 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003003 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003004 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003005 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003006 }
3007
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 *arg = skipwhite(*arg);
3009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003010 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3011 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003012 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003013 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
3015 /*
3016 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3017 */
3018 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003019 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003020 return ret;
3021}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003022
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003023/*
3024 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003025 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003026 * Adjusts "end_leaderp" until it is at "start_leader".
3027 */
3028 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003029eval7_leader(
3030 typval_T *rettv,
3031 int numeric_only,
3032 char_u *start_leader,
3033 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003034{
3035 char_u *end_leader = *end_leaderp;
3036 int ret = OK;
3037 int error = FALSE;
3038 varnumber_T val = 0;
3039#ifdef FEAT_FLOAT
3040 float_T f = 0.0;
3041
3042 if (rettv->v_type == VAR_FLOAT)
3043 f = rettv->vval.v_float;
3044 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003045#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003046 val = tv_get_number_chk(rettv, &error);
3047 if (error)
3048 {
3049 clear_tv(rettv);
3050 ret = FAIL;
3051 }
3052 else
3053 {
3054 while (end_leader > start_leader)
3055 {
3056 --end_leader;
3057 if (*end_leader == '!')
3058 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003059 if (numeric_only)
3060 {
3061 ++end_leader;
3062 break;
3063 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003064#ifdef FEAT_FLOAT
3065 if (rettv->v_type == VAR_FLOAT)
3066 f = !f;
3067 else
3068#endif
3069 val = !val;
3070 }
3071 else if (*end_leader == '-')
3072 {
3073#ifdef FEAT_FLOAT
3074 if (rettv->v_type == VAR_FLOAT)
3075 f = -f;
3076 else
3077#endif
3078 val = -val;
3079 }
3080 }
3081#ifdef FEAT_FLOAT
3082 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003084 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003085 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003087 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003088#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003089 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003090 clear_tv(rettv);
3091 rettv->v_type = VAR_NUMBER;
3092 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003095 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 return ret;
3097}
3098
3099/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003100 * Call the function referred to in "rettv".
3101 */
3102 static int
3103call_func_rettv(
3104 char_u **arg,
3105 typval_T *rettv,
3106 int evaluate,
3107 dict_T *selfdict,
3108 typval_T *basetv)
3109{
3110 partial_T *pt = NULL;
3111 funcexe_T funcexe;
3112 typval_T functv;
3113 char_u *s;
3114 int ret;
3115
3116 // need to copy the funcref so that we can clear rettv
3117 if (evaluate)
3118 {
3119 functv = *rettv;
3120 rettv->v_type = VAR_UNKNOWN;
3121
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003122 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003123 if (functv.v_type == VAR_PARTIAL)
3124 {
3125 pt = functv.vval.v_partial;
3126 s = partial_name(pt);
3127 }
3128 else
3129 s = functv.vval.v_string;
3130 }
3131 else
3132 s = (char_u *)"";
3133
Bram Moolenaara80faa82020-04-12 19:37:17 +02003134 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003135 funcexe.firstline = curwin->w_cursor.lnum;
3136 funcexe.lastline = curwin->w_cursor.lnum;
3137 funcexe.evaluate = evaluate;
3138 funcexe.partial = pt;
3139 funcexe.selfdict = selfdict;
3140 funcexe.basetv = basetv;
3141 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
3142
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003143 // Clear the funcref afterwards, so that deleting it while
3144 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003145 if (evaluate)
3146 clear_tv(&functv);
3147
3148 return ret;
3149}
3150
3151/*
3152 * Evaluate "->method()".
3153 * "*arg" points to the '-'.
3154 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3155 */
3156 static int
3157eval_lambda(
3158 char_u **arg,
3159 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003160 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003161 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003162{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003163 int evaluate = evalarg != NULL
3164 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003165 typval_T base = *rettv;
3166 int ret;
3167
3168 // Skip over the ->.
3169 *arg += 2;
3170 rettv->v_type = VAR_UNKNOWN;
3171
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003172 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003173 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003174 return FAIL;
3175 else if (**arg != '(')
3176 {
3177 if (verbose)
3178 {
3179 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003180 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003181 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003183 }
3184 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003185 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003186 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003187 else
3188 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
3189
3190 // Clear the funcref afterwards, so that deleting it while
3191 // evaluating the arguments is possible (see test55).
3192 if (evaluate)
3193 clear_tv(&base);
3194
3195 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003196}
3197
3198/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003199 * Evaluate "->method()".
3200 * "*arg" points to the '-'.
3201 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3202 */
3203 static int
3204eval_method(
3205 char_u **arg,
3206 typval_T *rettv,
3207 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003208 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003209{
3210 char_u *name;
3211 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003212 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003213 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003214 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003215
3216 // Skip over the ->.
3217 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003218 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003219
Bram Moolenaarac92e252019-08-03 21:58:38 +02003220 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003221 len = get_name_len(arg, &alias, evaluate, TRUE);
3222 if (alias != NULL)
3223 name = alias;
3224
3225 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003226 {
3227 if (verbose)
3228 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003229 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003230 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003231 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003232 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003233 if (**arg != '(')
3234 {
3235 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003237 ret = FAIL;
3238 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003239 else if (VIM_ISWHITE((*arg)[-1]))
3240 {
3241 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003242 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003243 ret = FAIL;
3244 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003245 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02003246 ret = eval_func(arg, name, len, rettv,
3247 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003248 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003249
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003250 // Clear the funcref afterwards, so that deleting it while
3251 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003252 if (evaluate)
3253 clear_tv(&base);
3254
Bram Moolenaarac92e252019-08-03 21:58:38 +02003255 return ret;
3256}
3257
3258/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003259 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3260 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003261 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3262 */
3263 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003264eval_index(
3265 char_u **arg,
3266 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003267 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003268 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003269{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003270 int evaluate = evalarg != NULL
3271 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003272 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003273 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003274 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003275 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003276 long len = -1;
3277 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003278 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003279 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003280
Bram Moolenaara03f2332016-02-06 18:09:59 +01003281 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003282 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003283 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003284 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003285 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003286 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003287 return FAIL;
3288 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003289#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003290 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003291 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003292 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003293#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003294 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003295 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003296 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003297 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003298 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003299 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003300 return FAIL;
3301 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003302 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003304 if (evaluate)
3305 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003306 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003307
3308 case VAR_STRING:
3309 case VAR_NUMBER:
3310 case VAR_LIST:
3311 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003312 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003313 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003314 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003315
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003316 init_tv(&var1);
3317 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003318 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003319 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003320 /*
3321 * dict.name
3322 */
3323 key = *arg + 1;
3324 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3325 ;
3326 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003327 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003328 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003329 }
3330 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003331 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003332 /*
3333 * something[idx]
3334 *
3335 * Get the (first) variable from inside the [].
3336 */
3337 *arg = skipwhite(*arg + 1);
3338 if (**arg == ':')
3339 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003340 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003341 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003342 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003344 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003345 clear_tv(&var1);
3346 return FAIL;
3347 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003348
3349 /*
3350 * Get the second variable from inside the [:].
3351 */
3352 if (**arg == ':')
3353 {
3354 range = TRUE;
3355 *arg = skipwhite(*arg + 1);
3356 if (**arg == ']')
3357 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003358 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003360 if (!empty1)
3361 clear_tv(&var1);
3362 return FAIL;
3363 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003364 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003365 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003366 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003367 if (!empty1)
3368 clear_tv(&var1);
3369 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003370 return FAIL;
3371 }
3372 }
3373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003374 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003375 if (**arg != ']')
3376 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003377 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003378 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003379 clear_tv(&var1);
3380 if (range)
3381 clear_tv(&var2);
3382 return FAIL;
3383 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003384 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003385 }
3386
3387 if (evaluate)
3388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003389 n1 = 0;
3390 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003391 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003392 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003393 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003394 }
3395 if (range)
3396 {
3397 if (empty2)
3398 n2 = -1;
3399 else
3400 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003401 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003402 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003403 }
3404 }
3405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003406 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003407 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003408 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003409 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003411 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003412 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003413 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003414 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003415 case VAR_SPECIAL:
3416 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003417 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003418 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003419
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003420 case VAR_NUMBER:
3421 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003422 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003423 len = (long)STRLEN(s);
3424 if (range)
3425 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003426 // The resulting variable is a substring. If the indexes
3427 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003428 if (n1 < 0)
3429 {
3430 n1 = len + n1;
3431 if (n1 < 0)
3432 n1 = 0;
3433 }
3434 if (n2 < 0)
3435 n2 = len + n2;
3436 else if (n2 >= len)
3437 n2 = len;
3438 if (n1 >= len || n2 < 0 || n1 > n2)
3439 s = NULL;
3440 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003441 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003442 }
3443 else
3444 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003445 // The resulting variable is a string of a single
3446 // character. If the index is too big or negative the
3447 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003448 if (n1 >= len || n1 < 0)
3449 s = NULL;
3450 else
3451 s = vim_strnsave(s + n1, 1);
3452 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003453 clear_tv(rettv);
3454 rettv->v_type = VAR_STRING;
3455 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003456 break;
3457
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003458 case VAR_BLOB:
3459 len = blob_len(rettv->vval.v_blob);
3460 if (range)
3461 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003462 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003463 // are out of range the result is empty.
3464 if (n1 < 0)
3465 {
3466 n1 = len + n1;
3467 if (n1 < 0)
3468 n1 = 0;
3469 }
3470 if (n2 < 0)
3471 n2 = len + n2;
3472 else if (n2 >= len)
3473 n2 = len - 1;
3474 if (n1 >= len || n2 < 0 || n1 > n2)
3475 {
3476 clear_tv(rettv);
3477 rettv->v_type = VAR_BLOB;
3478 rettv->vval.v_blob = NULL;
3479 }
3480 else
3481 {
3482 blob_T *blob = blob_alloc();
3483
3484 if (blob != NULL)
3485 {
3486 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3487 {
3488 blob_free(blob);
3489 return FAIL;
3490 }
3491 blob->bv_ga.ga_len = n2 - n1 + 1;
3492 for (i = n1; i <= n2; i++)
3493 blob_set(blob, i - n1,
3494 blob_get(rettv->vval.v_blob, i));
3495
3496 clear_tv(rettv);
3497 rettv_blob_set(rettv, blob);
3498 }
3499 }
3500 }
3501 else
3502 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003503 // The resulting variable is a byte value.
3504 // If the index is too big or negative that is an error.
3505 if (n1 < 0)
3506 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003507 if (n1 < len && n1 >= 0)
3508 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003509 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003510
3511 clear_tv(rettv);
3512 rettv->v_type = VAR_NUMBER;
3513 rettv->vval.v_number = v;
3514 }
3515 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003516 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003517 }
3518 break;
3519
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003520 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003521 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003522 if (n1 < 0)
3523 n1 = len + n1;
3524 if (!empty1 && (n1 < 0 || n1 >= len))
3525 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003526 // For a range we allow invalid values and return an empty
3527 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003528 if (!range)
3529 {
3530 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003531 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003532 return FAIL;
3533 }
3534 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003535 }
3536 if (range)
3537 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003538 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003539
3540 if (n2 < 0)
3541 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003542 else if (n2 >= len)
3543 n2 = len - 1;
3544 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003545 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003546 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 if (l == NULL)
3548 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003549 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003550 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003551 }
3552 else
3553 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003554 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003555 clear_tv(rettv);
3556 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003557 }
3558 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003559
3560 case VAR_DICT:
3561 if (range)
3562 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003563 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003564 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003565 if (len == -1)
3566 clear_tv(&var1);
3567 return FAIL;
3568 }
3569 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003570 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003571
3572 if (len == -1)
3573 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003574 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003575 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003576 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003577 clear_tv(&var1);
3578 return FAIL;
3579 }
3580 }
3581
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003582 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003583
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003584 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003585 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003586 if (len == -1)
3587 clear_tv(&var1);
3588 if (item == NULL)
3589 return FAIL;
3590
3591 copy_tv(&item->di_tv, &var1);
3592 clear_tv(rettv);
3593 *rettv = var1;
3594 }
3595 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003596 }
3597 }
3598
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003599 return OK;
3600}
3601
3602/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003603 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003604 */
3605 char_u *
3606partial_name(partial_T *pt)
3607{
3608 if (pt->pt_name != NULL)
3609 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003610 if (pt->pt_func != NULL)
3611 return pt->pt_func->uf_name;
3612 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003613}
3614
Bram Moolenaarddecc252016-04-06 22:59:37 +02003615 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003616partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003617{
3618 int i;
3619
3620 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003621 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003622 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003623 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003624 if (pt->pt_name != NULL)
3625 {
3626 func_unref(pt->pt_name);
3627 vim_free(pt->pt_name);
3628 }
3629 else
3630 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003631
3632 if (pt->pt_funcstack != NULL)
3633 {
3634 // Decrease the reference count for the context of a closure. If down
3635 // to zero free it and clear the variables on the stack.
3636 if (--pt->pt_funcstack->fs_refcount == 0)
3637 {
3638 garray_T *gap = &pt->pt_funcstack->fs_ga;
3639 typval_T *stack = gap->ga_data;
3640
3641 for (i = 0; i < gap->ga_len; ++i)
3642 clear_tv(stack + i);
3643 ga_clear(gap);
3644 vim_free(pt->pt_funcstack);
3645 }
3646 pt->pt_funcstack = NULL;
3647 }
3648
Bram Moolenaarddecc252016-04-06 22:59:37 +02003649 vim_free(pt);
3650}
3651
3652/*
3653 * Unreference a closure: decrement the reference count and free it when it
3654 * becomes zero.
3655 */
3656 void
3657partial_unref(partial_T *pt)
3658{
3659 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003660 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003661}
3662
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003663/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003664 * Return the next (unique) copy ID.
3665 * Used for serializing nested structures.
3666 */
3667 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003668get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003669{
3670 current_copyID += COPYID_INC;
3671 return current_copyID;
3672}
3673
3674/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003675 * Garbage collection for lists and dictionaries.
3676 *
3677 * We use reference counts to be able to free most items right away when they
3678 * are no longer used. But for composite items it's possible that it becomes
3679 * unused while the reference count is > 0: When there is a recursive
3680 * reference. Example:
3681 * :let l = [1, 2, 3]
3682 * :let d = {9: l}
3683 * :let l[1] = d
3684 *
3685 * Since this is quite unusual we handle this with garbage collection: every
3686 * once in a while find out which lists and dicts are not referenced from any
3687 * variable.
3688 *
3689 * Here is a good reference text about garbage collection (refers to Python
3690 * but it applies to all reference-counting mechanisms):
3691 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003692 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003693
3694/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003695 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003696 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003697 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003698 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003699 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003700garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003701{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003702 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003703 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003704 buf_T *buf;
3705 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003706 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003707 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003708
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003709 if (!testing)
3710 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003711 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003712 want_garbage_collect = FALSE;
3713 may_garbage_collect = FALSE;
3714 garbage_collect_at_exit = FALSE;
3715 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003716
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003717 // The execution stack can grow big, limit the size.
3718 if (exestack.ga_maxlen - exestack.ga_len > 500)
3719 {
3720 size_t new_len;
3721 char_u *pp;
3722 int n;
3723
3724 // Keep 150% of the current size, with a minimum of the growth size.
3725 n = exestack.ga_len / 2;
3726 if (n < exestack.ga_growsize)
3727 n = exestack.ga_growsize;
3728
3729 // Don't make it bigger though.
3730 if (exestack.ga_len + n < exestack.ga_maxlen)
3731 {
3732 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3733 pp = vim_realloc(exestack.ga_data, new_len);
3734 if (pp == NULL)
3735 return FAIL;
3736 exestack.ga_maxlen = exestack.ga_len + n;
3737 exestack.ga_data = pp;
3738 }
3739 }
3740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003741 // We advance by two because we add one for items referenced through
3742 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003743 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003744
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003745 /*
3746 * 1. Go through all accessible variables and mark all lists and dicts
3747 * with copyID.
3748 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003749
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003750 // Don't free variables in the previous_funccal list unless they are only
3751 // referenced through previous_funccal. This must be first, because if
3752 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003753 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003754
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003755 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003756 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003757
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003758 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003759 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003760 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3761 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003763 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003764 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003765 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3766 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003767 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003768 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3769 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003770#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003771 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003772 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3773 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003774 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003775 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003776 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3777 NULL, NULL);
3778#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003780 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003781 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003782 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3783 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003784 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003785 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003786
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003787 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003788 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003789
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003790 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003791 abort = abort || set_ref_in_functions(copyID);
3792
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003793 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003794 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003796 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003797 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003798
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003799 // callbacks in buffers
3800 abort = abort || set_ref_in_buffers(copyID);
3801
Bram Moolenaar1dced572012-04-05 16:54:08 +02003802#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003803 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003804#endif
3805
Bram Moolenaardb913952012-06-29 12:54:53 +02003806#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003807 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003808#endif
3809
3810#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003811 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003812#endif
3813
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003814#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003815 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003816 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003817#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003818#ifdef FEAT_NETBEANS_INTG
3819 abort = abort || set_ref_in_nb_channel(copyID);
3820#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003821
Bram Moolenaare3188e22016-05-31 21:13:04 +02003822#ifdef FEAT_TIMERS
3823 abort = abort || set_ref_in_timer(copyID);
3824#endif
3825
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003826#ifdef FEAT_QUICKFIX
3827 abort = abort || set_ref_in_quickfix(copyID);
3828#endif
3829
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003830#ifdef FEAT_TERMINAL
3831 abort = abort || set_ref_in_term(copyID);
3832#endif
3833
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003834#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003835 abort = abort || set_ref_in_popups(copyID);
3836#endif
3837
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003838 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003839 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003840 /*
3841 * 2. Free lists and dictionaries that are not referenced.
3842 */
3843 did_free = free_unref_items(copyID);
3844
3845 /*
3846 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003847 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003848 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003849 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003850 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003851 else if (p_verbose > 0)
3852 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003853 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003854 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003855
3856 return did_free;
3857}
3858
3859/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003860 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003861 */
3862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003863free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003864{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003865 int did_free = FALSE;
3866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003867 // Let all "free" functions know that we are here. This means no
3868 // dictionaries, lists, channels or jobs are to be freed, because we will
3869 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003870 in_free_unref_items = TRUE;
3871
3872 /*
3873 * PASS 1: free the contents of the items. We don't free the items
3874 * themselves yet, so that it is possible to decrement refcount counters
3875 */
3876
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003877 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003878 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003879
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003880 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003881 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003882
3883#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003884 // Go through the list of jobs and free items without the copyID. This
3885 // must happen before doing channels, because jobs refer to channels, but
3886 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003887 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3888
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003889 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003890 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3891#endif
3892
3893 /*
3894 * PASS 2: free the items themselves.
3895 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003896 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003897 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003898
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003899#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003900 // Go through the list of jobs and free items without the copyID. This
3901 // must happen before doing channels, because jobs refer to channels, but
3902 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003903 free_unused_jobs(copyID, COPYID_MASK);
3904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003905 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003906 free_unused_channels(copyID, COPYID_MASK);
3907#endif
3908
3909 in_free_unref_items = FALSE;
3910
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003911 return did_free;
3912}
3913
3914/*
3915 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003916 * "list_stack" is used to add lists to be marked. Can be NULL.
3917 *
3918 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003919 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003920 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003921set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003922{
3923 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003924 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003925 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003926 hashtab_T *cur_ht;
3927 ht_stack_T *ht_stack = NULL;
3928 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003929
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003930 cur_ht = ht;
3931 for (;;)
3932 {
3933 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003934 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003935 // Mark each item in the hashtab. If the item contains a hashtab
3936 // it is added to ht_stack, if it contains a list it is added to
3937 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003938 todo = (int)cur_ht->ht_used;
3939 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3940 if (!HASHITEM_EMPTY(hi))
3941 {
3942 --todo;
3943 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3944 &ht_stack, list_stack);
3945 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003946 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003947
3948 if (ht_stack == NULL)
3949 break;
3950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003951 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003952 cur_ht = ht_stack->ht;
3953 tempitem = ht_stack;
3954 ht_stack = ht_stack->prev;
3955 free(tempitem);
3956 }
3957
3958 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003959}
3960
3961/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003962 * Mark a dict and its items with "copyID".
3963 * Returns TRUE if setting references failed somehow.
3964 */
3965 int
3966set_ref_in_dict(dict_T *d, int copyID)
3967{
3968 if (d != NULL && d->dv_copyID != copyID)
3969 {
3970 d->dv_copyID = copyID;
3971 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3972 }
3973 return FALSE;
3974}
3975
3976/*
3977 * Mark a list and its items with "copyID".
3978 * Returns TRUE if setting references failed somehow.
3979 */
3980 int
3981set_ref_in_list(list_T *ll, int copyID)
3982{
3983 if (ll != NULL && ll->lv_copyID != copyID)
3984 {
3985 ll->lv_copyID = copyID;
3986 return set_ref_in_list_items(ll, copyID, NULL);
3987 }
3988 return FALSE;
3989}
3990
3991/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003992 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003993 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3994 *
3995 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003996 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003997 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003998set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003999{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004000 listitem_T *li;
4001 int abort = FALSE;
4002 list_T *cur_l;
4003 list_stack_T *list_stack = NULL;
4004 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004005
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004006 cur_l = l;
4007 for (;;)
4008 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004009 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004010 // Mark each item in the list. If the item contains a hashtab
4011 // it is added to ht_stack, if it contains a list it is added to
4012 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004013 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4014 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4015 ht_stack, &list_stack);
4016 if (list_stack == NULL)
4017 break;
4018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004019 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004020 cur_l = list_stack->list;
4021 tempitem = list_stack;
4022 list_stack = list_stack->prev;
4023 free(tempitem);
4024 }
4025
4026 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004027}
4028
4029/*
4030 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004031 * "list_stack" is used to add lists to be marked. Can be NULL.
4032 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4033 *
4034 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004035 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004036 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004037set_ref_in_item(
4038 typval_T *tv,
4039 int copyID,
4040 ht_stack_T **ht_stack,
4041 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004042{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004043 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004044
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004045 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004046 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004047 dict_T *dd = tv->vval.v_dict;
4048
Bram Moolenaara03f2332016-02-06 18:09:59 +01004049 if (dd != NULL && dd->dv_copyID != copyID)
4050 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004051 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004052 dd->dv_copyID = copyID;
4053 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004054 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004055 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4056 }
4057 else
4058 {
4059 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4060 if (newitem == NULL)
4061 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004062 else
4063 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004064 newitem->ht = &dd->dv_hashtab;
4065 newitem->prev = *ht_stack;
4066 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004067 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004068 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004069 }
4070 }
4071 else if (tv->v_type == VAR_LIST)
4072 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004073 list_T *ll = tv->vval.v_list;
4074
Bram Moolenaara03f2332016-02-06 18:09:59 +01004075 if (ll != NULL && ll->lv_copyID != copyID)
4076 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004077 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004078 ll->lv_copyID = copyID;
4079 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004080 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004081 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004082 }
4083 else
4084 {
4085 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004086 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004087 if (newitem == NULL)
4088 abort = TRUE;
4089 else
4090 {
4091 newitem->list = ll;
4092 newitem->prev = *list_stack;
4093 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004094 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004095 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004096 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004097 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004098 else if (tv->v_type == VAR_FUNC)
4099 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004100 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004101 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004102 else if (tv->v_type == VAR_PARTIAL)
4103 {
4104 partial_T *pt = tv->vval.v_partial;
4105 int i;
4106
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004107 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004108 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004109 // Didn't see this partial yet.
4110 pt->pt_copyID = copyID;
4111
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004112 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004113
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004114 if (pt->pt_dict != NULL)
4115 {
4116 typval_T dtv;
4117
4118 dtv.v_type = VAR_DICT;
4119 dtv.vval.v_dict = pt->pt_dict;
4120 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4121 }
4122
4123 for (i = 0; i < pt->pt_argc; ++i)
4124 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4125 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004126 if (pt->pt_funcstack != NULL)
4127 {
4128 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4129
4130 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4131 abort = abort || set_ref_in_item(stack + i, copyID,
4132 ht_stack, list_stack);
4133 }
4134
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004135 }
4136 }
4137#ifdef FEAT_JOB_CHANNEL
4138 else if (tv->v_type == VAR_JOB)
4139 {
4140 job_T *job = tv->vval.v_job;
4141 typval_T dtv;
4142
4143 if (job != NULL && job->jv_copyID != copyID)
4144 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004145 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004146 if (job->jv_channel != NULL)
4147 {
4148 dtv.v_type = VAR_CHANNEL;
4149 dtv.vval.v_channel = job->jv_channel;
4150 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4151 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004152 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004153 {
4154 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004155 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004156 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4157 }
4158 }
4159 }
4160 else if (tv->v_type == VAR_CHANNEL)
4161 {
4162 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004163 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004164 typval_T dtv;
4165 jsonq_T *jq;
4166 cbq_T *cq;
4167
4168 if (ch != NULL && ch->ch_copyID != copyID)
4169 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004170 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004171 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004172 {
4173 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4174 jq = jq->jq_next)
4175 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4176 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4177 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004178 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004179 {
4180 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004181 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004182 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4183 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004184 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004185 {
4186 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004187 dtv.vval.v_partial =
4188 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004189 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4190 }
4191 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004192 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004193 {
4194 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004195 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004196 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4197 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004198 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004199 {
4200 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004201 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004202 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4203 }
4204 }
4205 }
4206#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004207 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004208}
4209
Bram Moolenaar8c711452005-01-14 21:53:12 +00004210/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004211 * Return a string with the string representation of a variable.
4212 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004213 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004214 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004215 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4216 * stings as "string()", otherwise does not put quotes around strings, as
4217 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004218 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4219 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004220 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004221 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004222 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004223echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004224 typval_T *tv,
4225 char_u **tofree,
4226 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004227 int copyID,
4228 int echo_style,
4229 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004230 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004231{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004232 static int recurse = 0;
4233 char_u *r = NULL;
4234
Bram Moolenaar33570922005-01-25 22:26:29 +00004235 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004236 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004237 if (!did_echo_string_emsg)
4238 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004239 // Only give this message once for a recursive call to avoid
4240 // flooding the user with errors. And stop iterating over lists
4241 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004242 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004243 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004244 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004245 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004246 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004247 }
4248 ++recurse;
4249
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004250 switch (tv->v_type)
4251 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004252 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004253 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004254 {
4255 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004256 r = tv->vval.v_string;
4257 if (r == NULL)
4258 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004259 }
4260 else
4261 {
4262 *tofree = string_quote(tv->vval.v_string, FALSE);
4263 r = *tofree;
4264 }
4265 break;
4266
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004267 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004268 if (echo_style)
4269 {
4270 *tofree = NULL;
4271 r = tv->vval.v_string;
4272 }
4273 else
4274 {
4275 *tofree = string_quote(tv->vval.v_string, TRUE);
4276 r = *tofree;
4277 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004278 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004279
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004280 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004281 {
4282 partial_T *pt = tv->vval.v_partial;
4283 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004284 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004285 garray_T ga;
4286 int i;
4287 char_u *tf;
4288
4289 ga_init2(&ga, 1, 100);
4290 ga_concat(&ga, (char_u *)"function(");
4291 if (fname != NULL)
4292 {
4293 ga_concat(&ga, fname);
4294 vim_free(fname);
4295 }
4296 if (pt != NULL && pt->pt_argc > 0)
4297 {
4298 ga_concat(&ga, (char_u *)", [");
4299 for (i = 0; i < pt->pt_argc; ++i)
4300 {
4301 if (i > 0)
4302 ga_concat(&ga, (char_u *)", ");
4303 ga_concat(&ga,
4304 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4305 vim_free(tf);
4306 }
4307 ga_concat(&ga, (char_u *)"]");
4308 }
4309 if (pt != NULL && pt->pt_dict != NULL)
4310 {
4311 typval_T dtv;
4312
4313 ga_concat(&ga, (char_u *)", ");
4314 dtv.v_type = VAR_DICT;
4315 dtv.vval.v_dict = pt->pt_dict;
4316 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4317 vim_free(tf);
4318 }
4319 ga_concat(&ga, (char_u *)")");
4320
4321 *tofree = ga.ga_data;
4322 r = *tofree;
4323 break;
4324 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004325
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004326 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004327 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004328 break;
4329
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004330 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004331 if (tv->vval.v_list == NULL)
4332 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004333 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004334 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004335 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004336 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004337 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4338 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004339 {
4340 *tofree = NULL;
4341 r = (char_u *)"[...]";
4342 }
4343 else
4344 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004345 int old_copyID = tv->vval.v_list->lv_copyID;
4346
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004347 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004348 *tofree = list2string(tv, copyID, restore_copyID);
4349 if (restore_copyID)
4350 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004351 r = *tofree;
4352 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004353 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004354
Bram Moolenaar8c711452005-01-14 21:53:12 +00004355 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004356 if (tv->vval.v_dict == NULL)
4357 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004358 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004359 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004360 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004361 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004362 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4363 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004364 {
4365 *tofree = NULL;
4366 r = (char_u *)"{...}";
4367 }
4368 else
4369 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004370 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004371
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004372 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004373 *tofree = dict2string(tv, copyID, restore_copyID);
4374 if (restore_copyID)
4375 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004376 r = *tofree;
4377 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004378 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004379
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004381 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004382 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004384 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004385 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004386 break;
4387
Bram Moolenaar835dc632016-02-07 14:27:38 +01004388 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004389 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004390 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004391 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004392 if (composite_val)
4393 {
4394 *tofree = string_quote(r, FALSE);
4395 r = *tofree;
4396 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004397 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004398
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004399 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004400#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004401 *tofree = NULL;
4402 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4403 r = numbuf;
4404 break;
4405#endif
4406
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004407 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004408 case VAR_SPECIAL:
4409 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004410 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004411 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004412 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004413
Bram Moolenaar8502c702014-06-17 12:51:16 +02004414 if (--recurse == 0)
4415 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004416 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004417}
4418
4419/*
4420 * Return a string with the string representation of a variable.
4421 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4422 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004423 * Does not put quotes around strings, as ":echo" displays values.
4424 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4425 * May return NULL.
4426 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004427 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004428echo_string(
4429 typval_T *tv,
4430 char_u **tofree,
4431 char_u *numbuf,
4432 int copyID)
4433{
4434 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4435}
4436
4437/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004438 * Return string "str" in ' quotes, doubling ' characters.
4439 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004440 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004441 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004442 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004443string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004444{
Bram Moolenaar33570922005-01-25 22:26:29 +00004445 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004446 char_u *p, *r, *s;
4447
Bram Moolenaar33570922005-01-25 22:26:29 +00004448 len = (function ? 13 : 3);
4449 if (str != NULL)
4450 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004451 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004452 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004453 if (*p == '\'')
4454 ++len;
4455 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004456 s = r = alloc(len);
4457 if (r != NULL)
4458 {
4459 if (function)
4460 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004461 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004462 r += 10;
4463 }
4464 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004465 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004466 if (str != NULL)
4467 for (p = str; *p != NUL; )
4468 {
4469 if (*p == '\'')
4470 *r++ = '\'';
4471 MB_COPY_CHAR(p, r);
4472 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004473 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004474 if (function)
4475 *r++ = ')';
4476 *r++ = NUL;
4477 }
4478 return s;
4479}
4480
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004481#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004482/*
4483 * Convert the string "text" to a floating point number.
4484 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4485 * this always uses a decimal point.
4486 * Returns the length of the text that was consumed.
4487 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004488 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004489string2float(
4490 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004491 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004492{
4493 char *s = (char *)text;
4494 float_T f;
4495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004496 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004497 if (STRNICMP(text, "inf", 3) == 0)
4498 {
4499 *value = INFINITY;
4500 return 3;
4501 }
4502 if (STRNICMP(text, "-inf", 3) == 0)
4503 {
4504 *value = -INFINITY;
4505 return 4;
4506 }
4507 if (STRNICMP(text, "nan", 3) == 0)
4508 {
4509 *value = NAN;
4510 return 3;
4511 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004512 f = strtod(s, &s);
4513 *value = f;
4514 return (int)((char_u *)s - text);
4515}
4516#endif
4517
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004520 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004522 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004523var2fpos(
4524 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004525 int dollar_lnum, // TRUE when $ is last line
4526 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004528 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004530 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004532 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004533 if (varp->v_type == VAR_LIST)
4534 {
4535 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004536 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004537 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004538 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004539
4540 l = varp->vval.v_list;
4541 if (l == NULL)
4542 return NULL;
4543
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004544 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004545 pos.lnum = list_find_nr(l, 0L, &error);
4546 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004547 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004548
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004549 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004550 pos.col = list_find_nr(l, 1L, &error);
4551 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004552 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004553 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004555 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004556 li = list_find(l, 1L);
4557 if (li != NULL && li->li_tv.v_type == VAR_STRING
4558 && li->li_tv.vval.v_string != NULL
4559 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4560 pos.col = len + 1;
4561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004562 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004563 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004564 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004565 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004567 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004568 pos.coladd = list_find_nr(l, 2L, &error);
4569 if (error)
4570 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004571
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004572 return &pos;
4573 }
4574
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004575 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004576 if (name == NULL)
4577 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004578 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004580 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004581 {
4582 if (VIsual_active)
4583 return &VIsual;
4584 return &curwin->w_cursor;
4585 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004586 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004588 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4590 return NULL;
4591 return pp;
4592 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004593
Bram Moolenaara5525202006-03-02 22:52:09 +00004594 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004595
Bram Moolenaar477933c2007-07-17 14:32:23 +00004596 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004597 {
4598 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004599 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004600 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004601 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004602 // In silent Ex mode topline is zero, but that's not a valid line
4603 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004604 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004605 return &pos;
4606 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004607 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004608 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004609 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004610 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004611 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004612 return &pos;
4613 }
4614 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004615 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004617 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 {
4619 pos.lnum = curbuf->b_ml.ml_line_count;
4620 pos.col = 0;
4621 }
4622 else
4623 {
4624 pos.lnum = curwin->w_cursor.lnum;
4625 pos.col = (colnr_T)STRLEN(ml_get_curline());
4626 }
4627 return &pos;
4628 }
4629 return NULL;
4630}
4631
4632/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004633 * Convert list in "arg" into a position and optional file number.
4634 * When "fnump" is NULL there is no file number, only 3 items.
4635 * Note that the column is passed on as-is, the caller may want to decrement
4636 * it to use 1 for the first column.
4637 * Return FAIL when conversion is not possible, doesn't check the position for
4638 * validity.
4639 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004641list2fpos(
4642 typval_T *arg,
4643 pos_T *posp,
4644 int *fnump,
4645 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004646{
4647 list_T *l = arg->vval.v_list;
4648 long i = 0;
4649 long n;
4650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004651 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4652 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004653 if (arg->v_type != VAR_LIST
4654 || l == NULL
4655 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004656 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004657 return FAIL;
4658
4659 if (fnump != NULL)
4660 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004661 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004662 if (n < 0)
4663 return FAIL;
4664 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004665 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004666 *fnump = n;
4667 }
4668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004669 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004670 if (n < 0)
4671 return FAIL;
4672 posp->lnum = n;
4673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004674 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004675 if (n < 0)
4676 return FAIL;
4677 posp->col = n;
4678
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004679 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004680 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004681 posp->coladd = 0;
4682 else
4683 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004684
Bram Moolenaar493c1782014-05-28 14:34:46 +02004685 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004686 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004687
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004688 return OK;
4689}
4690
4691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 * Get the length of an environment variable name.
4693 * Advance "arg" to the first character after the name.
4694 * Return 0 for error.
4695 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004696 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004697get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698{
4699 char_u *p;
4700 int len;
4701
4702 for (p = *arg; vim_isIDc(*p); ++p)
4703 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004704 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return 0;
4706
4707 len = (int)(p - *arg);
4708 *arg = p;
4709 return len;
4710}
4711
4712/*
4713 * Get the length of the name of a function or internal variable.
4714 * "arg" is advanced to the first non-white character after the name.
4715 * Return 0 if something is wrong.
4716 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004717 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004718get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719{
4720 char_u *p;
4721 int len;
4722
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004723 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004725 {
4726 if (*p == ':')
4727 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004728 // "s:" is start of "s:var", but "n:" is not and can be used in
4729 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004730 len = (int)(p - *arg);
4731 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4732 || len > 1)
4733 break;
4734 }
4735 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004736 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 return 0;
4738
4739 len = (int)(p - *arg);
4740 *arg = skipwhite(p);
4741
4742 return len;
4743}
4744
4745/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004746 * Get the length of the name of a variable or function.
4747 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004749 * Return -1 if curly braces expansion failed.
4750 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 * If the name contains 'magic' {}'s, expand them and return the
4752 * expanded name in an allocated string via 'alias' - caller must free.
4753 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004754 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004755get_name_len(
4756 char_u **arg,
4757 char_u **alias,
4758 int evaluate,
4759 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
4761 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 char_u *p;
4763 char_u *expr_start;
4764 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004766 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767
4768 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4769 && (*arg)[2] == (int)KE_SNR)
4770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004771 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 *arg += 3;
4773 return get_id_len(arg) + 3;
4774 }
4775 len = eval_fname_script(*arg);
4776 if (len > 0)
4777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004778 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 *arg += len;
4780 }
4781
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004785 p = find_name_end(*arg, &expr_start, &expr_end,
4786 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (expr_start != NULL)
4788 {
4789 char_u *temp_string;
4790
4791 if (!evaluate)
4792 {
4793 len += (int)(p - *arg);
4794 *arg = skipwhite(p);
4795 return len;
4796 }
4797
4798 /*
4799 * Include any <SID> etc in the expanded string:
4800 * Thus the -len here.
4801 */
4802 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4803 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004804 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 *alias = temp_string;
4806 *arg = skipwhite(p);
4807 return (int)STRLEN(temp_string);
4808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
4810 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004811 // Only give an error when there is something, otherwise it will be
4812 // reported at a higher level.
4813 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004814 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
4816 return len;
4817}
4818
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819/*
4820 * Find the end of a variable or function name, taking care of magic braces.
4821 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4822 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004823 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004824 * Return a pointer to just after the name. Equal to "arg" if there is no
4825 * valid name.
4826 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004827 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004828find_name_end(
4829 char_u *arg,
4830 char_u **expr_start,
4831 char_u **expr_end,
4832 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004834 int mb_nest = 0;
4835 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004837 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004838 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004842 *expr_start = NULL;
4843 *expr_end = NULL;
4844 }
4845
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004846 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004847 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4848 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004849 return arg;
4850
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 for (p = arg; *p != NUL
4852 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004853 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004854 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004856 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004858 if (*p == '\'')
4859 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004860 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004861 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004862 ;
4863 if (*p == NUL)
4864 break;
4865 }
4866 else if (*p == '"')
4867 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004868 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004869 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004870 if (*p == '\\' && p[1] != NUL)
4871 ++p;
4872 if (*p == NUL)
4873 break;
4874 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004875 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4876 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004877 // "s:" is start of "s:var", but "n:" is not and can be used in
4878 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004879 len = (int)(p - arg);
4880 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004881 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004882 break;
4883 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004884
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 if (*p == '[')
4888 ++br_nest;
4889 else if (*p == ']')
4890 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004892
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004893 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 if (*p == '{')
4896 {
4897 mb_nest++;
4898 if (expr_start != NULL && *expr_start == NULL)
4899 *expr_start = p;
4900 }
4901 else if (*p == '}')
4902 {
4903 mb_nest--;
4904 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4905 *expr_end = p;
4906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909
4910 return p;
4911}
4912
4913/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004914 * Expands out the 'magic' {}'s in a variable/function name.
4915 * Note that this can call itself recursively, to deal with
4916 * constructs like foo{bar}{baz}{bam}
4917 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4918 * "in_start" ^
4919 * "expr_start" ^
4920 * "expr_end" ^
4921 * "in_end" ^
4922 *
4923 * Returns a new allocated string, which the caller must free.
4924 * Returns NULL for failure.
4925 */
4926 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004927make_expanded_name(
4928 char_u *in_start,
4929 char_u *expr_start,
4930 char_u *expr_end,
4931 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004932{
4933 char_u c1;
4934 char_u *retval = NULL;
4935 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004936
4937 if (expr_end == NULL || in_end == NULL)
4938 return NULL;
4939 *expr_start = NUL;
4940 *expr_end = NUL;
4941 c1 = *in_end;
4942 *in_end = NUL;
4943
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004944 temp_result = eval_to_string(expr_start + 1, FALSE);
4945 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004946 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004947 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4948 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004949 if (retval != NULL)
4950 {
4951 STRCPY(retval, in_start);
4952 STRCAT(retval, temp_result);
4953 STRCAT(retval, expr_end + 1);
4954 }
4955 }
4956 vim_free(temp_result);
4957
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004958 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004959 *expr_start = '{';
4960 *expr_end = '}';
4961
4962 if (retval != NULL)
4963 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004964 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004965 if (expr_start != NULL)
4966 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004967 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004968 temp_result = make_expanded_name(retval, expr_start,
4969 expr_end, temp_result);
4970 vim_free(retval);
4971 retval = temp_result;
4972 }
4973 }
4974
4975 return retval;
4976}
4977
4978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004980 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004983eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004985 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4986}
4987
4988/*
4989 * Return TRUE if character "c" can be used as the first character in a
4990 * variable or function name (excluding '{' and '}').
4991 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004992 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004993eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004994{
4995 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996}
4997
4998/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004999 * Handle:
5000 * - expr[expr], expr[expr:expr] subscript
5001 * - ".name" lookup
5002 * - function call with Funcref variable: func(expr)
5003 * - method call: var->method()
5004 *
5005 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005006 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005007 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005008handle_subscript(
5009 char_u **arg,
5010 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005011 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005012 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005013{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005014 int evaluate = evalarg != NULL
5015 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005016 int ret = OK;
5017 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005018
Bram Moolenaar61343f02019-07-20 21:11:13 +02005019 // "." is ".name" lookup when we found a dict or when evaluating and
5020 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005021 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005022 && (((**arg == '['
5023 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005024 || (!evaluate
5025 && (*arg)[1] != '.'
5026 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005027 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005028 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005029 && !VIM_ISWHITE(*(*arg - 1)))
5030 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005031 {
5032 if (**arg == '(')
5033 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005034 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005035
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005036 // Stop the expression evaluation when immediately aborting on
5037 // error, or when an interrupt occurred or an exception was thrown
5038 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005039 if (aborting())
5040 {
5041 if (ret == OK)
5042 clear_tv(rettv);
5043 ret = FAIL;
5044 }
5045 dict_unref(selfdict);
5046 selfdict = NULL;
5047 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005048 else if (**arg == '-')
5049 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005050 if (ret == OK)
5051 {
5052 if ((*arg)[2] == '{')
5053 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005054 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005055 else
5056 // expr->name()
5057 ret = eval_method(arg, rettv, evaluate, verbose);
5058 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005059 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005060 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005061 {
5062 dict_unref(selfdict);
5063 if (rettv->v_type == VAR_DICT)
5064 {
5065 selfdict = rettv->vval.v_dict;
5066 if (selfdict != NULL)
5067 ++selfdict->dv_refcount;
5068 }
5069 else
5070 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005071 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005072 {
5073 clear_tv(rettv);
5074 ret = FAIL;
5075 }
5076 }
5077 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005078
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005079 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5080 // Don't do this when "Func" is already a partial that was bound
5081 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005082 if (selfdict != NULL
5083 && (rettv->v_type == VAR_FUNC
5084 || (rettv->v_type == VAR_PARTIAL
5085 && (rettv->vval.v_partial->pt_auto
5086 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005087 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005088
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005089 dict_unref(selfdict);
5090 return ret;
5091}
5092
5093/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005094 * Make a copy of an item.
5095 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005096 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5097 * reference to an already copied list/dict can be used.
5098 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005099 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005100 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005101item_copy(
5102 typval_T *from,
5103 typval_T *to,
5104 int deep,
5105 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005106{
5107 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005108 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005109
Bram Moolenaar33570922005-01-25 22:26:29 +00005110 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005112 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005113 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005114 }
5115 ++recurse;
5116
5117 switch (from->v_type)
5118 {
5119 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005120 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005121 case VAR_STRING:
5122 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005123 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005124 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005125 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005126 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005127 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005128 copy_tv(from, to);
5129 break;
5130 case VAR_LIST:
5131 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005132 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005133 if (from->vval.v_list == NULL)
5134 to->vval.v_list = NULL;
5135 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005137 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005138 to->vval.v_list = from->vval.v_list->lv_copylist;
5139 ++to->vval.v_list->lv_refcount;
5140 }
5141 else
5142 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5143 if (to->vval.v_list == NULL)
5144 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005145 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005146 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005148 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005149 case VAR_DICT:
5150 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005151 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005152 if (from->vval.v_dict == NULL)
5153 to->vval.v_dict = NULL;
5154 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5155 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005156 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005157 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5158 ++to->vval.v_dict->dv_refcount;
5159 }
5160 else
5161 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5162 if (to->vval.v_dict == NULL)
5163 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005164 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005165 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005166 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005168 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005169 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170 }
5171 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005172 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173}
5174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005175 void
5176echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5177{
5178 char_u *tofree;
5179 char_u numbuf[NUMBUFLEN];
5180 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5181
5182 if (*atstart)
5183 {
5184 *atstart = FALSE;
5185 // Call msg_start() after eval1(), evaluating the expression
5186 // may cause a message to appear.
5187 if (with_space)
5188 {
5189 // Mark the saved text as finishing the line, so that what
5190 // follows is displayed on a new line when scrolling back
5191 // at the more prompt.
5192 msg_sb_eol();
5193 msg_start();
5194 }
5195 }
5196 else if (with_space)
5197 msg_puts_attr(" ", echo_attr);
5198
5199 if (p != NULL)
5200 for ( ; *p != NUL && !got_int; ++p)
5201 {
5202 if (*p == '\n' || *p == '\r' || *p == TAB)
5203 {
5204 if (*p != TAB && *needclr)
5205 {
5206 // remove any text still there from the command
5207 msg_clr_eos();
5208 *needclr = FALSE;
5209 }
5210 msg_putchar_attr(*p, echo_attr);
5211 }
5212 else
5213 {
5214 if (has_mbyte)
5215 {
5216 int i = (*mb_ptr2len)(p);
5217
5218 (void)msg_outtrans_len_attr(p, i, echo_attr);
5219 p += i - 1;
5220 }
5221 else
5222 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5223 }
5224 }
5225 vim_free(tofree);
5226}
5227
Bram Moolenaare9a41262005-01-15 22:18:47 +00005228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 * ":echo expr1 ..." print each argument separated with a space, add a
5230 * newline at the end.
5231 * ":echon expr1 ..." print each argument plain.
5232 */
5233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005234ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235{
5236 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005237 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 char_u *p;
5239 int needclr = TRUE;
5240 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005241 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005242 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005243 evalarg_T evalarg;
5244
Bram Moolenaar37c83712020-06-30 21:18:36 +02005245 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246
5247 if (eap->skip)
5248 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005249 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005251 // If eval1() causes an error message the text from the command may
5252 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005253 need_clr_eos = needclr;
5254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005256 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
5258 /*
5259 * Report the invalid expression unless the expression evaluation
5260 * has been cancelled due to an aborting error, an interrupt, or an
5261 * exception.
5262 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005263 if (!aborting() && did_emsg == did_emsg_before
5264 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005265 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005266 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 break;
5268 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005269 need_clr_eos = FALSE;
5270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005272 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005273
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 arg = skipwhite(arg);
5276 }
5277 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005278 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
5280 if (eap->skip)
5281 --emsg_skip;
5282 else
5283 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005284 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 if (needclr)
5286 msg_clr_eos();
5287 if (eap->cmdidx == CMD_echo)
5288 msg_end();
5289 }
5290}
5291
5292/*
5293 * ":echohl {name}".
5294 */
5295 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005296ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005298 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299}
5300
5301/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005302 * Returns the :echo attribute
5303 */
5304 int
5305get_echo_attr(void)
5306{
5307 return echo_attr;
5308}
5309
5310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 * ":execute expr1 ..." execute the result of an expression.
5312 * ":echomsg expr1 ..." Print a message
5313 * ":echoerr expr1 ..." Print an error
5314 * Each gets spaces around each argument and a newline at the end for
5315 * echo commands
5316 */
5317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005318ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
5320 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005321 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 int ret = OK;
5323 char_u *p;
5324 garray_T ga;
5325 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
5327 ga_init2(&ga, 1, 80);
5328
5329 if (eap->skip)
5330 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005331 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005333 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5334 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337 if (!eap->skip)
5338 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005339 char_u buf[NUMBUFLEN];
5340
5341 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005342 {
5343 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5344 {
5345 emsg(_(e_inval_string));
5346 p = NULL;
5347 }
5348 else
5349 p = tv_get_string_buf(&rettv, buf);
5350 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005351 else
5352 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005353 if (p == NULL)
5354 {
5355 clear_tv(&rettv);
5356 ret = FAIL;
5357 break;
5358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 len = (int)STRLEN(p);
5360 if (ga_grow(&ga, len + 2) == FAIL)
5361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005362 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 ret = FAIL;
5364 break;
5365 }
5366 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 ga.ga_len += len;
5370 }
5371
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 arg = skipwhite(arg);
5374 }
5375
5376 if (ret != FAIL && ga.ga_data != NULL)
5377 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005378 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5379 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005380 // Mark the already saved text as finishing the line, so that what
5381 // follows is displayed on a new line when scrolling back at the
5382 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005383 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005384 }
5385
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005387 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005388 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005389 out_flush();
5390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 else if (eap->cmdidx == CMD_echoerr)
5392 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005393 int save_did_emsg = did_emsg;
5394
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005395 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005396 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 if (!force_abort)
5398 did_emsg = save_did_emsg;
5399 }
5400 else if (eap->cmdidx == CMD_execute)
5401 do_cmdline((char_u *)ga.ga_data,
5402 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5403 }
5404
5405 ga_clear(&ga);
5406
5407 if (eap->skip)
5408 --emsg_skip;
5409
5410 eap->nextcmd = check_nextcmd(arg);
5411}
5412
5413/*
5414 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5415 * "arg" points to the "&" or '+' when called, to "option" when returning.
5416 * Returns NULL when no option name found. Otherwise pointer to the char
5417 * after the option name.
5418 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005419 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005420find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421{
5422 char_u *p = *arg;
5423
5424 ++p;
5425 if (*p == 'g' && p[1] == ':')
5426 {
5427 *opt_flags = OPT_GLOBAL;
5428 p += 2;
5429 }
5430 else if (*p == 'l' && p[1] == ':')
5431 {
5432 *opt_flags = OPT_LOCAL;
5433 p += 2;
5434 }
5435 else
5436 *opt_flags = 0;
5437
5438 if (!ASCII_ISALPHA(*p))
5439 return NULL;
5440 *arg = p;
5441
5442 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005443 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 else
5445 while (ASCII_ISALPHA(*p))
5446 ++p;
5447 return p;
5448}
5449
5450/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005451 * Display script name where an item was last set.
5452 * Should only be invoked when 'verbose' is non-zero.
5453 */
5454 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005455last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005456{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005457 char_u *p;
5458
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005459 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005460 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005461 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005462 if (p != NULL)
5463 {
5464 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005465 msg_puts(_("\n\tLast set from "));
5466 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005467 if (script_ctx.sc_lnum > 0)
5468 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005469 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005470 msg_outnum((long)script_ctx.sc_lnum);
5471 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005472 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005473 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005474 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005475 }
5476}
5477
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005478#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479
5480/*
5481 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005482 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 * "flags" can be "g" to do a global substitute.
5484 * Returns an allocated string, NULL for error.
5485 */
5486 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005487do_string_sub(
5488 char_u *str,
5489 char_u *pat,
5490 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005491 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005492 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 int sublen;
5495 regmatch_T regmatch;
5496 int i;
5497 int do_all;
5498 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005499 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 garray_T ga;
5501 char_u *ret;
5502 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005503 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005505 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005507 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
5509 ga_init2(&ga, 1, 200);
5510
5511 do_all = (flags[0] == 'g');
5512
5513 regmatch.rm_ic = p_ic;
5514 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5515 if (regmatch.regprog != NULL)
5516 {
5517 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005518 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5520 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005521 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005522 if (regmatch.startp[0] == regmatch.endp[0])
5523 {
5524 if (zero_width == regmatch.startp[0])
5525 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005526 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005527 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005528 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5529 (size_t)i);
5530 ga.ga_len += i;
5531 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005532 continue;
5533 }
5534 zero_width = regmatch.startp[0];
5535 }
5536
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 /*
5538 * Get some space for a temporary buffer to do the substitution
5539 * into. It will contain:
5540 * - The text up to where the match is.
5541 * - The substituted text.
5542 * - The text after the match.
5543 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005544 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005545 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5547 {
5548 ga_clear(&ga);
5549 break;
5550 }
5551
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005552 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 i = (int)(regmatch.startp[0] - tail);
5554 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005555 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005556 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 + ga.ga_len + i, TRUE, TRUE, FALSE);
5558 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005559 tail = regmatch.endp[0];
5560 if (*tail == NUL)
5561 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 if (!do_all)
5563 break;
5564 }
5565
5566 if (ga.ga_data != NULL)
5567 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5568
Bram Moolenaar473de612013-06-08 18:19:48 +02005569 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571
5572 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5573 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005574 if (p_cpo == empty_option)
5575 p_cpo = save_cpo;
5576 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005577 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005578 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579
5580 return ret;
5581}