blob: e6843cdd051816fa6052a68d7f21bf3715edaa32 [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
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200210eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100211{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100212 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100213 int ret;
214 int did_emsg_before = did_emsg;
215 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200216 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100217
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200218 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
219
220 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100221 if (ret == FAIL)
222 {
223 // Report the invalid expression unless the expression evaluation has
224 // been cancelled due to an aborting error, an interrupt, or an
225 // exception, or we already gave a more specific error.
226 // Also check called_emsg for when using assert_fails().
227 if (!aborting() && did_emsg == did_emsg_before
228 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100229 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100230 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200231 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100232 return ret;
233}
234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200236 * Return whether a typval is a valid expression to pass to eval_expr_typval()
237 * or eval_expr_to_bool(). An empty string returns FALSE;
238 */
239 int
240eval_expr_valid_arg(typval_T *tv)
241{
242 return tv->v_type != VAR_UNKNOWN
243 && (tv->v_type != VAR_STRING
244 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
245}
246
247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 * Evaluate an expression, which can be a function, partial or string.
249 * Pass arguments "argv[argc]".
250 * Return the result in "rettv" and OK or FAIL.
251 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200252 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100253eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
254{
255 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100256 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200257 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100258
259 if (expr->v_type == VAR_FUNC)
260 {
261 s = expr->vval.v_string;
262 if (s == NULL || *s == NUL)
263 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200264 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200265 funcexe.evaluate = TRUE;
266 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267 return FAIL;
268 }
269 else if (expr->v_type == VAR_PARTIAL)
270 {
271 partial_T *partial = expr->vval.v_partial;
272
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200273 if (partial == NULL)
274 return FAIL;
275
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200277 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200279 if (call_def_function(partial->pt_func, argc, argv,
280 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 return FAIL;
282 }
283 else
284 {
285 s = partial_name(partial);
286 if (s == NULL || *s == NUL)
287 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200288 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 funcexe.evaluate = TRUE;
290 funcexe.partial = partial;
291 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
292 return FAIL;
293 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 }
295 else
296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100297 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 if (s == NULL)
299 return FAIL;
300 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200301 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100303 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100304 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200305 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100306 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100307 return FAIL;
308 }
309 }
310 return OK;
311}
312
313/*
314 * Like eval_to_bool() but using a typval_T instead of a string.
315 * Works for string, funcref and partial.
316 */
317 int
318eval_expr_to_bool(typval_T *expr, int *error)
319{
320 typval_T rettv;
321 int res;
322
323 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
324 {
325 *error = TRUE;
326 return FALSE;
327 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100328 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100329 clear_tv(&rettv);
330 return res;
331}
332
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333/*
334 * Top level evaluation function, returning a string. If "skip" is TRUE,
335 * only parsing to "nextcmd" is done, without reporting errors. Return
336 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
337 */
338 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100339eval_to_string_skip(
340 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200341 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100342 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
Bram Moolenaar33570922005-01-25 22:26:29 +0000344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200346 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar37c83712020-06-30 21:18:36 +0200348 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 if (skip)
350 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200351 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 retval = NULL;
353 else
354 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100355 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000356 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 }
358 if (skip)
359 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200360 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
362 return retval;
363}
364
365/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366 * Skip over an expression at "*pp".
367 * Return FAIL for an error, OK otherwise.
368 */
369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100370skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000371{
Bram Moolenaar33570922005-01-25 22:26:29 +0000372 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000373
374 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200375 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000376}
377
378/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200379 * Skip over an expression at "*pp".
380 * If in Vim9 script and line breaks are encountered, the lines are
381 * concatenated. "evalarg->eval_tofree" will be set accordingly.
382 * Return FAIL for an error, OK otherwise.
383 */
384 int
385skip_expr_concatenate(char_u **start, char_u **end, evalarg_T *evalarg)
386{
387 typval_T rettv;
388 int res;
389 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
390 garray_T *gap = &evalarg->eval_ga;
391 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
392
393 if (vim9script && evalarg->eval_cookie != NULL)
394 {
395 ga_init2(gap, sizeof(char_u *), 10);
396 if (ga_grow(gap, 1) == OK)
397 // leave room for "start"
398 ++gap->ga_len;
399 }
400
401 // Don't evaluate the expression.
402 if (evalarg != NULL)
403 evalarg->eval_flags &= ~EVAL_EVALUATE;
404 *end = skipwhite(*end);
405 res = eval1(end, &rettv, evalarg);
406 if (evalarg != NULL)
407 evalarg->eval_flags = save_flags;
408
409 if (vim9script && evalarg->eval_cookie != NULL
410 && evalarg->eval_ga.ga_len > 1)
411 {
412 char_u *p;
413 size_t endoff = STRLEN(*end);
414
415 // Line breaks encountered, concatenate all the lines.
416 *((char_u **)gap->ga_data) = *start;
417 p = ga_concat_strings(gap, "");
418 *((char_u **)gap->ga_data) = NULL;
419 ga_clear_strings(gap);
420 gap->ga_itemsize = 0;
421 if (p == NULL)
422 return FAIL;
423 *start = p;
424 vim_free(evalarg->eval_tofree);
425 evalarg->eval_tofree = p;
426 // Compute "end" relative to the end.
427 *end = *start + STRLEN(*start) - endoff;
428 }
429
430 return res;
431}
432
433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000435 * When "convert" is TRUE convert a List into a sequence of lines and convert
436 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 * Return pointer to allocated memory, or NULL for failure.
438 */
439 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100440eval_to_string(
441 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100442 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443{
Bram Moolenaar33570922005-01-25 22:26:29 +0000444 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000446 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000447#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000448 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200451 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 retval = NULL;
453 else
454 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000455 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000456 {
457 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000458 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200459 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200460 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200461 if (tv.vval.v_list->lv_len > 0)
462 ga_append(&ga, NL);
463 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000464 ga_append(&ga, NUL);
465 retval = (char_u *)ga.ga_data;
466 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000467#ifdef FEAT_FLOAT
468 else if (convert && tv.v_type == VAR_FLOAT)
469 {
470 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
471 retval = vim_strsave(numbuf);
472 }
473#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000474 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100475 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000476 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200478 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479
480 return retval;
481}
482
483/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000484 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200485 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 */
487 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100488eval_to_string_safe(
489 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100490 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200493 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200495 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000496 if (use_sandbox)
497 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200498 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200499 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000500 if (use_sandbox)
501 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200502 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200503 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 return retval;
505}
506
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507/*
508 * Top level evaluation function, returning a number.
509 * Evaluates "expr" silently.
510 * Returns -1 for an error.
511 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200512 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100513eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514{
Bram Moolenaar33570922005-01-25 22:26:29 +0000515 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200516 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000517 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518
519 ++emsg_off;
520
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200521 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 retval = -1;
523 else
524 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100525 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000526 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 }
528 --emsg_off;
529
530 return retval;
531}
532
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000533/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000534 * Top level evaluation function.
535 * Returns an allocated typval_T with the result.
536 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000537 */
538 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200539eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000540{
541 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200542 evalarg_T evalarg;
543
544 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000545
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200546 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200547 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100548 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000549
Bram Moolenaar37c83712020-06-30 21:18:36 +0200550 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000551 return tv;
552}
553
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100555 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200556 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
557 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000558 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200560 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100561call_vim_function(
562 char_u *func,
563 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200564 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200565 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000567 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200568 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100570 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200571 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200572 funcexe.firstline = curwin->w_cursor.lnum;
573 funcexe.lastline = curwin->w_cursor.lnum;
574 funcexe.evaluate = TRUE;
575 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000576 if (ret == FAIL)
577 clear_tv(rettv);
578
579 return ret;
580}
581
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100582/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100583 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100584 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200585 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
586 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100587 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200588 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100589call_func_retnr(
590 char_u *func,
591 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200592 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100593{
594 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200595 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100596
Bram Moolenaarded27a12018-08-01 19:06:03 +0200597 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100598 return -1;
599
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100600 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100601 clear_tv(&rettv);
602 return retval;
603}
604
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000605/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100606 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000607 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200608 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
609 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000610 */
611 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100612call_func_retstr(
613 char_u *func,
614 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200615 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000616{
617 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000618 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000619
Bram Moolenaarded27a12018-08-01 19:06:03 +0200620 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000621 return NULL;
622
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100623 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000624 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 return retval;
626}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000627
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000628/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100629 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200630 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
631 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000632 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000633 */
634 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100635call_func_retlist(
636 char_u *func,
637 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200638 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000639{
640 typval_T rettv;
641
Bram Moolenaarded27a12018-08-01 19:06:03 +0200642 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000643 return NULL;
644
645 if (rettv.v_type != VAR_LIST)
646 {
647 clear_tv(&rettv);
648 return NULL;
649 }
650
651 return rettv.vval.v_list;
652}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#ifdef FEAT_FOLDING
655/*
656 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
657 * it in "*cp". Doesn't give error messages.
658 */
659 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100660eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661{
Bram Moolenaar33570922005-01-25 22:26:29 +0000662 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200663 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000665 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
666 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000669 if (use_sandbox)
670 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200671 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200673 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 retval = 0;
675 else
676 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100677 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000678 if (tv.v_type == VAR_NUMBER)
679 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000680 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 retval = 0;
682 else
683 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100684 // If the result is a string, check if there is a non-digit before
685 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000686 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 if (!VIM_ISDIGIT(*s) && *s != '-')
688 *cp = *s++;
689 retval = atol((char *)s);
690 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000691 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 }
693 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000694 if (use_sandbox)
695 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200696 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200697 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200699 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700}
701#endif
702
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000704 * Get an lval: variable, Dict item or List item that can be assigned a value
705 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
706 * "name.key", "name.key[expr]" etc.
707 * Indexing only works if "name" is an existing List or Dictionary.
708 * "name" points to the start of the name.
709 * If "rettv" is not NULL it points to the value to be assigned.
710 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
711 * wrong; must end in space or cmd separator.
712 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100713 * flags:
714 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100715 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100716 * GLV_NO_AUTOLOAD: do not use script autoloading
717 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000718 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000719 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000720 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000721 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200722 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100723get_lval(
724 char_u *name,
725 typval_T *rettv,
726 lval_T *lp,
727 int unlet,
728 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100729 int flags, // GLV_ values
730 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000731{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000732 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000733 char_u *expr_start, *expr_end;
734 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000735 dictitem_T *v;
736 typval_T var1;
737 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000738 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000739 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000740 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000741 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000742 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100743 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100745 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200746 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000747
748 if (skip)
749 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100750 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000751 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200752 lp->ll_name_end = find_name_end(name, NULL, NULL,
753 FNE_INCL_BR | fne_flags);
754 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000755 }
756
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100757 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000758 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000760 if (expr_start != NULL)
761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100762 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100763 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000764 && *p != '[' && *p != '.')
765 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100766 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000767 return NULL;
768 }
769
770 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
771 if (lp->ll_exp_name == NULL)
772 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100773 // Report an invalid expression in braces, unless the
774 // expression evaluation has been cancelled due to an
775 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000776 if (!aborting() && !quiet)
777 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000778 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100779 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000780 return NULL;
781 }
782 }
783 lp->ll_name = lp->ll_exp_name;
784 }
785 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000787 lp->ll_name = name;
788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
790 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100791 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792 char_u *tp = skipwhite(p + 1);
793
794 // parse the type after the name
795 lp->ll_type = parse_type(&tp, &si->sn_type_list);
796 lp->ll_name_end = tp;
797 }
798 }
799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100800 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
802 return p;
803
804 cc = *p;
805 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100806 // Only pass &ht when we would write to the variable, it prevents autoload
807 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100808 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
809 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000810 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100811 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000812 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000813 if (v == NULL)
814 return NULL;
815
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 /*
817 * Loop until no more [idx] or .key is following.
818 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000819 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100820 var1.v_type = VAR_UNKNOWN;
821 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000822 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000823 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000824 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
825 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100826 && lp->ll_tv->vval.v_dict != NULL)
827 && !(lp->ll_tv->v_type == VAR_BLOB
828 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000830 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100831 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000833 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000834 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100837 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000838 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000839 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000840
Bram Moolenaar8c711452005-01-14 21:53:12 +0000841 len = -1;
842 if (*p == '.')
843 {
844 key = p + 1;
845 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
846 ;
847 if (len == 0)
848 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000849 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100850 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000852 }
853 p = key + len;
854 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000855 else
856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100857 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000858 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000859 if (*p == ':')
860 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000861 else
862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000863 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200864 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100866 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000867 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100868 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000869 clear_tv(&var1);
870 return NULL;
871 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000872 }
873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100874 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000875 if (*p == ':')
876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100880 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100881 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000883 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100884 if (rettv != NULL
885 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100886 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100887 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100888 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000889 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100891 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100892 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000894 }
895 p = skipwhite(p + 1);
896 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000898 else
899 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200901 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200902 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000903 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100904 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000905 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000906 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100907 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000908 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100909 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100910 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000911 clear_tv(&var2);
912 return NULL;
913 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000914 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000916 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000917 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000918 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000919
Bram Moolenaar8c711452005-01-14 21:53:12 +0000920 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100923 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100924 clear_tv(&var1);
925 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000926 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000927 }
928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100929 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000930 ++p;
931 }
932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000934 {
935 if (len == -1)
936 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100937 // "[key]": get key from "var1"
938 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200939 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000940 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000941 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000943 }
944 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000945 lp->ll_list = NULL;
946 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000947 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100949 // When assigning to a scope dictionary check that a function and
950 // variable name is valid (only variable name unless it is l: or
951 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200952 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200953 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200954 int prevval;
955 int wrong;
956
957 if (len != -1)
958 {
959 prevval = key[len];
960 key[len] = NUL;
961 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200962 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100963 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200964 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
965 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200966 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200967 || !valid_varname(key);
968 if (len != -1)
969 key[len] = prevval;
970 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200971 {
972 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200973 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200974 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200975 }
976
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100979 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200980 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100981 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100983 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100984 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200985 return NULL;
986 }
987
Bram Moolenaar31b81602019-02-10 22:14:27 +0100988 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000989 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000991 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100992 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100993 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000995 }
996 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001000 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001001 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 p = NULL;
1003 break;
1004 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001005 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001006 else if ((flags & GLV_READ_ONLY) == 0
1007 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001008 {
1009 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001010 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001011 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001012
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001013 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001015 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001016 else if (lp->ll_tv->v_type == VAR_BLOB)
1017 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001018 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1019
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001020 /*
1021 * Get the number and item for the only or first index of the List.
1022 */
1023 if (empty1)
1024 lp->ll_n1 = 0;
1025 else
1026 // is number or string
1027 lp->ll_n1 = (long)tv_get_number(&var1);
1028 clear_tv(&var1);
1029
1030 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001031 || lp->ll_n1 > bloblen
1032 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001033 {
1034 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001035 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001036 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001037 return NULL;
1038 }
1039 if (lp->ll_range && !lp->ll_empty2)
1040 {
1041 lp->ll_n2 = (long)tv_get_number(&var2);
1042 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001043 if (lp->ll_n2 < 0
1044 || lp->ll_n2 >= bloblen
1045 || lp->ll_n2 < lp->ll_n1)
1046 {
1047 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001048 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001049 return NULL;
1050 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001051 }
1052 lp->ll_blob = lp->ll_tv->vval.v_blob;
1053 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001054 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001055 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 else
1057 {
1058 /*
1059 * Get the number and item for the only or first index of the List.
1060 */
1061 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001063 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001064 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001065 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001066 clear_tv(&var1);
1067
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001068 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001069 lp->ll_list = lp->ll_tv->vval.v_list;
1070 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1071 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001072 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001073 if (lp->ll_n1 < 0)
1074 {
1075 lp->ll_n1 = 0;
1076 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1077 }
1078 }
1079 if (lp->ll_li == NULL)
1080 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001081 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001082 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001083 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001084 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001085 }
1086
1087 /*
1088 * May need to find the item or absolute index for the second
1089 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001090 * When no index given: "lp->ll_empty2" is TRUE.
1091 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001092 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001095 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001096 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001098 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001099 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001100 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001101 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001102 {
1103 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001104 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001105 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001106 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001107 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001108 }
1109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001110 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 if (lp->ll_n1 < 0)
1112 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1113 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001114 {
1115 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001116 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001117 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001118 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001119 }
1120
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001121 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001122 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001123 }
1124
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001125 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 return p;
1128}
1129
1130/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001131 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001132 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001134clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135{
1136 vim_free(lp->ll_exp_name);
1137 vim_free(lp->ll_newkey);
1138}
1139
1140/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001141 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001143 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1144 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001147set_var_lval(
1148 lval_T *lp,
1149 char_u *endp,
1150 typval_T *rettv,
1151 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001153 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154{
1155 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001156 listitem_T *ri;
1157 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158
1159 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001161 cc = *endp;
1162 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001163 if (lp->ll_blob != NULL)
1164 {
1165 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001166
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001167 if (op != NULL && *op != '=')
1168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001169 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001170 return;
1171 }
1172
1173 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1174 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001175 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001176
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001177 if (lp->ll_empty2)
1178 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1179
1180 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001181 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001182 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001183 return;
1184 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001185 if (lp->ll_empty2)
1186 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001187
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001188 ir = 0;
1189 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1190 blob_set(lp->ll_blob, il,
1191 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001192 }
1193 else
1194 {
1195 val = (int)tv_get_number_chk(rettv, &error);
1196 if (!error)
1197 {
1198 garray_T *gap = &lp->ll_blob->bv_ga;
1199
1200 // Allow for appending a byte. Setting a byte beyond
1201 // the end is an error otherwise.
1202 if (lp->ll_n1 < gap->ga_len
1203 || (lp->ll_n1 == gap->ga_len
1204 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1205 {
1206 blob_set(lp->ll_blob, lp->ll_n1, val);
1207 if (lp->ll_n1 == gap->ga_len)
1208 ++gap->ga_len;
1209 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001210 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001211 }
1212 }
1213 }
1214 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001215 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001216 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001219 {
1220 emsg(_(e_cannot_mod));
1221 *endp = cc;
1222 return;
1223 }
1224
Bram Moolenaarff697e62019-02-12 22:28:33 +01001225 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001226 di = NULL;
1227 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1228 &tv, &di, TRUE, FALSE) == OK)
1229 {
1230 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001231 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1232 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001233 && tv_op(&tv, rettv, op) == OK)
1234 set_var(lp->ll_name, &tv, FALSE);
1235 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001236 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001238 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001240 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001242 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001243 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001244 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001245 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 else if (lp->ll_range)
1247 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001248 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001249 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001252 {
1253 emsg(_("E996: Cannot lock a range"));
1254 return;
1255 }
1256
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001257 /*
1258 * Check whether any of the list items is locked
1259 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001260 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001261 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001262 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001263 return;
1264 ri = ri->li_next;
1265 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1266 break;
1267 ll_li = ll_li->li_next;
1268 ++ll_n1;
1269 }
1270
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 /*
1272 * Assign the List values to the list items.
1273 */
1274 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001275 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001276 if (op != NULL && *op != '=')
1277 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1278 else
1279 {
1280 clear_tv(&lp->ll_li->li_tv);
1281 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1282 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001283 ri = ri->li_next;
1284 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1285 break;
1286 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001287 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001288 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001289 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001290 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001291 ri = NULL;
1292 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001293 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001294 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 lp->ll_li = lp->ll_li->li_next;
1296 ++lp->ll_n1;
1297 }
1298 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001299 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001300 else if (lp->ll_empty2
1301 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001302 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001303 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304 }
1305 else
1306 {
1307 /*
1308 * Assign to a List or Dictionary item.
1309 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001311 {
1312 emsg(_("E996: Cannot lock a list or dict"));
1313 return;
1314 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315 if (lp->ll_newkey != NULL)
1316 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001317 if (op != NULL && *op != '=')
1318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001319 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001320 return;
1321 }
1322
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001323 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001324 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 if (di == NULL)
1326 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001327 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1328 {
1329 vim_free(di);
1330 return;
1331 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001333 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334 else if (op != NULL && *op != '=')
1335 {
1336 tv_op(lp->ll_tv, rettv, op);
1337 return;
1338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001341
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 /*
1343 * Assign the value to the variable or list item.
1344 */
1345 if (copy)
1346 copy_tv(rettv, lp->ll_tv);
1347 else
1348 {
1349 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001350 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001351 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 }
1353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354}
1355
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001356/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001357 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1358 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359 * Returns OK or FAIL.
1360 */
1361 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001362tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001363{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001364 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001365 char_u numbuf[NUMBUFLEN];
1366 char_u *s;
1367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001368 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001369 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001370 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001371 {
1372 switch (tv1->v_type)
1373 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001374 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001375 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001377 case VAR_DICT:
1378 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001379 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001380 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001381 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001382 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001383 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001384 break;
1385
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001386 case VAR_BLOB:
1387 if (*op != '+' || tv2->v_type != VAR_BLOB)
1388 break;
1389 // BLOB += BLOB
1390 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1391 {
1392 blob_T *b1 = tv1->vval.v_blob;
1393 blob_T *b2 = tv2->vval.v_blob;
1394 int i, len = blob_len(b2);
1395 for (i = 0; i < len; i++)
1396 ga_append(&b1->bv_ga, blob_get(b2, i));
1397 }
1398 return OK;
1399
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001400 case VAR_LIST:
1401 if (*op != '+' || tv2->v_type != VAR_LIST)
1402 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001403 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001404 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1405 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1406 return OK;
1407
1408 case VAR_NUMBER:
1409 case VAR_STRING:
1410 if (tv2->v_type == VAR_LIST)
1411 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001412 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001413 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001414 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001415 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001416#ifdef FEAT_FLOAT
1417 if (tv2->v_type == VAR_FLOAT)
1418 {
1419 float_T f = n;
1420
Bram Moolenaarff697e62019-02-12 22:28:33 +01001421 if (*op == '%')
1422 break;
1423 switch (*op)
1424 {
1425 case '+': f += tv2->vval.v_float; break;
1426 case '-': f -= tv2->vval.v_float; break;
1427 case '*': f *= tv2->vval.v_float; break;
1428 case '/': f /= tv2->vval.v_float; break;
1429 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001430 clear_tv(tv1);
1431 tv1->v_type = VAR_FLOAT;
1432 tv1->vval.v_float = f;
1433 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001434 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001435#endif
1436 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001437 switch (*op)
1438 {
1439 case '+': n += tv_get_number(tv2); break;
1440 case '-': n -= tv_get_number(tv2); break;
1441 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001442 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1443 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001444 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001445 clear_tv(tv1);
1446 tv1->v_type = VAR_NUMBER;
1447 tv1->vval.v_number = n;
1448 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 }
1450 else
1451 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001452 if (tv2->v_type == VAR_FLOAT)
1453 break;
1454
Bram Moolenaarff697e62019-02-12 22:28:33 +01001455 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001456 s = tv_get_string(tv1);
1457 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458 clear_tv(tv1);
1459 tv1->v_type = VAR_STRING;
1460 tv1->vval.v_string = s;
1461 }
1462 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001463
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001464 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001465#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001466 {
1467 float_T f;
1468
Bram Moolenaarff697e62019-02-12 22:28:33 +01001469 if (*op == '%' || *op == '.'
1470 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001471 && tv2->v_type != VAR_NUMBER
1472 && tv2->v_type != VAR_STRING))
1473 break;
1474 if (tv2->v_type == VAR_FLOAT)
1475 f = tv2->vval.v_float;
1476 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001477 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001478 switch (*op)
1479 {
1480 case '+': tv1->vval.v_float += f; break;
1481 case '-': tv1->vval.v_float -= f; break;
1482 case '*': tv1->vval.v_float *= f; break;
1483 case '/': tv1->vval.v_float /= f; break;
1484 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001485 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001486#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001487 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001488 }
1489 }
1490
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001491 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492 return FAIL;
1493}
1494
1495/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001496 * Evaluate the expression used in a ":for var in expr" command.
1497 * "arg" points to "var".
1498 * Set "*errp" to TRUE for an error, FALSE otherwise;
1499 * Return a pointer that holds the info. Null when there is an error.
1500 */
1501 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502eval_for_line(
1503 char_u *arg,
1504 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001505 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001506 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001507{
Bram Moolenaar33570922005-01-25 22:26:29 +00001508 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001509 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001510 typval_T tv;
1511 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001512 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001514 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001515
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001516 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517 if (fi == NULL)
1518 return NULL;
1519
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001520 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001521 if (expr == NULL)
1522 return fi;
1523
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001524 expr = skipwhite_and_linebreak(expr, evalarg);
1525 if (expr[0] != 'i' || expr[1] != 'n'
1526 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001527 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001529 return fi;
1530 }
1531
1532 if (skip)
1533 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001534 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1535 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001536 {
1537 *errp = FALSE;
1538 if (!skip)
1539 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001540 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001541 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001542 l = tv.vval.v_list;
1543 if (l == NULL)
1544 {
1545 // a null list is like an empty list: do nothing
1546 clear_tv(&tv);
1547 }
1548 else
1549 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001551 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001553 // No need to increment the refcount, it's already set for
1554 // the list being used in "tv".
1555 fi->fi_list = l;
1556 list_add_watch(l, &fi->fi_lw);
1557 fi->fi_lw.lw_item = l->lv_first;
1558 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001559 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001560 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001561 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001562 fi->fi_bi = 0;
1563 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001564 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001565 typval_T btv;
1566
1567 // Make a copy, so that the iteration still works when the
1568 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001570 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001571 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001572 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001573 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001574 else
1575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001576 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001577 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001578 }
1579 }
1580 }
1581 if (skip)
1582 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001583 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001584
1585 return fi;
1586}
1587
1588/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001589 * Used when looping over a :for line, skip the "in expr" part.
1590 */
1591 void
1592skip_for_lines(void *fi_void, evalarg_T *evalarg)
1593{
1594 forinfo_T *fi = (forinfo_T *)fi_void;
1595 int i;
1596
1597 for (i = 0; i < fi->fi_break_count; ++i)
1598 eval_next_line(evalarg);
1599}
1600
1601/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001602 * Use the first item in a ":for" list. Advance to the next.
1603 * Assign the values to the variable (list). "arg" points to the first one.
1604 * Return TRUE when a valid item was found, FALSE when at end of list or
1605 * something wrong.
1606 */
1607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001608next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001609{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001610 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001611 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001612 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1613 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001614 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001615
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001616 if (fi->fi_blob != NULL)
1617 {
1618 typval_T tv;
1619
1620 if (fi->fi_bi >= blob_len(fi->fi_blob))
1621 return FALSE;
1622 tv.v_type = VAR_NUMBER;
1623 tv.v_lock = VAR_FIXED;
1624 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1625 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001626 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001627 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001628 }
1629
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630 item = fi->fi_lw.lw_item;
1631 if (item == NULL)
1632 result = FALSE;
1633 else
1634 {
1635 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001636 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001637 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638 }
1639 return result;
1640}
1641
1642/*
1643 * Free the structure used to store info used by ":for".
1644 */
1645 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001647{
Bram Moolenaar33570922005-01-25 22:26:29 +00001648 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001649
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001650 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001651 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001653 list_unref(fi->fi_list);
1654 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001655 if (fi != NULL && fi->fi_blob != NULL)
1656 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657 vim_free(fi);
1658}
1659
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001661set_context_for_expression(
1662 expand_T *xp,
1663 char_u *arg,
1664 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
1666 int got_eq = FALSE;
1667 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001670 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 {
1672 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001673 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001675 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001676 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 {
1678 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001679 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001680 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 break;
1682 }
1683 return;
1684 }
1685 }
1686 else
1687 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1688 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 while ((xp->xp_pattern = vim_strpbrk(arg,
1690 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1691 {
1692 c = *xp->xp_pattern;
1693 if (c == '&')
1694 {
1695 c = xp->xp_pattern[1];
1696 if (c == '&')
1697 {
1698 ++xp->xp_pattern;
1699 xp->xp_context = cmdidx != CMD_let || got_eq
1700 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1701 }
1702 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001705 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1706 xp->xp_pattern += 2;
1707
1708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
1710 else if (c == '$')
1711 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001712 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 xp->xp_context = EXPAND_ENV_VARS;
1714 }
1715 else if (c == '=')
1716 {
1717 got_eq = TRUE;
1718 xp->xp_context = EXPAND_EXPRESSION;
1719 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001720 else if (c == '#'
1721 && xp->xp_context == EXPAND_EXPRESSION)
1722 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001723 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001724 break;
1725 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001726 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 && xp->xp_context == EXPAND_FUNCTIONS
1728 && vim_strchr(xp->xp_pattern, '(') == NULL)
1729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001730 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 break;
1732 }
1733 else if (cmdidx != CMD_let || got_eq)
1734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001735 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
1737 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1738 if (c == '\\' && xp->xp_pattern[1] != NUL)
1739 ++xp->xp_pattern;
1740 xp->xp_context = EXPAND_NOTHING;
1741 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001742 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001744 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1746 /* skip */ ;
1747 xp->xp_context = EXPAND_NOTHING;
1748 }
1749 else if (c == '|')
1750 {
1751 if (xp->xp_pattern[1] == '|')
1752 {
1753 ++xp->xp_pattern;
1754 xp->xp_context = EXPAND_EXPRESSION;
1755 }
1756 else
1757 xp->xp_context = EXPAND_COMMANDS;
1758 }
1759 else
1760 xp->xp_context = EXPAND_EXPRESSION;
1761 }
1762 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001763 // Doesn't look like something valid, expand as an expression
1764 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 arg = xp->xp_pattern;
1767 if (*arg != NUL)
1768 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1769 /* skip */ ;
1770 }
1771 xp->xp_pattern = arg;
1772}
1773
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001775 * Return TRUE if "pat" matches "text".
1776 * Does not use 'cpo' and always uses 'magic'.
1777 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001778 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001779pattern_match(char_u *pat, char_u *text, int ic)
1780{
1781 int matches = FALSE;
1782 char_u *save_cpo;
1783 regmatch_T regmatch;
1784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001785 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001786 save_cpo = p_cpo;
1787 p_cpo = (char_u *)"";
1788 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1789 if (regmatch.regprog != NULL)
1790 {
1791 regmatch.rm_ic = ic;
1792 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1793 vim_regfree(regmatch.regprog);
1794 }
1795 p_cpo = save_cpo;
1796 return matches;
1797}
1798
1799/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001800 * Handle a name followed by "(". Both for just "name(arg)" and for
1801 * "expr->name(arg)".
1802 * Returns OK or FAIL.
1803 */
1804 static int
1805eval_func(
1806 char_u **arg, // points to "(", will be advanced
1807 char_u *name,
1808 int name_len,
1809 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001810 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001811 typval_T *basetv) // "expr" for "expr->name(arg)"
1812{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001813 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001814 char_u *s = name;
1815 int len = name_len;
1816 partial_T *partial;
1817 int ret = OK;
1818
1819 if (!evaluate)
1820 check_vars(s, len);
1821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001822 // If "s" is the name of a variable of type VAR_FUNC
1823 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001824 s = deref_func_name(s, &len, &partial, !evaluate);
1825
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001826 // Need to make a copy, in case evaluating the arguments makes
1827 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001828 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001829 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001830 ret = FAIL;
1831 else
1832 {
1833 funcexe_T funcexe;
1834
1835 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001836 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001837 funcexe.firstline = curwin->w_cursor.lnum;
1838 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001839 funcexe.evaluate = evaluate;
1840 funcexe.partial = partial;
1841 funcexe.basetv = basetv;
1842 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1843 }
1844 vim_free(s);
1845
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001846 // If evaluate is FALSE rettv->v_type was not set in
1847 // get_func_tv, but it's needed in handle_subscript() to parse
1848 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001849 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1850 {
1851 rettv->vval.v_string = NULL;
1852 rettv->v_type = VAR_FUNC;
1853 }
1854
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001855 // Stop the expression evaluation when immediately
1856 // aborting on error, or when an interrupt occurred or
1857 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001858 if (evaluate && aborting())
1859 {
1860 if (ret == OK)
1861 clear_tv(rettv);
1862 ret = FAIL;
1863 }
1864 return ret;
1865}
1866
1867/*
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001868 * If inside Vim9 script, "arg" points to the end of a line (ignoring comments)
1869 * and there is a next line, return the next line (skipping blanks) and set
1870 * "getnext".
1871 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1872 * "arg" must point somewhere inside a line, not at the start.
1873 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001874 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001875eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1876{
1877 *getnext = FALSE;
1878 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1879 && evalarg != NULL
1880 && evalarg->eval_cookie != NULL
1881 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
Bram Moolenaard5053d02020-06-28 15:51:16 +02001882 && (*arg == '"' || *arg == '#'))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001883 {
Bram Moolenaard5053d02020-06-28 15:51:16 +02001884 char_u *p = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001885
1886 if (p != NULL)
1887 {
1888 *getnext = TRUE;
1889 return skipwhite(p);
1890 }
1891 }
1892 return arg;
1893}
1894
1895/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001896 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001897 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001898 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001899eval_next_line(evalarg_T *evalarg)
1900{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001901 garray_T *gap = &evalarg->eval_ga;
1902 char_u *line;
1903
Bram Moolenaard5053d02020-06-28 15:51:16 +02001904 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001905 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001906 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1907 {
1908 // Going to concatenate the lines after parsing.
1909 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1910 ++gap->ga_len;
1911 }
1912 else
1913 {
1914 vim_free(evalarg->eval_tofree);
1915 evalarg->eval_tofree = line;
1916 }
1917 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001918}
1919
Bram Moolenaar9215f012020-06-27 21:18:00 +02001920 char_u *
1921skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1922{
1923 int getnext;
1924 char_u *p = skipwhite(arg);
1925
1926 eval_next_non_blank(p, evalarg, &getnext);
1927 if (getnext)
1928 return eval_next_line(evalarg);
1929 return p;
1930}
1931
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001932/*
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001933 * After using "evalarg" filled from "eap" free the memory.
1934 */
1935 void
1936clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1937{
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001938 if (evalarg != NULL && evalarg->eval_tofree != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001939 {
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001940 if (eap != NULL)
1941 {
1942 // We may need to keep the original command line, e.g. for
1943 // ":let" it has the variable names. But we may also need the
1944 // new one, "nextcmd" points into it. Keep both.
1945 vim_free(eap->cmdline_tofree);
1946 eap->cmdline_tofree = *eap->cmdlinep;
1947 *eap->cmdlinep = evalarg->eval_tofree;
1948 }
1949 else
1950 vim_free(evalarg->eval_tofree);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001951 evalarg->eval_tofree = NULL;
1952 }
1953}
1954
1955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1959 */
1960
1961/*
1962 * Handle zero level expression.
1963 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001964 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001965 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001966 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 * Return OK or FAIL.
1968 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001969 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001970eval0(
1971 char_u *arg,
1972 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001973 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001974 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
1976 int ret;
1977 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001978 int did_emsg_before = did_emsg;
1979 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001980 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981
1982 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001983 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001984
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001985 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
1987 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 /*
1990 * Report the invalid expression unless the expression evaluation has
1991 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001992 * exception, or we already gave a more specific error.
1993 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001995 if (!aborting()
1996 && did_emsg == did_emsg_before
1997 && called_emsg == called_emsg_before
1998 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001999 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 ret = FAIL;
2001 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002002
2003 if (eap != NULL)
2004 eap->nextcmd = check_nextcmd(p);
2005
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 return ret;
2007}
2008
2009/*
2010 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002011 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 *
2013 * "arg" must point to the first non-white of the expression.
2014 * "arg" is advanced to the next non-white after the recognized expression.
2015 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002016 * Note: "rettv.v_lock" is not set.
2017 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 * Return OK or FAIL.
2019 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002020 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002021eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002023 char_u *p;
2024 int getnext;
2025
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 /*
2027 * Get the first variable.
2028 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002029 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 return FAIL;
2031
Bram Moolenaar793648f2020-06-26 21:28:25 +02002032 p = eval_next_non_blank(*arg, evalarg, &getnext);
2033 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002035 int result;
2036 typval_T var2;
2037 evalarg_T nested_evalarg;
2038 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002039 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002040
Bram Moolenaar793648f2020-06-26 21:28:25 +02002041 if (getnext)
2042 *arg = eval_next_line(evalarg);
2043
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002044 if (evalarg == NULL)
2045 {
2046 CLEAR_FIELD(nested_evalarg);
2047 orig_flags = 0;
2048 }
2049 else
2050 {
2051 nested_evalarg = *evalarg;
2052 orig_flags = evalarg->eval_flags;
2053 }
2054
Bram Moolenaar7acde512020-06-24 23:02:40 +02002055 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002057 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002059 int error = FALSE;
2060
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002061 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002063 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002064 if (error)
2065 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067
2068 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002069 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002071 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002072 nested_evalarg.eval_flags = result ? orig_flags
2073 : orig_flags & ~EVAL_EVALUATE;
2074 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 return FAIL;
2076
2077 /*
2078 * Check for the ":".
2079 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002080 p = eval_next_non_blank(*arg, evalarg, &getnext);
2081 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002085 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 return FAIL;
2087 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002088 if (getnext)
2089 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002092 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002094 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002095 nested_evalarg.eval_flags = !result ? orig_flags
2096 : orig_flags & ~EVAL_EVALUATE;
2097 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
2099 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 return FAIL;
2102 }
2103 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002104 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106
2107 return OK;
2108}
2109
2110/*
2111 * Handle first level expression:
2112 * expr2 || expr2 || expr2 logical OR
2113 *
2114 * "arg" must point to the first non-white of the expression.
2115 * "arg" is advanced to the next non-white after the recognized expression.
2116 *
2117 * Return OK or FAIL.
2118 */
2119 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002120eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002122 char_u *p;
2123 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002124 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 long result;
2126 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002127 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 /*
2130 * Get the first variable.
2131 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002132 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 return FAIL;
2134
2135 /*
2136 * Repeat until there is no following "||".
2137 */
2138 first = TRUE;
2139 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002140 p = eval_next_non_blank(*arg, evalarg, &getnext);
2141 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002143 evalarg_T nested_evalarg;
2144 int evaluate;
2145 int orig_flags;
2146
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002147 if (getnext)
2148 *arg = eval_next_line(evalarg);
2149
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002150 if (evalarg == NULL)
2151 {
2152 CLEAR_FIELD(nested_evalarg);
2153 orig_flags = 0;
2154 evaluate = FALSE;
2155 }
2156 else
2157 {
2158 nested_evalarg = *evalarg;
2159 orig_flags = evalarg->eval_flags;
2160 evaluate = orig_flags & EVAL_EVALUATE;
2161 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (evaluate && first)
2164 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002165 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002167 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002168 if (error)
2169 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 first = FALSE;
2171 }
2172
2173 /*
2174 * Get the second variable.
2175 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002176 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002177 nested_evalarg.eval_flags = !result ? orig_flags
2178 : orig_flags & ~EVAL_EVALUATE;
2179 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 return FAIL;
2181
2182 /*
2183 * Compute the result.
2184 */
2185 if (evaluate && !result)
2186 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002187 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002190 if (error)
2191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 }
2193 if (evaluate)
2194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 rettv->v_type = VAR_NUMBER;
2196 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002198
2199 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 }
2201
2202 return OK;
2203}
2204
2205/*
2206 * Handle second level expression:
2207 * expr3 && expr3 && expr3 logical AND
2208 *
2209 * "arg" must point to the first non-white of the expression.
2210 * "arg" is advanced to the next non-white after the recognized expression.
2211 *
2212 * Return OK or FAIL.
2213 */
2214 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002215eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002217 char_u *p;
2218 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002219 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 long result;
2221 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002222 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
2224 /*
2225 * Get the first variable.
2226 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002227 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 return FAIL;
2229
2230 /*
2231 * Repeat until there is no following "&&".
2232 */
2233 first = TRUE;
2234 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002235 p = eval_next_non_blank(*arg, evalarg, &getnext);
2236 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002238 evalarg_T nested_evalarg;
2239 int orig_flags;
2240 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002241
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002242 if (getnext)
2243 *arg = eval_next_line(evalarg);
2244
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002245 if (evalarg == NULL)
2246 {
2247 CLEAR_FIELD(nested_evalarg);
2248 orig_flags = 0;
2249 evaluate = FALSE;
2250 }
2251 else
2252 {
2253 nested_evalarg = *evalarg;
2254 orig_flags = evalarg->eval_flags;
2255 evaluate = orig_flags & EVAL_EVALUATE;
2256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 if (evaluate && first)
2258 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002259 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002262 if (error)
2263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 first = FALSE;
2265 }
2266
2267 /*
2268 * Get the second variable.
2269 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002270 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002271 nested_evalarg.eval_flags = result ? orig_flags
2272 : orig_flags & ~EVAL_EVALUATE;
2273 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 return FAIL;
2275
2276 /*
2277 * Compute the result.
2278 */
2279 if (evaluate && result)
2280 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002281 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002284 if (error)
2285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287 if (evaluate)
2288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 rettv->v_type = VAR_NUMBER;
2290 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002292
2293 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295
2296 return OK;
2297}
2298
2299/*
2300 * Handle third level expression:
2301 * var1 == var2
2302 * var1 =~ var2
2303 * var1 != var2
2304 * var1 !~ var2
2305 * var1 > var2
2306 * var1 >= var2
2307 * var1 < var2
2308 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002309 * var1 is var2
2310 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 *
2312 * "arg" must point to the first non-white of the expression.
2313 * "arg" is advanced to the next non-white after the recognized expression.
2314 *
2315 * Return OK or FAIL.
2316 */
2317 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002318eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
Bram Moolenaar33570922005-01-25 22:26:29 +00002320 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002322 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002324 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328 /*
2329 * Get the first variable.
2330 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002331 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 return FAIL;
2333
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002334 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 switch (p[0])
2336 {
2337 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002338 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002340 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 break;
2342 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002343 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002345 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 break;
2347 case '>': if (p[1] != '=')
2348 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002349 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 len = 1;
2351 }
2352 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002353 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 break;
2355 case '<': if (p[1] != '=')
2356 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002357 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 len = 1;
2359 }
2360 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002361 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002363 case 'i': if (p[1] == 's')
2364 {
2365 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2366 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002367 i = p[len];
2368 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002369 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002370 }
2371 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373
2374 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002375 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002377 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002379 if (getnext)
2380 *arg = eval_next_line(evalarg);
2381
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002382 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (p[len] == '?')
2384 {
2385 ic = TRUE;
2386 ++len;
2387 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002388 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 else if (p[len] == '#')
2390 {
2391 ic = FALSE;
2392 ++len;
2393 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002394 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 else
2396 ic = p_ic;
2397
2398 /*
2399 * Get the second variable.
2400 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002401 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002402 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 return FAIL;
2406 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002407 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002408 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002409 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002410
2411 clear_tv(&var2);
2412 return ret;
2413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415
2416 return OK;
2417}
2418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 void
2420eval_addblob(typval_T *tv1, typval_T *tv2)
2421{
2422 blob_T *b1 = tv1->vval.v_blob;
2423 blob_T *b2 = tv2->vval.v_blob;
2424 blob_T *b = blob_alloc();
2425 int i;
2426
2427 if (b != NULL)
2428 {
2429 for (i = 0; i < blob_len(b1); i++)
2430 ga_append(&b->bv_ga, blob_get(b1, i));
2431 for (i = 0; i < blob_len(b2); i++)
2432 ga_append(&b->bv_ga, blob_get(b2, i));
2433
2434 clear_tv(tv1);
2435 rettv_blob_set(tv1, b);
2436 }
2437}
2438
2439 int
2440eval_addlist(typval_T *tv1, typval_T *tv2)
2441{
2442 typval_T var3;
2443
2444 // concatenate Lists
2445 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2446 {
2447 clear_tv(tv1);
2448 clear_tv(tv2);
2449 return FAIL;
2450 }
2451 clear_tv(tv1);
2452 *tv1 = var3;
2453 return OK;
2454}
2455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456/*
2457 * Handle fourth level expression:
2458 * + number addition
2459 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002460 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002461 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 *
2463 * "arg" must point to the first non-white of the expression.
2464 * "arg" is advanced to the next non-white after the recognized expression.
2465 *
2466 * Return OK or FAIL.
2467 */
2468 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002471 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
2473 /*
2474 * Get the first variable.
2475 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002476 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return FAIL;
2478
2479 /*
2480 * Repeat computing, until no '+', '-' or '.' is following.
2481 */
2482 for (;;)
2483 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002484 int getnext;
2485 char_u *p;
2486 int op;
2487 int concat;
2488 typval_T var2;
2489
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002490 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002491 p = eval_next_non_blank(*arg, evalarg, &getnext);
2492 op = *p;
2493 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002494 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002496 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002497 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002499 if ((op != '+' || (rettv->v_type != VAR_LIST
2500 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002501#ifdef FEAT_FLOAT
2502 && (op == '.' || rettv->v_type != VAR_FLOAT)
2503#endif
2504 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002506 // For "list + ...", an illegal use of the first operand as
2507 // a number cannot be determined before evaluating the 2nd
2508 // operand: if this is also a list, all is ok.
2509 // For "something . ...", "something - ..." or "non-list + ...",
2510 // we know that the first operand needs to be a string or number
2511 // without evaluating the 2nd operand. So check before to avoid
2512 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002513 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002514 {
2515 clear_tv(rettv);
2516 return FAIL;
2517 }
2518 }
2519
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 /*
2521 * Get the second variable.
2522 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002523 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2524 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002525 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002526 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 return FAIL;
2530 }
2531
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002532 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 {
2534 /*
2535 * Compute the result.
2536 */
2537 if (op == '.')
2538 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002539 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2540 char_u *s1 = tv_get_string_buf(rettv, buf1);
2541 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002543 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002544 {
2545 clear_tv(rettv);
2546 clear_tv(&var2);
2547 return FAIL;
2548 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002549 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 clear_tv(rettv);
2551 rettv->v_type = VAR_STRING;
2552 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002554 else if (op == '+' && rettv->v_type == VAR_BLOB
2555 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002557 else if (op == '+' && rettv->v_type == VAR_LIST
2558 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002559 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002561 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 else
2564 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002565 int error = FALSE;
2566 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002567#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002568 float_T f1 = 0, f2 = 0;
2569
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002570 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002571 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002572 f1 = rettv->vval.v_float;
2573 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002574 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002575 else
2576#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002577 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002578 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002579 if (error)
2580 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002581 // This can only happen for "list + non-list". For
2582 // "non-list + ..." or "something - ...", we returned
2583 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002584 clear_tv(rettv);
2585 return FAIL;
2586 }
2587#ifdef FEAT_FLOAT
2588 if (var2.v_type == VAR_FLOAT)
2589 f1 = n1;
2590#endif
2591 }
2592#ifdef FEAT_FLOAT
2593 if (var2.v_type == VAR_FLOAT)
2594 {
2595 f2 = var2.vval.v_float;
2596 n2 = 0;
2597 }
2598 else
2599#endif
2600 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002601 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002602 if (error)
2603 {
2604 clear_tv(rettv);
2605 clear_tv(&var2);
2606 return FAIL;
2607 }
2608#ifdef FEAT_FLOAT
2609 if (rettv->v_type == VAR_FLOAT)
2610 f2 = n2;
2611#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002612 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002614
2615#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002616 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002617 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2618 {
2619 if (op == '+')
2620 f1 = f1 + f2;
2621 else
2622 f1 = f1 - f2;
2623 rettv->v_type = VAR_FLOAT;
2624 rettv->vval.v_float = f1;
2625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002627#endif
2628 {
2629 if (op == '+')
2630 n1 = n1 + n2;
2631 else
2632 n1 = n1 - n2;
2633 rettv->v_type = VAR_NUMBER;
2634 rettv->vval.v_number = n1;
2635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639 }
2640 return OK;
2641}
2642
2643/*
2644 * Handle fifth level expression:
2645 * * number multiplication
2646 * / number division
2647 * % number modulo
2648 *
2649 * "arg" must point to the first non-white of the expression.
2650 * "arg" is advanced to the next non-white after the recognized expression.
2651 *
2652 * Return OK or FAIL.
2653 */
2654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002655eval6(
2656 char_u **arg,
2657 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002658 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002659 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
Bram Moolenaar33570922005-01-25 22:26:29 +00002661 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002663 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002664#ifdef FEAT_FLOAT
2665 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002666 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002667#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002668 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
2670 /*
2671 * Get the first variable.
2672 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002673 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 return FAIL;
2675
2676 /*
2677 * Repeat computing, until no '*', '/' or '%' is following.
2678 */
2679 for (;;)
2680 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002681 int evaluate = evalarg == NULL ? 0
2682 : (evalarg->eval_flags & EVAL_EVALUATE);
2683 int getnext;
2684
2685 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002686 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002688 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002689 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002691 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002693#ifdef FEAT_FLOAT
2694 if (rettv->v_type == VAR_FLOAT)
2695 {
2696 f1 = rettv->vval.v_float;
2697 use_float = TRUE;
2698 n1 = 0;
2699 }
2700 else
2701#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002702 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002704 if (error)
2705 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 }
2707 else
2708 n1 = 0;
2709
2710 /*
2711 * Get the second variable.
2712 */
2713 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002714 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 return FAIL;
2716
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002717 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002719#ifdef FEAT_FLOAT
2720 if (var2.v_type == VAR_FLOAT)
2721 {
2722 if (!use_float)
2723 {
2724 f1 = n1;
2725 use_float = TRUE;
2726 }
2727 f2 = var2.vval.v_float;
2728 n2 = 0;
2729 }
2730 else
2731#endif
2732 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002733 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002734 clear_tv(&var2);
2735 if (error)
2736 return FAIL;
2737#ifdef FEAT_FLOAT
2738 if (use_float)
2739 f2 = n2;
2740#endif
2741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
2743 /*
2744 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002745 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002747#ifdef FEAT_FLOAT
2748 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002750 if (op == '*')
2751 f1 = f1 * f2;
2752 else if (op == '/')
2753 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002754# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002755 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002756 if (f2 == 0.0)
2757 {
2758 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002759 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002760 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002761 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002762 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002763 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002764 }
2765 else
2766 f1 = f1 / f2;
2767# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002768 // We rely on the floating point library to handle divide
2769 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002770 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002771# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002774 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002776 return FAIL;
2777 }
2778 rettv->v_type = VAR_FLOAT;
2779 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 }
2781 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002784 if (op == '*')
2785 n1 = n1 * n2;
2786 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002787 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002789 n1 = num_modulus(n1, n2);
2790
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002791 rettv->v_type = VAR_NUMBER;
2792 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 }
2795 }
2796
2797 return OK;
2798}
2799
2800/*
2801 * Handle sixth level expression:
2802 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002803 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002804 * "string" string constant
2805 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 * &option-name option value
2807 * @r register contents
2808 * identifier variable value
2809 * function() function call
2810 * $VAR environment variable
2811 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002812 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002814 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002815 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 *
2817 * Also handle:
2818 * ! in front logical NOT
2819 * - in front unary minus
2820 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 * trailing [] subscript in String or List
2822 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002823 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 *
2825 * "arg" must point to the first non-white of the expression.
2826 * "arg" is advanced to the next non-white after the recognized expression.
2827 *
2828 * Return OK or FAIL.
2829 */
2830 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002831eval7(
2832 char_u **arg,
2833 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002834 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002837 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2838 int evaluate = evalarg != NULL
2839 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 int len;
2841 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 char_u *start_leader, *end_leader;
2843 int ret = OK;
2844 char_u *alias;
2845
2846 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002847 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002848 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002850 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851
2852 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002853 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 */
2855 start_leader = *arg;
2856 while (**arg == '!' || **arg == '-' || **arg == '+')
2857 *arg = skipwhite(*arg + 1);
2858 end_leader = *arg;
2859
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002860 if (**arg == '.' && (!isdigit(*(*arg + 1))
2861#ifdef FEAT_FLOAT
2862 || current_sctx.sc_version < 2
2863#endif
2864 ))
2865 {
2866 semsg(_(e_invexpr2), *arg);
2867 ++*arg;
2868 return FAIL;
2869 }
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 switch (**arg)
2872 {
2873 /*
2874 * Number constant.
2875 */
2876 case '0':
2877 case '1':
2878 case '2':
2879 case '3':
2880 case '4':
2881 case '5':
2882 case '6':
2883 case '7':
2884 case '8':
2885 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002887
2888 // Apply prefixed "-" and "+" now. Matters especially when
2889 // "->" follows.
2890 if (ret == OK && evaluate && end_leader > start_leader)
2891 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 break;
2893
2894 /*
2895 * String constant: "string".
2896 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002897 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 break;
2899
2900 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002903 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002904 break;
2905
2906 /*
2907 * List: [expr, expr]
2908 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002909 case '[': ret = get_list_tv(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 break;
2911
2912 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002913 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002914 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002915 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002916 {
2917 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002918 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002919 }
2920 else
2921 ret = NOTDONE;
2922 break;
2923
2924 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002925 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002926 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002927 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002928 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002929 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002930 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002931 break;
2932
2933 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002934 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002936 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 break;
2938
2939 /*
2940 * Environment variable: $VAR.
2941 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002942 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 break;
2944
2945 /*
2946 * Register contents: @r.
2947 */
2948 case '@': ++*arg;
2949 if (evaluate)
2950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002951 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002952 rettv->vval.v_string = get_reg_contents(**arg,
2953 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
2955 if (**arg != NUL)
2956 ++*arg;
2957 break;
2958
2959 /*
2960 * nested expression: (expression).
2961 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002962 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02002963 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002964 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002965
Bram Moolenaar9215f012020-06-27 21:18:00 +02002966 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002967 if (**arg == ')')
2968 ++*arg;
2969 else if (ret == OK)
2970 {
2971 emsg(_(e_missing_close));
2972 clear_tv(rettv);
2973 ret = FAIL;
2974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
2976 break;
2977
Bram Moolenaar8c711452005-01-14 21:53:12 +00002978 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 break;
2980 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002981
2982 if (ret == NOTDONE)
2983 {
2984 /*
2985 * Must be a variable or function name.
2986 * Can also be a curly-braces kind of name: {expr}.
2987 */
2988 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002989 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002990 if (alias != NULL)
2991 s = alias;
2992
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002993 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002994 ret = FAIL;
2995 else
2996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002997 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002998 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002999 else if (flags & EVAL_CONSTANT)
3000 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003001 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003002 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003003 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003004 {
3005 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003007 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003008 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003009 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003010 }
3011
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 *arg = skipwhite(*arg);
3013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003014 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3015 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003016 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003017 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018
3019 /*
3020 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3021 */
3022 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003023 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003024 return ret;
3025}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003026
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003027/*
3028 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003029 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003030 * Adjusts "end_leaderp" until it is at "start_leader".
3031 */
3032 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003033eval7_leader(
3034 typval_T *rettv,
3035 int numeric_only,
3036 char_u *start_leader,
3037 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003038{
3039 char_u *end_leader = *end_leaderp;
3040 int ret = OK;
3041 int error = FALSE;
3042 varnumber_T val = 0;
3043#ifdef FEAT_FLOAT
3044 float_T f = 0.0;
3045
3046 if (rettv->v_type == VAR_FLOAT)
3047 f = rettv->vval.v_float;
3048 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003049#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003050 val = tv_get_number_chk(rettv, &error);
3051 if (error)
3052 {
3053 clear_tv(rettv);
3054 ret = FAIL;
3055 }
3056 else
3057 {
3058 while (end_leader > start_leader)
3059 {
3060 --end_leader;
3061 if (*end_leader == '!')
3062 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003063 if (numeric_only)
3064 {
3065 ++end_leader;
3066 break;
3067 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003068#ifdef FEAT_FLOAT
3069 if (rettv->v_type == VAR_FLOAT)
3070 f = !f;
3071 else
3072#endif
3073 val = !val;
3074 }
3075 else if (*end_leader == '-')
3076 {
3077#ifdef FEAT_FLOAT
3078 if (rettv->v_type == VAR_FLOAT)
3079 f = -f;
3080 else
3081#endif
3082 val = -val;
3083 }
3084 }
3085#ifdef FEAT_FLOAT
3086 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003088 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003089 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003091 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003092#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003093 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003094 clear_tv(rettv);
3095 rettv->v_type = VAR_NUMBER;
3096 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003099 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 return ret;
3101}
3102
3103/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003104 * Call the function referred to in "rettv".
3105 */
3106 static int
3107call_func_rettv(
3108 char_u **arg,
3109 typval_T *rettv,
3110 int evaluate,
3111 dict_T *selfdict,
3112 typval_T *basetv)
3113{
3114 partial_T *pt = NULL;
3115 funcexe_T funcexe;
3116 typval_T functv;
3117 char_u *s;
3118 int ret;
3119
3120 // need to copy the funcref so that we can clear rettv
3121 if (evaluate)
3122 {
3123 functv = *rettv;
3124 rettv->v_type = VAR_UNKNOWN;
3125
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003126 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003127 if (functv.v_type == VAR_PARTIAL)
3128 {
3129 pt = functv.vval.v_partial;
3130 s = partial_name(pt);
3131 }
3132 else
3133 s = functv.vval.v_string;
3134 }
3135 else
3136 s = (char_u *)"";
3137
Bram Moolenaara80faa82020-04-12 19:37:17 +02003138 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003139 funcexe.firstline = curwin->w_cursor.lnum;
3140 funcexe.lastline = curwin->w_cursor.lnum;
3141 funcexe.evaluate = evaluate;
3142 funcexe.partial = pt;
3143 funcexe.selfdict = selfdict;
3144 funcexe.basetv = basetv;
3145 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
3146
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003147 // Clear the funcref afterwards, so that deleting it while
3148 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003149 if (evaluate)
3150 clear_tv(&functv);
3151
3152 return ret;
3153}
3154
3155/*
3156 * Evaluate "->method()".
3157 * "*arg" points to the '-'.
3158 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3159 */
3160 static int
3161eval_lambda(
3162 char_u **arg,
3163 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003164 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003165 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003166{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003167 int evaluate = evalarg != NULL
3168 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003169 typval_T base = *rettv;
3170 int ret;
3171
3172 // Skip over the ->.
3173 *arg += 2;
3174 rettv->v_type = VAR_UNKNOWN;
3175
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003176 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003177 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003178 return FAIL;
3179 else if (**arg != '(')
3180 {
3181 if (verbose)
3182 {
3183 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003184 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003185 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003187 }
3188 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003189 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003190 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003191 else
3192 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
3193
3194 // Clear the funcref afterwards, so that deleting it while
3195 // evaluating the arguments is possible (see test55).
3196 if (evaluate)
3197 clear_tv(&base);
3198
3199 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003200}
3201
3202/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003203 * Evaluate "->method()".
3204 * "*arg" points to the '-'.
3205 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3206 */
3207 static int
3208eval_method(
3209 char_u **arg,
3210 typval_T *rettv,
3211 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003212 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003213{
3214 char_u *name;
3215 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003216 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003217 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003218 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003219
3220 // Skip over the ->.
3221 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003222 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003223
Bram Moolenaarac92e252019-08-03 21:58:38 +02003224 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003225 len = get_name_len(arg, &alias, evaluate, TRUE);
3226 if (alias != NULL)
3227 name = alias;
3228
3229 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003230 {
3231 if (verbose)
3232 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003233 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003234 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003235 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003236 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003237 if (**arg != '(')
3238 {
3239 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003241 ret = FAIL;
3242 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003243 else if (VIM_ISWHITE((*arg)[-1]))
3244 {
3245 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003246 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003247 ret = FAIL;
3248 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003249 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02003250 ret = eval_func(arg, name, len, rettv,
3251 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003252 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003253
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003254 // Clear the funcref afterwards, so that deleting it while
3255 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003256 if (evaluate)
3257 clear_tv(&base);
3258
Bram Moolenaarac92e252019-08-03 21:58:38 +02003259 return ret;
3260}
3261
3262/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003263 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3264 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003265 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3266 */
3267 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268eval_index(
3269 char_u **arg,
3270 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003271 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003272 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003273{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003274 int evaluate = evalarg != NULL
3275 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003276 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003278 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003279 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003280 long len = -1;
3281 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003282 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003283 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003284
Bram Moolenaara03f2332016-02-06 18:09:59 +01003285 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003286 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003287 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003288 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003289 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003290 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003291 return FAIL;
3292 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003293#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003294 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003295 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003296 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003297#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003298 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003299 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003300 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003301 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003302 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003303 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003304 return FAIL;
3305 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003306 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003308 if (evaluate)
3309 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003310 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003311
3312 case VAR_STRING:
3313 case VAR_NUMBER:
3314 case VAR_LIST:
3315 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003316 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003317 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003318 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003319
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003320 init_tv(&var1);
3321 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003322 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003323 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003324 /*
3325 * dict.name
3326 */
3327 key = *arg + 1;
3328 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3329 ;
3330 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003331 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003332 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003333 }
3334 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003335 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003336 /*
3337 * something[idx]
3338 *
3339 * Get the (first) variable from inside the [].
3340 */
3341 *arg = skipwhite(*arg + 1);
3342 if (**arg == ':')
3343 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003344 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003345 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003346 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003347 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003348 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003349 clear_tv(&var1);
3350 return FAIL;
3351 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003352
3353 /*
3354 * Get the second variable from inside the [:].
3355 */
3356 if (**arg == ':')
3357 {
3358 range = TRUE;
3359 *arg = skipwhite(*arg + 1);
3360 if (**arg == ']')
3361 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003362 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003364 if (!empty1)
3365 clear_tv(&var1);
3366 return FAIL;
3367 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003368 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003369 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003370 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003371 if (!empty1)
3372 clear_tv(&var1);
3373 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003374 return FAIL;
3375 }
3376 }
3377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003378 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003379 if (**arg != ']')
3380 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003381 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003382 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003383 clear_tv(&var1);
3384 if (range)
3385 clear_tv(&var2);
3386 return FAIL;
3387 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003388 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003389 }
3390
3391 if (evaluate)
3392 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003393 n1 = 0;
3394 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003395 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003396 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003397 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003398 }
3399 if (range)
3400 {
3401 if (empty2)
3402 n2 = -1;
3403 else
3404 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003405 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003406 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003407 }
3408 }
3409
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003410 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003411 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003412 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003413 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003415 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003416 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003417 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003418 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003419 case VAR_SPECIAL:
3420 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003421 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003422 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003423
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003424 case VAR_NUMBER:
3425 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003426 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003427 len = (long)STRLEN(s);
3428 if (range)
3429 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003430 // The resulting variable is a substring. If the indexes
3431 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003432 if (n1 < 0)
3433 {
3434 n1 = len + n1;
3435 if (n1 < 0)
3436 n1 = 0;
3437 }
3438 if (n2 < 0)
3439 n2 = len + n2;
3440 else if (n2 >= len)
3441 n2 = len;
3442 if (n1 >= len || n2 < 0 || n1 > n2)
3443 s = NULL;
3444 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003445 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003446 }
3447 else
3448 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003449 // The resulting variable is a string of a single
3450 // character. If the index is too big or negative the
3451 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003452 if (n1 >= len || n1 < 0)
3453 s = NULL;
3454 else
3455 s = vim_strnsave(s + n1, 1);
3456 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003457 clear_tv(rettv);
3458 rettv->v_type = VAR_STRING;
3459 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003460 break;
3461
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003462 case VAR_BLOB:
3463 len = blob_len(rettv->vval.v_blob);
3464 if (range)
3465 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003466 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003467 // are out of range the result is empty.
3468 if (n1 < 0)
3469 {
3470 n1 = len + n1;
3471 if (n1 < 0)
3472 n1 = 0;
3473 }
3474 if (n2 < 0)
3475 n2 = len + n2;
3476 else if (n2 >= len)
3477 n2 = len - 1;
3478 if (n1 >= len || n2 < 0 || n1 > n2)
3479 {
3480 clear_tv(rettv);
3481 rettv->v_type = VAR_BLOB;
3482 rettv->vval.v_blob = NULL;
3483 }
3484 else
3485 {
3486 blob_T *blob = blob_alloc();
3487
3488 if (blob != NULL)
3489 {
3490 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3491 {
3492 blob_free(blob);
3493 return FAIL;
3494 }
3495 blob->bv_ga.ga_len = n2 - n1 + 1;
3496 for (i = n1; i <= n2; i++)
3497 blob_set(blob, i - n1,
3498 blob_get(rettv->vval.v_blob, i));
3499
3500 clear_tv(rettv);
3501 rettv_blob_set(rettv, blob);
3502 }
3503 }
3504 }
3505 else
3506 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003507 // The resulting variable is a byte value.
3508 // If the index is too big or negative that is an error.
3509 if (n1 < 0)
3510 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003511 if (n1 < len && n1 >= 0)
3512 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003513 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003514
3515 clear_tv(rettv);
3516 rettv->v_type = VAR_NUMBER;
3517 rettv->vval.v_number = v;
3518 }
3519 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003520 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003521 }
3522 break;
3523
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003524 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003525 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003526 if (n1 < 0)
3527 n1 = len + n1;
3528 if (!empty1 && (n1 < 0 || n1 >= len))
3529 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003530 // For a range we allow invalid values and return an empty
3531 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003532 if (!range)
3533 {
3534 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003535 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003536 return FAIL;
3537 }
3538 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003539 }
3540 if (range)
3541 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003542 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003543
3544 if (n2 < 0)
3545 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003546 else if (n2 >= len)
3547 n2 = len - 1;
3548 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003549 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003550 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003551 if (l == NULL)
3552 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003553 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003554 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003555 }
3556 else
3557 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003558 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003559 clear_tv(rettv);
3560 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003561 }
3562 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003563
3564 case VAR_DICT:
3565 if (range)
3566 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003567 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003568 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003569 if (len == -1)
3570 clear_tv(&var1);
3571 return FAIL;
3572 }
3573 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003574 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003575
3576 if (len == -1)
3577 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003578 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003579 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003581 clear_tv(&var1);
3582 return FAIL;
3583 }
3584 }
3585
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003586 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003587
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003588 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003589 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 if (len == -1)
3591 clear_tv(&var1);
3592 if (item == NULL)
3593 return FAIL;
3594
3595 copy_tv(&item->di_tv, &var1);
3596 clear_tv(rettv);
3597 *rettv = var1;
3598 }
3599 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003600 }
3601 }
3602
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003603 return OK;
3604}
3605
3606/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003607 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003608 */
3609 char_u *
3610partial_name(partial_T *pt)
3611{
3612 if (pt->pt_name != NULL)
3613 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003614 if (pt->pt_func != NULL)
3615 return pt->pt_func->uf_name;
3616 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003617}
3618
Bram Moolenaarddecc252016-04-06 22:59:37 +02003619 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003620partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003621{
3622 int i;
3623
3624 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003625 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003626 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003627 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003628 if (pt->pt_name != NULL)
3629 {
3630 func_unref(pt->pt_name);
3631 vim_free(pt->pt_name);
3632 }
3633 else
3634 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003635
3636 if (pt->pt_funcstack != NULL)
3637 {
3638 // Decrease the reference count for the context of a closure. If down
3639 // to zero free it and clear the variables on the stack.
3640 if (--pt->pt_funcstack->fs_refcount == 0)
3641 {
3642 garray_T *gap = &pt->pt_funcstack->fs_ga;
3643 typval_T *stack = gap->ga_data;
3644
3645 for (i = 0; i < gap->ga_len; ++i)
3646 clear_tv(stack + i);
3647 ga_clear(gap);
3648 vim_free(pt->pt_funcstack);
3649 }
3650 pt->pt_funcstack = NULL;
3651 }
3652
Bram Moolenaarddecc252016-04-06 22:59:37 +02003653 vim_free(pt);
3654}
3655
3656/*
3657 * Unreference a closure: decrement the reference count and free it when it
3658 * becomes zero.
3659 */
3660 void
3661partial_unref(partial_T *pt)
3662{
3663 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003664 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003665}
3666
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003667/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003668 * Return the next (unique) copy ID.
3669 * Used for serializing nested structures.
3670 */
3671 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003672get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003673{
3674 current_copyID += COPYID_INC;
3675 return current_copyID;
3676}
3677
3678/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003679 * Garbage collection for lists and dictionaries.
3680 *
3681 * We use reference counts to be able to free most items right away when they
3682 * are no longer used. But for composite items it's possible that it becomes
3683 * unused while the reference count is > 0: When there is a recursive
3684 * reference. Example:
3685 * :let l = [1, 2, 3]
3686 * :let d = {9: l}
3687 * :let l[1] = d
3688 *
3689 * Since this is quite unusual we handle this with garbage collection: every
3690 * once in a while find out which lists and dicts are not referenced from any
3691 * variable.
3692 *
3693 * Here is a good reference text about garbage collection (refers to Python
3694 * but it applies to all reference-counting mechanisms):
3695 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003696 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003697
3698/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003699 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003700 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003701 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003702 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003703 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003704garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003705{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003706 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003707 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003708 buf_T *buf;
3709 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003710 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003711 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003712
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003713 if (!testing)
3714 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003715 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003716 want_garbage_collect = FALSE;
3717 may_garbage_collect = FALSE;
3718 garbage_collect_at_exit = FALSE;
3719 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003720
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003721 // The execution stack can grow big, limit the size.
3722 if (exestack.ga_maxlen - exestack.ga_len > 500)
3723 {
3724 size_t new_len;
3725 char_u *pp;
3726 int n;
3727
3728 // Keep 150% of the current size, with a minimum of the growth size.
3729 n = exestack.ga_len / 2;
3730 if (n < exestack.ga_growsize)
3731 n = exestack.ga_growsize;
3732
3733 // Don't make it bigger though.
3734 if (exestack.ga_len + n < exestack.ga_maxlen)
3735 {
3736 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3737 pp = vim_realloc(exestack.ga_data, new_len);
3738 if (pp == NULL)
3739 return FAIL;
3740 exestack.ga_maxlen = exestack.ga_len + n;
3741 exestack.ga_data = pp;
3742 }
3743 }
3744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003745 // We advance by two because we add one for items referenced through
3746 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003747 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003748
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003749 /*
3750 * 1. Go through all accessible variables and mark all lists and dicts
3751 * with copyID.
3752 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003753
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003754 // Don't free variables in the previous_funccal list unless they are only
3755 // referenced through previous_funccal. This must be first, because if
3756 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003757 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003758
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003759 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003760 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003761
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003762 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003763 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003764 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3765 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003766
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003767 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003768 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003769 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3770 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003771 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003772 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3773 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003774#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003775 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003776 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3777 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003778 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003779 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003780 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3781 NULL, NULL);
3782#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003783
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003784 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003785 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003786 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3787 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003788 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003789 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003791 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003792 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003793
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003794 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003795 abort = abort || set_ref_in_functions(copyID);
3796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003797 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003798 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003800 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003801 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003802
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003803 // callbacks in buffers
3804 abort = abort || set_ref_in_buffers(copyID);
3805
Bram Moolenaar1dced572012-04-05 16:54:08 +02003806#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003807 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003808#endif
3809
Bram Moolenaardb913952012-06-29 12:54:53 +02003810#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003811 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003812#endif
3813
3814#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003815 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003816#endif
3817
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003818#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003819 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003820 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003821#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003822#ifdef FEAT_NETBEANS_INTG
3823 abort = abort || set_ref_in_nb_channel(copyID);
3824#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003825
Bram Moolenaare3188e22016-05-31 21:13:04 +02003826#ifdef FEAT_TIMERS
3827 abort = abort || set_ref_in_timer(copyID);
3828#endif
3829
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003830#ifdef FEAT_QUICKFIX
3831 abort = abort || set_ref_in_quickfix(copyID);
3832#endif
3833
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003834#ifdef FEAT_TERMINAL
3835 abort = abort || set_ref_in_term(copyID);
3836#endif
3837
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003838#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003839 abort = abort || set_ref_in_popups(copyID);
3840#endif
3841
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003842 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003843 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003844 /*
3845 * 2. Free lists and dictionaries that are not referenced.
3846 */
3847 did_free = free_unref_items(copyID);
3848
3849 /*
3850 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003851 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003852 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003854 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003855 else if (p_verbose > 0)
3856 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003857 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003858 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003859
3860 return did_free;
3861}
3862
3863/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003864 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003865 */
3866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003867free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003868{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003869 int did_free = FALSE;
3870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003871 // Let all "free" functions know that we are here. This means no
3872 // dictionaries, lists, channels or jobs are to be freed, because we will
3873 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003874 in_free_unref_items = TRUE;
3875
3876 /*
3877 * PASS 1: free the contents of the items. We don't free the items
3878 * themselves yet, so that it is possible to decrement refcount counters
3879 */
3880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003881 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003882 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003884 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003885 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003886
3887#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003888 // Go through the list of jobs and free items without the copyID. This
3889 // must happen before doing channels, because jobs refer to channels, but
3890 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003891 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3892
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003893 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003894 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3895#endif
3896
3897 /*
3898 * PASS 2: free the items themselves.
3899 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003900 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003901 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003902
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003903#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003904 // Go through the list of jobs and free items without the copyID. This
3905 // must happen before doing channels, because jobs refer to channels, but
3906 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003907 free_unused_jobs(copyID, COPYID_MASK);
3908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003909 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003910 free_unused_channels(copyID, COPYID_MASK);
3911#endif
3912
3913 in_free_unref_items = FALSE;
3914
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003915 return did_free;
3916}
3917
3918/*
3919 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003920 * "list_stack" is used to add lists to be marked. Can be NULL.
3921 *
3922 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003923 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003925set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003926{
3927 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003928 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003929 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003930 hashtab_T *cur_ht;
3931 ht_stack_T *ht_stack = NULL;
3932 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003933
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003934 cur_ht = ht;
3935 for (;;)
3936 {
3937 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003938 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003939 // Mark each item in the hashtab. If the item contains a hashtab
3940 // it is added to ht_stack, if it contains a list it is added to
3941 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003942 todo = (int)cur_ht->ht_used;
3943 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3944 if (!HASHITEM_EMPTY(hi))
3945 {
3946 --todo;
3947 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3948 &ht_stack, list_stack);
3949 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003950 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003951
3952 if (ht_stack == NULL)
3953 break;
3954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003955 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003956 cur_ht = ht_stack->ht;
3957 tempitem = ht_stack;
3958 ht_stack = ht_stack->prev;
3959 free(tempitem);
3960 }
3961
3962 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003963}
3964
3965/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003966 * Mark a dict and its items with "copyID".
3967 * Returns TRUE if setting references failed somehow.
3968 */
3969 int
3970set_ref_in_dict(dict_T *d, int copyID)
3971{
3972 if (d != NULL && d->dv_copyID != copyID)
3973 {
3974 d->dv_copyID = copyID;
3975 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3976 }
3977 return FALSE;
3978}
3979
3980/*
3981 * Mark a list and its items with "copyID".
3982 * Returns TRUE if setting references failed somehow.
3983 */
3984 int
3985set_ref_in_list(list_T *ll, int copyID)
3986{
3987 if (ll != NULL && ll->lv_copyID != copyID)
3988 {
3989 ll->lv_copyID = copyID;
3990 return set_ref_in_list_items(ll, copyID, NULL);
3991 }
3992 return FALSE;
3993}
3994
3995/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003996 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003997 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3998 *
3999 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004000 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004001 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004002set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004003{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004004 listitem_T *li;
4005 int abort = FALSE;
4006 list_T *cur_l;
4007 list_stack_T *list_stack = NULL;
4008 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004009
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004010 cur_l = l;
4011 for (;;)
4012 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004013 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004014 // Mark each item in the list. If the item contains a hashtab
4015 // it is added to ht_stack, if it contains a list it is added to
4016 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004017 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4018 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4019 ht_stack, &list_stack);
4020 if (list_stack == NULL)
4021 break;
4022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004023 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004024 cur_l = list_stack->list;
4025 tempitem = list_stack;
4026 list_stack = list_stack->prev;
4027 free(tempitem);
4028 }
4029
4030 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004031}
4032
4033/*
4034 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004035 * "list_stack" is used to add lists to be marked. Can be NULL.
4036 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4037 *
4038 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004039 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004040 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004041set_ref_in_item(
4042 typval_T *tv,
4043 int copyID,
4044 ht_stack_T **ht_stack,
4045 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004046{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004047 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004048
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004049 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004050 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004051 dict_T *dd = tv->vval.v_dict;
4052
Bram Moolenaara03f2332016-02-06 18:09:59 +01004053 if (dd != NULL && dd->dv_copyID != copyID)
4054 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004055 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004056 dd->dv_copyID = copyID;
4057 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004058 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004059 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4060 }
4061 else
4062 {
4063 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4064 if (newitem == NULL)
4065 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004066 else
4067 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004068 newitem->ht = &dd->dv_hashtab;
4069 newitem->prev = *ht_stack;
4070 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004071 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004072 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004073 }
4074 }
4075 else if (tv->v_type == VAR_LIST)
4076 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004077 list_T *ll = tv->vval.v_list;
4078
Bram Moolenaara03f2332016-02-06 18:09:59 +01004079 if (ll != NULL && ll->lv_copyID != copyID)
4080 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004081 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004082 ll->lv_copyID = copyID;
4083 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004084 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004085 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004086 }
4087 else
4088 {
4089 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004090 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004091 if (newitem == NULL)
4092 abort = TRUE;
4093 else
4094 {
4095 newitem->list = ll;
4096 newitem->prev = *list_stack;
4097 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004098 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004099 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004100 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004101 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004102 else if (tv->v_type == VAR_FUNC)
4103 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004104 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004105 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004106 else if (tv->v_type == VAR_PARTIAL)
4107 {
4108 partial_T *pt = tv->vval.v_partial;
4109 int i;
4110
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004111 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004112 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004113 // Didn't see this partial yet.
4114 pt->pt_copyID = copyID;
4115
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004116 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004117
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004118 if (pt->pt_dict != NULL)
4119 {
4120 typval_T dtv;
4121
4122 dtv.v_type = VAR_DICT;
4123 dtv.vval.v_dict = pt->pt_dict;
4124 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4125 }
4126
4127 for (i = 0; i < pt->pt_argc; ++i)
4128 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4129 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004130 if (pt->pt_funcstack != NULL)
4131 {
4132 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4133
4134 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4135 abort = abort || set_ref_in_item(stack + i, copyID,
4136 ht_stack, list_stack);
4137 }
4138
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004139 }
4140 }
4141#ifdef FEAT_JOB_CHANNEL
4142 else if (tv->v_type == VAR_JOB)
4143 {
4144 job_T *job = tv->vval.v_job;
4145 typval_T dtv;
4146
4147 if (job != NULL && job->jv_copyID != copyID)
4148 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004149 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004150 if (job->jv_channel != NULL)
4151 {
4152 dtv.v_type = VAR_CHANNEL;
4153 dtv.vval.v_channel = job->jv_channel;
4154 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4155 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004156 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004157 {
4158 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004159 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004160 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4161 }
4162 }
4163 }
4164 else if (tv->v_type == VAR_CHANNEL)
4165 {
4166 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004167 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004168 typval_T dtv;
4169 jsonq_T *jq;
4170 cbq_T *cq;
4171
4172 if (ch != NULL && ch->ch_copyID != copyID)
4173 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004174 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004175 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004176 {
4177 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4178 jq = jq->jq_next)
4179 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4180 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4181 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004182 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004183 {
4184 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004185 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004186 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4187 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004188 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004189 {
4190 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004191 dtv.vval.v_partial =
4192 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004193 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4194 }
4195 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004196 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004197 {
4198 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004199 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004200 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4201 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004202 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004203 {
4204 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004205 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004206 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4207 }
4208 }
4209 }
4210#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004211 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004212}
4213
Bram Moolenaar8c711452005-01-14 21:53:12 +00004214/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004215 * Return a string with the string representation of a variable.
4216 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004217 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004218 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004219 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4220 * stings as "string()", otherwise does not put quotes around strings, as
4221 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004222 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4223 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004224 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004225 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004226 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004227echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004228 typval_T *tv,
4229 char_u **tofree,
4230 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004231 int copyID,
4232 int echo_style,
4233 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004234 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004235{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004236 static int recurse = 0;
4237 char_u *r = NULL;
4238
Bram Moolenaar33570922005-01-25 22:26:29 +00004239 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004240 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004241 if (!did_echo_string_emsg)
4242 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004243 // Only give this message once for a recursive call to avoid
4244 // flooding the user with errors. And stop iterating over lists
4245 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004246 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004247 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004248 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004249 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004250 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004251 }
4252 ++recurse;
4253
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004254 switch (tv->v_type)
4255 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004256 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004257 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004258 {
4259 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004260 r = tv->vval.v_string;
4261 if (r == NULL)
4262 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004263 }
4264 else
4265 {
4266 *tofree = string_quote(tv->vval.v_string, FALSE);
4267 r = *tofree;
4268 }
4269 break;
4270
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004271 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004272 if (echo_style)
4273 {
4274 *tofree = NULL;
4275 r = tv->vval.v_string;
4276 }
4277 else
4278 {
4279 *tofree = string_quote(tv->vval.v_string, TRUE);
4280 r = *tofree;
4281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004282 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004283
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004284 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004285 {
4286 partial_T *pt = tv->vval.v_partial;
4287 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004288 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004289 garray_T ga;
4290 int i;
4291 char_u *tf;
4292
4293 ga_init2(&ga, 1, 100);
4294 ga_concat(&ga, (char_u *)"function(");
4295 if (fname != NULL)
4296 {
4297 ga_concat(&ga, fname);
4298 vim_free(fname);
4299 }
4300 if (pt != NULL && pt->pt_argc > 0)
4301 {
4302 ga_concat(&ga, (char_u *)", [");
4303 for (i = 0; i < pt->pt_argc; ++i)
4304 {
4305 if (i > 0)
4306 ga_concat(&ga, (char_u *)", ");
4307 ga_concat(&ga,
4308 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4309 vim_free(tf);
4310 }
4311 ga_concat(&ga, (char_u *)"]");
4312 }
4313 if (pt != NULL && pt->pt_dict != NULL)
4314 {
4315 typval_T dtv;
4316
4317 ga_concat(&ga, (char_u *)", ");
4318 dtv.v_type = VAR_DICT;
4319 dtv.vval.v_dict = pt->pt_dict;
4320 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4321 vim_free(tf);
4322 }
4323 ga_concat(&ga, (char_u *)")");
4324
4325 *tofree = ga.ga_data;
4326 r = *tofree;
4327 break;
4328 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004329
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004330 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004331 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004332 break;
4333
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004334 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004335 if (tv->vval.v_list == NULL)
4336 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004337 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004338 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004339 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004340 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004341 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4342 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004343 {
4344 *tofree = NULL;
4345 r = (char_u *)"[...]";
4346 }
4347 else
4348 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004349 int old_copyID = tv->vval.v_list->lv_copyID;
4350
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004351 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004352 *tofree = list2string(tv, copyID, restore_copyID);
4353 if (restore_copyID)
4354 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004355 r = *tofree;
4356 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004357 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004358
Bram Moolenaar8c711452005-01-14 21:53:12 +00004359 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004360 if (tv->vval.v_dict == NULL)
4361 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004362 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004363 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004364 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004365 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004366 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4367 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004368 {
4369 *tofree = NULL;
4370 r = (char_u *)"{...}";
4371 }
4372 else
4373 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004374 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004375
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004376 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004377 *tofree = dict2string(tv, copyID, restore_copyID);
4378 if (restore_copyID)
4379 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004380 r = *tofree;
4381 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004382 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004384 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004385 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004386 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004388 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004389 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004390 break;
4391
Bram Moolenaar835dc632016-02-07 14:27:38 +01004392 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004393 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004394 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004395 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004396 if (composite_val)
4397 {
4398 *tofree = string_quote(r, FALSE);
4399 r = *tofree;
4400 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004401 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004402
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004403 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004404#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004405 *tofree = NULL;
4406 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4407 r = numbuf;
4408 break;
4409#endif
4410
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004411 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004412 case VAR_SPECIAL:
4413 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004414 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004415 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004417
Bram Moolenaar8502c702014-06-17 12:51:16 +02004418 if (--recurse == 0)
4419 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004420 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004421}
4422
4423/*
4424 * Return a string with the string representation of a variable.
4425 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4426 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004427 * Does not put quotes around strings, as ":echo" displays values.
4428 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4429 * May return NULL.
4430 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004431 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004432echo_string(
4433 typval_T *tv,
4434 char_u **tofree,
4435 char_u *numbuf,
4436 int copyID)
4437{
4438 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4439}
4440
4441/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004442 * Return string "str" in ' quotes, doubling ' characters.
4443 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004444 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004445 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004446 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004447string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004448{
Bram Moolenaar33570922005-01-25 22:26:29 +00004449 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004450 char_u *p, *r, *s;
4451
Bram Moolenaar33570922005-01-25 22:26:29 +00004452 len = (function ? 13 : 3);
4453 if (str != NULL)
4454 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004455 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004456 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004457 if (*p == '\'')
4458 ++len;
4459 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004460 s = r = alloc(len);
4461 if (r != NULL)
4462 {
4463 if (function)
4464 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004465 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004466 r += 10;
4467 }
4468 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004469 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004470 if (str != NULL)
4471 for (p = str; *p != NUL; )
4472 {
4473 if (*p == '\'')
4474 *r++ = '\'';
4475 MB_COPY_CHAR(p, r);
4476 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004477 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004478 if (function)
4479 *r++ = ')';
4480 *r++ = NUL;
4481 }
4482 return s;
4483}
4484
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004485#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004486/*
4487 * Convert the string "text" to a floating point number.
4488 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4489 * this always uses a decimal point.
4490 * Returns the length of the text that was consumed.
4491 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004493string2float(
4494 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004495 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004496{
4497 char *s = (char *)text;
4498 float_T f;
4499
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004500 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004501 if (STRNICMP(text, "inf", 3) == 0)
4502 {
4503 *value = INFINITY;
4504 return 3;
4505 }
4506 if (STRNICMP(text, "-inf", 3) == 0)
4507 {
4508 *value = -INFINITY;
4509 return 4;
4510 }
4511 if (STRNICMP(text, "nan", 3) == 0)
4512 {
4513 *value = NAN;
4514 return 3;
4515 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004516 f = strtod(s, &s);
4517 *value = f;
4518 return (int)((char_u *)s - text);
4519}
4520#endif
4521
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004524 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004526 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004527var2fpos(
4528 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004529 int dollar_lnum, // TRUE when $ is last line
4530 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004532 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004534 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004536 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004537 if (varp->v_type == VAR_LIST)
4538 {
4539 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004540 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004541 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004542 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004543
4544 l = varp->vval.v_list;
4545 if (l == NULL)
4546 return NULL;
4547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004548 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004549 pos.lnum = list_find_nr(l, 0L, &error);
4550 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004551 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004552
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004553 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004554 pos.col = list_find_nr(l, 1L, &error);
4555 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004556 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004557 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004559 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004560 li = list_find(l, 1L);
4561 if (li != NULL && li->li_tv.v_type == VAR_STRING
4562 && li->li_tv.vval.v_string != NULL
4563 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4564 pos.col = len + 1;
4565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004566 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004567 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004568 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004569 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004570
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004571 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004572 pos.coladd = list_find_nr(l, 2L, &error);
4573 if (error)
4574 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004575
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004576 return &pos;
4577 }
4578
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004579 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004580 if (name == NULL)
4581 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004582 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004584 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004585 {
4586 if (VIsual_active)
4587 return &VIsual;
4588 return &curwin->w_cursor;
4589 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004590 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004592 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4594 return NULL;
4595 return pp;
4596 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004597
Bram Moolenaara5525202006-03-02 22:52:09 +00004598 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004599
Bram Moolenaar477933c2007-07-17 14:32:23 +00004600 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004601 {
4602 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004603 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004604 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004605 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004606 // In silent Ex mode topline is zero, but that's not a valid line
4607 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004608 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004609 return &pos;
4610 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004611 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004612 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004613 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004614 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004615 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004616 return &pos;
4617 }
4618 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004619 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004621 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
4623 pos.lnum = curbuf->b_ml.ml_line_count;
4624 pos.col = 0;
4625 }
4626 else
4627 {
4628 pos.lnum = curwin->w_cursor.lnum;
4629 pos.col = (colnr_T)STRLEN(ml_get_curline());
4630 }
4631 return &pos;
4632 }
4633 return NULL;
4634}
4635
4636/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004637 * Convert list in "arg" into a position and optional file number.
4638 * When "fnump" is NULL there is no file number, only 3 items.
4639 * Note that the column is passed on as-is, the caller may want to decrement
4640 * it to use 1 for the first column.
4641 * Return FAIL when conversion is not possible, doesn't check the position for
4642 * validity.
4643 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004644 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004645list2fpos(
4646 typval_T *arg,
4647 pos_T *posp,
4648 int *fnump,
4649 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004650{
4651 list_T *l = arg->vval.v_list;
4652 long i = 0;
4653 long n;
4654
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004655 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4656 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004657 if (arg->v_type != VAR_LIST
4658 || l == NULL
4659 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004660 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004661 return FAIL;
4662
4663 if (fnump != NULL)
4664 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004665 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004666 if (n < 0)
4667 return FAIL;
4668 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004669 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004670 *fnump = n;
4671 }
4672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004673 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004674 if (n < 0)
4675 return FAIL;
4676 posp->lnum = n;
4677
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004678 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004679 if (n < 0)
4680 return FAIL;
4681 posp->col = n;
4682
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004683 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004684 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004685 posp->coladd = 0;
4686 else
4687 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004688
Bram Moolenaar493c1782014-05-28 14:34:46 +02004689 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004690 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004691
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004692 return OK;
4693}
4694
4695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 * Get the length of an environment variable name.
4697 * Advance "arg" to the first character after the name.
4698 * Return 0 for error.
4699 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004701get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702{
4703 char_u *p;
4704 int len;
4705
4706 for (p = *arg; vim_isIDc(*p); ++p)
4707 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004708 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 return 0;
4710
4711 len = (int)(p - *arg);
4712 *arg = p;
4713 return len;
4714}
4715
4716/*
4717 * Get the length of the name of a function or internal variable.
4718 * "arg" is advanced to the first non-white character after the name.
4719 * Return 0 if something is wrong.
4720 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004721 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004722get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 char_u *p;
4725 int len;
4726
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004727 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004729 {
4730 if (*p == ':')
4731 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004732 // "s:" is start of "s:var", but "n:" is not and can be used in
4733 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004734 len = (int)(p - *arg);
4735 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4736 || len > 1)
4737 break;
4738 }
4739 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004740 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 return 0;
4742
4743 len = (int)(p - *arg);
4744 *arg = skipwhite(p);
4745
4746 return len;
4747}
4748
4749/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004750 * Get the length of the name of a variable or function.
4751 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004753 * Return -1 if curly braces expansion failed.
4754 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 * If the name contains 'magic' {}'s, expand them and return the
4756 * expanded name in an allocated string via 'alias' - caller must free.
4757 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004758 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004759get_name_len(
4760 char_u **arg,
4761 char_u **alias,
4762 int evaluate,
4763 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764{
4765 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 char_u *p;
4767 char_u *expr_start;
4768 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004770 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
4772 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4773 && (*arg)[2] == (int)KE_SNR)
4774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004775 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 *arg += 3;
4777 return get_id_len(arg) + 3;
4778 }
4779 len = eval_fname_script(*arg);
4780 if (len > 0)
4781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004782 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 *arg += len;
4784 }
4785
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004789 p = find_name_end(*arg, &expr_start, &expr_end,
4790 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 if (expr_start != NULL)
4792 {
4793 char_u *temp_string;
4794
4795 if (!evaluate)
4796 {
4797 len += (int)(p - *arg);
4798 *arg = skipwhite(p);
4799 return len;
4800 }
4801
4802 /*
4803 * Include any <SID> etc in the expanded string:
4804 * Thus the -len here.
4805 */
4806 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4807 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004808 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 *alias = temp_string;
4810 *arg = skipwhite(p);
4811 return (int)STRLEN(temp_string);
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004815 // Only give an error when there is something, otherwise it will be
4816 // reported at a higher level.
4817 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004818 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
4820 return len;
4821}
4822
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823/*
4824 * Find the end of a variable or function name, taking care of magic braces.
4825 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4826 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004827 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 * Return a pointer to just after the name. Equal to "arg" if there is no
4829 * valid name.
4830 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004831 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004832find_name_end(
4833 char_u *arg,
4834 char_u **expr_start,
4835 char_u **expr_end,
4836 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 int mb_nest = 0;
4839 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004841 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004842 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004844 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 *expr_start = NULL;
4847 *expr_end = NULL;
4848 }
4849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004850 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004851 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4852 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004853 return arg;
4854
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 for (p = arg; *p != NUL
4856 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004857 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004858 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004859 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004860 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004861 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004862 if (*p == '\'')
4863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004864 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004865 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004866 ;
4867 if (*p == NUL)
4868 break;
4869 }
4870 else if (*p == '"')
4871 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004872 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004873 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004874 if (*p == '\\' && p[1] != NUL)
4875 ++p;
4876 if (*p == NUL)
4877 break;
4878 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004879 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4880 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004881 // "s:" is start of "s:var", but "n:" is not and can be used in
4882 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004883 len = (int)(p - arg);
4884 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004885 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004886 break;
4887 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004888
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004889 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 if (*p == '[')
4892 ++br_nest;
4893 else if (*p == ']')
4894 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004896
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004897 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004899 if (*p == '{')
4900 {
4901 mb_nest++;
4902 if (expr_start != NULL && *expr_start == NULL)
4903 *expr_start = p;
4904 }
4905 else if (*p == '}')
4906 {
4907 mb_nest--;
4908 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4909 *expr_end = p;
4910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913
4914 return p;
4915}
4916
4917/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004918 * Expands out the 'magic' {}'s in a variable/function name.
4919 * Note that this can call itself recursively, to deal with
4920 * constructs like foo{bar}{baz}{bam}
4921 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4922 * "in_start" ^
4923 * "expr_start" ^
4924 * "expr_end" ^
4925 * "in_end" ^
4926 *
4927 * Returns a new allocated string, which the caller must free.
4928 * Returns NULL for failure.
4929 */
4930 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004931make_expanded_name(
4932 char_u *in_start,
4933 char_u *expr_start,
4934 char_u *expr_end,
4935 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004936{
4937 char_u c1;
4938 char_u *retval = NULL;
4939 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004940
4941 if (expr_end == NULL || in_end == NULL)
4942 return NULL;
4943 *expr_start = NUL;
4944 *expr_end = NUL;
4945 c1 = *in_end;
4946 *in_end = NUL;
4947
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004948 temp_result = eval_to_string(expr_start + 1, FALSE);
4949 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004950 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004951 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4952 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004953 if (retval != NULL)
4954 {
4955 STRCPY(retval, in_start);
4956 STRCAT(retval, temp_result);
4957 STRCAT(retval, expr_end + 1);
4958 }
4959 }
4960 vim_free(temp_result);
4961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004962 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004963 *expr_start = '{';
4964 *expr_end = '}';
4965
4966 if (retval != NULL)
4967 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004968 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004969 if (expr_start != NULL)
4970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004971 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004972 temp_result = make_expanded_name(retval, expr_start,
4973 expr_end, temp_result);
4974 vim_free(retval);
4975 retval = temp_result;
4976 }
4977 }
4978
4979 return retval;
4980}
4981
4982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004984 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004986 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004987eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004989 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4990}
4991
4992/*
4993 * Return TRUE if character "c" can be used as the first character in a
4994 * variable or function name (excluding '{' and '}').
4995 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004996 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004997eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004998{
4999 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000}
5001
5002/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005003 * Handle:
5004 * - expr[expr], expr[expr:expr] subscript
5005 * - ".name" lookup
5006 * - function call with Funcref variable: func(expr)
5007 * - method call: var->method()
5008 *
5009 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005010 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005012handle_subscript(
5013 char_u **arg,
5014 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005015 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005016 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005017{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005018 int evaluate = evalarg != NULL
5019 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005020 int ret = OK;
5021 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005022
Bram Moolenaar61343f02019-07-20 21:11:13 +02005023 // "." is ".name" lookup when we found a dict or when evaluating and
5024 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005025 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005026 && (((**arg == '['
5027 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005028 || (!evaluate
5029 && (*arg)[1] != '.'
5030 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005031 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005032 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005033 && !VIM_ISWHITE(*(*arg - 1)))
5034 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005035 {
5036 if (**arg == '(')
5037 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005038 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005039
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005040 // Stop the expression evaluation when immediately aborting on
5041 // error, or when an interrupt occurred or an exception was thrown
5042 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005043 if (aborting())
5044 {
5045 if (ret == OK)
5046 clear_tv(rettv);
5047 ret = FAIL;
5048 }
5049 dict_unref(selfdict);
5050 selfdict = NULL;
5051 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005052 else if (**arg == '-')
5053 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005054 if (ret == OK)
5055 {
5056 if ((*arg)[2] == '{')
5057 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005058 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005059 else
5060 // expr->name()
5061 ret = eval_method(arg, rettv, evaluate, verbose);
5062 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005063 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005064 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005065 {
5066 dict_unref(selfdict);
5067 if (rettv->v_type == VAR_DICT)
5068 {
5069 selfdict = rettv->vval.v_dict;
5070 if (selfdict != NULL)
5071 ++selfdict->dv_refcount;
5072 }
5073 else
5074 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005075 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005076 {
5077 clear_tv(rettv);
5078 ret = FAIL;
5079 }
5080 }
5081 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005083 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5084 // Don't do this when "Func" is already a partial that was bound
5085 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005086 if (selfdict != NULL
5087 && (rettv->v_type == VAR_FUNC
5088 || (rettv->v_type == VAR_PARTIAL
5089 && (rettv->vval.v_partial->pt_auto
5090 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005091 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005092
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005093 dict_unref(selfdict);
5094 return ret;
5095}
5096
5097/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005098 * Make a copy of an item.
5099 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005100 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5101 * reference to an already copied list/dict can be used.
5102 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005103 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005104 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005105item_copy(
5106 typval_T *from,
5107 typval_T *to,
5108 int deep,
5109 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005110{
5111 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005112 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005113
Bram Moolenaar33570922005-01-25 22:26:29 +00005114 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005116 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005117 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005118 }
5119 ++recurse;
5120
5121 switch (from->v_type)
5122 {
5123 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005124 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005125 case VAR_STRING:
5126 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005127 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005128 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005129 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005130 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005131 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005132 copy_tv(from, to);
5133 break;
5134 case VAR_LIST:
5135 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005136 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005137 if (from->vval.v_list == NULL)
5138 to->vval.v_list = NULL;
5139 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5140 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005141 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005142 to->vval.v_list = from->vval.v_list->lv_copylist;
5143 ++to->vval.v_list->lv_refcount;
5144 }
5145 else
5146 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5147 if (to->vval.v_list == NULL)
5148 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005149 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005150 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005152 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005153 case VAR_DICT:
5154 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005155 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005156 if (from->vval.v_dict == NULL)
5157 to->vval.v_dict = NULL;
5158 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5159 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005160 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005161 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5162 ++to->vval.v_dict->dv_refcount;
5163 }
5164 else
5165 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5166 if (to->vval.v_dict == NULL)
5167 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005168 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005169 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005170 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005172 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005173 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 }
5175 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005176 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177}
5178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 void
5180echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5181{
5182 char_u *tofree;
5183 char_u numbuf[NUMBUFLEN];
5184 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5185
5186 if (*atstart)
5187 {
5188 *atstart = FALSE;
5189 // Call msg_start() after eval1(), evaluating the expression
5190 // may cause a message to appear.
5191 if (with_space)
5192 {
5193 // Mark the saved text as finishing the line, so that what
5194 // follows is displayed on a new line when scrolling back
5195 // at the more prompt.
5196 msg_sb_eol();
5197 msg_start();
5198 }
5199 }
5200 else if (with_space)
5201 msg_puts_attr(" ", echo_attr);
5202
5203 if (p != NULL)
5204 for ( ; *p != NUL && !got_int; ++p)
5205 {
5206 if (*p == '\n' || *p == '\r' || *p == TAB)
5207 {
5208 if (*p != TAB && *needclr)
5209 {
5210 // remove any text still there from the command
5211 msg_clr_eos();
5212 *needclr = FALSE;
5213 }
5214 msg_putchar_attr(*p, echo_attr);
5215 }
5216 else
5217 {
5218 if (has_mbyte)
5219 {
5220 int i = (*mb_ptr2len)(p);
5221
5222 (void)msg_outtrans_len_attr(p, i, echo_attr);
5223 p += i - 1;
5224 }
5225 else
5226 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5227 }
5228 }
5229 vim_free(tofree);
5230}
5231
Bram Moolenaare9a41262005-01-15 22:18:47 +00005232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 * ":echo expr1 ..." print each argument separated with a space, add a
5234 * newline at the end.
5235 * ":echon expr1 ..." print each argument plain.
5236 */
5237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005238ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005241 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 char_u *p;
5243 int needclr = TRUE;
5244 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005245 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005246 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005247 evalarg_T evalarg;
5248
Bram Moolenaar37c83712020-06-30 21:18:36 +02005249 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250
5251 if (eap->skip)
5252 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005253 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005255 // If eval1() causes an error message the text from the command may
5256 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005257 need_clr_eos = needclr;
5258
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005260 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 {
5262 /*
5263 * Report the invalid expression unless the expression evaluation
5264 * has been cancelled due to an aborting error, an interrupt, or an
5265 * exception.
5266 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005267 if (!aborting() && did_emsg == did_emsg_before
5268 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005269 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005270 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 break;
5272 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005273 need_clr_eos = FALSE;
5274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005276 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005277
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005278 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 arg = skipwhite(arg);
5280 }
5281 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005282 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283
5284 if (eap->skip)
5285 --emsg_skip;
5286 else
5287 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005288 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 if (needclr)
5290 msg_clr_eos();
5291 if (eap->cmdidx == CMD_echo)
5292 msg_end();
5293 }
5294}
5295
5296/*
5297 * ":echohl {name}".
5298 */
5299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005300ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005302 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303}
5304
5305/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005306 * Returns the :echo attribute
5307 */
5308 int
5309get_echo_attr(void)
5310{
5311 return echo_attr;
5312}
5313
5314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 * ":execute expr1 ..." execute the result of an expression.
5316 * ":echomsg expr1 ..." Print a message
5317 * ":echoerr expr1 ..." Print an error
5318 * Each gets spaces around each argument and a newline at the end for
5319 * echo commands
5320 */
5321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005322ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323{
5324 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005325 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 int ret = OK;
5327 char_u *p;
5328 garray_T ga;
5329 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
5331 ga_init2(&ga, 1, 80);
5332
5333 if (eap->skip)
5334 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005335 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005337 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005338 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
5341 if (!eap->skip)
5342 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005343 char_u buf[NUMBUFLEN];
5344
5345 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005346 {
5347 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5348 {
5349 emsg(_(e_inval_string));
5350 p = NULL;
5351 }
5352 else
5353 p = tv_get_string_buf(&rettv, buf);
5354 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005355 else
5356 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005357 if (p == NULL)
5358 {
5359 clear_tv(&rettv);
5360 ret = FAIL;
5361 break;
5362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 len = (int)STRLEN(p);
5364 if (ga_grow(&ga, len + 2) == FAIL)
5365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005366 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 ret = FAIL;
5368 break;
5369 }
5370 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 ga.ga_len += len;
5374 }
5375
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005376 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 arg = skipwhite(arg);
5378 }
5379
5380 if (ret != FAIL && ga.ga_data != NULL)
5381 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005382 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5383 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005384 // Mark the already saved text as finishing the line, so that what
5385 // follows is displayed on a new line when scrolling back at the
5386 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005387 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005388 }
5389
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005391 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005392 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005393 out_flush();
5394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 else if (eap->cmdidx == CMD_echoerr)
5396 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005397 int save_did_emsg = did_emsg;
5398
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005399 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005400 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 if (!force_abort)
5402 did_emsg = save_did_emsg;
5403 }
5404 else if (eap->cmdidx == CMD_execute)
5405 do_cmdline((char_u *)ga.ga_data,
5406 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5407 }
5408
5409 ga_clear(&ga);
5410
5411 if (eap->skip)
5412 --emsg_skip;
5413
5414 eap->nextcmd = check_nextcmd(arg);
5415}
5416
5417/*
5418 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5419 * "arg" points to the "&" or '+' when called, to "option" when returning.
5420 * Returns NULL when no option name found. Otherwise pointer to the char
5421 * after the option name.
5422 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005423 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005424find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425{
5426 char_u *p = *arg;
5427
5428 ++p;
5429 if (*p == 'g' && p[1] == ':')
5430 {
5431 *opt_flags = OPT_GLOBAL;
5432 p += 2;
5433 }
5434 else if (*p == 'l' && p[1] == ':')
5435 {
5436 *opt_flags = OPT_LOCAL;
5437 p += 2;
5438 }
5439 else
5440 *opt_flags = 0;
5441
5442 if (!ASCII_ISALPHA(*p))
5443 return NULL;
5444 *arg = p;
5445
5446 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005447 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 else
5449 while (ASCII_ISALPHA(*p))
5450 ++p;
5451 return p;
5452}
5453
5454/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005455 * Display script name where an item was last set.
5456 * Should only be invoked when 'verbose' is non-zero.
5457 */
5458 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005459last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005460{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005461 char_u *p;
5462
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005463 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005464 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005465 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005466 if (p != NULL)
5467 {
5468 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005469 msg_puts(_("\n\tLast set from "));
5470 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005471 if (script_ctx.sc_lnum > 0)
5472 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005473 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005474 msg_outnum((long)script_ctx.sc_lnum);
5475 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005476 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005477 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005478 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005479 }
5480}
5481
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005482#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
5484/*
5485 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005486 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 * "flags" can be "g" to do a global substitute.
5488 * Returns an allocated string, NULL for error.
5489 */
5490 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005491do_string_sub(
5492 char_u *str,
5493 char_u *pat,
5494 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005495 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005496 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497{
5498 int sublen;
5499 regmatch_T regmatch;
5500 int i;
5501 int do_all;
5502 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005503 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 garray_T ga;
5505 char_u *ret;
5506 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005507 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005509 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005511 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512
5513 ga_init2(&ga, 1, 200);
5514
5515 do_all = (flags[0] == 'g');
5516
5517 regmatch.rm_ic = p_ic;
5518 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5519 if (regmatch.regprog != NULL)
5520 {
5521 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005522 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5524 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005525 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005526 if (regmatch.startp[0] == regmatch.endp[0])
5527 {
5528 if (zero_width == regmatch.startp[0])
5529 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005530 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005531 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005532 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5533 (size_t)i);
5534 ga.ga_len += i;
5535 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005536 continue;
5537 }
5538 zero_width = regmatch.startp[0];
5539 }
5540
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 /*
5542 * Get some space for a temporary buffer to do the substitution
5543 * into. It will contain:
5544 * - The text up to where the match is.
5545 * - The substituted text.
5546 * - The text after the match.
5547 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005548 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005549 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5551 {
5552 ga_clear(&ga);
5553 break;
5554 }
5555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005556 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 i = (int)(regmatch.startp[0] - tail);
5558 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005559 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005560 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 + ga.ga_len + i, TRUE, TRUE, FALSE);
5562 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005563 tail = regmatch.endp[0];
5564 if (*tail == NUL)
5565 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 if (!do_all)
5567 break;
5568 }
5569
5570 if (ga.ga_data != NULL)
5571 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5572
Bram Moolenaar473de612013-06-08 18:19:48 +02005573 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575
5576 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5577 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005578 if (p_cpo == empty_option)
5579 p_cpo = save_cpo;
5580 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005581 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005582 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583
5584 return ret;
5585}