blob: ce255e2b967bf66f4658884c738266d1ef13364d [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;
1227 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1228 &tv, &di, TRUE, FALSE) == OK)
1229 {
1230 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001231 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1232 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001233 && tv_op(&tv, rettv, op) == OK)
1234 set_var(lp->ll_name, &tv, FALSE);
1235 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001236 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001238 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001240 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001242 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001243 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001244 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001245 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 else if (lp->ll_range)
1247 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001248 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001249 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001252 {
1253 emsg(_("E996: Cannot lock a range"));
1254 return;
1255 }
1256
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001257 /*
1258 * Check whether any of the list items is locked
1259 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001260 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001261 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001262 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001263 return;
1264 ri = ri->li_next;
1265 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1266 break;
1267 ll_li = ll_li->li_next;
1268 ++ll_n1;
1269 }
1270
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 /*
1272 * Assign the List values to the list items.
1273 */
1274 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001275 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001276 if (op != NULL && *op != '=')
1277 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1278 else
1279 {
1280 clear_tv(&lp->ll_li->li_tv);
1281 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1282 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001283 ri = ri->li_next;
1284 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1285 break;
1286 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001287 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001288 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001289 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001290 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001291 ri = NULL;
1292 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001293 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001294 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 lp->ll_li = lp->ll_li->li_next;
1296 ++lp->ll_n1;
1297 }
1298 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001299 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001300 else if (lp->ll_empty2
1301 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001302 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001303 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304 }
1305 else
1306 {
1307 /*
1308 * Assign to a List or Dictionary item.
1309 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001311 {
1312 emsg(_("E996: Cannot lock a list or dict"));
1313 return;
1314 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315 if (lp->ll_newkey != NULL)
1316 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001317 if (op != NULL && *op != '=')
1318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001319 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001320 return;
1321 }
1322
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001323 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001324 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 if (di == NULL)
1326 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001327 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1328 {
1329 vim_free(di);
1330 return;
1331 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001333 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334 else if (op != NULL && *op != '=')
1335 {
1336 tv_op(lp->ll_tv, rettv, op);
1337 return;
1338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001341
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 /*
1343 * Assign the value to the variable or list item.
1344 */
1345 if (copy)
1346 copy_tv(rettv, lp->ll_tv);
1347 else
1348 {
1349 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001350 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001351 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 }
1353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354}
1355
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001356/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001357 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1358 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359 * Returns OK or FAIL.
1360 */
1361 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001362tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001363{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001364 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001365 char_u numbuf[NUMBUFLEN];
1366 char_u *s;
1367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001368 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001369 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001370 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001371 {
1372 switch (tv1->v_type)
1373 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001374 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001375 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001377 case VAR_DICT:
1378 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001379 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001380 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001381 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001382 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001383 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001384 break;
1385
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001386 case VAR_BLOB:
1387 if (*op != '+' || tv2->v_type != VAR_BLOB)
1388 break;
1389 // BLOB += BLOB
1390 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1391 {
1392 blob_T *b1 = tv1->vval.v_blob;
1393 blob_T *b2 = tv2->vval.v_blob;
1394 int i, len = blob_len(b2);
1395 for (i = 0; i < len; i++)
1396 ga_append(&b1->bv_ga, blob_get(b2, i));
1397 }
1398 return OK;
1399
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001400 case VAR_LIST:
1401 if (*op != '+' || tv2->v_type != VAR_LIST)
1402 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001403 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001404 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1405 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1406 return OK;
1407
1408 case VAR_NUMBER:
1409 case VAR_STRING:
1410 if (tv2->v_type == VAR_LIST)
1411 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001412 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001413 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001414 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001415 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001416#ifdef FEAT_FLOAT
1417 if (tv2->v_type == VAR_FLOAT)
1418 {
1419 float_T f = n;
1420
Bram Moolenaarff697e62019-02-12 22:28:33 +01001421 if (*op == '%')
1422 break;
1423 switch (*op)
1424 {
1425 case '+': f += tv2->vval.v_float; break;
1426 case '-': f -= tv2->vval.v_float; break;
1427 case '*': f *= tv2->vval.v_float; break;
1428 case '/': f /= tv2->vval.v_float; break;
1429 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001430 clear_tv(tv1);
1431 tv1->v_type = VAR_FLOAT;
1432 tv1->vval.v_float = f;
1433 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001434 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001435#endif
1436 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001437 switch (*op)
1438 {
1439 case '+': n += tv_get_number(tv2); break;
1440 case '-': n -= tv_get_number(tv2); break;
1441 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001442 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1443 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001444 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001445 clear_tv(tv1);
1446 tv1->v_type = VAR_NUMBER;
1447 tv1->vval.v_number = n;
1448 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 }
1450 else
1451 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001452 if (tv2->v_type == VAR_FLOAT)
1453 break;
1454
Bram Moolenaarff697e62019-02-12 22:28:33 +01001455 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001456 s = tv_get_string(tv1);
1457 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458 clear_tv(tv1);
1459 tv1->v_type = VAR_STRING;
1460 tv1->vval.v_string = s;
1461 }
1462 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001463
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001464 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001465#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001466 {
1467 float_T f;
1468
Bram Moolenaarff697e62019-02-12 22:28:33 +01001469 if (*op == '%' || *op == '.'
1470 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001471 && tv2->v_type != VAR_NUMBER
1472 && tv2->v_type != VAR_STRING))
1473 break;
1474 if (tv2->v_type == VAR_FLOAT)
1475 f = tv2->vval.v_float;
1476 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001477 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001478 switch (*op)
1479 {
1480 case '+': tv1->vval.v_float += f; break;
1481 case '-': tv1->vval.v_float -= f; break;
1482 case '*': tv1->vval.v_float *= f; break;
1483 case '/': tv1->vval.v_float /= f; break;
1484 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001485 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001486#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001487 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001488 }
1489 }
1490
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001491 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492 return FAIL;
1493}
1494
1495/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001496 * Evaluate the expression used in a ":for var in expr" command.
1497 * "arg" points to "var".
1498 * Set "*errp" to TRUE for an error, FALSE otherwise;
1499 * Return a pointer that holds the info. Null when there is an error.
1500 */
1501 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502eval_for_line(
1503 char_u *arg,
1504 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001505 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001506 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001507{
Bram Moolenaar33570922005-01-25 22:26:29 +00001508 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001509 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001510 typval_T tv;
1511 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001512 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001514 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001515
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001516 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517 if (fi == NULL)
1518 return NULL;
1519
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001520 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001521 if (expr == NULL)
1522 return fi;
1523
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001524 expr = skipwhite_and_linebreak(expr, evalarg);
1525 if (expr[0] != 'i' || expr[1] != 'n'
1526 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001527 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001529 return fi;
1530 }
1531
1532 if (skip)
1533 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001534 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1535 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001536 {
1537 *errp = FALSE;
1538 if (!skip)
1539 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001540 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001541 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001542 l = tv.vval.v_list;
1543 if (l == NULL)
1544 {
1545 // a null list is like an empty list: do nothing
1546 clear_tv(&tv);
1547 }
1548 else
1549 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001551 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001553 // No need to increment the refcount, it's already set for
1554 // the list being used in "tv".
1555 fi->fi_list = l;
1556 list_add_watch(l, &fi->fi_lw);
1557 fi->fi_lw.lw_item = l->lv_first;
1558 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001559 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001560 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001561 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001562 fi->fi_bi = 0;
1563 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001564 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001565 typval_T btv;
1566
1567 // Make a copy, so that the iteration still works when the
1568 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001570 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001571 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001572 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001573 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001574 else
1575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001576 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001577 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001578 }
1579 }
1580 }
1581 if (skip)
1582 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001583 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001584
1585 return fi;
1586}
1587
1588/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001589 * Used when looping over a :for line, skip the "in expr" part.
1590 */
1591 void
1592skip_for_lines(void *fi_void, evalarg_T *evalarg)
1593{
1594 forinfo_T *fi = (forinfo_T *)fi_void;
1595 int i;
1596
1597 for (i = 0; i < fi->fi_break_count; ++i)
1598 eval_next_line(evalarg);
1599}
1600
1601/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001602 * Use the first item in a ":for" list. Advance to the next.
1603 * Assign the values to the variable (list). "arg" points to the first one.
1604 * Return TRUE when a valid item was found, FALSE when at end of list or
1605 * something wrong.
1606 */
1607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001608next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001609{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001610 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001611 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001612 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1613 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001614 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001615
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001616 if (fi->fi_blob != NULL)
1617 {
1618 typval_T tv;
1619
1620 if (fi->fi_bi >= blob_len(fi->fi_blob))
1621 return FALSE;
1622 tv.v_type = VAR_NUMBER;
1623 tv.v_lock = VAR_FIXED;
1624 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1625 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001626 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001627 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001628 }
1629
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630 item = fi->fi_lw.lw_item;
1631 if (item == NULL)
1632 result = FALSE;
1633 else
1634 {
1635 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001636 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001637 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638 }
1639 return result;
1640}
1641
1642/*
1643 * Free the structure used to store info used by ":for".
1644 */
1645 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001647{
Bram Moolenaar33570922005-01-25 22:26:29 +00001648 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001649
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001650 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001651 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001653 list_unref(fi->fi_list);
1654 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001655 if (fi != NULL && fi->fi_blob != NULL)
1656 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657 vim_free(fi);
1658}
1659
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001661set_context_for_expression(
1662 expand_T *xp,
1663 char_u *arg,
1664 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
1666 int got_eq = FALSE;
1667 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001670 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 {
1672 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001673 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001675 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001676 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 {
1678 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001679 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001680 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 break;
1682 }
1683 return;
1684 }
1685 }
1686 else
1687 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1688 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 while ((xp->xp_pattern = vim_strpbrk(arg,
1690 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1691 {
1692 c = *xp->xp_pattern;
1693 if (c == '&')
1694 {
1695 c = xp->xp_pattern[1];
1696 if (c == '&')
1697 {
1698 ++xp->xp_pattern;
1699 xp->xp_context = cmdidx != CMD_let || got_eq
1700 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1701 }
1702 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001705 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1706 xp->xp_pattern += 2;
1707
1708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
1710 else if (c == '$')
1711 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001712 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 xp->xp_context = EXPAND_ENV_VARS;
1714 }
1715 else if (c == '=')
1716 {
1717 got_eq = TRUE;
1718 xp->xp_context = EXPAND_EXPRESSION;
1719 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001720 else if (c == '#'
1721 && xp->xp_context == EXPAND_EXPRESSION)
1722 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001723 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001724 break;
1725 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001726 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 && xp->xp_context == EXPAND_FUNCTIONS
1728 && vim_strchr(xp->xp_pattern, '(') == NULL)
1729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001730 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 break;
1732 }
1733 else if (cmdidx != CMD_let || got_eq)
1734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001735 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
1737 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1738 if (c == '\\' && xp->xp_pattern[1] != NUL)
1739 ++xp->xp_pattern;
1740 xp->xp_context = EXPAND_NOTHING;
1741 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001742 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001744 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1746 /* skip */ ;
1747 xp->xp_context = EXPAND_NOTHING;
1748 }
1749 else if (c == '|')
1750 {
1751 if (xp->xp_pattern[1] == '|')
1752 {
1753 ++xp->xp_pattern;
1754 xp->xp_context = EXPAND_EXPRESSION;
1755 }
1756 else
1757 xp->xp_context = EXPAND_COMMANDS;
1758 }
1759 else
1760 xp->xp_context = EXPAND_EXPRESSION;
1761 }
1762 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001763 // Doesn't look like something valid, expand as an expression
1764 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 arg = xp->xp_pattern;
1767 if (*arg != NUL)
1768 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1769 /* skip */ ;
1770 }
1771 xp->xp_pattern = arg;
1772}
1773
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001775 * Return TRUE if "pat" matches "text".
1776 * Does not use 'cpo' and always uses 'magic'.
1777 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001778 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001779pattern_match(char_u *pat, char_u *text, int ic)
1780{
1781 int matches = FALSE;
1782 char_u *save_cpo;
1783 regmatch_T regmatch;
1784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001785 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001786 save_cpo = p_cpo;
1787 p_cpo = (char_u *)"";
1788 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1789 if (regmatch.regprog != NULL)
1790 {
1791 regmatch.rm_ic = ic;
1792 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1793 vim_regfree(regmatch.regprog);
1794 }
1795 p_cpo = save_cpo;
1796 return matches;
1797}
1798
1799/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001800 * Handle a name followed by "(". Both for just "name(arg)" and for
1801 * "expr->name(arg)".
1802 * Returns OK or FAIL.
1803 */
1804 static int
1805eval_func(
1806 char_u **arg, // points to "(", will be advanced
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 Moolenaar5409f5d2020-06-24 18:37:35 +02001869 * If inside Vim9 script, "arg" points to the end of a line (ignoring comments)
1870 * and there is a next line, return the next line (skipping blanks) and set
1871 * "getnext".
1872 * 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 Moolenaard5053d02020-06-28 15:51:16 +02001883 && (*arg == '"' || *arg == '#'))))
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
1930 eval_next_non_blank(p, evalarg, &getnext);
1931 if (getnext)
1932 return eval_next_line(evalarg);
1933 return p;
1934}
1935
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001936/*
Bram Moolenaare6b53242020-07-01 17:28:33 +02001937 * Call eval_next_non_blank() and get the next line if needed, but not when a
1938 * double quote follows. Used inside an expression.
1939 */
1940 char_u *
1941skipwhite_and_linebreak_keep_string(char_u *arg, evalarg_T *evalarg)
1942{
1943 char_u *p = skipwhite(arg);
1944
1945 if (*p == '"')
1946 return p;
1947 return skipwhite_and_linebreak(arg, evalarg);
1948}
1949
1950/*
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001951 * After using "evalarg" filled from "eap" free the memory.
1952 */
1953 void
1954clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
1955{
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001956 if (evalarg != NULL && evalarg->eval_tofree != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001957 {
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001958 if (eap != NULL)
1959 {
1960 // We may need to keep the original command line, e.g. for
1961 // ":let" it has the variable names. But we may also need the
1962 // new one, "nextcmd" points into it. Keep both.
1963 vim_free(eap->cmdline_tofree);
1964 eap->cmdline_tofree = *eap->cmdlinep;
1965 *eap->cmdlinep = evalarg->eval_tofree;
1966 }
1967 else
1968 vim_free(evalarg->eval_tofree);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001969 evalarg->eval_tofree = NULL;
1970 }
1971}
1972
1973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001975 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1977 */
1978
1979/*
1980 * Handle zero level expression.
1981 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001982 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001983 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001984 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 * Return OK or FAIL.
1986 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001988eval0(
1989 char_u *arg,
1990 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001991 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001992 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
1994 int ret;
1995 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001996 int did_emsg_before = did_emsg;
1997 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001998 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002001 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002002
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002003 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 {
2005 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002006 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 /*
2008 * Report the invalid expression unless the expression evaluation has
2009 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002010 * exception, or we already gave a more specific error.
2011 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002013 if (!aborting()
2014 && did_emsg == did_emsg_before
2015 && called_emsg == called_emsg_before
2016 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002017 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 ret = FAIL;
2019 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002020
2021 if (eap != NULL)
2022 eap->nextcmd = check_nextcmd(p);
2023
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 return ret;
2025}
2026
2027/*
2028 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002029 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 *
2031 * "arg" must point to the first non-white of the expression.
2032 * "arg" is advanced to the next non-white after the recognized expression.
2033 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002034 * Note: "rettv.v_lock" is not set.
2035 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 * Return OK or FAIL.
2037 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002038 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002039eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002041 char_u *p;
2042 int getnext;
2043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 /*
2045 * Get the first variable.
2046 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002047 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 return FAIL;
2049
Bram Moolenaar793648f2020-06-26 21:28:25 +02002050 p = eval_next_non_blank(*arg, evalarg, &getnext);
2051 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002053 int result;
2054 typval_T var2;
2055 evalarg_T nested_evalarg;
2056 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002057 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002058
Bram Moolenaar793648f2020-06-26 21:28:25 +02002059 if (getnext)
2060 *arg = eval_next_line(evalarg);
2061
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002062 if (evalarg == NULL)
2063 {
2064 CLEAR_FIELD(nested_evalarg);
2065 orig_flags = 0;
2066 }
2067 else
2068 {
2069 nested_evalarg = *evalarg;
2070 orig_flags = evalarg->eval_flags;
2071 }
2072
Bram Moolenaar7acde512020-06-24 23:02:40 +02002073 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002075 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002077 int error = FALSE;
2078
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002079 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002082 if (error)
2083 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
2085
2086 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002087 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002089 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090 nested_evalarg.eval_flags = result ? orig_flags
2091 : orig_flags & ~EVAL_EVALUATE;
2092 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 return FAIL;
2094
2095 /*
2096 * Check for the ":".
2097 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002098 p = eval_next_non_blank(*arg, evalarg, &getnext);
2099 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002103 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 return FAIL;
2105 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002106 if (getnext)
2107 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108
2109 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002110 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002112 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002113 nested_evalarg.eval_flags = !result ? orig_flags
2114 : orig_flags & ~EVAL_EVALUATE;
2115 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
2117 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002118 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 return FAIL;
2120 }
2121 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002122 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
2124
2125 return OK;
2126}
2127
2128/*
2129 * Handle first level expression:
2130 * expr2 || expr2 || expr2 logical OR
2131 *
2132 * "arg" must point to the first non-white of the expression.
2133 * "arg" is advanced to the next non-white after the recognized expression.
2134 *
2135 * Return OK or FAIL.
2136 */
2137 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002138eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002140 char_u *p;
2141 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002142 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 long result;
2144 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002145 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
2147 /*
2148 * Get the first variable.
2149 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002150 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 return FAIL;
2152
2153 /*
2154 * Repeat until there is no following "||".
2155 */
2156 first = TRUE;
2157 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002158 p = eval_next_non_blank(*arg, evalarg, &getnext);
2159 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002161 evalarg_T nested_evalarg;
2162 int evaluate;
2163 int orig_flags;
2164
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002165 if (getnext)
2166 *arg = eval_next_line(evalarg);
2167
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002168 if (evalarg == NULL)
2169 {
2170 CLEAR_FIELD(nested_evalarg);
2171 orig_flags = 0;
2172 evaluate = FALSE;
2173 }
2174 else
2175 {
2176 nested_evalarg = *evalarg;
2177 orig_flags = evalarg->eval_flags;
2178 evaluate = orig_flags & EVAL_EVALUATE;
2179 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002180
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (evaluate && first)
2182 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002183 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002186 if (error)
2187 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 first = FALSE;
2189 }
2190
2191 /*
2192 * Get the second variable.
2193 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002194 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002195 nested_evalarg.eval_flags = !result ? orig_flags
2196 : orig_flags & ~EVAL_EVALUATE;
2197 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 return FAIL;
2199
2200 /*
2201 * Compute the result.
2202 */
2203 if (evaluate && !result)
2204 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002205 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002208 if (error)
2209 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 }
2211 if (evaluate)
2212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 rettv->v_type = VAR_NUMBER;
2214 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002216
2217 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 }
2219
2220 return OK;
2221}
2222
2223/*
2224 * Handle second level expression:
2225 * expr3 && expr3 && expr3 logical AND
2226 *
2227 * "arg" must point to the first non-white of the expression.
2228 * "arg" is advanced to the next non-white after the recognized expression.
2229 *
2230 * Return OK or FAIL.
2231 */
2232 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002233eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002235 char_u *p;
2236 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002237 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 long result;
2239 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002240 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241
2242 /*
2243 * Get the first variable.
2244 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002245 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 return FAIL;
2247
2248 /*
2249 * Repeat until there is no following "&&".
2250 */
2251 first = TRUE;
2252 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002253 p = eval_next_non_blank(*arg, evalarg, &getnext);
2254 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002256 evalarg_T nested_evalarg;
2257 int orig_flags;
2258 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002259
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002260 if (getnext)
2261 *arg = eval_next_line(evalarg);
2262
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002263 if (evalarg == NULL)
2264 {
2265 CLEAR_FIELD(nested_evalarg);
2266 orig_flags = 0;
2267 evaluate = FALSE;
2268 }
2269 else
2270 {
2271 nested_evalarg = *evalarg;
2272 orig_flags = evalarg->eval_flags;
2273 evaluate = orig_flags & EVAL_EVALUATE;
2274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (evaluate && first)
2276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002277 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002280 if (error)
2281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 first = FALSE;
2283 }
2284
2285 /*
2286 * Get the second variable.
2287 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002288 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002289 nested_evalarg.eval_flags = result ? orig_flags
2290 : orig_flags & ~EVAL_EVALUATE;
2291 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 return FAIL;
2293
2294 /*
2295 * Compute the result.
2296 */
2297 if (evaluate && result)
2298 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002299 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002302 if (error)
2303 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305 if (evaluate)
2306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 rettv->v_type = VAR_NUMBER;
2308 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002310
2311 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
2313
2314 return OK;
2315}
2316
2317/*
2318 * Handle third level expression:
2319 * var1 == var2
2320 * var1 =~ var2
2321 * var1 != var2
2322 * var1 !~ var2
2323 * var1 > var2
2324 * var1 >= var2
2325 * var1 < var2
2326 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002327 * var1 is var2
2328 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 *
2330 * "arg" must point to the first non-white of the expression.
2331 * "arg" is advanced to the next non-white after the recognized expression.
2332 *
2333 * Return OK or FAIL.
2334 */
2335 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002336eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337{
Bram Moolenaar33570922005-01-25 22:26:29 +00002338 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002340 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002342 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
2346 /*
2347 * Get the first variable.
2348 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002349 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 return FAIL;
2351
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002352 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 switch (p[0])
2354 {
2355 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002356 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002358 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 break;
2360 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002361 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002363 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 break;
2365 case '>': if (p[1] != '=')
2366 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002367 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 len = 1;
2369 }
2370 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002371 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 break;
2373 case '<': if (p[1] != '=')
2374 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002375 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 len = 1;
2377 }
2378 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002379 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002381 case 'i': if (p[1] == 's')
2382 {
2383 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2384 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002385 i = p[len];
2386 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002387 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002388 }
2389 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
2391
2392 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002393 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002395 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002397 if (getnext)
2398 *arg = eval_next_line(evalarg);
2399
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002400 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 if (p[len] == '?')
2402 {
2403 ic = TRUE;
2404 ++len;
2405 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002406 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 else if (p[len] == '#')
2408 {
2409 ic = FALSE;
2410 ++len;
2411 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002412 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 else
2414 ic = p_ic;
2415
2416 /*
2417 * Get the second variable.
2418 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002419 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002420 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 return FAIL;
2424 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002425 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002426 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002427 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002428
2429 clear_tv(&var2);
2430 return ret;
2431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433
2434 return OK;
2435}
2436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 void
2438eval_addblob(typval_T *tv1, typval_T *tv2)
2439{
2440 blob_T *b1 = tv1->vval.v_blob;
2441 blob_T *b2 = tv2->vval.v_blob;
2442 blob_T *b = blob_alloc();
2443 int i;
2444
2445 if (b != NULL)
2446 {
2447 for (i = 0; i < blob_len(b1); i++)
2448 ga_append(&b->bv_ga, blob_get(b1, i));
2449 for (i = 0; i < blob_len(b2); i++)
2450 ga_append(&b->bv_ga, blob_get(b2, i));
2451
2452 clear_tv(tv1);
2453 rettv_blob_set(tv1, b);
2454 }
2455}
2456
2457 int
2458eval_addlist(typval_T *tv1, typval_T *tv2)
2459{
2460 typval_T var3;
2461
2462 // concatenate Lists
2463 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2464 {
2465 clear_tv(tv1);
2466 clear_tv(tv2);
2467 return FAIL;
2468 }
2469 clear_tv(tv1);
2470 *tv1 = var3;
2471 return OK;
2472}
2473
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474/*
2475 * Handle fourth level expression:
2476 * + number addition
2477 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002478 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002479 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 *
2481 * "arg" must point to the first non-white of the expression.
2482 * "arg" is advanced to the next non-white after the recognized expression.
2483 *
2484 * Return OK or FAIL.
2485 */
2486 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002487eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002489 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
2491 /*
2492 * Get the first variable.
2493 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002494 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 return FAIL;
2496
2497 /*
2498 * Repeat computing, until no '+', '-' or '.' is following.
2499 */
2500 for (;;)
2501 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002502 int getnext;
2503 char_u *p;
2504 int op;
2505 int concat;
2506 typval_T var2;
2507
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002508 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002509 p = eval_next_non_blank(*arg, evalarg, &getnext);
2510 op = *p;
2511 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002512 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002514 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002515 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002517 if ((op != '+' || (rettv->v_type != VAR_LIST
2518 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002519#ifdef FEAT_FLOAT
2520 && (op == '.' || rettv->v_type != VAR_FLOAT)
2521#endif
2522 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002523 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002524 // For "list + ...", an illegal use of the first operand as
2525 // a number cannot be determined before evaluating the 2nd
2526 // operand: if this is also a list, all is ok.
2527 // For "something . ...", "something - ..." or "non-list + ...",
2528 // we know that the first operand needs to be a string or number
2529 // without evaluating the 2nd operand. So check before to avoid
2530 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002531 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002532 {
2533 clear_tv(rettv);
2534 return FAIL;
2535 }
2536 }
2537
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 /*
2539 * Get the second variable.
2540 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002541 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2542 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002543 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002544 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 return FAIL;
2548 }
2549
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002550 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
2552 /*
2553 * Compute the result.
2554 */
2555 if (op == '.')
2556 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002557 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2558 char_u *s1 = tv_get_string_buf(rettv, buf1);
2559 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2560
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002561 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002562 {
2563 clear_tv(rettv);
2564 clear_tv(&var2);
2565 return FAIL;
2566 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002567 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002568 clear_tv(rettv);
2569 rettv->v_type = VAR_STRING;
2570 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002572 else if (op == '+' && rettv->v_type == VAR_BLOB
2573 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002575 else if (op == '+' && rettv->v_type == VAR_LIST
2576 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002577 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002579 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 else
2582 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002583 int error = FALSE;
2584 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002585#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002586 float_T f1 = 0, f2 = 0;
2587
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002588 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002589 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002590 f1 = rettv->vval.v_float;
2591 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002592 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002593 else
2594#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002595 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002596 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002597 if (error)
2598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002599 // This can only happen for "list + non-list". For
2600 // "non-list + ..." or "something - ...", we returned
2601 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002602 clear_tv(rettv);
2603 return FAIL;
2604 }
2605#ifdef FEAT_FLOAT
2606 if (var2.v_type == VAR_FLOAT)
2607 f1 = n1;
2608#endif
2609 }
2610#ifdef FEAT_FLOAT
2611 if (var2.v_type == VAR_FLOAT)
2612 {
2613 f2 = var2.vval.v_float;
2614 n2 = 0;
2615 }
2616 else
2617#endif
2618 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002619 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002620 if (error)
2621 {
2622 clear_tv(rettv);
2623 clear_tv(&var2);
2624 return FAIL;
2625 }
2626#ifdef FEAT_FLOAT
2627 if (rettv->v_type == VAR_FLOAT)
2628 f2 = n2;
2629#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002632
2633#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002634 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002635 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2636 {
2637 if (op == '+')
2638 f1 = f1 + f2;
2639 else
2640 f1 = f1 - f2;
2641 rettv->v_type = VAR_FLOAT;
2642 rettv->vval.v_float = f1;
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002645#endif
2646 {
2647 if (op == '+')
2648 n1 = n1 + n2;
2649 else
2650 n1 = n1 - n2;
2651 rettv->v_type = VAR_NUMBER;
2652 rettv->vval.v_number = n1;
2653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
2657 }
2658 return OK;
2659}
2660
2661/*
2662 * Handle fifth level expression:
2663 * * number multiplication
2664 * / number division
2665 * % number modulo
2666 *
2667 * "arg" must point to the first non-white of the expression.
2668 * "arg" is advanced to the next non-white after the recognized expression.
2669 *
2670 * Return OK or FAIL.
2671 */
2672 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002673eval6(
2674 char_u **arg,
2675 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002676 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002677 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
Bram Moolenaar33570922005-01-25 22:26:29 +00002679 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002681 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002682#ifdef FEAT_FLOAT
2683 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002684 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002685#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002686 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
2688 /*
2689 * Get the first variable.
2690 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002691 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return FAIL;
2693
2694 /*
2695 * Repeat computing, until no '*', '/' or '%' is following.
2696 */
2697 for (;;)
2698 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002699 int evaluate = evalarg == NULL ? 0
2700 : (evalarg->eval_flags & EVAL_EVALUATE);
2701 int getnext;
2702
2703 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002704 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002706 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002707 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002709 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002711#ifdef FEAT_FLOAT
2712 if (rettv->v_type == VAR_FLOAT)
2713 {
2714 f1 = rettv->vval.v_float;
2715 use_float = TRUE;
2716 n1 = 0;
2717 }
2718 else
2719#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002720 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002721 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002722 if (error)
2723 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 else
2726 n1 = 0;
2727
2728 /*
2729 * Get the second variable.
2730 */
2731 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002732 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 return FAIL;
2734
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002735 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002737#ifdef FEAT_FLOAT
2738 if (var2.v_type == VAR_FLOAT)
2739 {
2740 if (!use_float)
2741 {
2742 f1 = n1;
2743 use_float = TRUE;
2744 }
2745 f2 = var2.vval.v_float;
2746 n2 = 0;
2747 }
2748 else
2749#endif
2750 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002751 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002752 clear_tv(&var2);
2753 if (error)
2754 return FAIL;
2755#ifdef FEAT_FLOAT
2756 if (use_float)
2757 f2 = n2;
2758#endif
2759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 /*
2762 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002763 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002765#ifdef FEAT_FLOAT
2766 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002768 if (op == '*')
2769 f1 = f1 * f2;
2770 else if (op == '/')
2771 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002772# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002773 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002774 if (f2 == 0.0)
2775 {
2776 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002777 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002778 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002779 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002780 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002781 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002782 }
2783 else
2784 f1 = f1 / f2;
2785# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002786 // We rely on the floating point library to handle divide
2787 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002788 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002789# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002792 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002794 return FAIL;
2795 }
2796 rettv->v_type = VAR_FLOAT;
2797 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
2799 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002802 if (op == '*')
2803 n1 = n1 * n2;
2804 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002805 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002807 n1 = num_modulus(n1, n2);
2808
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002809 rettv->v_type = VAR_NUMBER;
2810 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
2813 }
2814
2815 return OK;
2816}
2817
2818/*
2819 * Handle sixth level expression:
2820 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002821 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002822 * "string" string constant
2823 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 * &option-name option value
2825 * @r register contents
2826 * identifier variable value
2827 * function() function call
2828 * $VAR environment variable
2829 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002830 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002832 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002833 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 *
2835 * Also handle:
2836 * ! in front logical NOT
2837 * - in front unary minus
2838 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 * trailing [] subscript in String or List
2840 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002841 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 *
2843 * "arg" must point to the first non-white of the expression.
2844 * "arg" is advanced to the next non-white after the recognized expression.
2845 *
2846 * Return OK or FAIL.
2847 */
2848 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002849eval7(
2850 char_u **arg,
2851 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002852 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002855 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2856 int evaluate = evalarg != NULL
2857 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 int len;
2859 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 char_u *start_leader, *end_leader;
2861 int ret = OK;
2862 char_u *alias;
2863
2864 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002865 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002866 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002868 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
2870 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002871 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 */
2873 start_leader = *arg;
2874 while (**arg == '!' || **arg == '-' || **arg == '+')
2875 *arg = skipwhite(*arg + 1);
2876 end_leader = *arg;
2877
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002878 if (**arg == '.' && (!isdigit(*(*arg + 1))
2879#ifdef FEAT_FLOAT
2880 || current_sctx.sc_version < 2
2881#endif
2882 ))
2883 {
2884 semsg(_(e_invexpr2), *arg);
2885 ++*arg;
2886 return FAIL;
2887 }
2888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 switch (**arg)
2890 {
2891 /*
2892 * Number constant.
2893 */
2894 case '0':
2895 case '1':
2896 case '2':
2897 case '3':
2898 case '4':
2899 case '5':
2900 case '6':
2901 case '7':
2902 case '8':
2903 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002905
2906 // Apply prefixed "-" and "+" now. Matters especially when
2907 // "->" follows.
2908 if (ret == OK && evaluate && end_leader > start_leader)
2909 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 break;
2911
2912 /*
2913 * String constant: "string".
2914 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002915 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 break;
2917
2918 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002919 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002922 break;
2923
2924 /*
2925 * List: [expr, expr]
2926 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002927 case '[': ret = get_list_tv(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 break;
2929
2930 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002931 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002932 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002933 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002934 {
2935 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002936 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002937 }
2938 else
2939 ret = NOTDONE;
2940 break;
2941
2942 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002943 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002944 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002945 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002946 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002947 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002948 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002949 break;
2950
2951 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002952 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002954 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 break;
2956
2957 /*
2958 * Environment variable: $VAR.
2959 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002960 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 break;
2962
2963 /*
2964 * Register contents: @r.
2965 */
2966 case '@': ++*arg;
2967 if (evaluate)
2968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002969 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002970 rettv->vval.v_string = get_reg_contents(**arg,
2971 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 }
2973 if (**arg != NUL)
2974 ++*arg;
2975 break;
2976
2977 /*
2978 * nested expression: (expression).
2979 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002980 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02002981 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002982 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002983
Bram Moolenaar9215f012020-06-27 21:18:00 +02002984 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002985 if (**arg == ')')
2986 ++*arg;
2987 else if (ret == OK)
2988 {
2989 emsg(_(e_missing_close));
2990 clear_tv(rettv);
2991 ret = FAIL;
2992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
2994 break;
2995
Bram Moolenaar8c711452005-01-14 21:53:12 +00002996 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 break;
2998 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002999
3000 if (ret == NOTDONE)
3001 {
3002 /*
3003 * Must be a variable or function name.
3004 * Can also be a curly-braces kind of name: {expr}.
3005 */
3006 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003007 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003008 if (alias != NULL)
3009 s = alias;
3010
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003011 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003012 ret = FAIL;
3013 else
3014 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003015 if (**arg == '(') // recursive!
Bram Moolenaare6b53242020-07-01 17:28:33 +02003016 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003017 else if (flags & EVAL_CONSTANT)
3018 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003019 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003020 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003021 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003022 {
3023 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003025 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003026 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003027 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003028 }
3029
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 *arg = skipwhite(*arg);
3031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003032 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3033 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003034 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003035 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
3037 /*
3038 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3039 */
3040 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003041 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003042 return ret;
3043}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003044
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003045/*
3046 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003047 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003048 * Adjusts "end_leaderp" until it is at "start_leader".
3049 */
3050 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003051eval7_leader(
3052 typval_T *rettv,
3053 int numeric_only,
3054 char_u *start_leader,
3055 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003056{
3057 char_u *end_leader = *end_leaderp;
3058 int ret = OK;
3059 int error = FALSE;
3060 varnumber_T val = 0;
3061#ifdef FEAT_FLOAT
3062 float_T f = 0.0;
3063
3064 if (rettv->v_type == VAR_FLOAT)
3065 f = rettv->vval.v_float;
3066 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003068 val = tv_get_number_chk(rettv, &error);
3069 if (error)
3070 {
3071 clear_tv(rettv);
3072 ret = FAIL;
3073 }
3074 else
3075 {
3076 while (end_leader > start_leader)
3077 {
3078 --end_leader;
3079 if (*end_leader == '!')
3080 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003081 if (numeric_only)
3082 {
3083 ++end_leader;
3084 break;
3085 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003086#ifdef FEAT_FLOAT
3087 if (rettv->v_type == VAR_FLOAT)
3088 f = !f;
3089 else
3090#endif
3091 val = !val;
3092 }
3093 else if (*end_leader == '-')
3094 {
3095#ifdef FEAT_FLOAT
3096 if (rettv->v_type == VAR_FLOAT)
3097 f = -f;
3098 else
3099#endif
3100 val = -val;
3101 }
3102 }
3103#ifdef FEAT_FLOAT
3104 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003106 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003107 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003109 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003110#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003111 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003112 clear_tv(rettv);
3113 rettv->v_type = VAR_NUMBER;
3114 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003117 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 return ret;
3119}
3120
3121/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003122 * Call the function referred to in "rettv".
3123 */
3124 static int
3125call_func_rettv(
3126 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003127 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003128 typval_T *rettv,
3129 int evaluate,
3130 dict_T *selfdict,
3131 typval_T *basetv)
3132{
3133 partial_T *pt = NULL;
3134 funcexe_T funcexe;
3135 typval_T functv;
3136 char_u *s;
3137 int ret;
3138
3139 // need to copy the funcref so that we can clear rettv
3140 if (evaluate)
3141 {
3142 functv = *rettv;
3143 rettv->v_type = VAR_UNKNOWN;
3144
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003145 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003146 if (functv.v_type == VAR_PARTIAL)
3147 {
3148 pt = functv.vval.v_partial;
3149 s = partial_name(pt);
3150 }
3151 else
3152 s = functv.vval.v_string;
3153 }
3154 else
3155 s = (char_u *)"";
3156
Bram Moolenaara80faa82020-04-12 19:37:17 +02003157 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003158 funcexe.firstline = curwin->w_cursor.lnum;
3159 funcexe.lastline = curwin->w_cursor.lnum;
3160 funcexe.evaluate = evaluate;
3161 funcexe.partial = pt;
3162 funcexe.selfdict = selfdict;
3163 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003164 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003165
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003166 // Clear the funcref afterwards, so that deleting it while
3167 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003168 if (evaluate)
3169 clear_tv(&functv);
3170
3171 return ret;
3172}
3173
3174/*
3175 * Evaluate "->method()".
3176 * "*arg" points to the '-'.
3177 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3178 */
3179 static int
3180eval_lambda(
3181 char_u **arg,
3182 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003183 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003184 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003185{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003186 int evaluate = evalarg != NULL
3187 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003188 typval_T base = *rettv;
3189 int ret;
3190
3191 // Skip over the ->.
3192 *arg += 2;
3193 rettv->v_type = VAR_UNKNOWN;
3194
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003195 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003196 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003197 return FAIL;
3198 else if (**arg != '(')
3199 {
3200 if (verbose)
3201 {
3202 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003203 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003204 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003206 }
3207 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003208 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003209 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003210 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003211 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003212
3213 // Clear the funcref afterwards, so that deleting it while
3214 // evaluating the arguments is possible (see test55).
3215 if (evaluate)
3216 clear_tv(&base);
3217
3218 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003219}
3220
3221/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003222 * Evaluate "->method()".
3223 * "*arg" points to the '-'.
3224 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3225 */
3226 static int
3227eval_method(
3228 char_u **arg,
3229 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003230 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003231 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003232{
3233 char_u *name;
3234 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003235 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003236 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003237 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003238 int evaluate = evalarg != NULL
3239 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003240
3241 // Skip over the ->.
3242 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003243 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003244
Bram Moolenaarac92e252019-08-03 21:58:38 +02003245 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003246 len = get_name_len(arg, &alias, evaluate, TRUE);
3247 if (alias != NULL)
3248 name = alias;
3249
3250 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003251 {
3252 if (verbose)
3253 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003254 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003255 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003256 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003257 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003258 if (**arg != '(')
3259 {
3260 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003262 ret = FAIL;
3263 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003264 else if (VIM_ISWHITE((*arg)[-1]))
3265 {
3266 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003267 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003268 ret = FAIL;
3269 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003270 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003271 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003272 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003273 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003274
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003275 // Clear the funcref afterwards, so that deleting it while
3276 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003277 if (evaluate)
3278 clear_tv(&base);
3279
Bram Moolenaarac92e252019-08-03 21:58:38 +02003280 return ret;
3281}
3282
3283/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003284 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3285 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003286 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3287 */
3288 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003289eval_index(
3290 char_u **arg,
3291 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003292 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003293 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003294{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003295 int evaluate = evalarg != NULL
3296 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003297 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003298 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003299 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003300 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003301 long len = -1;
3302 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003303 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003304 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003305
Bram Moolenaara03f2332016-02-06 18:09:59 +01003306 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003307 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003308 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003309 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003310 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003311 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003312 return FAIL;
3313 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003314#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003315 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003316 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003317 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003318#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003319 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003320 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003321 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003322 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003323 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003324 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003325 return FAIL;
3326 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003327 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003329 if (evaluate)
3330 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003331 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003332
3333 case VAR_STRING:
3334 case VAR_NUMBER:
3335 case VAR_LIST:
3336 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003337 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003338 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003339 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003340
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003341 init_tv(&var1);
3342 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003343 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003344 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003345 /*
3346 * dict.name
3347 */
3348 key = *arg + 1;
3349 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3350 ;
3351 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003352 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003353 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003354 }
3355 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003356 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003357 /*
3358 * something[idx]
3359 *
3360 * Get the (first) variable from inside the [].
3361 */
3362 *arg = skipwhite(*arg + 1);
3363 if (**arg == ':')
3364 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003365 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003366 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003367 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003368 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003369 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003370 clear_tv(&var1);
3371 return FAIL;
3372 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003373
3374 /*
3375 * Get the second variable from inside the [:].
3376 */
3377 if (**arg == ':')
3378 {
3379 range = TRUE;
3380 *arg = skipwhite(*arg + 1);
3381 if (**arg == ']')
3382 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003383 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003385 if (!empty1)
3386 clear_tv(&var1);
3387 return FAIL;
3388 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003389 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003390 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003391 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003392 if (!empty1)
3393 clear_tv(&var1);
3394 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003395 return FAIL;
3396 }
3397 }
3398
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003399 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003400 if (**arg != ']')
3401 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003402 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003403 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003404 clear_tv(&var1);
3405 if (range)
3406 clear_tv(&var2);
3407 return FAIL;
3408 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003409 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003410 }
3411
3412 if (evaluate)
3413 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003414 n1 = 0;
3415 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003416 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003417 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003418 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003419 }
3420 if (range)
3421 {
3422 if (empty2)
3423 n2 = -1;
3424 else
3425 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003426 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003427 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003428 }
3429 }
3430
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003431 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003432 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003433 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003434 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003436 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003437 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003438 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003439 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003440 case VAR_SPECIAL:
3441 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003442 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003443 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003444
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003445 case VAR_NUMBER:
3446 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003447 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003448 len = (long)STRLEN(s);
3449 if (range)
3450 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003451 // The resulting variable is a substring. If the indexes
3452 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003453 if (n1 < 0)
3454 {
3455 n1 = len + n1;
3456 if (n1 < 0)
3457 n1 = 0;
3458 }
3459 if (n2 < 0)
3460 n2 = len + n2;
3461 else if (n2 >= len)
3462 n2 = len;
3463 if (n1 >= len || n2 < 0 || n1 > n2)
3464 s = NULL;
3465 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003466 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003467 }
3468 else
3469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003470 // The resulting variable is a string of a single
3471 // character. If the index is too big or negative the
3472 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003473 if (n1 >= len || n1 < 0)
3474 s = NULL;
3475 else
3476 s = vim_strnsave(s + n1, 1);
3477 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003478 clear_tv(rettv);
3479 rettv->v_type = VAR_STRING;
3480 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003481 break;
3482
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003483 case VAR_BLOB:
3484 len = blob_len(rettv->vval.v_blob);
3485 if (range)
3486 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003487 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003488 // are out of range the result is empty.
3489 if (n1 < 0)
3490 {
3491 n1 = len + n1;
3492 if (n1 < 0)
3493 n1 = 0;
3494 }
3495 if (n2 < 0)
3496 n2 = len + n2;
3497 else if (n2 >= len)
3498 n2 = len - 1;
3499 if (n1 >= len || n2 < 0 || n1 > n2)
3500 {
3501 clear_tv(rettv);
3502 rettv->v_type = VAR_BLOB;
3503 rettv->vval.v_blob = NULL;
3504 }
3505 else
3506 {
3507 blob_T *blob = blob_alloc();
3508
3509 if (blob != NULL)
3510 {
3511 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3512 {
3513 blob_free(blob);
3514 return FAIL;
3515 }
3516 blob->bv_ga.ga_len = n2 - n1 + 1;
3517 for (i = n1; i <= n2; i++)
3518 blob_set(blob, i - n1,
3519 blob_get(rettv->vval.v_blob, i));
3520
3521 clear_tv(rettv);
3522 rettv_blob_set(rettv, blob);
3523 }
3524 }
3525 }
3526 else
3527 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003528 // The resulting variable is a byte value.
3529 // If the index is too big or negative that is an error.
3530 if (n1 < 0)
3531 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003532 if (n1 < len && n1 >= 0)
3533 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003534 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003535
3536 clear_tv(rettv);
3537 rettv->v_type = VAR_NUMBER;
3538 rettv->vval.v_number = v;
3539 }
3540 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003541 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003542 }
3543 break;
3544
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003545 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003546 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 if (n1 < 0)
3548 n1 = len + n1;
3549 if (!empty1 && (n1 < 0 || n1 >= len))
3550 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003551 // For a range we allow invalid values and return an empty
3552 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003553 if (!range)
3554 {
3555 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003556 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003557 return FAIL;
3558 }
3559 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003560 }
3561 if (range)
3562 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003563 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003564
3565 if (n2 < 0)
3566 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003567 else if (n2 >= len)
3568 n2 = len - 1;
3569 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003570 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003571 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003572 if (l == NULL)
3573 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003574 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003575 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003576 }
3577 else
3578 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003579 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003580 clear_tv(rettv);
3581 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003582 }
3583 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003584
3585 case VAR_DICT:
3586 if (range)
3587 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003588 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003589 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 if (len == -1)
3591 clear_tv(&var1);
3592 return FAIL;
3593 }
3594 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003595 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596
3597 if (len == -1)
3598 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003599 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003600 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 clear_tv(&var1);
3603 return FAIL;
3604 }
3605 }
3606
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003607 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003608
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003609 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003610 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 if (len == -1)
3612 clear_tv(&var1);
3613 if (item == NULL)
3614 return FAIL;
3615
3616 copy_tv(&item->di_tv, &var1);
3617 clear_tv(rettv);
3618 *rettv = var1;
3619 }
3620 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003621 }
3622 }
3623
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003624 return OK;
3625}
3626
3627/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003628 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003629 */
3630 char_u *
3631partial_name(partial_T *pt)
3632{
3633 if (pt->pt_name != NULL)
3634 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003635 if (pt->pt_func != NULL)
3636 return pt->pt_func->uf_name;
3637 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003638}
3639
Bram Moolenaarddecc252016-04-06 22:59:37 +02003640 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003641partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003642{
3643 int i;
3644
3645 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003646 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003647 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003648 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003649 if (pt->pt_name != NULL)
3650 {
3651 func_unref(pt->pt_name);
3652 vim_free(pt->pt_name);
3653 }
3654 else
3655 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003656
3657 if (pt->pt_funcstack != NULL)
3658 {
3659 // Decrease the reference count for the context of a closure. If down
3660 // to zero free it and clear the variables on the stack.
3661 if (--pt->pt_funcstack->fs_refcount == 0)
3662 {
3663 garray_T *gap = &pt->pt_funcstack->fs_ga;
3664 typval_T *stack = gap->ga_data;
3665
3666 for (i = 0; i < gap->ga_len; ++i)
3667 clear_tv(stack + i);
3668 ga_clear(gap);
3669 vim_free(pt->pt_funcstack);
3670 }
3671 pt->pt_funcstack = NULL;
3672 }
3673
Bram Moolenaarddecc252016-04-06 22:59:37 +02003674 vim_free(pt);
3675}
3676
3677/*
3678 * Unreference a closure: decrement the reference count and free it when it
3679 * becomes zero.
3680 */
3681 void
3682partial_unref(partial_T *pt)
3683{
3684 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003685 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003686}
3687
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003688/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003689 * Return the next (unique) copy ID.
3690 * Used for serializing nested structures.
3691 */
3692 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003693get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003694{
3695 current_copyID += COPYID_INC;
3696 return current_copyID;
3697}
3698
3699/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003700 * Garbage collection for lists and dictionaries.
3701 *
3702 * We use reference counts to be able to free most items right away when they
3703 * are no longer used. But for composite items it's possible that it becomes
3704 * unused while the reference count is > 0: When there is a recursive
3705 * reference. Example:
3706 * :let l = [1, 2, 3]
3707 * :let d = {9: l}
3708 * :let l[1] = d
3709 *
3710 * Since this is quite unusual we handle this with garbage collection: every
3711 * once in a while find out which lists and dicts are not referenced from any
3712 * variable.
3713 *
3714 * Here is a good reference text about garbage collection (refers to Python
3715 * but it applies to all reference-counting mechanisms):
3716 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003717 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003718
3719/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003720 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003721 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003722 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003723 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003724 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003725garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003726{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003727 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003728 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003729 buf_T *buf;
3730 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003731 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003732 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003733
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003734 if (!testing)
3735 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003736 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003737 want_garbage_collect = FALSE;
3738 may_garbage_collect = FALSE;
3739 garbage_collect_at_exit = FALSE;
3740 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003741
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003742 // The execution stack can grow big, limit the size.
3743 if (exestack.ga_maxlen - exestack.ga_len > 500)
3744 {
3745 size_t new_len;
3746 char_u *pp;
3747 int n;
3748
3749 // Keep 150% of the current size, with a minimum of the growth size.
3750 n = exestack.ga_len / 2;
3751 if (n < exestack.ga_growsize)
3752 n = exestack.ga_growsize;
3753
3754 // Don't make it bigger though.
3755 if (exestack.ga_len + n < exestack.ga_maxlen)
3756 {
3757 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3758 pp = vim_realloc(exestack.ga_data, new_len);
3759 if (pp == NULL)
3760 return FAIL;
3761 exestack.ga_maxlen = exestack.ga_len + n;
3762 exestack.ga_data = pp;
3763 }
3764 }
3765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003766 // We advance by two because we add one for items referenced through
3767 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003768 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003769
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003770 /*
3771 * 1. Go through all accessible variables and mark all lists and dicts
3772 * with copyID.
3773 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003774
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003775 // Don't free variables in the previous_funccal list unless they are only
3776 // referenced through previous_funccal. This must be first, because if
3777 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003778 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003780 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003781 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003782
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003783 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003784 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003785 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3786 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003787
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003788 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003789 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003790 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3791 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003792 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003793 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3794 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003795#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003796 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003797 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3798 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003799 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003800 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003801 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3802 NULL, NULL);
3803#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003804
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003805 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003806 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003807 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3808 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003809 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003810 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003811
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003812 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003813 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003815 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003816 abort = abort || set_ref_in_functions(copyID);
3817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003818 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003821 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003822 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003823
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003824 // callbacks in buffers
3825 abort = abort || set_ref_in_buffers(copyID);
3826
Bram Moolenaar1dced572012-04-05 16:54:08 +02003827#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003828 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003829#endif
3830
Bram Moolenaardb913952012-06-29 12:54:53 +02003831#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003832 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003833#endif
3834
3835#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003836 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003837#endif
3838
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003839#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003840 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003841 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003842#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003843#ifdef FEAT_NETBEANS_INTG
3844 abort = abort || set_ref_in_nb_channel(copyID);
3845#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003846
Bram Moolenaare3188e22016-05-31 21:13:04 +02003847#ifdef FEAT_TIMERS
3848 abort = abort || set_ref_in_timer(copyID);
3849#endif
3850
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003851#ifdef FEAT_QUICKFIX
3852 abort = abort || set_ref_in_quickfix(copyID);
3853#endif
3854
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003855#ifdef FEAT_TERMINAL
3856 abort = abort || set_ref_in_term(copyID);
3857#endif
3858
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003859#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003860 abort = abort || set_ref_in_popups(copyID);
3861#endif
3862
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003863 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003864 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003865 /*
3866 * 2. Free lists and dictionaries that are not referenced.
3867 */
3868 did_free = free_unref_items(copyID);
3869
3870 /*
3871 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003872 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003873 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003874 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003875 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003876 else if (p_verbose > 0)
3877 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003878 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003879 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003880
3881 return did_free;
3882}
3883
3884/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003885 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003886 */
3887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003888free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003889{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003890 int did_free = FALSE;
3891
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003892 // Let all "free" functions know that we are here. This means no
3893 // dictionaries, lists, channels or jobs are to be freed, because we will
3894 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003895 in_free_unref_items = TRUE;
3896
3897 /*
3898 * PASS 1: free the contents of the items. We don't free the items
3899 * themselves yet, so that it is possible to decrement refcount counters
3900 */
3901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003902 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003903 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003905 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003906 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003907
3908#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003909 // Go through the list of jobs and free items without the copyID. This
3910 // must happen before doing channels, because jobs refer to channels, but
3911 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003912 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003914 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003915 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3916#endif
3917
3918 /*
3919 * PASS 2: free the items themselves.
3920 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003921 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003922 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003923
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003924#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003925 // Go through the list of jobs and free items without the copyID. This
3926 // must happen before doing channels, because jobs refer to channels, but
3927 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003928 free_unused_jobs(copyID, COPYID_MASK);
3929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003930 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003931 free_unused_channels(copyID, COPYID_MASK);
3932#endif
3933
3934 in_free_unref_items = FALSE;
3935
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003936 return did_free;
3937}
3938
3939/*
3940 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003941 * "list_stack" is used to add lists to be marked. Can be NULL.
3942 *
3943 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003944 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003945 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003946set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003947{
3948 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003949 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003950 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003951 hashtab_T *cur_ht;
3952 ht_stack_T *ht_stack = NULL;
3953 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003954
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003955 cur_ht = ht;
3956 for (;;)
3957 {
3958 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003960 // Mark each item in the hashtab. If the item contains a hashtab
3961 // it is added to ht_stack, if it contains a list it is added to
3962 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003963 todo = (int)cur_ht->ht_used;
3964 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3965 if (!HASHITEM_EMPTY(hi))
3966 {
3967 --todo;
3968 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3969 &ht_stack, list_stack);
3970 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003971 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003972
3973 if (ht_stack == NULL)
3974 break;
3975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003976 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003977 cur_ht = ht_stack->ht;
3978 tempitem = ht_stack;
3979 ht_stack = ht_stack->prev;
3980 free(tempitem);
3981 }
3982
3983 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003984}
3985
3986/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003987 * Mark a dict and its items with "copyID".
3988 * Returns TRUE if setting references failed somehow.
3989 */
3990 int
3991set_ref_in_dict(dict_T *d, int copyID)
3992{
3993 if (d != NULL && d->dv_copyID != copyID)
3994 {
3995 d->dv_copyID = copyID;
3996 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3997 }
3998 return FALSE;
3999}
4000
4001/*
4002 * Mark a list and its items with "copyID".
4003 * Returns TRUE if setting references failed somehow.
4004 */
4005 int
4006set_ref_in_list(list_T *ll, int copyID)
4007{
4008 if (ll != NULL && ll->lv_copyID != copyID)
4009 {
4010 ll->lv_copyID = copyID;
4011 return set_ref_in_list_items(ll, copyID, NULL);
4012 }
4013 return FALSE;
4014}
4015
4016/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004017 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004018 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4019 *
4020 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004021 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004022 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004023set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004024{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004025 listitem_T *li;
4026 int abort = FALSE;
4027 list_T *cur_l;
4028 list_stack_T *list_stack = NULL;
4029 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004030
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004031 cur_l = l;
4032 for (;;)
4033 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004034 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004035 // Mark each item in the list. If the item contains a hashtab
4036 // it is added to ht_stack, if it contains a list it is added to
4037 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004038 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4039 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4040 ht_stack, &list_stack);
4041 if (list_stack == NULL)
4042 break;
4043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004044 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004045 cur_l = list_stack->list;
4046 tempitem = list_stack;
4047 list_stack = list_stack->prev;
4048 free(tempitem);
4049 }
4050
4051 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004052}
4053
4054/*
4055 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004056 * "list_stack" is used to add lists to be marked. Can be NULL.
4057 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4058 *
4059 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004060 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004061 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004062set_ref_in_item(
4063 typval_T *tv,
4064 int copyID,
4065 ht_stack_T **ht_stack,
4066 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004067{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004068 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004069
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004070 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004071 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004072 dict_T *dd = tv->vval.v_dict;
4073
Bram Moolenaara03f2332016-02-06 18:09:59 +01004074 if (dd != NULL && dd->dv_copyID != copyID)
4075 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004076 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004077 dd->dv_copyID = copyID;
4078 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004079 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004080 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4081 }
4082 else
4083 {
4084 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4085 if (newitem == NULL)
4086 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004087 else
4088 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004089 newitem->ht = &dd->dv_hashtab;
4090 newitem->prev = *ht_stack;
4091 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004092 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004093 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004094 }
4095 }
4096 else if (tv->v_type == VAR_LIST)
4097 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004098 list_T *ll = tv->vval.v_list;
4099
Bram Moolenaara03f2332016-02-06 18:09:59 +01004100 if (ll != NULL && ll->lv_copyID != copyID)
4101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004102 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004103 ll->lv_copyID = copyID;
4104 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004105 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004106 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004107 }
4108 else
4109 {
4110 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004111 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004112 if (newitem == NULL)
4113 abort = TRUE;
4114 else
4115 {
4116 newitem->list = ll;
4117 newitem->prev = *list_stack;
4118 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004119 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004120 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004121 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004122 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004123 else if (tv->v_type == VAR_FUNC)
4124 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004125 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004126 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004127 else if (tv->v_type == VAR_PARTIAL)
4128 {
4129 partial_T *pt = tv->vval.v_partial;
4130 int i;
4131
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004132 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004133 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004134 // Didn't see this partial yet.
4135 pt->pt_copyID = copyID;
4136
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004137 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004138
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004139 if (pt->pt_dict != NULL)
4140 {
4141 typval_T dtv;
4142
4143 dtv.v_type = VAR_DICT;
4144 dtv.vval.v_dict = pt->pt_dict;
4145 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4146 }
4147
4148 for (i = 0; i < pt->pt_argc; ++i)
4149 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4150 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004151 if (pt->pt_funcstack != NULL)
4152 {
4153 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4154
4155 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4156 abort = abort || set_ref_in_item(stack + i, copyID,
4157 ht_stack, list_stack);
4158 }
4159
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004160 }
4161 }
4162#ifdef FEAT_JOB_CHANNEL
4163 else if (tv->v_type == VAR_JOB)
4164 {
4165 job_T *job = tv->vval.v_job;
4166 typval_T dtv;
4167
4168 if (job != NULL && job->jv_copyID != copyID)
4169 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004170 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004171 if (job->jv_channel != NULL)
4172 {
4173 dtv.v_type = VAR_CHANNEL;
4174 dtv.vval.v_channel = job->jv_channel;
4175 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4176 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004177 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004178 {
4179 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004180 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004181 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4182 }
4183 }
4184 }
4185 else if (tv->v_type == VAR_CHANNEL)
4186 {
4187 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004188 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004189 typval_T dtv;
4190 jsonq_T *jq;
4191 cbq_T *cq;
4192
4193 if (ch != NULL && ch->ch_copyID != copyID)
4194 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004195 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004196 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004197 {
4198 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4199 jq = jq->jq_next)
4200 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4201 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4202 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004203 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004204 {
4205 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004206 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004207 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4208 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004209 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004210 {
4211 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004212 dtv.vval.v_partial =
4213 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004214 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4215 }
4216 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004217 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004218 {
4219 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004220 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004221 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4222 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004223 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004224 {
4225 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004226 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004227 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4228 }
4229 }
4230 }
4231#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004232 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004233}
4234
Bram Moolenaar8c711452005-01-14 21:53:12 +00004235/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004236 * Return a string with the string representation of a variable.
4237 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004238 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004239 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004240 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4241 * stings as "string()", otherwise does not put quotes around strings, as
4242 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004243 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4244 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004245 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004246 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004247 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004248echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004249 typval_T *tv,
4250 char_u **tofree,
4251 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004252 int copyID,
4253 int echo_style,
4254 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004255 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004256{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004257 static int recurse = 0;
4258 char_u *r = NULL;
4259
Bram Moolenaar33570922005-01-25 22:26:29 +00004260 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004261 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004262 if (!did_echo_string_emsg)
4263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004264 // Only give this message once for a recursive call to avoid
4265 // flooding the user with errors. And stop iterating over lists
4266 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004267 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004268 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004269 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004270 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004271 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004272 }
4273 ++recurse;
4274
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004275 switch (tv->v_type)
4276 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004277 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004278 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004279 {
4280 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004281 r = tv->vval.v_string;
4282 if (r == NULL)
4283 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004284 }
4285 else
4286 {
4287 *tofree = string_quote(tv->vval.v_string, FALSE);
4288 r = *tofree;
4289 }
4290 break;
4291
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004292 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004293 if (echo_style)
4294 {
4295 *tofree = NULL;
4296 r = tv->vval.v_string;
4297 }
4298 else
4299 {
4300 *tofree = string_quote(tv->vval.v_string, TRUE);
4301 r = *tofree;
4302 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004303 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004304
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004305 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004306 {
4307 partial_T *pt = tv->vval.v_partial;
4308 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004309 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004310 garray_T ga;
4311 int i;
4312 char_u *tf;
4313
4314 ga_init2(&ga, 1, 100);
4315 ga_concat(&ga, (char_u *)"function(");
4316 if (fname != NULL)
4317 {
4318 ga_concat(&ga, fname);
4319 vim_free(fname);
4320 }
4321 if (pt != NULL && pt->pt_argc > 0)
4322 {
4323 ga_concat(&ga, (char_u *)", [");
4324 for (i = 0; i < pt->pt_argc; ++i)
4325 {
4326 if (i > 0)
4327 ga_concat(&ga, (char_u *)", ");
4328 ga_concat(&ga,
4329 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4330 vim_free(tf);
4331 }
4332 ga_concat(&ga, (char_u *)"]");
4333 }
4334 if (pt != NULL && pt->pt_dict != NULL)
4335 {
4336 typval_T dtv;
4337
4338 ga_concat(&ga, (char_u *)", ");
4339 dtv.v_type = VAR_DICT;
4340 dtv.vval.v_dict = pt->pt_dict;
4341 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4342 vim_free(tf);
4343 }
4344 ga_concat(&ga, (char_u *)")");
4345
4346 *tofree = ga.ga_data;
4347 r = *tofree;
4348 break;
4349 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004350
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004351 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004352 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004353 break;
4354
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004355 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004356 if (tv->vval.v_list == NULL)
4357 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004358 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004359 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004360 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004361 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004362 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4363 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004364 {
4365 *tofree = NULL;
4366 r = (char_u *)"[...]";
4367 }
4368 else
4369 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004370 int old_copyID = tv->vval.v_list->lv_copyID;
4371
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004372 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004373 *tofree = list2string(tv, copyID, restore_copyID);
4374 if (restore_copyID)
4375 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004376 r = *tofree;
4377 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004378 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004379
Bram Moolenaar8c711452005-01-14 21:53:12 +00004380 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004381 if (tv->vval.v_dict == NULL)
4382 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004383 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004384 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004385 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004386 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004387 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4388 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004389 {
4390 *tofree = NULL;
4391 r = (char_u *)"{...}";
4392 }
4393 else
4394 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004395 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004396
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004397 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004398 *tofree = dict2string(tv, copyID, restore_copyID);
4399 if (restore_copyID)
4400 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004401 r = *tofree;
4402 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004403 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004404
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004406 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004407 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004409 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004410 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004411 break;
4412
Bram Moolenaar835dc632016-02-07 14:27:38 +01004413 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004414 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004415 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004416 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004417 if (composite_val)
4418 {
4419 *tofree = string_quote(r, FALSE);
4420 r = *tofree;
4421 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004422 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004423
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004424 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004425#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004426 *tofree = NULL;
4427 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4428 r = numbuf;
4429 break;
4430#endif
4431
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004432 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004433 case VAR_SPECIAL:
4434 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004435 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004436 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004437 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004438
Bram Moolenaar8502c702014-06-17 12:51:16 +02004439 if (--recurse == 0)
4440 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004441 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004442}
4443
4444/*
4445 * Return a string with the string representation of a variable.
4446 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4447 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004448 * Does not put quotes around strings, as ":echo" displays values.
4449 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4450 * May return NULL.
4451 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004452 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004453echo_string(
4454 typval_T *tv,
4455 char_u **tofree,
4456 char_u *numbuf,
4457 int copyID)
4458{
4459 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4460}
4461
4462/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004463 * Return string "str" in ' quotes, doubling ' characters.
4464 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004465 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004466 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004467 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004468string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004469{
Bram Moolenaar33570922005-01-25 22:26:29 +00004470 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004471 char_u *p, *r, *s;
4472
Bram Moolenaar33570922005-01-25 22:26:29 +00004473 len = (function ? 13 : 3);
4474 if (str != NULL)
4475 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004476 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004477 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004478 if (*p == '\'')
4479 ++len;
4480 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004481 s = r = alloc(len);
4482 if (r != NULL)
4483 {
4484 if (function)
4485 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004486 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004487 r += 10;
4488 }
4489 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004491 if (str != NULL)
4492 for (p = str; *p != NUL; )
4493 {
4494 if (*p == '\'')
4495 *r++ = '\'';
4496 MB_COPY_CHAR(p, r);
4497 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004498 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004499 if (function)
4500 *r++ = ')';
4501 *r++ = NUL;
4502 }
4503 return s;
4504}
4505
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004506#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004507/*
4508 * Convert the string "text" to a floating point number.
4509 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4510 * this always uses a decimal point.
4511 * Returns the length of the text that was consumed.
4512 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004513 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004514string2float(
4515 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004516 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004517{
4518 char *s = (char *)text;
4519 float_T f;
4520
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004521 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004522 if (STRNICMP(text, "inf", 3) == 0)
4523 {
4524 *value = INFINITY;
4525 return 3;
4526 }
4527 if (STRNICMP(text, "-inf", 3) == 0)
4528 {
4529 *value = -INFINITY;
4530 return 4;
4531 }
4532 if (STRNICMP(text, "nan", 3) == 0)
4533 {
4534 *value = NAN;
4535 return 3;
4536 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004537 f = strtod(s, &s);
4538 *value = f;
4539 return (int)((char_u *)s - text);
4540}
4541#endif
4542
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004545 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004547 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004548var2fpos(
4549 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004550 int dollar_lnum, // TRUE when $ is last line
4551 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004553 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004555 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004557 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004558 if (varp->v_type == VAR_LIST)
4559 {
4560 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004561 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004562 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004563 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004564
4565 l = varp->vval.v_list;
4566 if (l == NULL)
4567 return NULL;
4568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004569 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004570 pos.lnum = list_find_nr(l, 0L, &error);
4571 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004572 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004573
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004574 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004575 pos.col = list_find_nr(l, 1L, &error);
4576 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004577 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004578 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004579
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004580 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004581 li = list_find(l, 1L);
4582 if (li != NULL && li->li_tv.v_type == VAR_STRING
4583 && li->li_tv.vval.v_string != NULL
4584 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4585 pos.col = len + 1;
4586
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004587 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004588 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004589 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004590 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004591
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004592 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004593 pos.coladd = list_find_nr(l, 2L, &error);
4594 if (error)
4595 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004596
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004597 return &pos;
4598 }
4599
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004600 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004601 if (name == NULL)
4602 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004603 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004605 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004606 {
4607 if (VIsual_active)
4608 return &VIsual;
4609 return &curwin->w_cursor;
4610 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004611 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004613 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4615 return NULL;
4616 return pp;
4617 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004618
Bram Moolenaara5525202006-03-02 22:52:09 +00004619 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004620
Bram Moolenaar477933c2007-07-17 14:32:23 +00004621 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004622 {
4623 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004624 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004625 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004626 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004627 // In silent Ex mode topline is zero, but that's not a valid line
4628 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004629 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004630 return &pos;
4631 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004632 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004633 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004634 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004635 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004636 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004637 return &pos;
4638 }
4639 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004640 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004642 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
4644 pos.lnum = curbuf->b_ml.ml_line_count;
4645 pos.col = 0;
4646 }
4647 else
4648 {
4649 pos.lnum = curwin->w_cursor.lnum;
4650 pos.col = (colnr_T)STRLEN(ml_get_curline());
4651 }
4652 return &pos;
4653 }
4654 return NULL;
4655}
4656
4657/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004658 * Convert list in "arg" into a position and optional file number.
4659 * When "fnump" is NULL there is no file number, only 3 items.
4660 * Note that the column is passed on as-is, the caller may want to decrement
4661 * it to use 1 for the first column.
4662 * Return FAIL when conversion is not possible, doesn't check the position for
4663 * validity.
4664 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004665 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004666list2fpos(
4667 typval_T *arg,
4668 pos_T *posp,
4669 int *fnump,
4670 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004671{
4672 list_T *l = arg->vval.v_list;
4673 long i = 0;
4674 long n;
4675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004676 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4677 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004678 if (arg->v_type != VAR_LIST
4679 || l == NULL
4680 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004681 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004682 return FAIL;
4683
4684 if (fnump != NULL)
4685 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004686 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004687 if (n < 0)
4688 return FAIL;
4689 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004690 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004691 *fnump = n;
4692 }
4693
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004694 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004695 if (n < 0)
4696 return FAIL;
4697 posp->lnum = n;
4698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004699 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004700 if (n < 0)
4701 return FAIL;
4702 posp->col = n;
4703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004704 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004705 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004706 posp->coladd = 0;
4707 else
4708 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004709
Bram Moolenaar493c1782014-05-28 14:34:46 +02004710 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004711 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004712
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004713 return OK;
4714}
4715
4716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 * Get the length of an environment variable name.
4718 * Advance "arg" to the first character after the name.
4719 * Return 0 for error.
4720 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004721 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004722get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 char_u *p;
4725 int len;
4726
4727 for (p = *arg; vim_isIDc(*p); ++p)
4728 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004729 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 return 0;
4731
4732 len = (int)(p - *arg);
4733 *arg = p;
4734 return len;
4735}
4736
4737/*
4738 * Get the length of the name of a function or internal variable.
4739 * "arg" is advanced to the first non-white character after the name.
4740 * Return 0 if something is wrong.
4741 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004742 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004743get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744{
4745 char_u *p;
4746 int len;
4747
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004748 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004750 {
4751 if (*p == ':')
4752 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004753 // "s:" is start of "s:var", but "n:" is not and can be used in
4754 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004755 len = (int)(p - *arg);
4756 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4757 || len > 1)
4758 break;
4759 }
4760 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004761 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 return 0;
4763
4764 len = (int)(p - *arg);
4765 *arg = skipwhite(p);
4766
4767 return len;
4768}
4769
4770/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004771 * Get the length of the name of a variable or function.
4772 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004774 * Return -1 if curly braces expansion failed.
4775 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 * If the name contains 'magic' {}'s, expand them and return the
4777 * expanded name in an allocated string via 'alias' - caller must free.
4778 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004780get_name_len(
4781 char_u **arg,
4782 char_u **alias,
4783 int evaluate,
4784 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785{
4786 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 char_u *p;
4788 char_u *expr_start;
4789 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004791 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792
4793 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4794 && (*arg)[2] == (int)KE_SNR)
4795 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004796 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 *arg += 3;
4798 return get_id_len(arg) + 3;
4799 }
4800 len = eval_fname_script(*arg);
4801 if (len > 0)
4802 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004803 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 *arg += len;
4805 }
4806
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004808 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004810 p = find_name_end(*arg, &expr_start, &expr_end,
4811 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 if (expr_start != NULL)
4813 {
4814 char_u *temp_string;
4815
4816 if (!evaluate)
4817 {
4818 len += (int)(p - *arg);
4819 *arg = skipwhite(p);
4820 return len;
4821 }
4822
4823 /*
4824 * Include any <SID> etc in the expanded string:
4825 * Thus the -len here.
4826 */
4827 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4828 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004829 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 *alias = temp_string;
4831 *arg = skipwhite(p);
4832 return (int)STRLEN(temp_string);
4833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
4835 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004836 // Only give an error when there is something, otherwise it will be
4837 // reported at a higher level.
4838 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004839 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840
4841 return len;
4842}
4843
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004844/*
4845 * Find the end of a variable or function name, taking care of magic braces.
4846 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4847 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004848 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 * Return a pointer to just after the name. Equal to "arg" if there is no
4850 * valid name.
4851 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004852 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004853find_name_end(
4854 char_u *arg,
4855 char_u **expr_start,
4856 char_u **expr_end,
4857 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004859 int mb_nest = 0;
4860 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004862 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004863 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004865 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004867 *expr_start = NULL;
4868 *expr_end = NULL;
4869 }
4870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004871 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004872 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4873 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004874 return arg;
4875
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004876 for (p = arg; *p != NUL
4877 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004878 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004879 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004881 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004883 if (*p == '\'')
4884 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004885 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004886 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004887 ;
4888 if (*p == NUL)
4889 break;
4890 }
4891 else if (*p == '"')
4892 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004893 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004894 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004895 if (*p == '\\' && p[1] != NUL)
4896 ++p;
4897 if (*p == NUL)
4898 break;
4899 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004900 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004902 // "s:" is start of "s:var", but "n:" is not and can be used in
4903 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004904 len = (int)(p - arg);
4905 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004906 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004907 break;
4908 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004909
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004910 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 if (*p == '[')
4913 ++br_nest;
4914 else if (*p == ']')
4915 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004917
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004918 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004920 if (*p == '{')
4921 {
4922 mb_nest++;
4923 if (expr_start != NULL && *expr_start == NULL)
4924 *expr_start = p;
4925 }
4926 else if (*p == '}')
4927 {
4928 mb_nest--;
4929 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4930 *expr_end = p;
4931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934
4935 return p;
4936}
4937
4938/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004939 * Expands out the 'magic' {}'s in a variable/function name.
4940 * Note that this can call itself recursively, to deal with
4941 * constructs like foo{bar}{baz}{bam}
4942 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4943 * "in_start" ^
4944 * "expr_start" ^
4945 * "expr_end" ^
4946 * "in_end" ^
4947 *
4948 * Returns a new allocated string, which the caller must free.
4949 * Returns NULL for failure.
4950 */
4951 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004952make_expanded_name(
4953 char_u *in_start,
4954 char_u *expr_start,
4955 char_u *expr_end,
4956 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004957{
4958 char_u c1;
4959 char_u *retval = NULL;
4960 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004961
4962 if (expr_end == NULL || in_end == NULL)
4963 return NULL;
4964 *expr_start = NUL;
4965 *expr_end = NUL;
4966 c1 = *in_end;
4967 *in_end = NUL;
4968
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004969 temp_result = eval_to_string(expr_start + 1, FALSE);
4970 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004971 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004972 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4973 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004974 if (retval != NULL)
4975 {
4976 STRCPY(retval, in_start);
4977 STRCAT(retval, temp_result);
4978 STRCAT(retval, expr_end + 1);
4979 }
4980 }
4981 vim_free(temp_result);
4982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004983 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004984 *expr_start = '{';
4985 *expr_end = '}';
4986
4987 if (retval != NULL)
4988 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004989 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004990 if (expr_start != NULL)
4991 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004992 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004993 temp_result = make_expanded_name(retval, expr_start,
4994 expr_end, temp_result);
4995 vim_free(retval);
4996 retval = temp_result;
4997 }
4998 }
4999
5000 return retval;
5001}
5002
5003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005005 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005007 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005008eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005010 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5011}
5012
5013/*
5014 * Return TRUE if character "c" can be used as the first character in a
5015 * variable or function name (excluding '{' and '}').
5016 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005017 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005018eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005019{
5020 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021}
5022
5023/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005024 * Handle:
5025 * - expr[expr], expr[expr:expr] subscript
5026 * - ".name" lookup
5027 * - function call with Funcref variable: func(expr)
5028 * - method call: var->method()
5029 *
5030 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005031 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005032 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005033handle_subscript(
5034 char_u **arg,
5035 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005036 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005037 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005038{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005039 int evaluate = evalarg != NULL
5040 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005041 int ret = OK;
5042 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005043
Bram Moolenaar61343f02019-07-20 21:11:13 +02005044 // "." is ".name" lookup when we found a dict or when evaluating and
5045 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005046 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005047 && (((**arg == '['
5048 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005049 || (!evaluate
5050 && (*arg)[1] != '.'
5051 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005052 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005053 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005054 && !VIM_ISWHITE(*(*arg - 1)))
5055 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005056 {
5057 if (**arg == '(')
5058 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005059 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5060 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005061
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005062 // Stop the expression evaluation when immediately aborting on
5063 // error, or when an interrupt occurred or an exception was thrown
5064 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005065 if (aborting())
5066 {
5067 if (ret == OK)
5068 clear_tv(rettv);
5069 ret = FAIL;
5070 }
5071 dict_unref(selfdict);
5072 selfdict = NULL;
5073 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005074 else if (**arg == '-')
5075 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005076 if (ret == OK)
5077 {
5078 if ((*arg)[2] == '{')
5079 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005080 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005081 else
5082 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005083 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005084 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005085 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005086 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005087 {
5088 dict_unref(selfdict);
5089 if (rettv->v_type == VAR_DICT)
5090 {
5091 selfdict = rettv->vval.v_dict;
5092 if (selfdict != NULL)
5093 ++selfdict->dv_refcount;
5094 }
5095 else
5096 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005097 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005098 {
5099 clear_tv(rettv);
5100 ret = FAIL;
5101 }
5102 }
5103 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005104
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005105 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5106 // Don't do this when "Func" is already a partial that was bound
5107 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005108 if (selfdict != NULL
5109 && (rettv->v_type == VAR_FUNC
5110 || (rettv->v_type == VAR_PARTIAL
5111 && (rettv->vval.v_partial->pt_auto
5112 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005113 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005114
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005115 dict_unref(selfdict);
5116 return ret;
5117}
5118
5119/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005120 * Make a copy of an item.
5121 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005122 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5123 * reference to an already copied list/dict can be used.
5124 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005125 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005126 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005127item_copy(
5128 typval_T *from,
5129 typval_T *to,
5130 int deep,
5131 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005132{
5133 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005134 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005135
Bram Moolenaar33570922005-01-25 22:26:29 +00005136 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005138 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005139 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005140 }
5141 ++recurse;
5142
5143 switch (from->v_type)
5144 {
5145 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005147 case VAR_STRING:
5148 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005149 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005150 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005151 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005152 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005153 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005154 copy_tv(from, to);
5155 break;
5156 case VAR_LIST:
5157 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005158 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005159 if (from->vval.v_list == NULL)
5160 to->vval.v_list = NULL;
5161 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5162 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005163 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005164 to->vval.v_list = from->vval.v_list->lv_copylist;
5165 ++to->vval.v_list->lv_refcount;
5166 }
5167 else
5168 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5169 if (to->vval.v_list == NULL)
5170 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005171 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005172 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005174 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175 case VAR_DICT:
5176 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005177 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005178 if (from->vval.v_dict == NULL)
5179 to->vval.v_dict = NULL;
5180 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5181 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005182 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005183 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5184 ++to->vval.v_dict->dv_refcount;
5185 }
5186 else
5187 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5188 if (to->vval.v_dict == NULL)
5189 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005190 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005191 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005192 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005194 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005195 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005196 }
5197 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005198 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005199}
5200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201 void
5202echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5203{
5204 char_u *tofree;
5205 char_u numbuf[NUMBUFLEN];
5206 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5207
5208 if (*atstart)
5209 {
5210 *atstart = FALSE;
5211 // Call msg_start() after eval1(), evaluating the expression
5212 // may cause a message to appear.
5213 if (with_space)
5214 {
5215 // Mark the saved text as finishing the line, so that what
5216 // follows is displayed on a new line when scrolling back
5217 // at the more prompt.
5218 msg_sb_eol();
5219 msg_start();
5220 }
5221 }
5222 else if (with_space)
5223 msg_puts_attr(" ", echo_attr);
5224
5225 if (p != NULL)
5226 for ( ; *p != NUL && !got_int; ++p)
5227 {
5228 if (*p == '\n' || *p == '\r' || *p == TAB)
5229 {
5230 if (*p != TAB && *needclr)
5231 {
5232 // remove any text still there from the command
5233 msg_clr_eos();
5234 *needclr = FALSE;
5235 }
5236 msg_putchar_attr(*p, echo_attr);
5237 }
5238 else
5239 {
5240 if (has_mbyte)
5241 {
5242 int i = (*mb_ptr2len)(p);
5243
5244 (void)msg_outtrans_len_attr(p, i, echo_attr);
5245 p += i - 1;
5246 }
5247 else
5248 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5249 }
5250 }
5251 vim_free(tofree);
5252}
5253
Bram Moolenaare9a41262005-01-15 22:18:47 +00005254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 * ":echo expr1 ..." print each argument separated with a space, add a
5256 * newline at the end.
5257 * ":echon expr1 ..." print each argument plain.
5258 */
5259 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005260ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 char_u *p;
5265 int needclr = TRUE;
5266 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005267 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005268 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005269 evalarg_T evalarg;
5270
Bram Moolenaare707c882020-07-01 13:07:37 +02005271 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 if (eap->skip)
5274 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005275 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005277 // If eval1() causes an error message the text from the command may
5278 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005279 need_clr_eos = needclr;
5280
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005282 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {
5284 /*
5285 * Report the invalid expression unless the expression evaluation
5286 * has been cancelled due to an aborting error, an interrupt, or an
5287 * exception.
5288 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005289 if (!aborting() && did_emsg == did_emsg_before
5290 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005291 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005292 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 break;
5294 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005295 need_clr_eos = FALSE;
5296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005298 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005299
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005300 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 arg = skipwhite(arg);
5302 }
5303 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005304 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
5306 if (eap->skip)
5307 --emsg_skip;
5308 else
5309 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005310 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 if (needclr)
5312 msg_clr_eos();
5313 if (eap->cmdidx == CMD_echo)
5314 msg_end();
5315 }
5316}
5317
5318/*
5319 * ":echohl {name}".
5320 */
5321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005322ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005324 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325}
5326
5327/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005328 * Returns the :echo attribute
5329 */
5330 int
5331get_echo_attr(void)
5332{
5333 return echo_attr;
5334}
5335
5336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 * ":execute expr1 ..." execute the result of an expression.
5338 * ":echomsg expr1 ..." Print a message
5339 * ":echoerr expr1 ..." Print an error
5340 * Each gets spaces around each argument and a newline at the end for
5341 * echo commands
5342 */
5343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005344ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345{
5346 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005347 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 int ret = OK;
5349 char_u *p;
5350 garray_T ga;
5351 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352
5353 ga_init2(&ga, 1, 80);
5354
5355 if (eap->skip)
5356 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005357 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005359 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005360 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
5363 if (!eap->skip)
5364 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005365 char_u buf[NUMBUFLEN];
5366
5367 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005368 {
5369 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5370 {
5371 emsg(_(e_inval_string));
5372 p = NULL;
5373 }
5374 else
5375 p = tv_get_string_buf(&rettv, buf);
5376 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005377 else
5378 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005379 if (p == NULL)
5380 {
5381 clear_tv(&rettv);
5382 ret = FAIL;
5383 break;
5384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 len = (int)STRLEN(p);
5386 if (ga_grow(&ga, len + 2) == FAIL)
5387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005388 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 ret = FAIL;
5390 break;
5391 }
5392 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 ga.ga_len += len;
5396 }
5397
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005398 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 arg = skipwhite(arg);
5400 }
5401
5402 if (ret != FAIL && ga.ga_data != NULL)
5403 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005404 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5405 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005406 // Mark the already saved text as finishing the line, so that what
5407 // follows is displayed on a new line when scrolling back at the
5408 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005409 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005410 }
5411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005413 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005414 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005415 out_flush();
5416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 else if (eap->cmdidx == CMD_echoerr)
5418 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005419 int save_did_emsg = did_emsg;
5420
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005421 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005422 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 if (!force_abort)
5424 did_emsg = save_did_emsg;
5425 }
5426 else if (eap->cmdidx == CMD_execute)
5427 do_cmdline((char_u *)ga.ga_data,
5428 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5429 }
5430
5431 ga_clear(&ga);
5432
5433 if (eap->skip)
5434 --emsg_skip;
5435
5436 eap->nextcmd = check_nextcmd(arg);
5437}
5438
5439/*
5440 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5441 * "arg" points to the "&" or '+' when called, to "option" when returning.
5442 * Returns NULL when no option name found. Otherwise pointer to the char
5443 * after the option name.
5444 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005445 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005446find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
5448 char_u *p = *arg;
5449
5450 ++p;
5451 if (*p == 'g' && p[1] == ':')
5452 {
5453 *opt_flags = OPT_GLOBAL;
5454 p += 2;
5455 }
5456 else if (*p == 'l' && p[1] == ':')
5457 {
5458 *opt_flags = OPT_LOCAL;
5459 p += 2;
5460 }
5461 else
5462 *opt_flags = 0;
5463
5464 if (!ASCII_ISALPHA(*p))
5465 return NULL;
5466 *arg = p;
5467
5468 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005469 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 else
5471 while (ASCII_ISALPHA(*p))
5472 ++p;
5473 return p;
5474}
5475
5476/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005477 * Display script name where an item was last set.
5478 * Should only be invoked when 'verbose' is non-zero.
5479 */
5480 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005481last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005482{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005483 char_u *p;
5484
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005485 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005486 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005487 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005488 if (p != NULL)
5489 {
5490 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005491 msg_puts(_("\n\tLast set from "));
5492 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005493 if (script_ctx.sc_lnum > 0)
5494 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005495 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005496 msg_outnum((long)script_ctx.sc_lnum);
5497 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005498 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005499 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005500 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005501 }
5502}
5503
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005504#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
5506/*
5507 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005508 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 * "flags" can be "g" to do a global substitute.
5510 * Returns an allocated string, NULL for error.
5511 */
5512 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005513do_string_sub(
5514 char_u *str,
5515 char_u *pat,
5516 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005517 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005518 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519{
5520 int sublen;
5521 regmatch_T regmatch;
5522 int i;
5523 int do_all;
5524 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005525 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 garray_T ga;
5527 char_u *ret;
5528 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005529 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005531 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005533 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
5535 ga_init2(&ga, 1, 200);
5536
5537 do_all = (flags[0] == 'g');
5538
5539 regmatch.rm_ic = p_ic;
5540 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5541 if (regmatch.regprog != NULL)
5542 {
5543 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005544 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5546 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005547 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005548 if (regmatch.startp[0] == regmatch.endp[0])
5549 {
5550 if (zero_width == regmatch.startp[0])
5551 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005552 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005553 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005554 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5555 (size_t)i);
5556 ga.ga_len += i;
5557 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005558 continue;
5559 }
5560 zero_width = regmatch.startp[0];
5561 }
5562
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 /*
5564 * Get some space for a temporary buffer to do the substitution
5565 * into. It will contain:
5566 * - The text up to where the match is.
5567 * - The substituted text.
5568 * - The text after the match.
5569 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005570 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005571 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5573 {
5574 ga_clear(&ga);
5575 break;
5576 }
5577
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005578 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 i = (int)(regmatch.startp[0] - tail);
5580 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005581 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005582 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 + ga.ga_len + i, TRUE, TRUE, FALSE);
5584 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005585 tail = regmatch.endp[0];
5586 if (*tail == NUL)
5587 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 if (!do_all)
5589 break;
5590 }
5591
5592 if (ga.ga_data != NULL)
5593 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5594
Bram Moolenaar473de612013-06-08 18:19:48 +02005595 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597
5598 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5599 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005600 if (p_cpo == empty_option)
5601 p_cpo = save_cpo;
5602 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005603 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005604 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
5606 return ret;
5607}