blob: 9a2fdf65403dc922822939fd06b0781397c24844 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010025#define NAMESPACE_CHAR (char_u *)"abglstvw"
26
Bram Moolenaar532c7802005-01-27 14:44:31 +000027/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000028 * When recursively copying lists and dicts we need to remember which ones we
29 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000030 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000031 */
32static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020033
Bram Moolenaar071d4272004-06-13 20:20:40 +000034/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000035 * Info used by a ":for" loop.
36 */
Bram Moolenaar33570922005-01-25 22:26:29 +000037typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000038{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010039 int fi_semicolon; // TRUE if ending in '; var]'
40 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020041 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010042 listwatch_T fi_lw; // keep an eye on the item used.
43 list_T *fi_list; // list being used
44 int fi_bi; // index of blob
45 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
54static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
62 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020063 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010064num_divide(varnumber_T n1, varnumber_T n2)
65{
66 varnumber_T result;
67
68 if (n2 == 0) // give an error message?
69 {
70 if (n1 == 0)
71 result = VARNUM_MIN; // similar to NaN
72 else if (n1 < 0)
73 result = -VARNUM_MAX;
74 else
75 result = VARNUM_MAX;
76 }
77 else
78 result = n1 / n2;
79
80 return result;
81}
82
83/*
84 * Return "n1" modulus "n2", taking care of dividing by zero.
85 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020086 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010087num_modulus(varnumber_T n1, varnumber_T n2)
88{
89 // Give an error when n2 is 0?
90 return (n2 == 0) ? 0 : (n1 % n2);
91}
92
Bram Moolenaara1fa8922017-01-12 20:06:33 +010093#if defined(EBCDIC) || defined(PROTO)
94/*
95 * Compare struct fst by function name.
96 */
97 static int
98compare_func_name(const void *s1, const void *s2)
99{
100 struct fst *p1 = (struct fst *)s1;
101 struct fst *p2 = (struct fst *)s2;
102
103 return STRCMP(p1->f_name, p2->f_name);
104}
105
106/*
107 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100108 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100109 * On machines using EBCDIC we have to sort it.
110 */
111 static void
112sortFunctions(void)
113{
114 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
115
116 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
117}
118#endif
119
Bram Moolenaar33570922005-01-25 22:26:29 +0000120/*
121 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000122 */
123 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100124eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000125{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200127 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000128
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200129#ifdef EBCDIC
130 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100131 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200132 */
133 sortFunctions();
134#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000135}
136
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000137#if defined(EXITFREE) || defined(PROTO)
138 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100139eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200141 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100142 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200143 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000144
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200145 // autoloaded script names
146 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000147
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200148 // unreferenced lists and dicts
149 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200150
151 // functions not garbage collected
152 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000153}
154#endif
155
Bram Moolenaare6b53242020-07-01 17:28:33 +0200156 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200157fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
158{
159 CLEAR_FIELD(*evalarg);
160 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
161 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
162 {
163 evalarg->eval_getline = eap->getline;
164 evalarg->eval_cookie = eap->cookie;
165 }
166}
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
169 * Top level evaluation function, returning a boolean.
170 * Sets "error" to TRUE if there was an error.
171 * Return TRUE or FALSE.
172 */
173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100174eval_to_bool(
175 char_u *arg,
176 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200177 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100178 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179{
Bram Moolenaar33570922005-01-25 22:26:29 +0000180 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200181 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200182 evalarg_T evalarg;
183
Bram Moolenaar37c83712020-06-30 21:18:36 +0200184 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186 if (skip)
187 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200188 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 else
191 {
192 *error = FALSE;
193 if (!skip)
194 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100195 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000196 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 }
198 }
199 if (skip)
200 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200201 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200203 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204}
205
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206/*
207 * Call eval1() and give an error message if not done at a lower level.
208 */
209 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200210eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100211{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100212 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100213 int ret;
214 int did_emsg_before = did_emsg;
215 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200216 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100217
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200218 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
219
220 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100221 if (ret == FAIL)
222 {
223 // Report the invalid expression unless the expression evaluation has
224 // been cancelled due to an aborting error, an interrupt, or an
225 // exception, or we already gave a more specific error.
226 // Also check called_emsg for when using assert_fails().
227 if (!aborting() && did_emsg == did_emsg_before
228 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100229 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100230 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200231 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100232 return ret;
233}
234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100235/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200236 * Return whether a typval is a valid expression to pass to eval_expr_typval()
237 * or eval_expr_to_bool(). An empty string returns FALSE;
238 */
239 int
240eval_expr_valid_arg(typval_T *tv)
241{
242 return tv->v_type != VAR_UNKNOWN
243 && (tv->v_type != VAR_STRING
244 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
245}
246
247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 * Evaluate an expression, which can be a function, partial or string.
249 * Pass arguments "argv[argc]".
250 * Return the result in "rettv" and OK or FAIL.
251 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200252 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100253eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
254{
255 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100256 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200257 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100258
259 if (expr->v_type == VAR_FUNC)
260 {
261 s = expr->vval.v_string;
262 if (s == NULL || *s == NUL)
263 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200264 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200265 funcexe.evaluate = TRUE;
266 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100267 return FAIL;
268 }
269 else if (expr->v_type == VAR_PARTIAL)
270 {
271 partial_T *partial = expr->vval.v_partial;
272
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200273 if (partial == NULL)
274 return FAIL;
275
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200277 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200279 if (call_def_function(partial->pt_func, argc, argv,
280 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 return FAIL;
282 }
283 else
284 {
285 s = partial_name(partial);
286 if (s == NULL || *s == NUL)
287 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200288 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100289 funcexe.evaluate = TRUE;
290 funcexe.partial = partial;
291 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
292 return FAIL;
293 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 }
295 else
296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100297 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 if (s == NULL)
299 return FAIL;
300 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200301 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100303 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100304 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200305 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100306 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100307 return FAIL;
308 }
309 }
310 return OK;
311}
312
313/*
314 * Like eval_to_bool() but using a typval_T instead of a string.
315 * Works for string, funcref and partial.
316 */
317 int
318eval_expr_to_bool(typval_T *expr, int *error)
319{
320 typval_T rettv;
321 int res;
322
323 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
324 {
325 *error = TRUE;
326 return FALSE;
327 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100328 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100329 clear_tv(&rettv);
330 return res;
331}
332
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333/*
334 * Top level evaluation function, returning a string. If "skip" is TRUE,
335 * only parsing to "nextcmd" is done, without reporting errors. Return
336 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
337 */
338 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100339eval_to_string_skip(
340 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200341 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100342 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
Bram Moolenaar33570922005-01-25 22:26:29 +0000344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200346 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar37c83712020-06-30 21:18:36 +0200348 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 if (skip)
350 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200351 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 retval = NULL;
353 else
354 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100355 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000356 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 }
358 if (skip)
359 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200360 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
362 return retval;
363}
364
365/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366 * Skip over an expression at "*pp".
367 * Return FAIL for an error, OK otherwise.
368 */
369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100370skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000371{
Bram Moolenaar33570922005-01-25 22:26:29 +0000372 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000373
374 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200375 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000376}
377
378/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200379 * Skip over an expression at "*pp".
380 * If in Vim9 script and line breaks are encountered, the lines are
381 * concatenated. "evalarg->eval_tofree" will be set accordingly.
382 * Return FAIL for an error, OK otherwise.
383 */
384 int
385skip_expr_concatenate(char_u **start, char_u **end, evalarg_T *evalarg)
386{
387 typval_T rettv;
388 int res;
389 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
390 garray_T *gap = &evalarg->eval_ga;
391 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
392
393 if (vim9script && evalarg->eval_cookie != NULL)
394 {
395 ga_init2(gap, sizeof(char_u *), 10);
396 if (ga_grow(gap, 1) == OK)
397 // leave room for "start"
398 ++gap->ga_len;
399 }
400
401 // Don't evaluate the expression.
402 if (evalarg != NULL)
403 evalarg->eval_flags &= ~EVAL_EVALUATE;
404 *end = skipwhite(*end);
405 res = eval1(end, &rettv, evalarg);
406 if (evalarg != NULL)
407 evalarg->eval_flags = save_flags;
408
409 if (vim9script && evalarg->eval_cookie != NULL
410 && evalarg->eval_ga.ga_len > 1)
411 {
412 char_u *p;
413 size_t endoff = STRLEN(*end);
414
415 // Line breaks encountered, concatenate all the lines.
416 *((char_u **)gap->ga_data) = *start;
417 p = ga_concat_strings(gap, "");
418 *((char_u **)gap->ga_data) = NULL;
419 ga_clear_strings(gap);
420 gap->ga_itemsize = 0;
421 if (p == NULL)
422 return FAIL;
423 *start = p;
424 vim_free(evalarg->eval_tofree);
425 evalarg->eval_tofree = p;
426 // Compute "end" relative to the end.
427 *end = *start + STRLEN(*start) - endoff;
428 }
429
430 return res;
431}
432
433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000435 * When "convert" is TRUE convert a List into a sequence of lines and convert
436 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 * Return pointer to allocated memory, or NULL for failure.
438 */
439 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100440eval_to_string(
441 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100442 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443{
Bram Moolenaar33570922005-01-25 22:26:29 +0000444 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000446 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000447#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000448 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200451 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 retval = NULL;
453 else
454 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000455 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000456 {
457 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000458 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200459 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200460 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200461 if (tv.vval.v_list->lv_len > 0)
462 ga_append(&ga, NL);
463 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000464 ga_append(&ga, NUL);
465 retval = (char_u *)ga.ga_data;
466 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000467#ifdef FEAT_FLOAT
468 else if (convert && tv.v_type == VAR_FLOAT)
469 {
470 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
471 retval = vim_strsave(numbuf);
472 }
473#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000474 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100475 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000476 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200478 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479
480 return retval;
481}
482
483/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000484 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200485 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 */
487 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100488eval_to_string_safe(
489 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100490 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200493 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200495 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000496 if (use_sandbox)
497 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200498 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200499 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000500 if (use_sandbox)
501 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200502 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200503 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 return retval;
505}
506
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507/*
508 * Top level evaluation function, returning a number.
509 * Evaluates "expr" silently.
510 * Returns -1 for an error.
511 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200512 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100513eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514{
Bram Moolenaar33570922005-01-25 22:26:29 +0000515 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200516 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000517 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518
519 ++emsg_off;
520
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200521 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 retval = -1;
523 else
524 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100525 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000526 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 }
528 --emsg_off;
529
530 return retval;
531}
532
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000533/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000534 * Top level evaluation function.
535 * Returns an allocated typval_T with the result.
536 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000537 */
538 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200539eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000540{
541 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200542 evalarg_T evalarg;
543
544 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000545
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200546 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200547 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100548 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000549
Bram Moolenaar37c83712020-06-30 21:18:36 +0200550 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000551 return tv;
552}
553
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100555 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200556 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
557 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000558 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200560 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100561call_vim_function(
562 char_u *func,
563 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200564 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200565 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000567 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200568 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100570 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200571 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200572 funcexe.firstline = curwin->w_cursor.lnum;
573 funcexe.lastline = curwin->w_cursor.lnum;
574 funcexe.evaluate = TRUE;
575 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000576 if (ret == FAIL)
577 clear_tv(rettv);
578
579 return ret;
580}
581
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100582/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100583 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100584 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200585 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
586 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100587 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200588 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100589call_func_retnr(
590 char_u *func,
591 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200592 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100593{
594 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200595 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100596
Bram Moolenaarded27a12018-08-01 19:06:03 +0200597 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100598 return -1;
599
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100600 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100601 clear_tv(&rettv);
602 return retval;
603}
604
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000605/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100606 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000607 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200608 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
609 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000610 */
611 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100612call_func_retstr(
613 char_u *func,
614 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200615 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000616{
617 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000618 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000619
Bram Moolenaarded27a12018-08-01 19:06:03 +0200620 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000621 return NULL;
622
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100623 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000624 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 return retval;
626}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000627
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000628/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100629 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200630 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
631 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000632 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000633 */
634 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100635call_func_retlist(
636 char_u *func,
637 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200638 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000639{
640 typval_T rettv;
641
Bram Moolenaarded27a12018-08-01 19:06:03 +0200642 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000643 return NULL;
644
645 if (rettv.v_type != VAR_LIST)
646 {
647 clear_tv(&rettv);
648 return NULL;
649 }
650
651 return rettv.vval.v_list;
652}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#ifdef FEAT_FOLDING
655/*
656 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
657 * it in "*cp". Doesn't give error messages.
658 */
659 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100660eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661{
Bram Moolenaar33570922005-01-25 22:26:29 +0000662 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200663 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000665 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
666 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000669 if (use_sandbox)
670 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200671 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200673 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 retval = 0;
675 else
676 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100677 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000678 if (tv.v_type == VAR_NUMBER)
679 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000680 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 retval = 0;
682 else
683 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100684 // If the result is a string, check if there is a non-digit before
685 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000686 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 if (!VIM_ISDIGIT(*s) && *s != '-')
688 *cp = *s++;
689 retval = atol((char *)s);
690 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000691 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 }
693 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000694 if (use_sandbox)
695 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200696 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200697 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200699 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700}
701#endif
702
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000704 * Get an lval: variable, Dict item or List item that can be assigned a value
705 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
706 * "name.key", "name.key[expr]" etc.
707 * Indexing only works if "name" is an existing List or Dictionary.
708 * "name" points to the start of the name.
709 * If "rettv" is not NULL it points to the value to be assigned.
710 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
711 * wrong; must end in space or cmd separator.
712 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100713 * flags:
714 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100715 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100716 * GLV_NO_AUTOLOAD: do not use script autoloading
717 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000718 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000719 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000720 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000721 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200722 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100723get_lval(
724 char_u *name,
725 typval_T *rettv,
726 lval_T *lp,
727 int unlet,
728 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100729 int flags, // GLV_ values
730 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000731{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000732 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000733 char_u *expr_start, *expr_end;
734 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000735 dictitem_T *v;
736 typval_T var1;
737 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000738 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000739 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000740 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000741 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000742 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100743 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100745 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200746 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000747
748 if (skip)
749 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100750 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000751 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200752 lp->ll_name_end = find_name_end(name, NULL, NULL,
753 FNE_INCL_BR | fne_flags);
754 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000755 }
756
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100757 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000758 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000760 if (expr_start != NULL)
761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100762 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100763 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000764 && *p != '[' && *p != '.')
765 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100766 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000767 return NULL;
768 }
769
770 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
771 if (lp->ll_exp_name == NULL)
772 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100773 // Report an invalid expression in braces, unless the
774 // expression evaluation has been cancelled due to an
775 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000776 if (!aborting() && !quiet)
777 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000778 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100779 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000780 return NULL;
781 }
782 }
783 lp->ll_name = lp->ll_exp_name;
784 }
785 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000787 lp->ll_name = name;
788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
790 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100791 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792 char_u *tp = skipwhite(p + 1);
793
794 // parse the type after the name
795 lp->ll_type = parse_type(&tp, &si->sn_type_list);
796 lp->ll_name_end = tp;
797 }
798 }
799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100800 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
802 return p;
803
804 cc = *p;
805 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100806 // Only pass &ht when we would write to the variable, it prevents autoload
807 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100808 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
809 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000810 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100811 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000812 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000813 if (v == NULL)
814 return NULL;
815
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 /*
817 * Loop until no more [idx] or .key is following.
818 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000819 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100820 var1.v_type = VAR_UNKNOWN;
821 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000822 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000823 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000824 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
825 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100826 && lp->ll_tv->vval.v_dict != NULL)
827 && !(lp->ll_tv->v_type == VAR_BLOB
828 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000829 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000830 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100831 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000833 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000834 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100837 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000838 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000839 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000840
Bram Moolenaar8c711452005-01-14 21:53:12 +0000841 len = -1;
842 if (*p == '.')
843 {
844 key = p + 1;
845 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
846 ;
847 if (len == 0)
848 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000849 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100850 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000852 }
853 p = key + len;
854 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000855 else
856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100857 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000858 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000859 if (*p == ':')
860 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000861 else
862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000863 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200864 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100866 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000867 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100868 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000869 clear_tv(&var1);
870 return NULL;
871 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000872 }
873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100874 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000875 if (*p == ':')
876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100880 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100881 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000883 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100884 if (rettv != NULL
885 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100886 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100887 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100888 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000889 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100891 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100892 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000894 }
895 p = skipwhite(p + 1);
896 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000898 else
899 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200901 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200902 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000903 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100904 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000905 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000906 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100907 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000908 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100909 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100910 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000911 clear_tv(&var2);
912 return NULL;
913 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000914 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000916 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000917 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000918 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000919
Bram Moolenaar8c711452005-01-14 21:53:12 +0000920 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100923 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100924 clear_tv(&var1);
925 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000926 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000927 }
928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100929 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000930 ++p;
931 }
932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000934 {
935 if (len == -1)
936 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100937 // "[key]": get key from "var1"
938 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200939 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000940 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000941 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000943 }
944 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000945 lp->ll_list = NULL;
946 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000947 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100949 // When assigning to a scope dictionary check that a function and
950 // variable name is valid (only variable name unless it is l: or
951 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200952 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200953 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200954 int prevval;
955 int wrong;
956
957 if (len != -1)
958 {
959 prevval = key[len];
960 key[len] = NUL;
961 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200962 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100963 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200964 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
965 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200966 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200967 || !valid_varname(key);
968 if (len != -1)
969 key[len] = prevval;
970 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200971 {
972 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200973 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200974 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200975 }
976
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100979 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200980 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100981 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100983 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100984 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200985 return NULL;
986 }
987
Bram Moolenaar31b81602019-02-10 22:14:27 +0100988 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000989 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000991 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100992 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100993 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000995 }
996 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001000 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001001 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 p = NULL;
1003 break;
1004 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001005 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001006 else if ((flags & GLV_READ_ONLY) == 0
1007 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001008 {
1009 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001010 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001011 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001012
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001013 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001015 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001016 else if (lp->ll_tv->v_type == VAR_BLOB)
1017 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001018 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1019
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001020 /*
1021 * Get the number and item for the only or first index of the List.
1022 */
1023 if (empty1)
1024 lp->ll_n1 = 0;
1025 else
1026 // is number or string
1027 lp->ll_n1 = (long)tv_get_number(&var1);
1028 clear_tv(&var1);
1029
1030 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001031 || lp->ll_n1 > bloblen
1032 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001033 {
1034 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001035 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001036 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001037 return NULL;
1038 }
1039 if (lp->ll_range && !lp->ll_empty2)
1040 {
1041 lp->ll_n2 = (long)tv_get_number(&var2);
1042 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001043 if (lp->ll_n2 < 0
1044 || lp->ll_n2 >= bloblen
1045 || lp->ll_n2 < lp->ll_n1)
1046 {
1047 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001048 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001049 return NULL;
1050 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001051 }
1052 lp->ll_blob = lp->ll_tv->vval.v_blob;
1053 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001054 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001055 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 else
1057 {
1058 /*
1059 * Get the number and item for the only or first index of the List.
1060 */
1061 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001063 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001064 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001065 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001066 clear_tv(&var1);
1067
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001068 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001069 lp->ll_list = lp->ll_tv->vval.v_list;
1070 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1071 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001072 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001073 if (lp->ll_n1 < 0)
1074 {
1075 lp->ll_n1 = 0;
1076 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1077 }
1078 }
1079 if (lp->ll_li == NULL)
1080 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001081 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001082 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001083 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001084 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001085 }
1086
1087 /*
1088 * May need to find the item or absolute index for the second
1089 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001090 * When no index given: "lp->ll_empty2" is TRUE.
1091 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001092 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001095 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001096 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001098 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001099 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001100 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001101 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001102 {
1103 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001104 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001105 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001106 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001107 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001108 }
1109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001110 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 if (lp->ll_n1 < 0)
1112 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1113 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001114 {
1115 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001116 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001117 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001118 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001119 }
1120
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001121 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001122 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001123 }
1124
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001125 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 return p;
1128}
1129
1130/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001131 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001132 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001134clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135{
1136 vim_free(lp->ll_exp_name);
1137 vim_free(lp->ll_newkey);
1138}
1139
1140/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001141 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001143 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1144 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001147set_var_lval(
1148 lval_T *lp,
1149 char_u *endp,
1150 typval_T *rettv,
1151 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001153 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154{
1155 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001156 listitem_T *ri;
1157 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158
1159 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001161 cc = *endp;
1162 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001163 if (lp->ll_blob != NULL)
1164 {
1165 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001166
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001167 if (op != NULL && *op != '=')
1168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001169 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001170 return;
1171 }
1172
1173 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1174 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001175 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001176
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001177 if (lp->ll_empty2)
1178 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1179
1180 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001181 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001182 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001183 return;
1184 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001185 if (lp->ll_empty2)
1186 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001187
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001188 ir = 0;
1189 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1190 blob_set(lp->ll_blob, il,
1191 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001192 }
1193 else
1194 {
1195 val = (int)tv_get_number_chk(rettv, &error);
1196 if (!error)
1197 {
1198 garray_T *gap = &lp->ll_blob->bv_ga;
1199
1200 // Allow for appending a byte. Setting a byte beyond
1201 // the end is an error otherwise.
1202 if (lp->ll_n1 < gap->ga_len
1203 || (lp->ll_n1 == gap->ga_len
1204 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1205 {
1206 blob_set(lp->ll_blob, lp->ll_n1, val);
1207 if (lp->ll_n1 == gap->ga_len)
1208 ++gap->ga_len;
1209 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001210 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001211 }
1212 }
1213 }
1214 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001215 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001216 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001219 {
1220 emsg(_(e_cannot_mod));
1221 *endp = cc;
1222 return;
1223 }
1224
Bram Moolenaarff697e62019-02-12 22:28:33 +01001225 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001226 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001227 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001228 &tv, &di, TRUE, FALSE) == OK)
1229 {
1230 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001231 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1232 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001233 && tv_op(&tv, rettv, op) == OK)
1234 set_var(lp->ll_name, &tv, FALSE);
1235 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001236 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001238 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001240 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001242 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001243 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001244 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001245 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 else if (lp->ll_range)
1247 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001248 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001249 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001252 {
1253 emsg(_("E996: Cannot lock a range"));
1254 return;
1255 }
1256
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001257 /*
1258 * Check whether any of the list items is locked
1259 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001260 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001261 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001262 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001263 return;
1264 ri = ri->li_next;
1265 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1266 break;
1267 ll_li = ll_li->li_next;
1268 ++ll_n1;
1269 }
1270
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271 /*
1272 * Assign the List values to the list items.
1273 */
1274 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001275 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001276 if (op != NULL && *op != '=')
1277 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1278 else
1279 {
1280 clear_tv(&lp->ll_li->li_tv);
1281 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1282 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001283 ri = ri->li_next;
1284 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1285 break;
1286 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001287 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001288 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001289 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001290 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001291 ri = NULL;
1292 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001293 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001294 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295 lp->ll_li = lp->ll_li->li_next;
1296 ++lp->ll_n1;
1297 }
1298 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001299 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001300 else if (lp->ll_empty2
1301 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001302 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001303 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304 }
1305 else
1306 {
1307 /*
1308 * Assign to a List or Dictionary item.
1309 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001311 {
1312 emsg(_("E996: Cannot lock a list or dict"));
1313 return;
1314 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315 if (lp->ll_newkey != NULL)
1316 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001317 if (op != NULL && *op != '=')
1318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001319 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001320 return;
1321 }
1322
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001323 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001324 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 if (di == NULL)
1326 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001327 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1328 {
1329 vim_free(di);
1330 return;
1331 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001333 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334 else if (op != NULL && *op != '=')
1335 {
1336 tv_op(lp->ll_tv, rettv, op);
1337 return;
1338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001341
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 /*
1343 * Assign the value to the variable or list item.
1344 */
1345 if (copy)
1346 copy_tv(rettv, lp->ll_tv);
1347 else
1348 {
1349 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001350 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001351 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 }
1353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354}
1355
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001356/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001357 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1358 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359 * Returns OK or FAIL.
1360 */
1361 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001362tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001363{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001364 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001365 char_u numbuf[NUMBUFLEN];
1366 char_u *s;
1367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001368 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001369 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001370 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001371 {
1372 switch (tv1->v_type)
1373 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001374 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001375 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001377 case VAR_DICT:
1378 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001379 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001380 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001381 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001382 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001383 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001384 break;
1385
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001386 case VAR_BLOB:
1387 if (*op != '+' || tv2->v_type != VAR_BLOB)
1388 break;
1389 // BLOB += BLOB
1390 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1391 {
1392 blob_T *b1 = tv1->vval.v_blob;
1393 blob_T *b2 = tv2->vval.v_blob;
1394 int i, len = blob_len(b2);
1395 for (i = 0; i < len; i++)
1396 ga_append(&b1->bv_ga, blob_get(b2, i));
1397 }
1398 return OK;
1399
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001400 case VAR_LIST:
1401 if (*op != '+' || tv2->v_type != VAR_LIST)
1402 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001403 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001404 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1405 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1406 return OK;
1407
1408 case VAR_NUMBER:
1409 case VAR_STRING:
1410 if (tv2->v_type == VAR_LIST)
1411 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001412 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001413 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001414 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001415 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001416#ifdef FEAT_FLOAT
1417 if (tv2->v_type == VAR_FLOAT)
1418 {
1419 float_T f = n;
1420
Bram Moolenaarff697e62019-02-12 22:28:33 +01001421 if (*op == '%')
1422 break;
1423 switch (*op)
1424 {
1425 case '+': f += tv2->vval.v_float; break;
1426 case '-': f -= tv2->vval.v_float; break;
1427 case '*': f *= tv2->vval.v_float; break;
1428 case '/': f /= tv2->vval.v_float; break;
1429 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001430 clear_tv(tv1);
1431 tv1->v_type = VAR_FLOAT;
1432 tv1->vval.v_float = f;
1433 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001434 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001435#endif
1436 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001437 switch (*op)
1438 {
1439 case '+': n += tv_get_number(tv2); break;
1440 case '-': n -= tv_get_number(tv2); break;
1441 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001442 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1443 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001444 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001445 clear_tv(tv1);
1446 tv1->v_type = VAR_NUMBER;
1447 tv1->vval.v_number = n;
1448 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 }
1450 else
1451 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001452 if (tv2->v_type == VAR_FLOAT)
1453 break;
1454
Bram Moolenaarff697e62019-02-12 22:28:33 +01001455 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001456 s = tv_get_string(tv1);
1457 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458 clear_tv(tv1);
1459 tv1->v_type = VAR_STRING;
1460 tv1->vval.v_string = s;
1461 }
1462 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001463
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001464 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001465#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001466 {
1467 float_T f;
1468
Bram Moolenaarff697e62019-02-12 22:28:33 +01001469 if (*op == '%' || *op == '.'
1470 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001471 && tv2->v_type != VAR_NUMBER
1472 && tv2->v_type != VAR_STRING))
1473 break;
1474 if (tv2->v_type == VAR_FLOAT)
1475 f = tv2->vval.v_float;
1476 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001477 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001478 switch (*op)
1479 {
1480 case '+': tv1->vval.v_float += f; break;
1481 case '-': tv1->vval.v_float -= f; break;
1482 case '*': tv1->vval.v_float *= f; break;
1483 case '/': tv1->vval.v_float /= f; break;
1484 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001485 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001486#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001487 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001488 }
1489 }
1490
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001491 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492 return FAIL;
1493}
1494
1495/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001496 * Evaluate the expression used in a ":for var in expr" command.
1497 * "arg" points to "var".
1498 * Set "*errp" to TRUE for an error, FALSE otherwise;
1499 * Return a pointer that holds the info. Null when there is an error.
1500 */
1501 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001502eval_for_line(
1503 char_u *arg,
1504 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001505 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001506 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001507{
Bram Moolenaar33570922005-01-25 22:26:29 +00001508 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001509 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001510 typval_T tv;
1511 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001512 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001514 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001515
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001516 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517 if (fi == NULL)
1518 return NULL;
1519
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001520 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001521 if (expr == NULL)
1522 return fi;
1523
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001524 expr = skipwhite_and_linebreak(expr, evalarg);
1525 if (expr[0] != 'i' || expr[1] != 'n'
1526 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001527 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001529 return fi;
1530 }
1531
1532 if (skip)
1533 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001534 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1535 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001536 {
1537 *errp = FALSE;
1538 if (!skip)
1539 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001540 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001541 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001542 l = tv.vval.v_list;
1543 if (l == NULL)
1544 {
1545 // a null list is like an empty list: do nothing
1546 clear_tv(&tv);
1547 }
1548 else
1549 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001551 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001553 // No need to increment the refcount, it's already set for
1554 // the list being used in "tv".
1555 fi->fi_list = l;
1556 list_add_watch(l, &fi->fi_lw);
1557 fi->fi_lw.lw_item = l->lv_first;
1558 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001559 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001560 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001561 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001562 fi->fi_bi = 0;
1563 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001564 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001565 typval_T btv;
1566
1567 // Make a copy, so that the iteration still works when the
1568 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001570 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001571 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001572 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001573 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001574 else
1575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001576 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001577 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001578 }
1579 }
1580 }
1581 if (skip)
1582 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001583 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001584
1585 return fi;
1586}
1587
1588/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001589 * Used when looping over a :for line, skip the "in expr" part.
1590 */
1591 void
1592skip_for_lines(void *fi_void, evalarg_T *evalarg)
1593{
1594 forinfo_T *fi = (forinfo_T *)fi_void;
1595 int i;
1596
1597 for (i = 0; i < fi->fi_break_count; ++i)
1598 eval_next_line(evalarg);
1599}
1600
1601/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001602 * Use the first item in a ":for" list. Advance to the next.
1603 * Assign the values to the variable (list). "arg" points to the first one.
1604 * Return TRUE when a valid item was found, FALSE when at end of list or
1605 * something wrong.
1606 */
1607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001608next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001609{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001610 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001611 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001612 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1613 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001614 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001615
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001616 if (fi->fi_blob != NULL)
1617 {
1618 typval_T tv;
1619
1620 if (fi->fi_bi >= blob_len(fi->fi_blob))
1621 return FALSE;
1622 tv.v_type = VAR_NUMBER;
1623 tv.v_lock = VAR_FIXED;
1624 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1625 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001626 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001627 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001628 }
1629
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630 item = fi->fi_lw.lw_item;
1631 if (item == NULL)
1632 result = FALSE;
1633 else
1634 {
1635 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001636 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001637 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638 }
1639 return result;
1640}
1641
1642/*
1643 * Free the structure used to store info used by ":for".
1644 */
1645 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001646free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001647{
Bram Moolenaar33570922005-01-25 22:26:29 +00001648 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001649
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001650 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001651 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001653 list_unref(fi->fi_list);
1654 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001655 if (fi != NULL && fi->fi_blob != NULL)
1656 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657 vim_free(fi);
1658}
1659
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001661set_context_for_expression(
1662 expand_T *xp,
1663 char_u *arg,
1664 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
1666 int got_eq = FALSE;
1667 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001670 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 {
1672 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001673 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001675 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001676 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 {
1678 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001679 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001680 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 break;
1682 }
1683 return;
1684 }
1685 }
1686 else
1687 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1688 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 while ((xp->xp_pattern = vim_strpbrk(arg,
1690 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1691 {
1692 c = *xp->xp_pattern;
1693 if (c == '&')
1694 {
1695 c = xp->xp_pattern[1];
1696 if (c == '&')
1697 {
1698 ++xp->xp_pattern;
1699 xp->xp_context = cmdidx != CMD_let || got_eq
1700 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1701 }
1702 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001705 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1706 xp->xp_pattern += 2;
1707
1708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
1710 else if (c == '$')
1711 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001712 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 xp->xp_context = EXPAND_ENV_VARS;
1714 }
1715 else if (c == '=')
1716 {
1717 got_eq = TRUE;
1718 xp->xp_context = EXPAND_EXPRESSION;
1719 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001720 else if (c == '#'
1721 && xp->xp_context == EXPAND_EXPRESSION)
1722 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001723 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001724 break;
1725 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001726 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 && xp->xp_context == EXPAND_FUNCTIONS
1728 && vim_strchr(xp->xp_pattern, '(') == NULL)
1729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001730 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 break;
1732 }
1733 else if (cmdidx != CMD_let || got_eq)
1734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001735 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 {
1737 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1738 if (c == '\\' && xp->xp_pattern[1] != NUL)
1739 ++xp->xp_pattern;
1740 xp->xp_context = EXPAND_NOTHING;
1741 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001742 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001744 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1746 /* skip */ ;
1747 xp->xp_context = EXPAND_NOTHING;
1748 }
1749 else if (c == '|')
1750 {
1751 if (xp->xp_pattern[1] == '|')
1752 {
1753 ++xp->xp_pattern;
1754 xp->xp_context = EXPAND_EXPRESSION;
1755 }
1756 else
1757 xp->xp_context = EXPAND_COMMANDS;
1758 }
1759 else
1760 xp->xp_context = EXPAND_EXPRESSION;
1761 }
1762 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001763 // Doesn't look like something valid, expand as an expression
1764 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 arg = xp->xp_pattern;
1767 if (*arg != NUL)
1768 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1769 /* skip */ ;
1770 }
1771 xp->xp_pattern = arg;
1772}
1773
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001775 * Return TRUE if "pat" matches "text".
1776 * Does not use 'cpo' and always uses 'magic'.
1777 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001778 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001779pattern_match(char_u *pat, char_u *text, int ic)
1780{
1781 int matches = FALSE;
1782 char_u *save_cpo;
1783 regmatch_T regmatch;
1784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001785 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001786 save_cpo = p_cpo;
1787 p_cpo = (char_u *)"";
1788 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1789 if (regmatch.regprog != NULL)
1790 {
1791 regmatch.rm_ic = ic;
1792 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1793 vim_regfree(regmatch.regprog);
1794 }
1795 p_cpo = save_cpo;
1796 return matches;
1797}
1798
1799/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001800 * Handle a name followed by "(". Both for just "name(arg)" and for
1801 * "expr->name(arg)".
1802 * Returns OK or FAIL.
1803 */
1804 static int
1805eval_func(
1806 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001807 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001808 char_u *name,
1809 int name_len,
1810 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001811 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001812 typval_T *basetv) // "expr" for "expr->name(arg)"
1813{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001814 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001815 char_u *s = name;
1816 int len = name_len;
1817 partial_T *partial;
1818 int ret = OK;
1819
1820 if (!evaluate)
1821 check_vars(s, len);
1822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001823 // If "s" is the name of a variable of type VAR_FUNC
1824 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001825 s = deref_func_name(s, &len, &partial, !evaluate);
1826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001827 // Need to make a copy, in case evaluating the arguments makes
1828 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001829 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001830 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001831 ret = FAIL;
1832 else
1833 {
1834 funcexe_T funcexe;
1835
1836 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001837 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001838 funcexe.firstline = curwin->w_cursor.lnum;
1839 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001840 funcexe.evaluate = evaluate;
1841 funcexe.partial = partial;
1842 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001843 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001844 }
1845 vim_free(s);
1846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001847 // If evaluate is FALSE rettv->v_type was not set in
1848 // get_func_tv, but it's needed in handle_subscript() to parse
1849 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001850 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1851 {
1852 rettv->vval.v_string = NULL;
1853 rettv->v_type = VAR_FUNC;
1854 }
1855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001856 // Stop the expression evaluation when immediately
1857 // aborting on error, or when an interrupt occurred or
1858 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001859 if (evaluate && aborting())
1860 {
1861 if (ret == OK)
1862 clear_tv(rettv);
1863 ret = FAIL;
1864 }
1865 return ret;
1866}
1867
1868/*
Bram 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 Moolenaar9a78e6d2020-07-01 18:29:55 +02002904 case '.': ret = eval_number(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 Moolenaar9a78e6d2020-07-01 18:29:55 +02002915 case '"': ret = eval_string(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 Moolenaar9a78e6d2020-07-01 18:29:55 +02002921 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002922 break;
2923
2924 /*
2925 * List: [expr, expr]
2926 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002927 case '[': ret = eval_list(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 Moolenaar9a78e6d2020-07-01 18:29:55 +02002954 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 break;
2956
2957 /*
2958 * Environment variable: $VAR.
2959 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002960 case '$': ret = eval_env_var(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 Moolenaar9a78e6d2020-07-01 18:29:55 +02003015 if (**arg == '(')
3016 // "name(..." recursive!
Bram Moolenaare6b53242020-07-01 17:28:33 +02003017 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003018 else if (flags & EVAL_CONSTANT)
3019 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003020 else if (evaluate)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003021 // get value of variable
3022 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003024 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003025 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003026 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003028 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003029 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003030 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003031 }
3032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 *arg = skipwhite(*arg);
3034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003035 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3036 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003037 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003038 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
3040 /*
3041 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3042 */
3043 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003044 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003045 return ret;
3046}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003047
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003048/*
3049 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003050 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003051 * Adjusts "end_leaderp" until it is at "start_leader".
3052 */
3053 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003054eval7_leader(
3055 typval_T *rettv,
3056 int numeric_only,
3057 char_u *start_leader,
3058 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003059{
3060 char_u *end_leader = *end_leaderp;
3061 int ret = OK;
3062 int error = FALSE;
3063 varnumber_T val = 0;
3064#ifdef FEAT_FLOAT
3065 float_T f = 0.0;
3066
3067 if (rettv->v_type == VAR_FLOAT)
3068 f = rettv->vval.v_float;
3069 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003070#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003071 val = tv_get_number_chk(rettv, &error);
3072 if (error)
3073 {
3074 clear_tv(rettv);
3075 ret = FAIL;
3076 }
3077 else
3078 {
3079 while (end_leader > start_leader)
3080 {
3081 --end_leader;
3082 if (*end_leader == '!')
3083 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003084 if (numeric_only)
3085 {
3086 ++end_leader;
3087 break;
3088 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003089#ifdef FEAT_FLOAT
3090 if (rettv->v_type == VAR_FLOAT)
3091 f = !f;
3092 else
3093#endif
3094 val = !val;
3095 }
3096 else if (*end_leader == '-')
3097 {
3098#ifdef FEAT_FLOAT
3099 if (rettv->v_type == VAR_FLOAT)
3100 f = -f;
3101 else
3102#endif
3103 val = -val;
3104 }
3105 }
3106#ifdef FEAT_FLOAT
3107 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003109 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003110 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003112 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003113#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003114 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003115 clear_tv(rettv);
3116 rettv->v_type = VAR_NUMBER;
3117 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003120 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 return ret;
3122}
3123
3124/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003125 * Call the function referred to in "rettv".
3126 */
3127 static int
3128call_func_rettv(
3129 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003130 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003131 typval_T *rettv,
3132 int evaluate,
3133 dict_T *selfdict,
3134 typval_T *basetv)
3135{
3136 partial_T *pt = NULL;
3137 funcexe_T funcexe;
3138 typval_T functv;
3139 char_u *s;
3140 int ret;
3141
3142 // need to copy the funcref so that we can clear rettv
3143 if (evaluate)
3144 {
3145 functv = *rettv;
3146 rettv->v_type = VAR_UNKNOWN;
3147
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003148 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003149 if (functv.v_type == VAR_PARTIAL)
3150 {
3151 pt = functv.vval.v_partial;
3152 s = partial_name(pt);
3153 }
3154 else
3155 s = functv.vval.v_string;
3156 }
3157 else
3158 s = (char_u *)"";
3159
Bram Moolenaara80faa82020-04-12 19:37:17 +02003160 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003161 funcexe.firstline = curwin->w_cursor.lnum;
3162 funcexe.lastline = curwin->w_cursor.lnum;
3163 funcexe.evaluate = evaluate;
3164 funcexe.partial = pt;
3165 funcexe.selfdict = selfdict;
3166 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003167 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003168
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003169 // Clear the funcref afterwards, so that deleting it while
3170 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003171 if (evaluate)
3172 clear_tv(&functv);
3173
3174 return ret;
3175}
3176
3177/*
3178 * Evaluate "->method()".
3179 * "*arg" points to the '-'.
3180 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3181 */
3182 static int
3183eval_lambda(
3184 char_u **arg,
3185 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003186 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003187 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003188{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003189 int evaluate = evalarg != NULL
3190 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003191 typval_T base = *rettv;
3192 int ret;
3193
3194 // Skip over the ->.
3195 *arg += 2;
3196 rettv->v_type = VAR_UNKNOWN;
3197
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003198 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003199 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003200 return FAIL;
3201 else if (**arg != '(')
3202 {
3203 if (verbose)
3204 {
3205 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003206 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003207 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003209 }
3210 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003211 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003212 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003213 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003214 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003215
3216 // Clear the funcref afterwards, so that deleting it while
3217 // evaluating the arguments is possible (see test55).
3218 if (evaluate)
3219 clear_tv(&base);
3220
3221 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003222}
3223
3224/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003225 * Evaluate "->method()".
3226 * "*arg" points to the '-'.
3227 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3228 */
3229 static int
3230eval_method(
3231 char_u **arg,
3232 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003233 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003234 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003235{
3236 char_u *name;
3237 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003238 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003239 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003240 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003241 int evaluate = evalarg != NULL
3242 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003243
3244 // Skip over the ->.
3245 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003246 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003247
Bram Moolenaarac92e252019-08-03 21:58:38 +02003248 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003249 len = get_name_len(arg, &alias, evaluate, TRUE);
3250 if (alias != NULL)
3251 name = alias;
3252
3253 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003254 {
3255 if (verbose)
3256 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003257 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003258 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003259 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003260 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003261 if (**arg != '(')
3262 {
3263 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003265 ret = FAIL;
3266 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003267 else if (VIM_ISWHITE((*arg)[-1]))
3268 {
3269 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003270 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003271 ret = FAIL;
3272 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003273 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003274 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003275 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003276 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003277
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003278 // Clear the funcref afterwards, so that deleting it while
3279 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003280 if (evaluate)
3281 clear_tv(&base);
3282
Bram Moolenaarac92e252019-08-03 21:58:38 +02003283 return ret;
3284}
3285
3286/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003287 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3288 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003289 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3290 */
3291 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003292eval_index(
3293 char_u **arg,
3294 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003295 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003296 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003297{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003298 int evaluate = evalarg != NULL
3299 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003300 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003301 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003302 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003303 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003304 long len = -1;
3305 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003306 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003307 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003308
Bram Moolenaara03f2332016-02-06 18:09:59 +01003309 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003310 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003311 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003312 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003313 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003314 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003315 return FAIL;
3316 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003317#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003318 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003319 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003320 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003321#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003322 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003323 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003324 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003325 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003326 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003327 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003328 return FAIL;
3329 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003330 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003332 if (evaluate)
3333 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003334 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003335
3336 case VAR_STRING:
3337 case VAR_NUMBER:
3338 case VAR_LIST:
3339 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003340 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003341 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003342 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003343
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003344 init_tv(&var1);
3345 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003346 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003348 /*
3349 * dict.name
3350 */
3351 key = *arg + 1;
3352 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3353 ;
3354 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003355 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003356 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003357 }
3358 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003359 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003360 /*
3361 * something[idx]
3362 *
3363 * Get the (first) variable from inside the [].
3364 */
3365 *arg = skipwhite(*arg + 1);
3366 if (**arg == ':')
3367 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003368 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003369 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003370 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003371 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003372 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003373 clear_tv(&var1);
3374 return FAIL;
3375 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003376
3377 /*
3378 * Get the second variable from inside the [:].
3379 */
3380 if (**arg == ':')
3381 {
3382 range = TRUE;
3383 *arg = skipwhite(*arg + 1);
3384 if (**arg == ']')
3385 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003386 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003388 if (!empty1)
3389 clear_tv(&var1);
3390 return FAIL;
3391 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003392 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003393 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003394 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003395 if (!empty1)
3396 clear_tv(&var1);
3397 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003398 return FAIL;
3399 }
3400 }
3401
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003402 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003403 if (**arg != ']')
3404 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003405 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003406 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003407 clear_tv(&var1);
3408 if (range)
3409 clear_tv(&var2);
3410 return FAIL;
3411 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003412 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003413 }
3414
3415 if (evaluate)
3416 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003417 n1 = 0;
3418 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003419 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003420 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003421 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003422 }
3423 if (range)
3424 {
3425 if (empty2)
3426 n2 = -1;
3427 else
3428 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003429 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003430 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003431 }
3432 }
3433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003434 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003435 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003436 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003437 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003439 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003440 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003441 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003442 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003443 case VAR_SPECIAL:
3444 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003445 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003446 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003447
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003448 case VAR_NUMBER:
3449 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003450 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003451 len = (long)STRLEN(s);
3452 if (range)
3453 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003454 // The resulting variable is a substring. If the indexes
3455 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003456 if (n1 < 0)
3457 {
3458 n1 = len + n1;
3459 if (n1 < 0)
3460 n1 = 0;
3461 }
3462 if (n2 < 0)
3463 n2 = len + n2;
3464 else if (n2 >= len)
3465 n2 = len;
3466 if (n1 >= len || n2 < 0 || n1 > n2)
3467 s = NULL;
3468 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003469 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003470 }
3471 else
3472 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003473 // The resulting variable is a string of a single
3474 // character. If the index is too big or negative the
3475 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003476 if (n1 >= len || n1 < 0)
3477 s = NULL;
3478 else
3479 s = vim_strnsave(s + n1, 1);
3480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 clear_tv(rettv);
3482 rettv->v_type = VAR_STRING;
3483 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003484 break;
3485
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003486 case VAR_BLOB:
3487 len = blob_len(rettv->vval.v_blob);
3488 if (range)
3489 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003490 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003491 // are out of range the result is empty.
3492 if (n1 < 0)
3493 {
3494 n1 = len + n1;
3495 if (n1 < 0)
3496 n1 = 0;
3497 }
3498 if (n2 < 0)
3499 n2 = len + n2;
3500 else if (n2 >= len)
3501 n2 = len - 1;
3502 if (n1 >= len || n2 < 0 || n1 > n2)
3503 {
3504 clear_tv(rettv);
3505 rettv->v_type = VAR_BLOB;
3506 rettv->vval.v_blob = NULL;
3507 }
3508 else
3509 {
3510 blob_T *blob = blob_alloc();
3511
3512 if (blob != NULL)
3513 {
3514 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3515 {
3516 blob_free(blob);
3517 return FAIL;
3518 }
3519 blob->bv_ga.ga_len = n2 - n1 + 1;
3520 for (i = n1; i <= n2; i++)
3521 blob_set(blob, i - n1,
3522 blob_get(rettv->vval.v_blob, i));
3523
3524 clear_tv(rettv);
3525 rettv_blob_set(rettv, blob);
3526 }
3527 }
3528 }
3529 else
3530 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003531 // The resulting variable is a byte value.
3532 // If the index is too big or negative that is an error.
3533 if (n1 < 0)
3534 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003535 if (n1 < len && n1 >= 0)
3536 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003537 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003538
3539 clear_tv(rettv);
3540 rettv->v_type = VAR_NUMBER;
3541 rettv->vval.v_number = v;
3542 }
3543 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003544 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003545 }
3546 break;
3547
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003549 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003550 if (n1 < 0)
3551 n1 = len + n1;
3552 if (!empty1 && (n1 < 0 || n1 >= len))
3553 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003554 // For a range we allow invalid values and return an empty
3555 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003556 if (!range)
3557 {
3558 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003559 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003560 return FAIL;
3561 }
3562 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003563 }
3564 if (range)
3565 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003566 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003567
3568 if (n2 < 0)
3569 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003570 else if (n2 >= len)
3571 n2 = len - 1;
3572 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003573 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003574 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003575 if (l == NULL)
3576 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003577 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003578 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003579 }
3580 else
3581 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003582 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003583 clear_tv(rettv);
3584 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003585 }
3586 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003587
3588 case VAR_DICT:
3589 if (range)
3590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003591 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003592 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003593 if (len == -1)
3594 clear_tv(&var1);
3595 return FAIL;
3596 }
3597 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003598 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599
3600 if (len == -1)
3601 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003602 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003603 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003605 clear_tv(&var1);
3606 return FAIL;
3607 }
3608 }
3609
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003610 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003612 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003613 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 if (len == -1)
3615 clear_tv(&var1);
3616 if (item == NULL)
3617 return FAIL;
3618
3619 copy_tv(&item->di_tv, &var1);
3620 clear_tv(rettv);
3621 *rettv = var1;
3622 }
3623 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003624 }
3625 }
3626
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003627 return OK;
3628}
3629
3630/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003631 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003632 */
3633 char_u *
3634partial_name(partial_T *pt)
3635{
3636 if (pt->pt_name != NULL)
3637 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003638 if (pt->pt_func != NULL)
3639 return pt->pt_func->uf_name;
3640 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003641}
3642
Bram Moolenaarddecc252016-04-06 22:59:37 +02003643 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003644partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003645{
3646 int i;
3647
3648 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003649 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003650 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003651 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003652 if (pt->pt_name != NULL)
3653 {
3654 func_unref(pt->pt_name);
3655 vim_free(pt->pt_name);
3656 }
3657 else
3658 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003659
3660 if (pt->pt_funcstack != NULL)
3661 {
3662 // Decrease the reference count for the context of a closure. If down
3663 // to zero free it and clear the variables on the stack.
3664 if (--pt->pt_funcstack->fs_refcount == 0)
3665 {
3666 garray_T *gap = &pt->pt_funcstack->fs_ga;
3667 typval_T *stack = gap->ga_data;
3668
3669 for (i = 0; i < gap->ga_len; ++i)
3670 clear_tv(stack + i);
3671 ga_clear(gap);
3672 vim_free(pt->pt_funcstack);
3673 }
3674 pt->pt_funcstack = NULL;
3675 }
3676
Bram Moolenaarddecc252016-04-06 22:59:37 +02003677 vim_free(pt);
3678}
3679
3680/*
3681 * Unreference a closure: decrement the reference count and free it when it
3682 * becomes zero.
3683 */
3684 void
3685partial_unref(partial_T *pt)
3686{
3687 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003688 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003689}
3690
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003691/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003692 * Return the next (unique) copy ID.
3693 * Used for serializing nested structures.
3694 */
3695 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003696get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003697{
3698 current_copyID += COPYID_INC;
3699 return current_copyID;
3700}
3701
3702/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003703 * Garbage collection for lists and dictionaries.
3704 *
3705 * We use reference counts to be able to free most items right away when they
3706 * are no longer used. But for composite items it's possible that it becomes
3707 * unused while the reference count is > 0: When there is a recursive
3708 * reference. Example:
3709 * :let l = [1, 2, 3]
3710 * :let d = {9: l}
3711 * :let l[1] = d
3712 *
3713 * Since this is quite unusual we handle this with garbage collection: every
3714 * once in a while find out which lists and dicts are not referenced from any
3715 * variable.
3716 *
3717 * Here is a good reference text about garbage collection (refers to Python
3718 * but it applies to all reference-counting mechanisms):
3719 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003720 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003721
3722/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003723 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003724 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003725 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003726 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003727 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003728garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003729{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003730 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003731 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003732 buf_T *buf;
3733 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003734 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003735 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003736
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003737 if (!testing)
3738 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003739 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003740 want_garbage_collect = FALSE;
3741 may_garbage_collect = FALSE;
3742 garbage_collect_at_exit = FALSE;
3743 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003744
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003745 // The execution stack can grow big, limit the size.
3746 if (exestack.ga_maxlen - exestack.ga_len > 500)
3747 {
3748 size_t new_len;
3749 char_u *pp;
3750 int n;
3751
3752 // Keep 150% of the current size, with a minimum of the growth size.
3753 n = exestack.ga_len / 2;
3754 if (n < exestack.ga_growsize)
3755 n = exestack.ga_growsize;
3756
3757 // Don't make it bigger though.
3758 if (exestack.ga_len + n < exestack.ga_maxlen)
3759 {
3760 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3761 pp = vim_realloc(exestack.ga_data, new_len);
3762 if (pp == NULL)
3763 return FAIL;
3764 exestack.ga_maxlen = exestack.ga_len + n;
3765 exestack.ga_data = pp;
3766 }
3767 }
3768
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003769 // We advance by two because we add one for items referenced through
3770 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003771 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003772
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003773 /*
3774 * 1. Go through all accessible variables and mark all lists and dicts
3775 * with copyID.
3776 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003777
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003778 // Don't free variables in the previous_funccal list unless they are only
3779 // referenced through previous_funccal. This must be first, because if
3780 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003781 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003782
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003783 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003784 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003786 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003787 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003788 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3789 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003791 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003792 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003793 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3794 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003795 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003796 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3797 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003798#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003799 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003800 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3801 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003802 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003803 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003804 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3805 NULL, NULL);
3806#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003807
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003808 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003809 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003810 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3811 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003812 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003813 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003815 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003816 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003818 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003819 abort = abort || set_ref_in_functions(copyID);
3820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003821 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003822 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003823
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003824 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003825 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003826
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003827 // callbacks in buffers
3828 abort = abort || set_ref_in_buffers(copyID);
3829
Bram Moolenaar1dced572012-04-05 16:54:08 +02003830#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003831 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003832#endif
3833
Bram Moolenaardb913952012-06-29 12:54:53 +02003834#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003835 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003836#endif
3837
3838#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003839 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003840#endif
3841
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003842#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003843 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003844 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003845#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003846#ifdef FEAT_NETBEANS_INTG
3847 abort = abort || set_ref_in_nb_channel(copyID);
3848#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003849
Bram Moolenaare3188e22016-05-31 21:13:04 +02003850#ifdef FEAT_TIMERS
3851 abort = abort || set_ref_in_timer(copyID);
3852#endif
3853
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003854#ifdef FEAT_QUICKFIX
3855 abort = abort || set_ref_in_quickfix(copyID);
3856#endif
3857
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003858#ifdef FEAT_TERMINAL
3859 abort = abort || set_ref_in_term(copyID);
3860#endif
3861
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003862#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003863 abort = abort || set_ref_in_popups(copyID);
3864#endif
3865
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003866 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003867 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003868 /*
3869 * 2. Free lists and dictionaries that are not referenced.
3870 */
3871 did_free = free_unref_items(copyID);
3872
3873 /*
3874 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003875 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003876 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003877 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003878 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003879 else if (p_verbose > 0)
3880 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003881 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003882 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003883
3884 return did_free;
3885}
3886
3887/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003888 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003889 */
3890 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003891free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003892{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003893 int did_free = FALSE;
3894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003895 // Let all "free" functions know that we are here. This means no
3896 // dictionaries, lists, channels or jobs are to be freed, because we will
3897 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003898 in_free_unref_items = TRUE;
3899
3900 /*
3901 * PASS 1: free the contents of the items. We don't free the items
3902 * themselves yet, so that it is possible to decrement refcount counters
3903 */
3904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003905 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003906 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003908 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003909 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003910
3911#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003912 // Go through the list of jobs and free items without the copyID. This
3913 // must happen before doing channels, because jobs refer to channels, but
3914 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003915 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3916
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003917 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003918 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3919#endif
3920
3921 /*
3922 * PASS 2: free the items themselves.
3923 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003924 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003925 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003926
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003927#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003928 // Go through the list of jobs and free items without the copyID. This
3929 // must happen before doing channels, because jobs refer to channels, but
3930 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003931 free_unused_jobs(copyID, COPYID_MASK);
3932
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003933 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003934 free_unused_channels(copyID, COPYID_MASK);
3935#endif
3936
3937 in_free_unref_items = FALSE;
3938
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003939 return did_free;
3940}
3941
3942/*
3943 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003944 * "list_stack" is used to add lists to be marked. Can be NULL.
3945 *
3946 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003947 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003948 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003949set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003950{
3951 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003952 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003953 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003954 hashtab_T *cur_ht;
3955 ht_stack_T *ht_stack = NULL;
3956 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003957
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003958 cur_ht = ht;
3959 for (;;)
3960 {
3961 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003963 // Mark each item in the hashtab. If the item contains a hashtab
3964 // it is added to ht_stack, if it contains a list it is added to
3965 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003966 todo = (int)cur_ht->ht_used;
3967 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3968 if (!HASHITEM_EMPTY(hi))
3969 {
3970 --todo;
3971 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3972 &ht_stack, list_stack);
3973 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003974 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003975
3976 if (ht_stack == NULL)
3977 break;
3978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003979 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003980 cur_ht = ht_stack->ht;
3981 tempitem = ht_stack;
3982 ht_stack = ht_stack->prev;
3983 free(tempitem);
3984 }
3985
3986 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003987}
3988
3989/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003990 * Mark a dict and its items with "copyID".
3991 * Returns TRUE if setting references failed somehow.
3992 */
3993 int
3994set_ref_in_dict(dict_T *d, int copyID)
3995{
3996 if (d != NULL && d->dv_copyID != copyID)
3997 {
3998 d->dv_copyID = copyID;
3999 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4000 }
4001 return FALSE;
4002}
4003
4004/*
4005 * Mark a list and its items with "copyID".
4006 * Returns TRUE if setting references failed somehow.
4007 */
4008 int
4009set_ref_in_list(list_T *ll, int copyID)
4010{
4011 if (ll != NULL && ll->lv_copyID != copyID)
4012 {
4013 ll->lv_copyID = copyID;
4014 return set_ref_in_list_items(ll, copyID, NULL);
4015 }
4016 return FALSE;
4017}
4018
4019/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004020 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004021 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4022 *
4023 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004024 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004025 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004026set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004027{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004028 listitem_T *li;
4029 int abort = FALSE;
4030 list_T *cur_l;
4031 list_stack_T *list_stack = NULL;
4032 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004033
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004034 cur_l = l;
4035 for (;;)
4036 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004037 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004038 // Mark each item in the list. If the item contains a hashtab
4039 // it is added to ht_stack, if it contains a list it is added to
4040 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004041 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4042 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4043 ht_stack, &list_stack);
4044 if (list_stack == NULL)
4045 break;
4046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004047 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004048 cur_l = list_stack->list;
4049 tempitem = list_stack;
4050 list_stack = list_stack->prev;
4051 free(tempitem);
4052 }
4053
4054 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004055}
4056
4057/*
4058 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004059 * "list_stack" is used to add lists to be marked. Can be NULL.
4060 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4061 *
4062 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004063 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004065set_ref_in_item(
4066 typval_T *tv,
4067 int copyID,
4068 ht_stack_T **ht_stack,
4069 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004070{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004071 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004072
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004073 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004074 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004075 dict_T *dd = tv->vval.v_dict;
4076
Bram Moolenaara03f2332016-02-06 18:09:59 +01004077 if (dd != NULL && dd->dv_copyID != copyID)
4078 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004079 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004080 dd->dv_copyID = copyID;
4081 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004082 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004083 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4084 }
4085 else
4086 {
4087 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4088 if (newitem == NULL)
4089 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004090 else
4091 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004092 newitem->ht = &dd->dv_hashtab;
4093 newitem->prev = *ht_stack;
4094 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004095 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004096 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004097 }
4098 }
4099 else if (tv->v_type == VAR_LIST)
4100 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004101 list_T *ll = tv->vval.v_list;
4102
Bram Moolenaara03f2332016-02-06 18:09:59 +01004103 if (ll != NULL && ll->lv_copyID != copyID)
4104 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004105 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004106 ll->lv_copyID = copyID;
4107 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004108 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004109 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004110 }
4111 else
4112 {
4113 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004114 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004115 if (newitem == NULL)
4116 abort = TRUE;
4117 else
4118 {
4119 newitem->list = ll;
4120 newitem->prev = *list_stack;
4121 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004122 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004123 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004124 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004125 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004126 else if (tv->v_type == VAR_FUNC)
4127 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004128 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004129 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004130 else if (tv->v_type == VAR_PARTIAL)
4131 {
4132 partial_T *pt = tv->vval.v_partial;
4133 int i;
4134
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004135 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004136 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004137 // Didn't see this partial yet.
4138 pt->pt_copyID = copyID;
4139
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004140 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004141
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004142 if (pt->pt_dict != NULL)
4143 {
4144 typval_T dtv;
4145
4146 dtv.v_type = VAR_DICT;
4147 dtv.vval.v_dict = pt->pt_dict;
4148 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4149 }
4150
4151 for (i = 0; i < pt->pt_argc; ++i)
4152 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4153 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004154 if (pt->pt_funcstack != NULL)
4155 {
4156 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4157
4158 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4159 abort = abort || set_ref_in_item(stack + i, copyID,
4160 ht_stack, list_stack);
4161 }
4162
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004163 }
4164 }
4165#ifdef FEAT_JOB_CHANNEL
4166 else if (tv->v_type == VAR_JOB)
4167 {
4168 job_T *job = tv->vval.v_job;
4169 typval_T dtv;
4170
4171 if (job != NULL && job->jv_copyID != copyID)
4172 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004173 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004174 if (job->jv_channel != NULL)
4175 {
4176 dtv.v_type = VAR_CHANNEL;
4177 dtv.vval.v_channel = job->jv_channel;
4178 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4179 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004180 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004181 {
4182 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004183 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004184 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4185 }
4186 }
4187 }
4188 else if (tv->v_type == VAR_CHANNEL)
4189 {
4190 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004191 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004192 typval_T dtv;
4193 jsonq_T *jq;
4194 cbq_T *cq;
4195
4196 if (ch != NULL && ch->ch_copyID != copyID)
4197 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004198 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004199 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004200 {
4201 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4202 jq = jq->jq_next)
4203 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4204 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4205 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004206 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004207 {
4208 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004209 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004210 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4211 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004212 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004213 {
4214 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004215 dtv.vval.v_partial =
4216 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004217 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4218 }
4219 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004220 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004221 {
4222 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004223 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004224 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4225 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004226 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004227 {
4228 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004229 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004230 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4231 }
4232 }
4233 }
4234#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004235 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004236}
4237
Bram Moolenaar8c711452005-01-14 21:53:12 +00004238/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004239 * Return a string with the string representation of a variable.
4240 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004241 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004242 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004243 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4244 * stings as "string()", otherwise does not put quotes around strings, as
4245 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004246 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4247 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004248 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004249 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004250 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004251echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004252 typval_T *tv,
4253 char_u **tofree,
4254 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004255 int copyID,
4256 int echo_style,
4257 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004258 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004259{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004260 static int recurse = 0;
4261 char_u *r = NULL;
4262
Bram Moolenaar33570922005-01-25 22:26:29 +00004263 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004264 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004265 if (!did_echo_string_emsg)
4266 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004267 // Only give this message once for a recursive call to avoid
4268 // flooding the user with errors. And stop iterating over lists
4269 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004270 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004271 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004272 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004273 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004274 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004275 }
4276 ++recurse;
4277
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004278 switch (tv->v_type)
4279 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004280 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004281 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004282 {
4283 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004284 r = tv->vval.v_string;
4285 if (r == NULL)
4286 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004287 }
4288 else
4289 {
4290 *tofree = string_quote(tv->vval.v_string, FALSE);
4291 r = *tofree;
4292 }
4293 break;
4294
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004295 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004296 if (echo_style)
4297 {
4298 *tofree = NULL;
4299 r = tv->vval.v_string;
4300 }
4301 else
4302 {
4303 *tofree = string_quote(tv->vval.v_string, TRUE);
4304 r = *tofree;
4305 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004306 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004307
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004308 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004309 {
4310 partial_T *pt = tv->vval.v_partial;
4311 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004312 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004313 garray_T ga;
4314 int i;
4315 char_u *tf;
4316
4317 ga_init2(&ga, 1, 100);
4318 ga_concat(&ga, (char_u *)"function(");
4319 if (fname != NULL)
4320 {
4321 ga_concat(&ga, fname);
4322 vim_free(fname);
4323 }
4324 if (pt != NULL && pt->pt_argc > 0)
4325 {
4326 ga_concat(&ga, (char_u *)", [");
4327 for (i = 0; i < pt->pt_argc; ++i)
4328 {
4329 if (i > 0)
4330 ga_concat(&ga, (char_u *)", ");
4331 ga_concat(&ga,
4332 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4333 vim_free(tf);
4334 }
4335 ga_concat(&ga, (char_u *)"]");
4336 }
4337 if (pt != NULL && pt->pt_dict != NULL)
4338 {
4339 typval_T dtv;
4340
4341 ga_concat(&ga, (char_u *)", ");
4342 dtv.v_type = VAR_DICT;
4343 dtv.vval.v_dict = pt->pt_dict;
4344 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4345 vim_free(tf);
4346 }
4347 ga_concat(&ga, (char_u *)")");
4348
4349 *tofree = ga.ga_data;
4350 r = *tofree;
4351 break;
4352 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004353
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004354 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004355 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004356 break;
4357
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004358 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004359 if (tv->vval.v_list == NULL)
4360 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004361 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004362 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004363 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004364 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004365 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4366 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004367 {
4368 *tofree = NULL;
4369 r = (char_u *)"[...]";
4370 }
4371 else
4372 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004373 int old_copyID = tv->vval.v_list->lv_copyID;
4374
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004375 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004376 *tofree = list2string(tv, copyID, restore_copyID);
4377 if (restore_copyID)
4378 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004379 r = *tofree;
4380 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004381 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004382
Bram Moolenaar8c711452005-01-14 21:53:12 +00004383 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004384 if (tv->vval.v_dict == NULL)
4385 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004386 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004387 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004388 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004389 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004390 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4391 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004392 {
4393 *tofree = NULL;
4394 r = (char_u *)"{...}";
4395 }
4396 else
4397 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004398 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004399
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004400 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004401 *tofree = dict2string(tv, copyID, restore_copyID);
4402 if (restore_copyID)
4403 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004404 r = *tofree;
4405 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004406 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004407
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004409 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004410 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004412 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004413 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004414 break;
4415
Bram Moolenaar835dc632016-02-07 14:27:38 +01004416 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004417 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004418 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004419 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004420 if (composite_val)
4421 {
4422 *tofree = string_quote(r, FALSE);
4423 r = *tofree;
4424 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004425 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004426
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004427 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004428#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004429 *tofree = NULL;
4430 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4431 r = numbuf;
4432 break;
4433#endif
4434
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004435 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004436 case VAR_SPECIAL:
4437 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004438 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004439 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004440 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004441
Bram Moolenaar8502c702014-06-17 12:51:16 +02004442 if (--recurse == 0)
4443 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004444 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004445}
4446
4447/*
4448 * Return a string with the string representation of a variable.
4449 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4450 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004451 * Does not put quotes around strings, as ":echo" displays values.
4452 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4453 * May return NULL.
4454 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004455 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004456echo_string(
4457 typval_T *tv,
4458 char_u **tofree,
4459 char_u *numbuf,
4460 int copyID)
4461{
4462 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4463}
4464
4465/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004466 * Return string "str" in ' quotes, doubling ' characters.
4467 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004468 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004469 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004470 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004471string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004472{
Bram Moolenaar33570922005-01-25 22:26:29 +00004473 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004474 char_u *p, *r, *s;
4475
Bram Moolenaar33570922005-01-25 22:26:29 +00004476 len = (function ? 13 : 3);
4477 if (str != NULL)
4478 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004479 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004480 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004481 if (*p == '\'')
4482 ++len;
4483 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004484 s = r = alloc(len);
4485 if (r != NULL)
4486 {
4487 if (function)
4488 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004489 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004490 r += 10;
4491 }
4492 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004493 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004494 if (str != NULL)
4495 for (p = str; *p != NUL; )
4496 {
4497 if (*p == '\'')
4498 *r++ = '\'';
4499 MB_COPY_CHAR(p, r);
4500 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004501 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004502 if (function)
4503 *r++ = ')';
4504 *r++ = NUL;
4505 }
4506 return s;
4507}
4508
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004509#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004510/*
4511 * Convert the string "text" to a floating point number.
4512 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4513 * this always uses a decimal point.
4514 * Returns the length of the text that was consumed.
4515 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004516 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004517string2float(
4518 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004519 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004520{
4521 char *s = (char *)text;
4522 float_T f;
4523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004524 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004525 if (STRNICMP(text, "inf", 3) == 0)
4526 {
4527 *value = INFINITY;
4528 return 3;
4529 }
4530 if (STRNICMP(text, "-inf", 3) == 0)
4531 {
4532 *value = -INFINITY;
4533 return 4;
4534 }
4535 if (STRNICMP(text, "nan", 3) == 0)
4536 {
4537 *value = NAN;
4538 return 3;
4539 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004540 f = strtod(s, &s);
4541 *value = f;
4542 return (int)((char_u *)s - text);
4543}
4544#endif
4545
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004548 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004550 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004551var2fpos(
4552 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004553 int dollar_lnum, // TRUE when $ is last line
4554 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004556 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004558 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004560 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004561 if (varp->v_type == VAR_LIST)
4562 {
4563 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004564 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004565 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004566 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004567
4568 l = varp->vval.v_list;
4569 if (l == NULL)
4570 return NULL;
4571
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004572 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004573 pos.lnum = list_find_nr(l, 0L, &error);
4574 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004575 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004577 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004578 pos.col = list_find_nr(l, 1L, &error);
4579 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004580 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004581 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004583 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004584 li = list_find(l, 1L);
4585 if (li != NULL && li->li_tv.v_type == VAR_STRING
4586 && li->li_tv.vval.v_string != NULL
4587 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4588 pos.col = len + 1;
4589
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004590 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004591 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004592 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004593 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004594
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004595 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004596 pos.coladd = list_find_nr(l, 2L, &error);
4597 if (error)
4598 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004599
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004600 return &pos;
4601 }
4602
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004603 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004604 if (name == NULL)
4605 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004606 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004608 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004609 {
4610 if (VIsual_active)
4611 return &VIsual;
4612 return &curwin->w_cursor;
4613 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004614 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004616 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4618 return NULL;
4619 return pp;
4620 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004621
Bram Moolenaara5525202006-03-02 22:52:09 +00004622 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004623
Bram Moolenaar477933c2007-07-17 14:32:23 +00004624 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004625 {
4626 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004627 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004628 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004629 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004630 // In silent Ex mode topline is zero, but that's not a valid line
4631 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004632 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004633 return &pos;
4634 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004635 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004636 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004637 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004638 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004639 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004640 return &pos;
4641 }
4642 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004643 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004645 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
4647 pos.lnum = curbuf->b_ml.ml_line_count;
4648 pos.col = 0;
4649 }
4650 else
4651 {
4652 pos.lnum = curwin->w_cursor.lnum;
4653 pos.col = (colnr_T)STRLEN(ml_get_curline());
4654 }
4655 return &pos;
4656 }
4657 return NULL;
4658}
4659
4660/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004661 * Convert list in "arg" into a position and optional file number.
4662 * When "fnump" is NULL there is no file number, only 3 items.
4663 * Note that the column is passed on as-is, the caller may want to decrement
4664 * it to use 1 for the first column.
4665 * Return FAIL when conversion is not possible, doesn't check the position for
4666 * validity.
4667 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004668 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004669list2fpos(
4670 typval_T *arg,
4671 pos_T *posp,
4672 int *fnump,
4673 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004674{
4675 list_T *l = arg->vval.v_list;
4676 long i = 0;
4677 long n;
4678
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004679 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4680 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004681 if (arg->v_type != VAR_LIST
4682 || l == NULL
4683 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004684 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004685 return FAIL;
4686
4687 if (fnump != NULL)
4688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004689 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004690 if (n < 0)
4691 return FAIL;
4692 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004693 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004694 *fnump = n;
4695 }
4696
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004697 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004698 if (n < 0)
4699 return FAIL;
4700 posp->lnum = n;
4701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004702 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004703 if (n < 0)
4704 return FAIL;
4705 posp->col = n;
4706
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004707 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004708 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004709 posp->coladd = 0;
4710 else
4711 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004712
Bram Moolenaar493c1782014-05-28 14:34:46 +02004713 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004714 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004715
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004716 return OK;
4717}
4718
4719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 * Get the length of an environment variable name.
4721 * Advance "arg" to the first character after the name.
4722 * Return 0 for error.
4723 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004724 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004725get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
4727 char_u *p;
4728 int len;
4729
4730 for (p = *arg; vim_isIDc(*p); ++p)
4731 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004732 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 return 0;
4734
4735 len = (int)(p - *arg);
4736 *arg = p;
4737 return len;
4738}
4739
4740/*
4741 * Get the length of the name of a function or internal variable.
4742 * "arg" is advanced to the first non-white character after the name.
4743 * Return 0 if something is wrong.
4744 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004746get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 char_u *p;
4749 int len;
4750
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004751 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004753 {
4754 if (*p == ':')
4755 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004756 // "s:" is start of "s:var", but "n:" is not and can be used in
4757 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004758 len = (int)(p - *arg);
4759 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4760 || len > 1)
4761 break;
4762 }
4763 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004764 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 return 0;
4766
4767 len = (int)(p - *arg);
4768 *arg = skipwhite(p);
4769
4770 return len;
4771}
4772
4773/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004774 * Get the length of the name of a variable or function.
4775 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004777 * Return -1 if curly braces expansion failed.
4778 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 * If the name contains 'magic' {}'s, expand them and return the
4780 * expanded name in an allocated string via 'alias' - caller must free.
4781 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004782 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004783get_name_len(
4784 char_u **arg,
4785 char_u **alias,
4786 int evaluate,
4787 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
4789 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 char_u *p;
4791 char_u *expr_start;
4792 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004794 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795
4796 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4797 && (*arg)[2] == (int)KE_SNR)
4798 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004799 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 *arg += 3;
4801 return get_id_len(arg) + 3;
4802 }
4803 len = eval_fname_script(*arg);
4804 if (len > 0)
4805 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004806 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 *arg += len;
4808 }
4809
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004811 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004813 p = find_name_end(*arg, &expr_start, &expr_end,
4814 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 if (expr_start != NULL)
4816 {
4817 char_u *temp_string;
4818
4819 if (!evaluate)
4820 {
4821 len += (int)(p - *arg);
4822 *arg = skipwhite(p);
4823 return len;
4824 }
4825
4826 /*
4827 * Include any <SID> etc in the expanded string:
4828 * Thus the -len here.
4829 */
4830 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4831 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004832 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 *alias = temp_string;
4834 *arg = skipwhite(p);
4835 return (int)STRLEN(temp_string);
4836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
4838 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004839 // Only give an error when there is something, otherwise it will be
4840 // reported at a higher level.
4841 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004842 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
4844 return len;
4845}
4846
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004847/*
4848 * Find the end of a variable or function name, taking care of magic braces.
4849 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4850 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004851 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 * Return a pointer to just after the name. Equal to "arg" if there is no
4853 * valid name.
4854 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004855 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004856find_name_end(
4857 char_u *arg,
4858 char_u **expr_start,
4859 char_u **expr_end,
4860 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004862 int mb_nest = 0;
4863 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004865 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004866 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004868 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 *expr_start = NULL;
4871 *expr_end = NULL;
4872 }
4873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004874 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004875 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4876 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004877 return arg;
4878
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 for (p = arg; *p != NUL
4880 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004881 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004882 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004883 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004884 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004886 if (*p == '\'')
4887 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004888 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004889 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004890 ;
4891 if (*p == NUL)
4892 break;
4893 }
4894 else if (*p == '"')
4895 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004896 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004897 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004898 if (*p == '\\' && p[1] != NUL)
4899 ++p;
4900 if (*p == NUL)
4901 break;
4902 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004903 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004905 // "s:" is start of "s:var", but "n:" is not and can be used in
4906 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004907 len = (int)(p - arg);
4908 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004909 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004910 break;
4911 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004912
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 if (*p == '[')
4916 ++br_nest;
4917 else if (*p == ']')
4918 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004920
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004921 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004923 if (*p == '{')
4924 {
4925 mb_nest++;
4926 if (expr_start != NULL && *expr_start == NULL)
4927 *expr_start = p;
4928 }
4929 else if (*p == '}')
4930 {
4931 mb_nest--;
4932 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4933 *expr_end = p;
4934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937
4938 return p;
4939}
4940
4941/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004942 * Expands out the 'magic' {}'s in a variable/function name.
4943 * Note that this can call itself recursively, to deal with
4944 * constructs like foo{bar}{baz}{bam}
4945 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4946 * "in_start" ^
4947 * "expr_start" ^
4948 * "expr_end" ^
4949 * "in_end" ^
4950 *
4951 * Returns a new allocated string, which the caller must free.
4952 * Returns NULL for failure.
4953 */
4954 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004955make_expanded_name(
4956 char_u *in_start,
4957 char_u *expr_start,
4958 char_u *expr_end,
4959 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004960{
4961 char_u c1;
4962 char_u *retval = NULL;
4963 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004964
4965 if (expr_end == NULL || in_end == NULL)
4966 return NULL;
4967 *expr_start = NUL;
4968 *expr_end = NUL;
4969 c1 = *in_end;
4970 *in_end = NUL;
4971
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004972 temp_result = eval_to_string(expr_start + 1, FALSE);
4973 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004974 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004975 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4976 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004977 if (retval != NULL)
4978 {
4979 STRCPY(retval, in_start);
4980 STRCAT(retval, temp_result);
4981 STRCAT(retval, expr_end + 1);
4982 }
4983 }
4984 vim_free(temp_result);
4985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004987 *expr_start = '{';
4988 *expr_end = '}';
4989
4990 if (retval != NULL)
4991 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004992 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004993 if (expr_start != NULL)
4994 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004995 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004996 temp_result = make_expanded_name(retval, expr_start,
4997 expr_end, temp_result);
4998 vim_free(retval);
4999 retval = temp_result;
5000 }
5001 }
5002
5003 return retval;
5004}
5005
5006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005008 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005010 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005011eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005013 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5014}
5015
5016/*
5017 * Return TRUE if character "c" can be used as the first character in a
5018 * variable or function name (excluding '{' and '}').
5019 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005020 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005021eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005022{
5023 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024}
5025
5026/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005027 * Handle:
5028 * - expr[expr], expr[expr:expr] subscript
5029 * - ".name" lookup
5030 * - function call with Funcref variable: func(expr)
5031 * - method call: var->method()
5032 *
5033 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005034 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005036handle_subscript(
5037 char_u **arg,
5038 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005039 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005040 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005041{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005042 int evaluate = evalarg != NULL
5043 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005044 int ret = OK;
5045 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005046
Bram Moolenaar61343f02019-07-20 21:11:13 +02005047 // "." is ".name" lookup when we found a dict or when evaluating and
5048 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005049 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005050 && (((**arg == '['
5051 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005052 || (!evaluate
5053 && (*arg)[1] != '.'
5054 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005055 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005056 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005057 && !VIM_ISWHITE(*(*arg - 1)))
5058 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005059 {
5060 if (**arg == '(')
5061 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005062 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5063 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005064
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005065 // Stop the expression evaluation when immediately aborting on
5066 // error, or when an interrupt occurred or an exception was thrown
5067 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005068 if (aborting())
5069 {
5070 if (ret == OK)
5071 clear_tv(rettv);
5072 ret = FAIL;
5073 }
5074 dict_unref(selfdict);
5075 selfdict = NULL;
5076 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005077 else if (**arg == '-')
5078 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005079 if (ret == OK)
5080 {
5081 if ((*arg)[2] == '{')
5082 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005083 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005084 else
5085 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005086 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005087 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005088 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005089 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005090 {
5091 dict_unref(selfdict);
5092 if (rettv->v_type == VAR_DICT)
5093 {
5094 selfdict = rettv->vval.v_dict;
5095 if (selfdict != NULL)
5096 ++selfdict->dv_refcount;
5097 }
5098 else
5099 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005100 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005101 {
5102 clear_tv(rettv);
5103 ret = FAIL;
5104 }
5105 }
5106 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005108 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5109 // Don't do this when "Func" is already a partial that was bound
5110 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005111 if (selfdict != NULL
5112 && (rettv->v_type == VAR_FUNC
5113 || (rettv->v_type == VAR_PARTIAL
5114 && (rettv->vval.v_partial->pt_auto
5115 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005116 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005117
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005118 dict_unref(selfdict);
5119 return ret;
5120}
5121
5122/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005123 * Make a copy of an item.
5124 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005125 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5126 * reference to an already copied list/dict can be used.
5127 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005128 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005129 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005130item_copy(
5131 typval_T *from,
5132 typval_T *to,
5133 int deep,
5134 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005135{
5136 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005137 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005138
Bram Moolenaar33570922005-01-25 22:26:29 +00005139 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005141 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005142 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005143 }
5144 ++recurse;
5145
5146 switch (from->v_type)
5147 {
5148 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005149 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005150 case VAR_STRING:
5151 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005152 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005153 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005154 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005155 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005156 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005157 copy_tv(from, to);
5158 break;
5159 case VAR_LIST:
5160 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005161 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005162 if (from->vval.v_list == NULL)
5163 to->vval.v_list = NULL;
5164 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5165 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005166 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005167 to->vval.v_list = from->vval.v_list->lv_copylist;
5168 ++to->vval.v_list->lv_refcount;
5169 }
5170 else
5171 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5172 if (to->vval.v_list == NULL)
5173 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005175 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005177 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005178 case VAR_DICT:
5179 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005180 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005181 if (from->vval.v_dict == NULL)
5182 to->vval.v_dict = NULL;
5183 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005185 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005186 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5187 ++to->vval.v_dict->dv_refcount;
5188 }
5189 else
5190 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5191 if (to->vval.v_dict == NULL)
5192 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005193 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005194 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005195 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005197 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005198 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005199 }
5200 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005201 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005202}
5203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 void
5205echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5206{
5207 char_u *tofree;
5208 char_u numbuf[NUMBUFLEN];
5209 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5210
5211 if (*atstart)
5212 {
5213 *atstart = FALSE;
5214 // Call msg_start() after eval1(), evaluating the expression
5215 // may cause a message to appear.
5216 if (with_space)
5217 {
5218 // Mark the saved text as finishing the line, so that what
5219 // follows is displayed on a new line when scrolling back
5220 // at the more prompt.
5221 msg_sb_eol();
5222 msg_start();
5223 }
5224 }
5225 else if (with_space)
5226 msg_puts_attr(" ", echo_attr);
5227
5228 if (p != NULL)
5229 for ( ; *p != NUL && !got_int; ++p)
5230 {
5231 if (*p == '\n' || *p == '\r' || *p == TAB)
5232 {
5233 if (*p != TAB && *needclr)
5234 {
5235 // remove any text still there from the command
5236 msg_clr_eos();
5237 *needclr = FALSE;
5238 }
5239 msg_putchar_attr(*p, echo_attr);
5240 }
5241 else
5242 {
5243 if (has_mbyte)
5244 {
5245 int i = (*mb_ptr2len)(p);
5246
5247 (void)msg_outtrans_len_attr(p, i, echo_attr);
5248 p += i - 1;
5249 }
5250 else
5251 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5252 }
5253 }
5254 vim_free(tofree);
5255}
5256
Bram Moolenaare9a41262005-01-15 22:18:47 +00005257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 * ":echo expr1 ..." print each argument separated with a space, add a
5259 * newline at the end.
5260 * ":echon expr1 ..." print each argument plain.
5261 */
5262 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005263ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264{
5265 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005266 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 char_u *p;
5268 int needclr = TRUE;
5269 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005270 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005271 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005272 evalarg_T evalarg;
5273
Bram Moolenaare707c882020-07-01 13:07:37 +02005274 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275
5276 if (eap->skip)
5277 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005278 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005280 // If eval1() causes an error message the text from the command may
5281 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005282 need_clr_eos = needclr;
5283
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005285 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
5287 /*
5288 * Report the invalid expression unless the expression evaluation
5289 * has been cancelled due to an aborting error, an interrupt, or an
5290 * exception.
5291 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005292 if (!aborting() && did_emsg == did_emsg_before
5293 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005294 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005295 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 break;
5297 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005298 need_clr_eos = FALSE;
5299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005302
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005303 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 arg = skipwhite(arg);
5305 }
5306 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005307 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308
5309 if (eap->skip)
5310 --emsg_skip;
5311 else
5312 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005313 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 if (needclr)
5315 msg_clr_eos();
5316 if (eap->cmdidx == CMD_echo)
5317 msg_end();
5318 }
5319}
5320
5321/*
5322 * ":echohl {name}".
5323 */
5324 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005325ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005327 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328}
5329
5330/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005331 * Returns the :echo attribute
5332 */
5333 int
5334get_echo_attr(void)
5335{
5336 return echo_attr;
5337}
5338
5339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 * ":execute expr1 ..." execute the result of an expression.
5341 * ":echomsg expr1 ..." Print a message
5342 * ":echoerr expr1 ..." Print an error
5343 * Each gets spaces around each argument and a newline at the end for
5344 * echo commands
5345 */
5346 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005347ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348{
5349 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 int ret = OK;
5352 char_u *p;
5353 garray_T ga;
5354 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
5356 ga_init2(&ga, 1, 80);
5357
5358 if (eap->skip)
5359 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005360 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005362 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005363 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
5366 if (!eap->skip)
5367 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005368 char_u buf[NUMBUFLEN];
5369
5370 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005371 {
5372 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5373 {
5374 emsg(_(e_inval_string));
5375 p = NULL;
5376 }
5377 else
5378 p = tv_get_string_buf(&rettv, buf);
5379 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005380 else
5381 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005382 if (p == NULL)
5383 {
5384 clear_tv(&rettv);
5385 ret = FAIL;
5386 break;
5387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 len = (int)STRLEN(p);
5389 if (ga_grow(&ga, len + 2) == FAIL)
5390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005391 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 ret = FAIL;
5393 break;
5394 }
5395 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 ga.ga_len += len;
5399 }
5400
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005401 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 arg = skipwhite(arg);
5403 }
5404
5405 if (ret != FAIL && ga.ga_data != NULL)
5406 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005407 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005409 // Mark the already saved text as finishing the line, so that what
5410 // follows is displayed on a new line when scrolling back at the
5411 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005412 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005413 }
5414
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005416 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005417 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005418 out_flush();
5419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 else if (eap->cmdidx == CMD_echoerr)
5421 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005422 int save_did_emsg = did_emsg;
5423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005424 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005425 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 if (!force_abort)
5427 did_emsg = save_did_emsg;
5428 }
5429 else if (eap->cmdidx == CMD_execute)
5430 do_cmdline((char_u *)ga.ga_data,
5431 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5432 }
5433
5434 ga_clear(&ga);
5435
5436 if (eap->skip)
5437 --emsg_skip;
5438
5439 eap->nextcmd = check_nextcmd(arg);
5440}
5441
5442/*
5443 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5444 * "arg" points to the "&" or '+' when called, to "option" when returning.
5445 * Returns NULL when no option name found. Otherwise pointer to the char
5446 * after the option name.
5447 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005448 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005449find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
5451 char_u *p = *arg;
5452
5453 ++p;
5454 if (*p == 'g' && p[1] == ':')
5455 {
5456 *opt_flags = OPT_GLOBAL;
5457 p += 2;
5458 }
5459 else if (*p == 'l' && p[1] == ':')
5460 {
5461 *opt_flags = OPT_LOCAL;
5462 p += 2;
5463 }
5464 else
5465 *opt_flags = 0;
5466
5467 if (!ASCII_ISALPHA(*p))
5468 return NULL;
5469 *arg = p;
5470
5471 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005472 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 else
5474 while (ASCII_ISALPHA(*p))
5475 ++p;
5476 return p;
5477}
5478
5479/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005480 * Display script name where an item was last set.
5481 * Should only be invoked when 'verbose' is non-zero.
5482 */
5483 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005484last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005485{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005486 char_u *p;
5487
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005488 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005489 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005490 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005491 if (p != NULL)
5492 {
5493 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005494 msg_puts(_("\n\tLast set from "));
5495 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005496 if (script_ctx.sc_lnum > 0)
5497 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005498 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005499 msg_outnum((long)script_ctx.sc_lnum);
5500 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005501 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005502 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005503 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005504 }
5505}
5506
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005507#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
5509/*
5510 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005511 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 * "flags" can be "g" to do a global substitute.
5513 * Returns an allocated string, NULL for error.
5514 */
5515 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005516do_string_sub(
5517 char_u *str,
5518 char_u *pat,
5519 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005520 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005521 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
5523 int sublen;
5524 regmatch_T regmatch;
5525 int i;
5526 int do_all;
5527 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005528 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 garray_T ga;
5530 char_u *ret;
5531 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005532 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005534 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005536 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
5538 ga_init2(&ga, 1, 200);
5539
5540 do_all = (flags[0] == 'g');
5541
5542 regmatch.rm_ic = p_ic;
5543 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5544 if (regmatch.regprog != NULL)
5545 {
5546 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005547 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5549 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005550 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005551 if (regmatch.startp[0] == regmatch.endp[0])
5552 {
5553 if (zero_width == regmatch.startp[0])
5554 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005555 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005556 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005557 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5558 (size_t)i);
5559 ga.ga_len += i;
5560 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005561 continue;
5562 }
5563 zero_width = regmatch.startp[0];
5564 }
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 /*
5567 * Get some space for a temporary buffer to do the substitution
5568 * into. It will contain:
5569 * - The text up to where the match is.
5570 * - The substituted text.
5571 * - The text after the match.
5572 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005573 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005574 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5576 {
5577 ga_clear(&ga);
5578 break;
5579 }
5580
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005581 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 i = (int)(regmatch.startp[0] - tail);
5583 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005584 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005585 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 + ga.ga_len + i, TRUE, TRUE, FALSE);
5587 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005588 tail = regmatch.endp[0];
5589 if (*tail == NUL)
5590 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 if (!do_all)
5592 break;
5593 }
5594
5595 if (ga.ga_data != NULL)
5596 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5597
Bram Moolenaar473de612013-06-08 18:19:48 +02005598 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 }
5600
5601 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5602 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005603 if (p_cpo == empty_option)
5604 p_cpo = save_cpo;
5605 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005606 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005607 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
5609 return ret;
5610}