blob: 1468070ae6c09aa7ccda515221ba3f604ae0dc0b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010025#define NAMESPACE_CHAR (char_u *)"abglstvw"
26
Bram Moolenaar532c7802005-01-27 14:44:31 +000027/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000028 * When recursively copying lists and dicts we need to remember which ones we
29 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000030 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000031 */
32static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020033
Bram Moolenaar071d4272004-06-13 20:20:40 +000034/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000035 * Info used by a ":for" loop.
36 */
Bram Moolenaar33570922005-01-25 22:26:29 +000037typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000038{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010039 int fi_semicolon; // TRUE if ending in '; var]'
40 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020041 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010042 listwatch_T fi_lw; // keep an eye on the item used.
43 list_T *fi_list; // list being used
44 int fi_bi; // index of blob
45 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
54static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
62 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020063 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010064num_divide(varnumber_T n1, varnumber_T n2)
65{
66 varnumber_T result;
67
68 if (n2 == 0) // give an error message?
69 {
70 if (n1 == 0)
71 result = VARNUM_MIN; // similar to NaN
72 else if (n1 < 0)
73 result = -VARNUM_MAX;
74 else
75 result = VARNUM_MAX;
76 }
77 else
78 result = n1 / n2;
79
80 return result;
81}
82
83/*
84 * Return "n1" modulus "n2", taking care of dividing by zero.
85 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020086 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010087num_modulus(varnumber_T n1, varnumber_T n2)
88{
89 // Give an error when n2 is 0?
90 return (n2 == 0) ? 0 : (n1 % n2);
91}
92
Bram Moolenaara1fa8922017-01-12 20:06:33 +010093#if defined(EBCDIC) || defined(PROTO)
94/*
95 * Compare struct fst by function name.
96 */
97 static int
98compare_func_name(const void *s1, const void *s2)
99{
100 struct fst *p1 = (struct fst *)s1;
101 struct fst *p2 = (struct fst *)s2;
102
103 return STRCMP(p1->f_name, p2->f_name);
104}
105
106/*
107 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100108 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100109 * On machines using EBCDIC we have to sort it.
110 */
111 static void
112sortFunctions(void)
113{
114 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
115
116 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
117}
118#endif
119
Bram Moolenaar33570922005-01-25 22:26:29 +0000120/*
121 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000122 */
123 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100124eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000125{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200127 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000128
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200129#ifdef EBCDIC
130 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100131 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200132 */
133 sortFunctions();
134#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000135}
136
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000137#if defined(EXITFREE) || defined(PROTO)
138 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100139eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200141 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100142 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200143 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000144
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200145 // autoloaded script names
146 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000147
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200148 // unreferenced lists and dicts
149 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200150
151 // functions not garbage collected
152 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000153}
154#endif
155
Bram Moolenaare6b53242020-07-01 17:28:33 +0200156 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200157fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
158{
159 CLEAR_FIELD(*evalarg);
160 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
161 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
162 {
163 evalarg->eval_getline = eap->getline;
164 evalarg->eval_cookie = eap->cookie;
165 }
166}
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
169 * Top level evaluation function, returning a boolean.
170 * Sets "error" to TRUE if there was an error.
171 * Return TRUE or FALSE.
172 */
173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100174eval_to_bool(
175 char_u *arg,
176 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200177 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100178 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179{
Bram Moolenaar33570922005-01-25 22:26:29 +0000180 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200181 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200182 evalarg_T evalarg;
183
Bram Moolenaar37c83712020-06-30 21:18:36 +0200184 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186 if (skip)
187 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200188 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 else
191 {
192 *error = FALSE;
193 if (!skip)
194 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100195 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000196 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 }
198 }
199 if (skip)
200 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200201 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200203 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204}
205
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206/*
207 * Call eval1() and give an error message if not done at a lower level.
208 */
209 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200210eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100211{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100212 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100213 int ret;
214 int did_emsg_before = did_emsg;
215 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200216 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100217
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200218 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
219
220 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100221 if (ret == FAIL)
222 {
223 // Report the invalid expression unless the expression evaluation has
224 // been cancelled due to an aborting error, an interrupt, or an
225 // exception, or we already gave a more specific error.
226 // Also check called_emsg for when using assert_fails().
227 if (!aborting() && did_emsg == did_emsg_before
228 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100229 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100230 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200231 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100232 return ret;
233}
234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200236 * Return whether a typval is a valid expression to pass to eval_expr_typval()
237 * or eval_expr_to_bool(). An empty string returns FALSE;
238 */
239 int
240eval_expr_valid_arg(typval_T *tv)
241{
242 return tv->v_type != VAR_UNKNOWN
243 && (tv->v_type != VAR_STRING
244 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
245}
246
247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 * Evaluate an expression, which can be a function, partial or string.
249 * Pass arguments "argv[argc]".
250 * Return the result in "rettv" and OK or FAIL.
251 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200252 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100253eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
254{
255 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100256 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200257 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100258
259 if (expr->v_type == VAR_FUNC)
260 {
261 s = expr->vval.v_string;
262 if (s == NULL || *s == NUL)
263 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200264 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200265 funcexe.evaluate = TRUE;
266 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267 return FAIL;
268 }
269 else if (expr->v_type == VAR_PARTIAL)
270 {
271 partial_T *partial = expr->vval.v_partial;
272
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200273 if (partial == NULL)
274 return FAIL;
275
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200277 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200279 if (call_def_function(partial->pt_func, argc, argv,
280 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 return FAIL;
282 }
283 else
284 {
285 s = partial_name(partial);
286 if (s == NULL || *s == NUL)
287 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200288 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 funcexe.evaluate = TRUE;
290 funcexe.partial = partial;
291 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
292 return FAIL;
293 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 }
295 else
296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100297 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 if (s == NULL)
299 return FAIL;
300 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200301 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 return FAIL;
Bram 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;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001227 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001228 &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
Bram Moolenaare6b53242020-07-01 17:28:33 +02001807 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001808 char_u *name,
1809 int name_len,
1810 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001811 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001812 typval_T *basetv) // "expr" for "expr->name(arg)"
1813{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001814 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001815 char_u *s = name;
1816 int len = name_len;
1817 partial_T *partial;
1818 int ret = OK;
1819
1820 if (!evaluate)
1821 check_vars(s, len);
1822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001823 // If "s" is the name of a variable of type VAR_FUNC
1824 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001825 s = deref_func_name(s, &len, &partial, !evaluate);
1826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001827 // Need to make a copy, in case evaluating the arguments makes
1828 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001829 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001830 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001831 ret = FAIL;
1832 else
1833 {
1834 funcexe_T funcexe;
1835
1836 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001837 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001838 funcexe.firstline = curwin->w_cursor.lnum;
1839 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001840 funcexe.evaluate = evaluate;
1841 funcexe.partial = partial;
1842 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001843 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001844 }
1845 vim_free(s);
1846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001847 // If evaluate is FALSE rettv->v_type was not set in
1848 // get_func_tv, but it's needed in handle_subscript() to parse
1849 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001850 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1851 {
1852 rettv->vval.v_string = NULL;
1853 rettv->v_type = VAR_FUNC;
1854 }
1855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001856 // Stop the expression evaluation when immediately
1857 // aborting on error, or when an interrupt occurred or
1858 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001859 if (evaluate && aborting())
1860 {
1861 if (ret == OK)
1862 clear_tv(rettv);
1863 ret = FAIL;
1864 }
1865 return ret;
1866}
1867
1868/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001869 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1870 * comment) and there is a next line, return the next line (skipping blanks)
1871 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001872 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1873 * "arg" must point somewhere inside a line, not at the start.
1874 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001875 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001876eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1877{
1878 *getnext = FALSE;
1879 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1880 && evalarg != NULL
1881 && evalarg->eval_cookie != NULL
1882 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
Bram Moolenaar962d7212020-07-04 14:15:00 +02001883 && *arg == '#' && arg[1] != '{')))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001884 {
Bram Moolenaard5053d02020-06-28 15:51:16 +02001885 char_u *p = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001886
1887 if (p != NULL)
1888 {
1889 *getnext = TRUE;
1890 return skipwhite(p);
1891 }
1892 }
1893 return arg;
1894}
1895
1896/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001897 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001898 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001899 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001900eval_next_line(evalarg_T *evalarg)
1901{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001902 garray_T *gap = &evalarg->eval_ga;
1903 char_u *line;
1904
Bram Moolenaard5053d02020-06-28 15:51:16 +02001905 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001906 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001907 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1908 {
1909 // Going to concatenate the lines after parsing.
1910 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1911 ++gap->ga_len;
1912 }
1913 else
1914 {
1915 vim_free(evalarg->eval_tofree);
1916 evalarg->eval_tofree = line;
1917 }
1918 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001919}
1920
Bram Moolenaare6b53242020-07-01 17:28:33 +02001921/*
1922 * Call eval_next_non_blank() and get the next line if needed.
1923 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001924 char_u *
1925skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1926{
1927 int getnext;
1928 char_u *p = skipwhite(arg);
1929
Bram Moolenaar962d7212020-07-04 14:15:00 +02001930 if (evalarg == NULL)
1931 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02001932 eval_next_non_blank(p, evalarg, &getnext);
1933 if (getnext)
1934 return eval_next_line(evalarg);
1935 return p;
1936}
1937
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001938/*
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001939 * After using "evalarg" filled from "eap" free the memory.
1940 */
1941 void
1942clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1943{
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001944 if (evalarg != NULL && evalarg->eval_tofree != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001945 {
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001946 if (eap != NULL)
1947 {
1948 // We may need to keep the original command line, e.g. for
1949 // ":let" it has the variable names. But we may also need the
1950 // new one, "nextcmd" points into it. Keep both.
1951 vim_free(eap->cmdline_tofree);
1952 eap->cmdline_tofree = *eap->cmdlinep;
1953 *eap->cmdlinep = evalarg->eval_tofree;
1954 }
1955 else
1956 vim_free(evalarg->eval_tofree);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001957 evalarg->eval_tofree = NULL;
1958 }
1959}
1960
1961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1965 */
1966
1967/*
1968 * Handle zero level expression.
1969 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001970 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001971 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001972 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 * Return OK or FAIL.
1974 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001975 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001976eval0(
1977 char_u *arg,
1978 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001979 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001980 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
1982 int ret;
1983 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001984 int did_emsg_before = did_emsg;
1985 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001986 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
1988 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001989 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001990
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001991 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
1993 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 /*
1996 * Report the invalid expression unless the expression evaluation has
1997 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001998 * exception, or we already gave a more specific error.
1999 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002001 if (!aborting()
2002 && did_emsg == did_emsg_before
2003 && called_emsg == called_emsg_before
2004 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002005 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 ret = FAIL;
2007 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002008
2009 if (eap != NULL)
2010 eap->nextcmd = check_nextcmd(p);
2011
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 return ret;
2013}
2014
2015/*
2016 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002017 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 *
2019 * "arg" must point to the first non-white of the expression.
2020 * "arg" is advanced to the next non-white after the recognized expression.
2021 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002022 * Note: "rettv.v_lock" is not set.
2023 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 * Return OK or FAIL.
2025 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002026 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002027eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002029 char_u *p;
2030 int getnext;
2031
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 /*
2033 * Get the first variable.
2034 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002035 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 return FAIL;
2037
Bram Moolenaar793648f2020-06-26 21:28:25 +02002038 p = eval_next_non_blank(*arg, evalarg, &getnext);
2039 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002041 int result;
2042 typval_T var2;
2043 evalarg_T nested_evalarg;
2044 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002045 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002046
Bram Moolenaar793648f2020-06-26 21:28:25 +02002047 if (getnext)
2048 *arg = eval_next_line(evalarg);
2049
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002050 if (evalarg == NULL)
2051 {
2052 CLEAR_FIELD(nested_evalarg);
2053 orig_flags = 0;
2054 }
2055 else
2056 {
2057 nested_evalarg = *evalarg;
2058 orig_flags = evalarg->eval_flags;
2059 }
2060
Bram Moolenaar7acde512020-06-24 23:02:40 +02002061 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002063 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002065 int error = FALSE;
2066
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002067 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002069 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002070 if (error)
2071 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
2073
2074 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002075 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002077 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002078 nested_evalarg.eval_flags = result ? orig_flags
2079 : orig_flags & ~EVAL_EVALUATE;
2080 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 return FAIL;
2082
2083 /*
2084 * Check for the ":".
2085 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002086 p = eval_next_non_blank(*arg, evalarg, &getnext);
2087 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002091 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 return FAIL;
2093 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002094 if (getnext)
2095 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096
2097 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002098 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002100 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002101 nested_evalarg.eval_flags = !result ? orig_flags
2102 : orig_flags & ~EVAL_EVALUATE;
2103 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
2105 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002106 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 return FAIL;
2108 }
2109 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 }
2112
2113 return OK;
2114}
2115
2116/*
2117 * Handle first level expression:
2118 * expr2 || expr2 || expr2 logical OR
2119 *
2120 * "arg" must point to the first non-white of the expression.
2121 * "arg" is advanced to the next non-white after the recognized expression.
2122 *
2123 * Return OK or FAIL.
2124 */
2125 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002126eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002128 char_u *p;
2129 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002130 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 long result;
2132 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002133 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
2135 /*
2136 * Get the first variable.
2137 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002138 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 return FAIL;
2140
2141 /*
2142 * Repeat until there is no following "||".
2143 */
2144 first = TRUE;
2145 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002146 p = eval_next_non_blank(*arg, evalarg, &getnext);
2147 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002149 evalarg_T nested_evalarg;
2150 int evaluate;
2151 int orig_flags;
2152
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002153 if (getnext)
2154 *arg = eval_next_line(evalarg);
2155
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002156 if (evalarg == NULL)
2157 {
2158 CLEAR_FIELD(nested_evalarg);
2159 orig_flags = 0;
2160 evaluate = FALSE;
2161 }
2162 else
2163 {
2164 nested_evalarg = *evalarg;
2165 orig_flags = evalarg->eval_flags;
2166 evaluate = orig_flags & EVAL_EVALUATE;
2167 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002168
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 if (evaluate && first)
2170 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002171 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002174 if (error)
2175 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 first = FALSE;
2177 }
2178
2179 /*
2180 * Get the second variable.
2181 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002182 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002183 nested_evalarg.eval_flags = !result ? orig_flags
2184 : orig_flags & ~EVAL_EVALUATE;
2185 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 return FAIL;
2187
2188 /*
2189 * Compute the result.
2190 */
2191 if (evaluate && !result)
2192 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002193 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 if (error)
2197 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 }
2199 if (evaluate)
2200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 rettv->v_type = VAR_NUMBER;
2202 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002204
2205 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207
2208 return OK;
2209}
2210
2211/*
2212 * Handle second level expression:
2213 * expr3 && expr3 && expr3 logical AND
2214 *
2215 * "arg" must point to the first non-white of the expression.
2216 * "arg" is advanced to the next non-white after the recognized expression.
2217 *
2218 * Return OK or FAIL.
2219 */
2220 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002221eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002223 char_u *p;
2224 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002225 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 long result;
2227 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002228 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 /*
2231 * Get the first variable.
2232 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002233 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 return FAIL;
2235
2236 /*
2237 * Repeat until there is no following "&&".
2238 */
2239 first = TRUE;
2240 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002241 p = eval_next_non_blank(*arg, evalarg, &getnext);
2242 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002244 evalarg_T nested_evalarg;
2245 int orig_flags;
2246 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002247
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002248 if (getnext)
2249 *arg = eval_next_line(evalarg);
2250
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002251 if (evalarg == NULL)
2252 {
2253 CLEAR_FIELD(nested_evalarg);
2254 orig_flags = 0;
2255 evaluate = FALSE;
2256 }
2257 else
2258 {
2259 nested_evalarg = *evalarg;
2260 orig_flags = evalarg->eval_flags;
2261 evaluate = orig_flags & EVAL_EVALUATE;
2262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 if (evaluate && first)
2264 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002265 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002268 if (error)
2269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 first = FALSE;
2271 }
2272
2273 /*
2274 * Get the second variable.
2275 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002276 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002277 nested_evalarg.eval_flags = result ? orig_flags
2278 : orig_flags & ~EVAL_EVALUATE;
2279 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 return FAIL;
2281
2282 /*
2283 * Compute the result.
2284 */
2285 if (evaluate && result)
2286 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002287 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002290 if (error)
2291 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
2293 if (evaluate)
2294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 rettv->v_type = VAR_NUMBER;
2296 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002298
2299 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301
2302 return OK;
2303}
2304
2305/*
2306 * Handle third level expression:
2307 * var1 == var2
2308 * var1 =~ var2
2309 * var1 != var2
2310 * var1 !~ var2
2311 * var1 > var2
2312 * var1 >= var2
2313 * var1 < var2
2314 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002315 * var1 is var2
2316 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 *
2318 * "arg" must point to the first non-white of the expression.
2319 * "arg" is advanced to the next non-white after the recognized expression.
2320 *
2321 * Return OK or FAIL.
2322 */
2323 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002324eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
Bram Moolenaar33570922005-01-25 22:26:29 +00002326 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002328 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002330 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
2334 /*
2335 * Get the first variable.
2336 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002337 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 return FAIL;
2339
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002340 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 switch (p[0])
2342 {
2343 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002344 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002346 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 break;
2348 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002349 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002351 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 break;
2353 case '>': if (p[1] != '=')
2354 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002355 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 len = 1;
2357 }
2358 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002359 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 break;
2361 case '<': if (p[1] != '=')
2362 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002363 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 len = 1;
2365 }
2366 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002367 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002369 case 'i': if (p[1] == 's')
2370 {
2371 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2372 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002373 i = p[len];
2374 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002375 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002376 }
2377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 }
2379
2380 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002381 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002383 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002385 if (getnext)
2386 *arg = eval_next_line(evalarg);
2387
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002388 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (p[len] == '?')
2390 {
2391 ic = TRUE;
2392 ++len;
2393 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002394 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 else if (p[len] == '#')
2396 {
2397 ic = FALSE;
2398 ++len;
2399 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002400 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 else
2402 ic = p_ic;
2403
2404 /*
2405 * Get the second variable.
2406 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002407 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002408 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 return FAIL;
2412 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002413 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002414 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002415 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002416
2417 clear_tv(&var2);
2418 return ret;
2419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 }
2421
2422 return OK;
2423}
2424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 void
2426eval_addblob(typval_T *tv1, typval_T *tv2)
2427{
2428 blob_T *b1 = tv1->vval.v_blob;
2429 blob_T *b2 = tv2->vval.v_blob;
2430 blob_T *b = blob_alloc();
2431 int i;
2432
2433 if (b != NULL)
2434 {
2435 for (i = 0; i < blob_len(b1); i++)
2436 ga_append(&b->bv_ga, blob_get(b1, i));
2437 for (i = 0; i < blob_len(b2); i++)
2438 ga_append(&b->bv_ga, blob_get(b2, i));
2439
2440 clear_tv(tv1);
2441 rettv_blob_set(tv1, b);
2442 }
2443}
2444
2445 int
2446eval_addlist(typval_T *tv1, typval_T *tv2)
2447{
2448 typval_T var3;
2449
2450 // concatenate Lists
2451 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2452 {
2453 clear_tv(tv1);
2454 clear_tv(tv2);
2455 return FAIL;
2456 }
2457 clear_tv(tv1);
2458 *tv1 = var3;
2459 return OK;
2460}
2461
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462/*
2463 * Handle fourth level expression:
2464 * + number addition
2465 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002466 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002467 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 *
2469 * "arg" must point to the first non-white of the expression.
2470 * "arg" is advanced to the next non-white after the recognized expression.
2471 *
2472 * Return OK or FAIL.
2473 */
2474 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002475eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002477 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
2479 /*
2480 * Get the first variable.
2481 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002482 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return FAIL;
2484
2485 /*
2486 * Repeat computing, until no '+', '-' or '.' is following.
2487 */
2488 for (;;)
2489 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002490 int getnext;
2491 char_u *p;
2492 int op;
2493 int concat;
2494 typval_T var2;
2495
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002496 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002497 p = eval_next_non_blank(*arg, evalarg, &getnext);
2498 op = *p;
2499 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002500 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002502 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002503 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002505 if ((op != '+' || (rettv->v_type != VAR_LIST
2506 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002507#ifdef FEAT_FLOAT
2508 && (op == '.' || rettv->v_type != VAR_FLOAT)
2509#endif
2510 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002511 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002512 // For "list + ...", an illegal use of the first operand as
2513 // a number cannot be determined before evaluating the 2nd
2514 // operand: if this is also a list, all is ok.
2515 // For "something . ...", "something - ..." or "non-list + ...",
2516 // we know that the first operand needs to be a string or number
2517 // without evaluating the 2nd operand. So check before to avoid
2518 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002519 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002520 {
2521 clear_tv(rettv);
2522 return FAIL;
2523 }
2524 }
2525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 /*
2527 * Get the second variable.
2528 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002529 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2530 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002531 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002532 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 return FAIL;
2536 }
2537
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002538 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
2540 /*
2541 * Compute the result.
2542 */
2543 if (op == '.')
2544 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002545 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2546 char_u *s1 = tv_get_string_buf(rettv, buf1);
2547 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2548
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002549 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002550 {
2551 clear_tv(rettv);
2552 clear_tv(&var2);
2553 return FAIL;
2554 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002555 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 clear_tv(rettv);
2557 rettv->v_type = VAR_STRING;
2558 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002560 else if (op == '+' && rettv->v_type == VAR_BLOB
2561 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002563 else if (op == '+' && rettv->v_type == VAR_LIST
2564 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002565 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002567 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 else
2570 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002571 int error = FALSE;
2572 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002573#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002574 float_T f1 = 0, f2 = 0;
2575
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002576 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002577 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002578 f1 = rettv->vval.v_float;
2579 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002580 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002581 else
2582#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002584 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002585 if (error)
2586 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002587 // This can only happen for "list + non-list". For
2588 // "non-list + ..." or "something - ...", we returned
2589 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002590 clear_tv(rettv);
2591 return FAIL;
2592 }
2593#ifdef FEAT_FLOAT
2594 if (var2.v_type == VAR_FLOAT)
2595 f1 = n1;
2596#endif
2597 }
2598#ifdef FEAT_FLOAT
2599 if (var2.v_type == VAR_FLOAT)
2600 {
2601 f2 = var2.vval.v_float;
2602 n2 = 0;
2603 }
2604 else
2605#endif
2606 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002607 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002608 if (error)
2609 {
2610 clear_tv(rettv);
2611 clear_tv(&var2);
2612 return FAIL;
2613 }
2614#ifdef FEAT_FLOAT
2615 if (rettv->v_type == VAR_FLOAT)
2616 f2 = n2;
2617#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002618 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002620
2621#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002622 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002623 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2624 {
2625 if (op == '+')
2626 f1 = f1 + f2;
2627 else
2628 f1 = f1 - f2;
2629 rettv->v_type = VAR_FLOAT;
2630 rettv->vval.v_float = f1;
2631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002633#endif
2634 {
2635 if (op == '+')
2636 n1 = n1 + n2;
2637 else
2638 n1 = n1 - n2;
2639 rettv->v_type = VAR_NUMBER;
2640 rettv->vval.v_number = n1;
2641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
2645 }
2646 return OK;
2647}
2648
2649/*
2650 * Handle fifth level expression:
2651 * * number multiplication
2652 * / number division
2653 * % number modulo
2654 *
2655 * "arg" must point to the first non-white of the expression.
2656 * "arg" is advanced to the next non-white after the recognized expression.
2657 *
2658 * Return OK or FAIL.
2659 */
2660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002661eval6(
2662 char_u **arg,
2663 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002664 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002665 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
Bram Moolenaar33570922005-01-25 22:26:29 +00002667 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002669 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002670#ifdef FEAT_FLOAT
2671 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002672 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002673#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002674 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
2676 /*
2677 * Get the first variable.
2678 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002679 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 return FAIL;
2681
2682 /*
2683 * Repeat computing, until no '*', '/' or '%' is following.
2684 */
2685 for (;;)
2686 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002687 int evaluate = evalarg == NULL ? 0
2688 : (evalarg->eval_flags & EVAL_EVALUATE);
2689 int getnext;
2690
2691 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002692 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002694 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002695 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002697 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002699#ifdef FEAT_FLOAT
2700 if (rettv->v_type == VAR_FLOAT)
2701 {
2702 f1 = rettv->vval.v_float;
2703 use_float = TRUE;
2704 n1 = 0;
2705 }
2706 else
2707#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002708 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002709 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 if (error)
2711 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 else
2714 n1 = 0;
2715
2716 /*
2717 * Get the second variable.
2718 */
2719 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002720 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 return FAIL;
2722
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002723 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002725#ifdef FEAT_FLOAT
2726 if (var2.v_type == VAR_FLOAT)
2727 {
2728 if (!use_float)
2729 {
2730 f1 = n1;
2731 use_float = TRUE;
2732 }
2733 f2 = var2.vval.v_float;
2734 n2 = 0;
2735 }
2736 else
2737#endif
2738 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002739 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002740 clear_tv(&var2);
2741 if (error)
2742 return FAIL;
2743#ifdef FEAT_FLOAT
2744 if (use_float)
2745 f2 = n2;
2746#endif
2747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
2749 /*
2750 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002751 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002753#ifdef FEAT_FLOAT
2754 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002756 if (op == '*')
2757 f1 = f1 * f2;
2758 else if (op == '/')
2759 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002760# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002761 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002762 if (f2 == 0.0)
2763 {
2764 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002765 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002766 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002767 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002768 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002769 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002770 }
2771 else
2772 f1 = f1 / f2;
2773# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002774 // We rely on the floating point library to handle divide
2775 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002776 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002777# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002780 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002782 return FAIL;
2783 }
2784 rettv->v_type = VAR_FLOAT;
2785 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
2787 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002790 if (op == '*')
2791 n1 = n1 * n2;
2792 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002793 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002795 n1 = num_modulus(n1, n2);
2796
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002797 rettv->v_type = VAR_NUMBER;
2798 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801 }
2802
2803 return OK;
2804}
2805
2806/*
2807 * Handle sixth level expression:
2808 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002809 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002810 * "string" string constant
2811 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 * &option-name option value
2813 * @r register contents
2814 * identifier variable value
2815 * function() function call
2816 * $VAR environment variable
2817 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002818 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002820 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002821 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 *
2823 * Also handle:
2824 * ! in front logical NOT
2825 * - in front unary minus
2826 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 * trailing [] subscript in String or List
2828 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002829 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 *
2831 * "arg" must point to the first non-white of the expression.
2832 * "arg" is advanced to the next non-white after the recognized expression.
2833 *
2834 * Return OK or FAIL.
2835 */
2836 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002837eval7(
2838 char_u **arg,
2839 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002840 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002843 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2844 int evaluate = evalarg != NULL
2845 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 int len;
2847 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 char_u *start_leader, *end_leader;
2849 int ret = OK;
2850 char_u *alias;
2851
2852 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002853 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002854 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002856 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
2858 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002859 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 */
2861 start_leader = *arg;
2862 while (**arg == '!' || **arg == '-' || **arg == '+')
2863 *arg = skipwhite(*arg + 1);
2864 end_leader = *arg;
2865
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002866 if (**arg == '.' && (!isdigit(*(*arg + 1))
2867#ifdef FEAT_FLOAT
2868 || current_sctx.sc_version < 2
2869#endif
2870 ))
2871 {
2872 semsg(_(e_invexpr2), *arg);
2873 ++*arg;
2874 return FAIL;
2875 }
2876
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 switch (**arg)
2878 {
2879 /*
2880 * Number constant.
2881 */
2882 case '0':
2883 case '1':
2884 case '2':
2885 case '3':
2886 case '4':
2887 case '5':
2888 case '6':
2889 case '7':
2890 case '8':
2891 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002892 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002893
2894 // Apply prefixed "-" and "+" now. Matters especially when
2895 // "->" follows.
2896 if (ret == OK && evaluate && end_leader > start_leader)
2897 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 break;
2899
2900 /*
2901 * String constant: "string".
2902 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002903 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 break;
2905
2906 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002909 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002910 break;
2911
2912 /*
2913 * List: [expr, expr]
2914 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002915 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 break;
2917
2918 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002919 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002920 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002921 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002922 {
2923 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002924 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002925 }
2926 else
2927 ret = NOTDONE;
2928 break;
2929
2930 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002931 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002932 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002933 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002934 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002935 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002936 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002937 break;
2938
2939 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002940 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002942 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 break;
2944
2945 /*
2946 * Environment variable: $VAR.
2947 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002948 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 break;
2950
2951 /*
2952 * Register contents: @r.
2953 */
2954 case '@': ++*arg;
2955 if (evaluate)
2956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002957 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002958 rettv->vval.v_string = get_reg_contents(**arg,
2959 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
2961 if (**arg != NUL)
2962 ++*arg;
2963 break;
2964
2965 /*
2966 * nested expression: (expression).
2967 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002968 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02002969 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002970 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002971
Bram Moolenaar9215f012020-06-27 21:18:00 +02002972 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002973 if (**arg == ')')
2974 ++*arg;
2975 else if (ret == OK)
2976 {
2977 emsg(_(e_missing_close));
2978 clear_tv(rettv);
2979 ret = FAIL;
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
2982 break;
2983
Bram Moolenaar8c711452005-01-14 21:53:12 +00002984 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 break;
2986 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002987
2988 if (ret == NOTDONE)
2989 {
2990 /*
2991 * Must be a variable or function name.
2992 * Can also be a curly-braces kind of name: {expr}.
2993 */
2994 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002995 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002996 if (alias != NULL)
2997 s = alias;
2998
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002999 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003000 ret = FAIL;
3001 else
3002 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003003 if (**arg == '(')
3004 // "name(..." recursive!
Bram Moolenaare6b53242020-07-01 17:28:33 +02003005 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003006 else if (flags & EVAL_CONSTANT)
3007 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003008 else if (evaluate)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003009 // get value of variable
3010 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003012 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003013 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003014 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003016 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003017 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003018 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003019 }
3020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 *arg = skipwhite(*arg);
3022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003023 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3024 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003025 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003026 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
3028 /*
3029 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3030 */
3031 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003032 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003033 return ret;
3034}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003035
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003036/*
3037 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003038 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003039 * Adjusts "end_leaderp" until it is at "start_leader".
3040 */
3041 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003042eval7_leader(
3043 typval_T *rettv,
3044 int numeric_only,
3045 char_u *start_leader,
3046 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003047{
3048 char_u *end_leader = *end_leaderp;
3049 int ret = OK;
3050 int error = FALSE;
3051 varnumber_T val = 0;
3052#ifdef FEAT_FLOAT
3053 float_T f = 0.0;
3054
3055 if (rettv->v_type == VAR_FLOAT)
3056 f = rettv->vval.v_float;
3057 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003058#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003059 val = tv_get_number_chk(rettv, &error);
3060 if (error)
3061 {
3062 clear_tv(rettv);
3063 ret = FAIL;
3064 }
3065 else
3066 {
3067 while (end_leader > start_leader)
3068 {
3069 --end_leader;
3070 if (*end_leader == '!')
3071 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003072 if (numeric_only)
3073 {
3074 ++end_leader;
3075 break;
3076 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003077#ifdef FEAT_FLOAT
3078 if (rettv->v_type == VAR_FLOAT)
3079 f = !f;
3080 else
3081#endif
3082 val = !val;
3083 }
3084 else if (*end_leader == '-')
3085 {
3086#ifdef FEAT_FLOAT
3087 if (rettv->v_type == VAR_FLOAT)
3088 f = -f;
3089 else
3090#endif
3091 val = -val;
3092 }
3093 }
3094#ifdef FEAT_FLOAT
3095 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003097 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003098 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003100 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003101#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003102 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003103 clear_tv(rettv);
3104 rettv->v_type = VAR_NUMBER;
3105 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003108 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 return ret;
3110}
3111
3112/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003113 * Call the function referred to in "rettv".
3114 */
3115 static int
3116call_func_rettv(
3117 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003118 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003119 typval_T *rettv,
3120 int evaluate,
3121 dict_T *selfdict,
3122 typval_T *basetv)
3123{
3124 partial_T *pt = NULL;
3125 funcexe_T funcexe;
3126 typval_T functv;
3127 char_u *s;
3128 int ret;
3129
3130 // need to copy the funcref so that we can clear rettv
3131 if (evaluate)
3132 {
3133 functv = *rettv;
3134 rettv->v_type = VAR_UNKNOWN;
3135
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003136 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003137 if (functv.v_type == VAR_PARTIAL)
3138 {
3139 pt = functv.vval.v_partial;
3140 s = partial_name(pt);
3141 }
3142 else
3143 s = functv.vval.v_string;
3144 }
3145 else
3146 s = (char_u *)"";
3147
Bram Moolenaara80faa82020-04-12 19:37:17 +02003148 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003149 funcexe.firstline = curwin->w_cursor.lnum;
3150 funcexe.lastline = curwin->w_cursor.lnum;
3151 funcexe.evaluate = evaluate;
3152 funcexe.partial = pt;
3153 funcexe.selfdict = selfdict;
3154 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003155 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003157 // Clear the funcref afterwards, so that deleting it while
3158 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003159 if (evaluate)
3160 clear_tv(&functv);
3161
3162 return ret;
3163}
3164
3165/*
3166 * Evaluate "->method()".
3167 * "*arg" points to the '-'.
3168 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3169 */
3170 static int
3171eval_lambda(
3172 char_u **arg,
3173 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003174 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003175 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003176{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003177 int evaluate = evalarg != NULL
3178 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003179 typval_T base = *rettv;
3180 int ret;
3181
3182 // Skip over the ->.
3183 *arg += 2;
3184 rettv->v_type = VAR_UNKNOWN;
3185
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003186 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003187 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003188 return FAIL;
3189 else if (**arg != '(')
3190 {
3191 if (verbose)
3192 {
3193 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003194 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003195 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003197 }
3198 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003199 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003200 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003201 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003202 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003203
3204 // Clear the funcref afterwards, so that deleting it while
3205 // evaluating the arguments is possible (see test55).
3206 if (evaluate)
3207 clear_tv(&base);
3208
3209 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003210}
3211
3212/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003213 * Evaluate "->method()".
3214 * "*arg" points to the '-'.
3215 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3216 */
3217 static int
3218eval_method(
3219 char_u **arg,
3220 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003221 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003222 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003223{
3224 char_u *name;
3225 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003226 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003227 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003228 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003229 int evaluate = evalarg != NULL
3230 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003231
3232 // Skip over the ->.
3233 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003234 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003235
Bram Moolenaarac92e252019-08-03 21:58:38 +02003236 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003237 len = get_name_len(arg, &alias, evaluate, TRUE);
3238 if (alias != NULL)
3239 name = alias;
3240
3241 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003242 {
3243 if (verbose)
3244 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003245 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003246 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003247 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003248 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003249 if (**arg != '(')
3250 {
3251 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003253 ret = FAIL;
3254 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003255 else if (VIM_ISWHITE((*arg)[-1]))
3256 {
3257 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003258 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003259 ret = FAIL;
3260 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003261 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003262 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003263 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003264 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003265
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003266 // Clear the funcref afterwards, so that deleting it while
3267 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003268 if (evaluate)
3269 clear_tv(&base);
3270
Bram Moolenaarac92e252019-08-03 21:58:38 +02003271 return ret;
3272}
3273
3274/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003275 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3276 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003277 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3278 */
3279 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003280eval_index(
3281 char_u **arg,
3282 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003283 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003284 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003285{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003286 int evaluate = evalarg != NULL
3287 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003288 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003289 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003290 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003291 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003292 long len = -1;
3293 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003294 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003295 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003296
Bram Moolenaara03f2332016-02-06 18:09:59 +01003297 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003298 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003299 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003300 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003301 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003302 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003303 return FAIL;
3304 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003305#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003306 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003307 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003308 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003309#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003310 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003311 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003312 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003313 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003314 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003315 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003316 return FAIL;
3317 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003318 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003320 if (evaluate)
3321 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003322 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003323
3324 case VAR_STRING:
3325 case VAR_NUMBER:
3326 case VAR_LIST:
3327 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003328 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003329 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003330 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003331
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003332 init_tv(&var1);
3333 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003334 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003335 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003336 /*
3337 * dict.name
3338 */
3339 key = *arg + 1;
3340 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3341 ;
3342 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003343 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003344 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003345 }
3346 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003348 /*
3349 * something[idx]
3350 *
3351 * Get the (first) variable from inside the [].
3352 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003353 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003354 if (**arg == ':')
3355 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003356 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003357 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003358 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003359 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003360 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003361 clear_tv(&var1);
3362 return FAIL;
3363 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003364
3365 /*
3366 * Get the second variable from inside the [:].
3367 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003368 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003369 if (**arg == ':')
3370 {
3371 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003372 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003373 if (**arg == ']')
3374 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003375 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003377 if (!empty1)
3378 clear_tv(&var1);
3379 return FAIL;
3380 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003381 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003382 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003383 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003384 if (!empty1)
3385 clear_tv(&var1);
3386 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003387 return FAIL;
3388 }
3389 }
3390
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003391 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003392 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003393 if (**arg != ']')
3394 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003395 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003396 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003397 clear_tv(&var1);
3398 if (range)
3399 clear_tv(&var2);
3400 return FAIL;
3401 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003402 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003403 }
3404
3405 if (evaluate)
3406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003407 n1 = 0;
3408 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003409 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003410 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003411 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003412 }
3413 if (range)
3414 {
3415 if (empty2)
3416 n2 = -1;
3417 else
3418 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003419 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003420 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003421 }
3422 }
3423
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003424 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003425 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003426 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003427 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003429 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003430 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003431 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003432 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003433 case VAR_SPECIAL:
3434 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003435 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003436 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003437
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003438 case VAR_NUMBER:
3439 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003440 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003441 len = (long)STRLEN(s);
3442 if (range)
3443 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003444 // The resulting variable is a substring. If the indexes
3445 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003446 if (n1 < 0)
3447 {
3448 n1 = len + n1;
3449 if (n1 < 0)
3450 n1 = 0;
3451 }
3452 if (n2 < 0)
3453 n2 = len + n2;
3454 else if (n2 >= len)
3455 n2 = len;
3456 if (n1 >= len || n2 < 0 || n1 > n2)
3457 s = NULL;
3458 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003459 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003460 }
3461 else
3462 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003463 // The resulting variable is a string of a single
3464 // character. If the index is too big or negative the
3465 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003466 if (n1 >= len || n1 < 0)
3467 s = NULL;
3468 else
3469 s = vim_strnsave(s + n1, 1);
3470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003471 clear_tv(rettv);
3472 rettv->v_type = VAR_STRING;
3473 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003474 break;
3475
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003476 case VAR_BLOB:
3477 len = blob_len(rettv->vval.v_blob);
3478 if (range)
3479 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003480 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003481 // are out of range the result is empty.
3482 if (n1 < 0)
3483 {
3484 n1 = len + n1;
3485 if (n1 < 0)
3486 n1 = 0;
3487 }
3488 if (n2 < 0)
3489 n2 = len + n2;
3490 else if (n2 >= len)
3491 n2 = len - 1;
3492 if (n1 >= len || n2 < 0 || n1 > n2)
3493 {
3494 clear_tv(rettv);
3495 rettv->v_type = VAR_BLOB;
3496 rettv->vval.v_blob = NULL;
3497 }
3498 else
3499 {
3500 blob_T *blob = blob_alloc();
3501
3502 if (blob != NULL)
3503 {
3504 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3505 {
3506 blob_free(blob);
3507 return FAIL;
3508 }
3509 blob->bv_ga.ga_len = n2 - n1 + 1;
3510 for (i = n1; i <= n2; i++)
3511 blob_set(blob, i - n1,
3512 blob_get(rettv->vval.v_blob, i));
3513
3514 clear_tv(rettv);
3515 rettv_blob_set(rettv, blob);
3516 }
3517 }
3518 }
3519 else
3520 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003521 // The resulting variable is a byte value.
3522 // If the index is too big or negative that is an error.
3523 if (n1 < 0)
3524 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003525 if (n1 < len && n1 >= 0)
3526 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003527 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003528
3529 clear_tv(rettv);
3530 rettv->v_type = VAR_NUMBER;
3531 rettv->vval.v_number = v;
3532 }
3533 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003534 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003535 }
3536 break;
3537
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003538 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003540 if (n1 < 0)
3541 n1 = len + n1;
3542 if (!empty1 && (n1 < 0 || n1 >= len))
3543 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003544 // For a range we allow invalid values and return an empty
3545 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003546 if (!range)
3547 {
3548 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003549 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003550 return FAIL;
3551 }
3552 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003553 }
3554 if (range)
3555 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003556 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003557
3558 if (n2 < 0)
3559 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003560 else if (n2 >= len)
3561 n2 = len - 1;
3562 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003563 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003564 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003565 if (l == NULL)
3566 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003567 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003568 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003569 }
3570 else
3571 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003572 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003573 clear_tv(rettv);
3574 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003575 }
3576 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003577
3578 case VAR_DICT:
3579 if (range)
3580 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003581 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003582 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003583 if (len == -1)
3584 clear_tv(&var1);
3585 return FAIL;
3586 }
3587 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003588 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589
3590 if (len == -1)
3591 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003592 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003593 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003594 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003595 clear_tv(&var1);
3596 return FAIL;
3597 }
3598 }
3599
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003600 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003602 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003603 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 if (len == -1)
3605 clear_tv(&var1);
3606 if (item == NULL)
3607 return FAIL;
3608
3609 copy_tv(&item->di_tv, &var1);
3610 clear_tv(rettv);
3611 *rettv = var1;
3612 }
3613 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003614 }
3615 }
3616
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003617 return OK;
3618}
3619
3620/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003621 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003622 */
3623 char_u *
3624partial_name(partial_T *pt)
3625{
3626 if (pt->pt_name != NULL)
3627 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003628 if (pt->pt_func != NULL)
3629 return pt->pt_func->uf_name;
3630 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003631}
3632
Bram Moolenaarddecc252016-04-06 22:59:37 +02003633 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003634partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003635{
3636 int i;
3637
3638 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003639 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003640 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003641 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003642 if (pt->pt_name != NULL)
3643 {
3644 func_unref(pt->pt_name);
3645 vim_free(pt->pt_name);
3646 }
3647 else
3648 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003649
3650 if (pt->pt_funcstack != NULL)
3651 {
3652 // Decrease the reference count for the context of a closure. If down
3653 // to zero free it and clear the variables on the stack.
3654 if (--pt->pt_funcstack->fs_refcount == 0)
3655 {
3656 garray_T *gap = &pt->pt_funcstack->fs_ga;
3657 typval_T *stack = gap->ga_data;
3658
3659 for (i = 0; i < gap->ga_len; ++i)
3660 clear_tv(stack + i);
3661 ga_clear(gap);
3662 vim_free(pt->pt_funcstack);
3663 }
3664 pt->pt_funcstack = NULL;
3665 }
3666
Bram Moolenaarddecc252016-04-06 22:59:37 +02003667 vim_free(pt);
3668}
3669
3670/*
3671 * Unreference a closure: decrement the reference count and free it when it
3672 * becomes zero.
3673 */
3674 void
3675partial_unref(partial_T *pt)
3676{
3677 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003678 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003679}
3680
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003681/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003682 * Return the next (unique) copy ID.
3683 * Used for serializing nested structures.
3684 */
3685 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003686get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003687{
3688 current_copyID += COPYID_INC;
3689 return current_copyID;
3690}
3691
3692/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003693 * Garbage collection for lists and dictionaries.
3694 *
3695 * We use reference counts to be able to free most items right away when they
3696 * are no longer used. But for composite items it's possible that it becomes
3697 * unused while the reference count is > 0: When there is a recursive
3698 * reference. Example:
3699 * :let l = [1, 2, 3]
3700 * :let d = {9: l}
3701 * :let l[1] = d
3702 *
3703 * Since this is quite unusual we handle this with garbage collection: every
3704 * once in a while find out which lists and dicts are not referenced from any
3705 * variable.
3706 *
3707 * Here is a good reference text about garbage collection (refers to Python
3708 * but it applies to all reference-counting mechanisms):
3709 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003710 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003711
3712/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003713 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003714 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003715 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003716 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003717 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003718garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003719{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003720 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003721 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003722 buf_T *buf;
3723 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003724 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003725 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003726
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003727 if (!testing)
3728 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003729 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003730 want_garbage_collect = FALSE;
3731 may_garbage_collect = FALSE;
3732 garbage_collect_at_exit = FALSE;
3733 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003734
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003735 // The execution stack can grow big, limit the size.
3736 if (exestack.ga_maxlen - exestack.ga_len > 500)
3737 {
3738 size_t new_len;
3739 char_u *pp;
3740 int n;
3741
3742 // Keep 150% of the current size, with a minimum of the growth size.
3743 n = exestack.ga_len / 2;
3744 if (n < exestack.ga_growsize)
3745 n = exestack.ga_growsize;
3746
3747 // Don't make it bigger though.
3748 if (exestack.ga_len + n < exestack.ga_maxlen)
3749 {
3750 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3751 pp = vim_realloc(exestack.ga_data, new_len);
3752 if (pp == NULL)
3753 return FAIL;
3754 exestack.ga_maxlen = exestack.ga_len + n;
3755 exestack.ga_data = pp;
3756 }
3757 }
3758
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003759 // We advance by two because we add one for items referenced through
3760 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003761 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003762
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003763 /*
3764 * 1. Go through all accessible variables and mark all lists and dicts
3765 * with copyID.
3766 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003768 // Don't free variables in the previous_funccal list unless they are only
3769 // referenced through previous_funccal. This must be first, because if
3770 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003771 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003773 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003774 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003776 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003777 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003778 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3779 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003781 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003782 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003783 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3784 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003785 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003786 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3787 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003788#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003789 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003790 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3791 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003792 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003793 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003794 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3795 NULL, NULL);
3796#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003798 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003799 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003800 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3801 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003802 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003803 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003804
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003805 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003806 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003807
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003808 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003809 abort = abort || set_ref_in_functions(copyID);
3810
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003811 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003812 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003813
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003814 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003815 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003816
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003817 // callbacks in buffers
3818 abort = abort || set_ref_in_buffers(copyID);
3819
Bram Moolenaar1dced572012-04-05 16:54:08 +02003820#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003821 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003822#endif
3823
Bram Moolenaardb913952012-06-29 12:54:53 +02003824#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003825 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003826#endif
3827
3828#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003829 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003830#endif
3831
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003832#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003833 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003834 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003835#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003836#ifdef FEAT_NETBEANS_INTG
3837 abort = abort || set_ref_in_nb_channel(copyID);
3838#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003839
Bram Moolenaare3188e22016-05-31 21:13:04 +02003840#ifdef FEAT_TIMERS
3841 abort = abort || set_ref_in_timer(copyID);
3842#endif
3843
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003844#ifdef FEAT_QUICKFIX
3845 abort = abort || set_ref_in_quickfix(copyID);
3846#endif
3847
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003848#ifdef FEAT_TERMINAL
3849 abort = abort || set_ref_in_term(copyID);
3850#endif
3851
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003852#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003853 abort = abort || set_ref_in_popups(copyID);
3854#endif
3855
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003856 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003857 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003858 /*
3859 * 2. Free lists and dictionaries that are not referenced.
3860 */
3861 did_free = free_unref_items(copyID);
3862
3863 /*
3864 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003865 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003866 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003868 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003869 else if (p_verbose > 0)
3870 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003871 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003872 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003873
3874 return did_free;
3875}
3876
3877/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003878 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003879 */
3880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003881free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003882{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003883 int did_free = FALSE;
3884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003885 // Let all "free" functions know that we are here. This means no
3886 // dictionaries, lists, channels or jobs are to be freed, because we will
3887 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003888 in_free_unref_items = TRUE;
3889
3890 /*
3891 * PASS 1: free the contents of the items. We don't free the items
3892 * themselves yet, so that it is possible to decrement refcount counters
3893 */
3894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003895 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003896 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003898 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003899 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003900
3901#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003902 // Go through the list of jobs and free items without the copyID. This
3903 // must happen before doing channels, because jobs refer to channels, but
3904 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003905 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003907 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003908 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3909#endif
3910
3911 /*
3912 * PASS 2: free the items themselves.
3913 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003914 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003915 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003916
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003917#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003918 // Go through the list of jobs and free items without the copyID. This
3919 // must happen before doing channels, because jobs refer to channels, but
3920 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003921 free_unused_jobs(copyID, COPYID_MASK);
3922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003923 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003924 free_unused_channels(copyID, COPYID_MASK);
3925#endif
3926
3927 in_free_unref_items = FALSE;
3928
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003929 return did_free;
3930}
3931
3932/*
3933 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003934 * "list_stack" is used to add lists to be marked. Can be NULL.
3935 *
3936 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003937 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003939set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003940{
3941 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003942 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003943 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003944 hashtab_T *cur_ht;
3945 ht_stack_T *ht_stack = NULL;
3946 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003947
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003948 cur_ht = ht;
3949 for (;;)
3950 {
3951 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003952 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003953 // Mark each item in the hashtab. If the item contains a hashtab
3954 // it is added to ht_stack, if it contains a list it is added to
3955 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003956 todo = (int)cur_ht->ht_used;
3957 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3958 if (!HASHITEM_EMPTY(hi))
3959 {
3960 --todo;
3961 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3962 &ht_stack, list_stack);
3963 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003964 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003965
3966 if (ht_stack == NULL)
3967 break;
3968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003969 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003970 cur_ht = ht_stack->ht;
3971 tempitem = ht_stack;
3972 ht_stack = ht_stack->prev;
3973 free(tempitem);
3974 }
3975
3976 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003977}
3978
3979/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003980 * Mark a dict and its items with "copyID".
3981 * Returns TRUE if setting references failed somehow.
3982 */
3983 int
3984set_ref_in_dict(dict_T *d, int copyID)
3985{
3986 if (d != NULL && d->dv_copyID != copyID)
3987 {
3988 d->dv_copyID = copyID;
3989 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3990 }
3991 return FALSE;
3992}
3993
3994/*
3995 * Mark a list and its items with "copyID".
3996 * Returns TRUE if setting references failed somehow.
3997 */
3998 int
3999set_ref_in_list(list_T *ll, int copyID)
4000{
4001 if (ll != NULL && ll->lv_copyID != copyID)
4002 {
4003 ll->lv_copyID = copyID;
4004 return set_ref_in_list_items(ll, copyID, NULL);
4005 }
4006 return FALSE;
4007}
4008
4009/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004010 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004011 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4012 *
4013 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004014 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004015 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004016set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004017{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004018 listitem_T *li;
4019 int abort = FALSE;
4020 list_T *cur_l;
4021 list_stack_T *list_stack = NULL;
4022 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004023
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004024 cur_l = l;
4025 for (;;)
4026 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004027 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004028 // Mark each item in the list. If the item contains a hashtab
4029 // it is added to ht_stack, if it contains a list it is added to
4030 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004031 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4032 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4033 ht_stack, &list_stack);
4034 if (list_stack == NULL)
4035 break;
4036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004037 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004038 cur_l = list_stack->list;
4039 tempitem = list_stack;
4040 list_stack = list_stack->prev;
4041 free(tempitem);
4042 }
4043
4044 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004045}
4046
4047/*
4048 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004049 * "list_stack" is used to add lists to be marked. Can be NULL.
4050 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4051 *
4052 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004053 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004055set_ref_in_item(
4056 typval_T *tv,
4057 int copyID,
4058 ht_stack_T **ht_stack,
4059 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004060{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004061 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004062
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004063 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004064 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004065 dict_T *dd = tv->vval.v_dict;
4066
Bram Moolenaara03f2332016-02-06 18:09:59 +01004067 if (dd != NULL && dd->dv_copyID != copyID)
4068 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004069 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004070 dd->dv_copyID = copyID;
4071 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004072 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004073 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4074 }
4075 else
4076 {
4077 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4078 if (newitem == NULL)
4079 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004080 else
4081 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004082 newitem->ht = &dd->dv_hashtab;
4083 newitem->prev = *ht_stack;
4084 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004085 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004086 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004087 }
4088 }
4089 else if (tv->v_type == VAR_LIST)
4090 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004091 list_T *ll = tv->vval.v_list;
4092
Bram Moolenaara03f2332016-02-06 18:09:59 +01004093 if (ll != NULL && ll->lv_copyID != copyID)
4094 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004095 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004096 ll->lv_copyID = copyID;
4097 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004098 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004099 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004100 }
4101 else
4102 {
4103 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004104 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004105 if (newitem == NULL)
4106 abort = TRUE;
4107 else
4108 {
4109 newitem->list = ll;
4110 newitem->prev = *list_stack;
4111 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004112 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004113 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004114 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004115 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004116 else if (tv->v_type == VAR_FUNC)
4117 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004118 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004119 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004120 else if (tv->v_type == VAR_PARTIAL)
4121 {
4122 partial_T *pt = tv->vval.v_partial;
4123 int i;
4124
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004125 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004126 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004127 // Didn't see this partial yet.
4128 pt->pt_copyID = copyID;
4129
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004130 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004131
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004132 if (pt->pt_dict != NULL)
4133 {
4134 typval_T dtv;
4135
4136 dtv.v_type = VAR_DICT;
4137 dtv.vval.v_dict = pt->pt_dict;
4138 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4139 }
4140
4141 for (i = 0; i < pt->pt_argc; ++i)
4142 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4143 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004144 if (pt->pt_funcstack != NULL)
4145 {
4146 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4147
4148 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4149 abort = abort || set_ref_in_item(stack + i, copyID,
4150 ht_stack, list_stack);
4151 }
4152
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004153 }
4154 }
4155#ifdef FEAT_JOB_CHANNEL
4156 else if (tv->v_type == VAR_JOB)
4157 {
4158 job_T *job = tv->vval.v_job;
4159 typval_T dtv;
4160
4161 if (job != NULL && job->jv_copyID != copyID)
4162 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004163 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004164 if (job->jv_channel != NULL)
4165 {
4166 dtv.v_type = VAR_CHANNEL;
4167 dtv.vval.v_channel = job->jv_channel;
4168 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4169 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004170 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004171 {
4172 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004173 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004174 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4175 }
4176 }
4177 }
4178 else if (tv->v_type == VAR_CHANNEL)
4179 {
4180 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004181 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004182 typval_T dtv;
4183 jsonq_T *jq;
4184 cbq_T *cq;
4185
4186 if (ch != NULL && ch->ch_copyID != copyID)
4187 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004188 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004189 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004190 {
4191 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4192 jq = jq->jq_next)
4193 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4194 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4195 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004196 if (cq->cq_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 = cq->cq_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_part[part].ch_callback.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 =
4206 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004207 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4208 }
4209 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004210 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004211 {
4212 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004213 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004214 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4215 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004216 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004217 {
4218 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004219 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004220 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4221 }
4222 }
4223 }
4224#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004225 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004226}
4227
Bram Moolenaar8c711452005-01-14 21:53:12 +00004228/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004229 * Return a string with the string representation of a variable.
4230 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004231 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004232 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004233 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4234 * stings as "string()", otherwise does not put quotes around strings, as
4235 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004236 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4237 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004238 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004239 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004240 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004241echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004242 typval_T *tv,
4243 char_u **tofree,
4244 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004245 int copyID,
4246 int echo_style,
4247 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004248 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004249{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004250 static int recurse = 0;
4251 char_u *r = NULL;
4252
Bram Moolenaar33570922005-01-25 22:26:29 +00004253 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004254 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004255 if (!did_echo_string_emsg)
4256 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004257 // Only give this message once for a recursive call to avoid
4258 // flooding the user with errors. And stop iterating over lists
4259 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004260 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004261 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004262 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004263 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004264 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004265 }
4266 ++recurse;
4267
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004268 switch (tv->v_type)
4269 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004270 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004271 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004272 {
4273 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004274 r = tv->vval.v_string;
4275 if (r == NULL)
4276 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004277 }
4278 else
4279 {
4280 *tofree = string_quote(tv->vval.v_string, FALSE);
4281 r = *tofree;
4282 }
4283 break;
4284
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004285 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004286 if (echo_style)
4287 {
4288 *tofree = NULL;
4289 r = tv->vval.v_string;
4290 }
4291 else
4292 {
4293 *tofree = string_quote(tv->vval.v_string, TRUE);
4294 r = *tofree;
4295 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004296 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004297
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004298 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004299 {
4300 partial_T *pt = tv->vval.v_partial;
4301 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004302 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004303 garray_T ga;
4304 int i;
4305 char_u *tf;
4306
4307 ga_init2(&ga, 1, 100);
4308 ga_concat(&ga, (char_u *)"function(");
4309 if (fname != NULL)
4310 {
4311 ga_concat(&ga, fname);
4312 vim_free(fname);
4313 }
4314 if (pt != NULL && pt->pt_argc > 0)
4315 {
4316 ga_concat(&ga, (char_u *)", [");
4317 for (i = 0; i < pt->pt_argc; ++i)
4318 {
4319 if (i > 0)
4320 ga_concat(&ga, (char_u *)", ");
4321 ga_concat(&ga,
4322 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4323 vim_free(tf);
4324 }
4325 ga_concat(&ga, (char_u *)"]");
4326 }
4327 if (pt != NULL && pt->pt_dict != NULL)
4328 {
4329 typval_T dtv;
4330
4331 ga_concat(&ga, (char_u *)", ");
4332 dtv.v_type = VAR_DICT;
4333 dtv.vval.v_dict = pt->pt_dict;
4334 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4335 vim_free(tf);
4336 }
4337 ga_concat(&ga, (char_u *)")");
4338
4339 *tofree = ga.ga_data;
4340 r = *tofree;
4341 break;
4342 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004343
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004344 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004345 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004346 break;
4347
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004348 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004349 if (tv->vval.v_list == NULL)
4350 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004351 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004352 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004353 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004354 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004355 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4356 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004357 {
4358 *tofree = NULL;
4359 r = (char_u *)"[...]";
4360 }
4361 else
4362 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004363 int old_copyID = tv->vval.v_list->lv_copyID;
4364
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004365 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004366 *tofree = list2string(tv, copyID, restore_copyID);
4367 if (restore_copyID)
4368 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004369 r = *tofree;
4370 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004371 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004372
Bram Moolenaar8c711452005-01-14 21:53:12 +00004373 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004374 if (tv->vval.v_dict == NULL)
4375 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004376 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004377 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004378 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004379 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004380 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4381 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004382 {
4383 *tofree = NULL;
4384 r = (char_u *)"{...}";
4385 }
4386 else
4387 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004388 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004389
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004390 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004391 *tofree = dict2string(tv, copyID, restore_copyID);
4392 if (restore_copyID)
4393 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004394 r = *tofree;
4395 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004396 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004397
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004399 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004400 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004402 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004403 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004404 break;
4405
Bram Moolenaar835dc632016-02-07 14:27:38 +01004406 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004407 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004408 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004409 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004410 if (composite_val)
4411 {
4412 *tofree = string_quote(r, FALSE);
4413 r = *tofree;
4414 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004415 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004416
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004417 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004418#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004419 *tofree = NULL;
4420 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4421 r = numbuf;
4422 break;
4423#endif
4424
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004425 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004426 case VAR_SPECIAL:
4427 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004428 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004429 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004430 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004431
Bram Moolenaar8502c702014-06-17 12:51:16 +02004432 if (--recurse == 0)
4433 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004434 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004435}
4436
4437/*
4438 * Return a string with the string representation of a variable.
4439 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4440 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004441 * Does not put quotes around strings, as ":echo" displays values.
4442 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4443 * May return NULL.
4444 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004446echo_string(
4447 typval_T *tv,
4448 char_u **tofree,
4449 char_u *numbuf,
4450 int copyID)
4451{
4452 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4453}
4454
4455/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004456 * Return string "str" in ' quotes, doubling ' characters.
4457 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004458 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004459 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004461string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004462{
Bram Moolenaar33570922005-01-25 22:26:29 +00004463 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004464 char_u *p, *r, *s;
4465
Bram Moolenaar33570922005-01-25 22:26:29 +00004466 len = (function ? 13 : 3);
4467 if (str != NULL)
4468 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004469 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004470 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004471 if (*p == '\'')
4472 ++len;
4473 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004474 s = r = alloc(len);
4475 if (r != NULL)
4476 {
4477 if (function)
4478 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004479 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004480 r += 10;
4481 }
4482 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004483 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004484 if (str != NULL)
4485 for (p = str; *p != NUL; )
4486 {
4487 if (*p == '\'')
4488 *r++ = '\'';
4489 MB_COPY_CHAR(p, r);
4490 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004491 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004492 if (function)
4493 *r++ = ')';
4494 *r++ = NUL;
4495 }
4496 return s;
4497}
4498
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004499#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004500/*
4501 * Convert the string "text" to a floating point number.
4502 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4503 * this always uses a decimal point.
4504 * Returns the length of the text that was consumed.
4505 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004506 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004507string2float(
4508 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004509 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004510{
4511 char *s = (char *)text;
4512 float_T f;
4513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004514 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004515 if (STRNICMP(text, "inf", 3) == 0)
4516 {
4517 *value = INFINITY;
4518 return 3;
4519 }
4520 if (STRNICMP(text, "-inf", 3) == 0)
4521 {
4522 *value = -INFINITY;
4523 return 4;
4524 }
4525 if (STRNICMP(text, "nan", 3) == 0)
4526 {
4527 *value = NAN;
4528 return 3;
4529 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004530 f = strtod(s, &s);
4531 *value = f;
4532 return (int)((char_u *)s - text);
4533}
4534#endif
4535
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004538 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004540 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004541var2fpos(
4542 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004543 int dollar_lnum, // TRUE when $ is last line
4544 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004546 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004548 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004550 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004551 if (varp->v_type == VAR_LIST)
4552 {
4553 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004554 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004555 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004556 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004557
4558 l = varp->vval.v_list;
4559 if (l == NULL)
4560 return NULL;
4561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004562 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004563 pos.lnum = list_find_nr(l, 0L, &error);
4564 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004565 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004567 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004568 pos.col = list_find_nr(l, 1L, &error);
4569 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004570 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004571 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004572
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004573 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004574 li = list_find(l, 1L);
4575 if (li != NULL && li->li_tv.v_type == VAR_STRING
4576 && li->li_tv.vval.v_string != NULL
4577 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4578 pos.col = len + 1;
4579
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004580 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004581 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004582 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004583 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004585 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004586 pos.coladd = list_find_nr(l, 2L, &error);
4587 if (error)
4588 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004589
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004590 return &pos;
4591 }
4592
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004593 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004594 if (name == NULL)
4595 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004596 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004598 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004599 {
4600 if (VIsual_active)
4601 return &VIsual;
4602 return &curwin->w_cursor;
4603 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004604 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004606 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4608 return NULL;
4609 return pp;
4610 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004611
Bram Moolenaara5525202006-03-02 22:52:09 +00004612 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004613
Bram Moolenaar477933c2007-07-17 14:32:23 +00004614 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004615 {
4616 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004617 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004618 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004619 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004620 // In silent Ex mode topline is zero, but that's not a valid line
4621 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004622 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004623 return &pos;
4624 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004625 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004626 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004627 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004628 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004629 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004630 return &pos;
4631 }
4632 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004633 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004635 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 {
4637 pos.lnum = curbuf->b_ml.ml_line_count;
4638 pos.col = 0;
4639 }
4640 else
4641 {
4642 pos.lnum = curwin->w_cursor.lnum;
4643 pos.col = (colnr_T)STRLEN(ml_get_curline());
4644 }
4645 return &pos;
4646 }
4647 return NULL;
4648}
4649
4650/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004651 * Convert list in "arg" into a position and optional file number.
4652 * When "fnump" is NULL there is no file number, only 3 items.
4653 * Note that the column is passed on as-is, the caller may want to decrement
4654 * it to use 1 for the first column.
4655 * Return FAIL when conversion is not possible, doesn't check the position for
4656 * validity.
4657 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004658 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004659list2fpos(
4660 typval_T *arg,
4661 pos_T *posp,
4662 int *fnump,
4663 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004664{
4665 list_T *l = arg->vval.v_list;
4666 long i = 0;
4667 long n;
4668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004669 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4670 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004671 if (arg->v_type != VAR_LIST
4672 || l == NULL
4673 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004674 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004675 return FAIL;
4676
4677 if (fnump != NULL)
4678 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004679 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004680 if (n < 0)
4681 return FAIL;
4682 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004683 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004684 *fnump = n;
4685 }
4686
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004687 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004688 if (n < 0)
4689 return FAIL;
4690 posp->lnum = n;
4691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004692 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004693 if (n < 0)
4694 return FAIL;
4695 posp->col = n;
4696
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004697 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004698 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004699 posp->coladd = 0;
4700 else
4701 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004702
Bram Moolenaar493c1782014-05-28 14:34:46 +02004703 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004704 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004705
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004706 return OK;
4707}
4708
4709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 * Get the length of an environment variable name.
4711 * Advance "arg" to the first character after the name.
4712 * Return 0 for error.
4713 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004714 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004715get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 char_u *p;
4718 int len;
4719
4720 for (p = *arg; vim_isIDc(*p); ++p)
4721 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004722 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 return 0;
4724
4725 len = (int)(p - *arg);
4726 *arg = p;
4727 return len;
4728}
4729
4730/*
4731 * Get the length of the name of a function or internal variable.
4732 * "arg" is advanced to the first non-white character after the name.
4733 * Return 0 if something is wrong.
4734 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004735 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004736get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
4738 char_u *p;
4739 int len;
4740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004741 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004743 {
4744 if (*p == ':')
4745 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004746 // "s:" is start of "s:var", but "n:" is not and can be used in
4747 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004748 len = (int)(p - *arg);
4749 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4750 || len > 1)
4751 break;
4752 }
4753 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004754 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 return 0;
4756
4757 len = (int)(p - *arg);
4758 *arg = skipwhite(p);
4759
4760 return len;
4761}
4762
4763/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004764 * Get the length of the name of a variable or function.
4765 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004767 * Return -1 if curly braces expansion failed.
4768 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 * If the name contains 'magic' {}'s, expand them and return the
4770 * expanded name in an allocated string via 'alias' - caller must free.
4771 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004773get_name_len(
4774 char_u **arg,
4775 char_u **alias,
4776 int evaluate,
4777 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
4779 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 char_u *p;
4781 char_u *expr_start;
4782 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004784 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
4786 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4787 && (*arg)[2] == (int)KE_SNR)
4788 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004789 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 *arg += 3;
4791 return get_id_len(arg) + 3;
4792 }
4793 len = eval_fname_script(*arg);
4794 if (len > 0)
4795 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004796 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 *arg += len;
4798 }
4799
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004801 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004803 p = find_name_end(*arg, &expr_start, &expr_end,
4804 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (expr_start != NULL)
4806 {
4807 char_u *temp_string;
4808
4809 if (!evaluate)
4810 {
4811 len += (int)(p - *arg);
4812 *arg = skipwhite(p);
4813 return len;
4814 }
4815
4816 /*
4817 * Include any <SID> etc in the expanded string:
4818 * Thus the -len here.
4819 */
4820 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4821 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004822 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 *alias = temp_string;
4824 *arg = skipwhite(p);
4825 return (int)STRLEN(temp_string);
4826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827
4828 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004829 // Only give an error when there is something, otherwise it will be
4830 // reported at a higher level.
4831 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004832 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833
4834 return len;
4835}
4836
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837/*
4838 * Find the end of a variable or function name, taking care of magic braces.
4839 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4840 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004841 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004842 * Return a pointer to just after the name. Equal to "arg" if there is no
4843 * valid name.
4844 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004845 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004846find_name_end(
4847 char_u *arg,
4848 char_u **expr_start,
4849 char_u **expr_end,
4850 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 int mb_nest = 0;
4853 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004855 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004856 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004858 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 *expr_start = NULL;
4861 *expr_end = NULL;
4862 }
4863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004864 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004865 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4866 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004867 return arg;
4868
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004869 for (p = arg; *p != NUL
4870 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004871 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004872 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004873 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004874 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004875 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004876 if (*p == '\'')
4877 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004878 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004879 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004880 ;
4881 if (*p == NUL)
4882 break;
4883 }
4884 else if (*p == '"')
4885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004886 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004887 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004888 if (*p == '\\' && p[1] != NUL)
4889 ++p;
4890 if (*p == NUL)
4891 break;
4892 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004893 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4894 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004895 // "s:" is start of "s:var", but "n:" is not and can be used in
4896 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004897 len = (int)(p - arg);
4898 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004899 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004900 break;
4901 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004902
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004903 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004905 if (*p == '[')
4906 ++br_nest;
4907 else if (*p == ']')
4908 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004910
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004911 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 if (*p == '{')
4914 {
4915 mb_nest++;
4916 if (expr_start != NULL && *expr_start == NULL)
4917 *expr_start = p;
4918 }
4919 else if (*p == '}')
4920 {
4921 mb_nest--;
4922 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4923 *expr_end = p;
4924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927
4928 return p;
4929}
4930
4931/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004932 * Expands out the 'magic' {}'s in a variable/function name.
4933 * Note that this can call itself recursively, to deal with
4934 * constructs like foo{bar}{baz}{bam}
4935 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4936 * "in_start" ^
4937 * "expr_start" ^
4938 * "expr_end" ^
4939 * "in_end" ^
4940 *
4941 * Returns a new allocated string, which the caller must free.
4942 * Returns NULL for failure.
4943 */
4944 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004945make_expanded_name(
4946 char_u *in_start,
4947 char_u *expr_start,
4948 char_u *expr_end,
4949 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004950{
4951 char_u c1;
4952 char_u *retval = NULL;
4953 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004954
4955 if (expr_end == NULL || in_end == NULL)
4956 return NULL;
4957 *expr_start = NUL;
4958 *expr_end = NUL;
4959 c1 = *in_end;
4960 *in_end = NUL;
4961
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004962 temp_result = eval_to_string(expr_start + 1, FALSE);
4963 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004964 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004965 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4966 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004967 if (retval != NULL)
4968 {
4969 STRCPY(retval, in_start);
4970 STRCAT(retval, temp_result);
4971 STRCAT(retval, expr_end + 1);
4972 }
4973 }
4974 vim_free(temp_result);
4975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004976 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004977 *expr_start = '{';
4978 *expr_end = '}';
4979
4980 if (retval != NULL)
4981 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004982 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004983 if (expr_start != NULL)
4984 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004985 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004986 temp_result = make_expanded_name(retval, expr_start,
4987 expr_end, temp_result);
4988 vim_free(retval);
4989 retval = temp_result;
4990 }
4991 }
4992
4993 return retval;
4994}
4995
4996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004998 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005001eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005003 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5004}
5005
5006/*
5007 * Return TRUE if character "c" can be used as the first character in a
5008 * variable or function name (excluding '{' and '}').
5009 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005010 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005011eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005012{
5013 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014}
5015
5016/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005017 * Handle:
5018 * - expr[expr], expr[expr:expr] subscript
5019 * - ".name" lookup
5020 * - function call with Funcref variable: func(expr)
5021 * - method call: var->method()
5022 *
5023 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005024 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005026handle_subscript(
5027 char_u **arg,
5028 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005029 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005030 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005031{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005032 int evaluate = evalarg != NULL
5033 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005034 int ret = OK;
5035 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005036 int check_white = TRUE;
5037
5038 // When at the end of the line and ".name" follows in the next line then
5039 // consume the line break. Only when rettv is a dict.
5040 if (rettv->v_type == VAR_DICT)
5041 {
5042 int getnext;
5043 char_u *p = eval_next_non_blank(*arg, evalarg, &getnext);
5044
5045 if (getnext && *p == '.' && ASCII_ISALPHA(p[1]))
5046 {
5047 *arg = eval_next_line(evalarg);
5048 check_white = FALSE;
5049 }
5050 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005051
Bram Moolenaar61343f02019-07-20 21:11:13 +02005052 // "." is ".name" lookup when we found a dict or when evaluating and
5053 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005054 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005055 && (((**arg == '['
5056 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005057 || (!evaluate
5058 && (*arg)[1] != '.'
5059 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005060 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005061 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005062 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005063 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005064 {
5065 if (**arg == '(')
5066 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005067 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5068 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005069
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005070 // Stop the expression evaluation when immediately aborting on
5071 // error, or when an interrupt occurred or an exception was thrown
5072 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005073 if (aborting())
5074 {
5075 if (ret == OK)
5076 clear_tv(rettv);
5077 ret = FAIL;
5078 }
5079 dict_unref(selfdict);
5080 selfdict = NULL;
5081 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005082 else if (**arg == '-')
5083 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005084 if (ret == OK)
5085 {
5086 if ((*arg)[2] == '{')
5087 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005088 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005089 else
5090 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005091 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005092 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005093 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005094 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005095 {
5096 dict_unref(selfdict);
5097 if (rettv->v_type == VAR_DICT)
5098 {
5099 selfdict = rettv->vval.v_dict;
5100 if (selfdict != NULL)
5101 ++selfdict->dv_refcount;
5102 }
5103 else
5104 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005105 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005106 {
5107 clear_tv(rettv);
5108 ret = FAIL;
5109 }
5110 }
5111 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005112
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005113 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5114 // Don't do this when "Func" is already a partial that was bound
5115 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005116 if (selfdict != NULL
5117 && (rettv->v_type == VAR_FUNC
5118 || (rettv->v_type == VAR_PARTIAL
5119 && (rettv->vval.v_partial->pt_auto
5120 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005121 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005122
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005123 dict_unref(selfdict);
5124 return ret;
5125}
5126
5127/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005128 * Make a copy of an item.
5129 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005130 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5131 * reference to an already copied list/dict can be used.
5132 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005133 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005134 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005135item_copy(
5136 typval_T *from,
5137 typval_T *to,
5138 int deep,
5139 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005140{
5141 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005142 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005143
Bram Moolenaar33570922005-01-25 22:26:29 +00005144 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005146 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005147 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005148 }
5149 ++recurse;
5150
5151 switch (from->v_type)
5152 {
5153 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005154 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005155 case VAR_STRING:
5156 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005157 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005158 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005159 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005160 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005161 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005162 copy_tv(from, to);
5163 break;
5164 case VAR_LIST:
5165 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005166 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005167 if (from->vval.v_list == NULL)
5168 to->vval.v_list = NULL;
5169 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005171 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005172 to->vval.v_list = from->vval.v_list->lv_copylist;
5173 ++to->vval.v_list->lv_refcount;
5174 }
5175 else
5176 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5177 if (to->vval.v_list == NULL)
5178 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005179 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005180 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005182 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005183 case VAR_DICT:
5184 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005185 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005186 if (from->vval.v_dict == NULL)
5187 to->vval.v_dict = NULL;
5188 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5189 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005190 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005191 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5192 ++to->vval.v_dict->dv_refcount;
5193 }
5194 else
5195 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5196 if (to->vval.v_dict == NULL)
5197 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005198 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005199 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005200 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005202 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005203 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005204 }
5205 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005206 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005207}
5208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209 void
5210echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5211{
5212 char_u *tofree;
5213 char_u numbuf[NUMBUFLEN];
5214 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5215
5216 if (*atstart)
5217 {
5218 *atstart = FALSE;
5219 // Call msg_start() after eval1(), evaluating the expression
5220 // may cause a message to appear.
5221 if (with_space)
5222 {
5223 // Mark the saved text as finishing the line, so that what
5224 // follows is displayed on a new line when scrolling back
5225 // at the more prompt.
5226 msg_sb_eol();
5227 msg_start();
5228 }
5229 }
5230 else if (with_space)
5231 msg_puts_attr(" ", echo_attr);
5232
5233 if (p != NULL)
5234 for ( ; *p != NUL && !got_int; ++p)
5235 {
5236 if (*p == '\n' || *p == '\r' || *p == TAB)
5237 {
5238 if (*p != TAB && *needclr)
5239 {
5240 // remove any text still there from the command
5241 msg_clr_eos();
5242 *needclr = FALSE;
5243 }
5244 msg_putchar_attr(*p, echo_attr);
5245 }
5246 else
5247 {
5248 if (has_mbyte)
5249 {
5250 int i = (*mb_ptr2len)(p);
5251
5252 (void)msg_outtrans_len_attr(p, i, echo_attr);
5253 p += i - 1;
5254 }
5255 else
5256 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5257 }
5258 }
5259 vim_free(tofree);
5260}
5261
Bram Moolenaare9a41262005-01-15 22:18:47 +00005262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 * ":echo expr1 ..." print each argument separated with a space, add a
5264 * newline at the end.
5265 * ":echon expr1 ..." print each argument plain.
5266 */
5267 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005268ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269{
5270 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005271 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 char_u *p;
5273 int needclr = TRUE;
5274 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005275 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005276 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005277 evalarg_T evalarg;
5278
Bram Moolenaare707c882020-07-01 13:07:37 +02005279 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
5281 if (eap->skip)
5282 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005283 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005285 // If eval1() causes an error message the text from the command may
5286 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005287 need_clr_eos = needclr;
5288
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005290 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
5292 /*
5293 * Report the invalid expression unless the expression evaluation
5294 * has been cancelled due to an aborting error, an interrupt, or an
5295 * exception.
5296 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005297 if (!aborting() && did_emsg == did_emsg_before
5298 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005299 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005300 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 break;
5302 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 need_clr_eos = FALSE;
5304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005307
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005308 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 arg = skipwhite(arg);
5310 }
5311 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005312 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
5314 if (eap->skip)
5315 --emsg_skip;
5316 else
5317 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005318 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 if (needclr)
5320 msg_clr_eos();
5321 if (eap->cmdidx == CMD_echo)
5322 msg_end();
5323 }
5324}
5325
5326/*
5327 * ":echohl {name}".
5328 */
5329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005330ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005332 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333}
5334
5335/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005336 * Returns the :echo attribute
5337 */
5338 int
5339get_echo_attr(void)
5340{
5341 return echo_attr;
5342}
5343
5344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 * ":execute expr1 ..." execute the result of an expression.
5346 * ":echomsg expr1 ..." Print a message
5347 * ":echoerr expr1 ..." Print an error
5348 * Each gets spaces around each argument and a newline at the end for
5349 * echo commands
5350 */
5351 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005352ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005355 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 int ret = OK;
5357 char_u *p;
5358 garray_T ga;
5359 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360
5361 ga_init2(&ga, 1, 80);
5362
5363 if (eap->skip)
5364 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005365 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005367 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005368 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
5371 if (!eap->skip)
5372 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005373 char_u buf[NUMBUFLEN];
5374
5375 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005376 {
5377 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5378 {
5379 emsg(_(e_inval_string));
5380 p = NULL;
5381 }
5382 else
5383 p = tv_get_string_buf(&rettv, buf);
5384 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005385 else
5386 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005387 if (p == NULL)
5388 {
5389 clear_tv(&rettv);
5390 ret = FAIL;
5391 break;
5392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 len = (int)STRLEN(p);
5394 if (ga_grow(&ga, len + 2) == FAIL)
5395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005396 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 ret = FAIL;
5398 break;
5399 }
5400 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 ga.ga_len += len;
5404 }
5405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005406 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 arg = skipwhite(arg);
5408 }
5409
5410 if (ret != FAIL && ga.ga_data != NULL)
5411 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005412 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5413 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 // Mark the already saved text as finishing the line, so that what
5415 // follows is displayed on a new line when scrolling back at the
5416 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005417 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005418 }
5419
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005421 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005422 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005423 out_flush();
5424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 else if (eap->cmdidx == CMD_echoerr)
5426 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005427 int save_did_emsg = did_emsg;
5428
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005429 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005430 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 if (!force_abort)
5432 did_emsg = save_did_emsg;
5433 }
5434 else if (eap->cmdidx == CMD_execute)
5435 do_cmdline((char_u *)ga.ga_data,
5436 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5437 }
5438
5439 ga_clear(&ga);
5440
5441 if (eap->skip)
5442 --emsg_skip;
5443
5444 eap->nextcmd = check_nextcmd(arg);
5445}
5446
5447/*
5448 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5449 * "arg" points to the "&" or '+' when called, to "option" when returning.
5450 * Returns NULL when no option name found. Otherwise pointer to the char
5451 * after the option name.
5452 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005453 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005454find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 char_u *p = *arg;
5457
5458 ++p;
5459 if (*p == 'g' && p[1] == ':')
5460 {
5461 *opt_flags = OPT_GLOBAL;
5462 p += 2;
5463 }
5464 else if (*p == 'l' && p[1] == ':')
5465 {
5466 *opt_flags = OPT_LOCAL;
5467 p += 2;
5468 }
5469 else
5470 *opt_flags = 0;
5471
5472 if (!ASCII_ISALPHA(*p))
5473 return NULL;
5474 *arg = p;
5475
5476 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005477 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 else
5479 while (ASCII_ISALPHA(*p))
5480 ++p;
5481 return p;
5482}
5483
5484/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005485 * Display script name where an item was last set.
5486 * Should only be invoked when 'verbose' is non-zero.
5487 */
5488 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005489last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005490{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005491 char_u *p;
5492
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005493 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005494 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005495 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005496 if (p != NULL)
5497 {
5498 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005499 msg_puts(_("\n\tLast set from "));
5500 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005501 if (script_ctx.sc_lnum > 0)
5502 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005503 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005504 msg_outnum((long)script_ctx.sc_lnum);
5505 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005506 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005507 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005508 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005509 }
5510}
5511
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005512#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
5514/*
5515 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005516 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 * "flags" can be "g" to do a global substitute.
5518 * Returns an allocated string, NULL for error.
5519 */
5520 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005521do_string_sub(
5522 char_u *str,
5523 char_u *pat,
5524 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005525 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005526 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
5528 int sublen;
5529 regmatch_T regmatch;
5530 int i;
5531 int do_all;
5532 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005533 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 garray_T ga;
5535 char_u *ret;
5536 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005537 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005539 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005541 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
5543 ga_init2(&ga, 1, 200);
5544
5545 do_all = (flags[0] == 'g');
5546
5547 regmatch.rm_ic = p_ic;
5548 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5549 if (regmatch.regprog != NULL)
5550 {
5551 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005552 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5554 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005555 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005556 if (regmatch.startp[0] == regmatch.endp[0])
5557 {
5558 if (zero_width == regmatch.startp[0])
5559 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005560 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005561 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005562 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5563 (size_t)i);
5564 ga.ga_len += i;
5565 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005566 continue;
5567 }
5568 zero_width = regmatch.startp[0];
5569 }
5570
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 /*
5572 * Get some space for a temporary buffer to do the substitution
5573 * into. It will contain:
5574 * - The text up to where the match is.
5575 * - The substituted text.
5576 * - The text after the match.
5577 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005578 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005579 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5581 {
5582 ga_clear(&ga);
5583 break;
5584 }
5585
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005586 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 i = (int)(regmatch.startp[0] - tail);
5588 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005589 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005590 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 + ga.ga_len + i, TRUE, TRUE, FALSE);
5592 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005593 tail = regmatch.endp[0];
5594 if (*tail == NUL)
5595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 if (!do_all)
5597 break;
5598 }
5599
5600 if (ga.ga_data != NULL)
5601 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5602
Bram Moolenaar473de612013-06-08 18:19:48 +02005603 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 }
5605
5606 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5607 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005608 if (p_cpo == empty_option)
5609 p_cpo = save_cpo;
5610 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005611 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005612 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614 return ret;
5615}