blob: c569710ab5579edbde9b21cf26bf6eb9b16be7b5 [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 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003365 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003366 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 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003380 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003381 if (**arg == ':')
3382 {
3383 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003384 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003385 if (**arg == ']')
3386 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003387 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003389 if (!empty1)
3390 clear_tv(&var1);
3391 return FAIL;
3392 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003393 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003394 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003395 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003396 if (!empty1)
3397 clear_tv(&var1);
3398 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003399 return FAIL;
3400 }
3401 }
3402
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003403 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003404 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003405 if (**arg != ']')
3406 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003407 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003408 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003409 clear_tv(&var1);
3410 if (range)
3411 clear_tv(&var2);
3412 return FAIL;
3413 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003414 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003415 }
3416
3417 if (evaluate)
3418 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003419 n1 = 0;
3420 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003421 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003422 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003423 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003424 }
3425 if (range)
3426 {
3427 if (empty2)
3428 n2 = -1;
3429 else
3430 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003431 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003432 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003433 }
3434 }
3435
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003436 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003437 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003438 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003439 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003441 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003442 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003443 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003444 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003445 case VAR_SPECIAL:
3446 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003447 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003448 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003449
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003450 case VAR_NUMBER:
3451 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003452 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003453 len = (long)STRLEN(s);
3454 if (range)
3455 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003456 // The resulting variable is a substring. If the indexes
3457 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003458 if (n1 < 0)
3459 {
3460 n1 = len + n1;
3461 if (n1 < 0)
3462 n1 = 0;
3463 }
3464 if (n2 < 0)
3465 n2 = len + n2;
3466 else if (n2 >= len)
3467 n2 = len;
3468 if (n1 >= len || n2 < 0 || n1 > n2)
3469 s = NULL;
3470 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003471 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003472 }
3473 else
3474 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003475 // The resulting variable is a string of a single
3476 // character. If the index is too big or negative the
3477 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003478 if (n1 >= len || n1 < 0)
3479 s = NULL;
3480 else
3481 s = vim_strnsave(s + n1, 1);
3482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003483 clear_tv(rettv);
3484 rettv->v_type = VAR_STRING;
3485 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003486 break;
3487
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003488 case VAR_BLOB:
3489 len = blob_len(rettv->vval.v_blob);
3490 if (range)
3491 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003492 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003493 // are out of range the result is empty.
3494 if (n1 < 0)
3495 {
3496 n1 = len + n1;
3497 if (n1 < 0)
3498 n1 = 0;
3499 }
3500 if (n2 < 0)
3501 n2 = len + n2;
3502 else if (n2 >= len)
3503 n2 = len - 1;
3504 if (n1 >= len || n2 < 0 || n1 > n2)
3505 {
3506 clear_tv(rettv);
3507 rettv->v_type = VAR_BLOB;
3508 rettv->vval.v_blob = NULL;
3509 }
3510 else
3511 {
3512 blob_T *blob = blob_alloc();
3513
3514 if (blob != NULL)
3515 {
3516 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3517 {
3518 blob_free(blob);
3519 return FAIL;
3520 }
3521 blob->bv_ga.ga_len = n2 - n1 + 1;
3522 for (i = n1; i <= n2; i++)
3523 blob_set(blob, i - n1,
3524 blob_get(rettv->vval.v_blob, i));
3525
3526 clear_tv(rettv);
3527 rettv_blob_set(rettv, blob);
3528 }
3529 }
3530 }
3531 else
3532 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003533 // The resulting variable is a byte value.
3534 // If the index is too big or negative that is an error.
3535 if (n1 < 0)
3536 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003537 if (n1 < len && n1 >= 0)
3538 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003539 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003540
3541 clear_tv(rettv);
3542 rettv->v_type = VAR_NUMBER;
3543 rettv->vval.v_number = v;
3544 }
3545 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003546 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003547 }
3548 break;
3549
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003550 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003551 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003552 if (n1 < 0)
3553 n1 = len + n1;
3554 if (!empty1 && (n1 < 0 || n1 >= len))
3555 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003556 // For a range we allow invalid values and return an empty
3557 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003558 if (!range)
3559 {
3560 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003561 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003562 return FAIL;
3563 }
3564 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003565 }
3566 if (range)
3567 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003568 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003569
3570 if (n2 < 0)
3571 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003572 else if (n2 >= len)
3573 n2 = len - 1;
3574 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003575 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003576 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003577 if (l == NULL)
3578 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003579 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003580 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003581 }
3582 else
3583 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003584 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003585 clear_tv(rettv);
3586 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003587 }
3588 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589
3590 case VAR_DICT:
3591 if (range)
3592 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003593 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003594 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003595 if (len == -1)
3596 clear_tv(&var1);
3597 return FAIL;
3598 }
3599 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003600 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601
3602 if (len == -1)
3603 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003604 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003605 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003607 clear_tv(&var1);
3608 return FAIL;
3609 }
3610 }
3611
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003612 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003613
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003614 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003615 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 if (len == -1)
3617 clear_tv(&var1);
3618 if (item == NULL)
3619 return FAIL;
3620
3621 copy_tv(&item->di_tv, &var1);
3622 clear_tv(rettv);
3623 *rettv = var1;
3624 }
3625 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003626 }
3627 }
3628
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003629 return OK;
3630}
3631
3632/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003633 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003634 */
3635 char_u *
3636partial_name(partial_T *pt)
3637{
3638 if (pt->pt_name != NULL)
3639 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003640 if (pt->pt_func != NULL)
3641 return pt->pt_func->uf_name;
3642 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003643}
3644
Bram Moolenaarddecc252016-04-06 22:59:37 +02003645 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003646partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003647{
3648 int i;
3649
3650 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003651 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003652 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003653 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003654 if (pt->pt_name != NULL)
3655 {
3656 func_unref(pt->pt_name);
3657 vim_free(pt->pt_name);
3658 }
3659 else
3660 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003661
3662 if (pt->pt_funcstack != NULL)
3663 {
3664 // Decrease the reference count for the context of a closure. If down
3665 // to zero free it and clear the variables on the stack.
3666 if (--pt->pt_funcstack->fs_refcount == 0)
3667 {
3668 garray_T *gap = &pt->pt_funcstack->fs_ga;
3669 typval_T *stack = gap->ga_data;
3670
3671 for (i = 0; i < gap->ga_len; ++i)
3672 clear_tv(stack + i);
3673 ga_clear(gap);
3674 vim_free(pt->pt_funcstack);
3675 }
3676 pt->pt_funcstack = NULL;
3677 }
3678
Bram Moolenaarddecc252016-04-06 22:59:37 +02003679 vim_free(pt);
3680}
3681
3682/*
3683 * Unreference a closure: decrement the reference count and free it when it
3684 * becomes zero.
3685 */
3686 void
3687partial_unref(partial_T *pt)
3688{
3689 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003690 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003691}
3692
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003693/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003694 * Return the next (unique) copy ID.
3695 * Used for serializing nested structures.
3696 */
3697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003698get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003699{
3700 current_copyID += COPYID_INC;
3701 return current_copyID;
3702}
3703
3704/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003705 * Garbage collection for lists and dictionaries.
3706 *
3707 * We use reference counts to be able to free most items right away when they
3708 * are no longer used. But for composite items it's possible that it becomes
3709 * unused while the reference count is > 0: When there is a recursive
3710 * reference. Example:
3711 * :let l = [1, 2, 3]
3712 * :let d = {9: l}
3713 * :let l[1] = d
3714 *
3715 * Since this is quite unusual we handle this with garbage collection: every
3716 * once in a while find out which lists and dicts are not referenced from any
3717 * variable.
3718 *
3719 * Here is a good reference text about garbage collection (refers to Python
3720 * but it applies to all reference-counting mechanisms):
3721 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003722 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003723
3724/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003725 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003726 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003727 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003728 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003729 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003730garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003731{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003732 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003733 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003734 buf_T *buf;
3735 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003736 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003737 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003738
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003739 if (!testing)
3740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003741 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003742 want_garbage_collect = FALSE;
3743 may_garbage_collect = FALSE;
3744 garbage_collect_at_exit = FALSE;
3745 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003746
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003747 // The execution stack can grow big, limit the size.
3748 if (exestack.ga_maxlen - exestack.ga_len > 500)
3749 {
3750 size_t new_len;
3751 char_u *pp;
3752 int n;
3753
3754 // Keep 150% of the current size, with a minimum of the growth size.
3755 n = exestack.ga_len / 2;
3756 if (n < exestack.ga_growsize)
3757 n = exestack.ga_growsize;
3758
3759 // Don't make it bigger though.
3760 if (exestack.ga_len + n < exestack.ga_maxlen)
3761 {
3762 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3763 pp = vim_realloc(exestack.ga_data, new_len);
3764 if (pp == NULL)
3765 return FAIL;
3766 exestack.ga_maxlen = exestack.ga_len + n;
3767 exestack.ga_data = pp;
3768 }
3769 }
3770
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003771 // We advance by two because we add one for items referenced through
3772 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003773 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003774
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003775 /*
3776 * 1. Go through all accessible variables and mark all lists and dicts
3777 * with copyID.
3778 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003780 // Don't free variables in the previous_funccal list unless they are only
3781 // referenced through previous_funccal. This must be first, because if
3782 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003783 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003784
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003785 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003786 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003787
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003788 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003789 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003790 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3791 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003792
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003793 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003794 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003795 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3796 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003797 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003798 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3799 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003800#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003801 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003802 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3803 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003804 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003805 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003806 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3807 NULL, NULL);
3808#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003809
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003810 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003811 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003812 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3813 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003814 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003815 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003816
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003817 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003818 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003819
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003820 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003821 abort = abort || set_ref_in_functions(copyID);
3822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003823 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003824 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003825
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003826 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003827 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003828
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003829 // callbacks in buffers
3830 abort = abort || set_ref_in_buffers(copyID);
3831
Bram Moolenaar1dced572012-04-05 16:54:08 +02003832#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003833 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003834#endif
3835
Bram Moolenaardb913952012-06-29 12:54:53 +02003836#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003837 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003838#endif
3839
3840#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003841 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003842#endif
3843
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003844#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003845 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003846 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003847#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003848#ifdef FEAT_NETBEANS_INTG
3849 abort = abort || set_ref_in_nb_channel(copyID);
3850#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003851
Bram Moolenaare3188e22016-05-31 21:13:04 +02003852#ifdef FEAT_TIMERS
3853 abort = abort || set_ref_in_timer(copyID);
3854#endif
3855
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003856#ifdef FEAT_QUICKFIX
3857 abort = abort || set_ref_in_quickfix(copyID);
3858#endif
3859
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003860#ifdef FEAT_TERMINAL
3861 abort = abort || set_ref_in_term(copyID);
3862#endif
3863
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003864#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003865 abort = abort || set_ref_in_popups(copyID);
3866#endif
3867
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003868 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003869 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003870 /*
3871 * 2. Free lists and dictionaries that are not referenced.
3872 */
3873 did_free = free_unref_items(copyID);
3874
3875 /*
3876 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003877 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003878 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003879 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003880 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003881 else if (p_verbose > 0)
3882 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003883 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003884 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003885
3886 return did_free;
3887}
3888
3889/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003890 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003891 */
3892 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003893free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003894{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003895 int did_free = FALSE;
3896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003897 // Let all "free" functions know that we are here. This means no
3898 // dictionaries, lists, channels or jobs are to be freed, because we will
3899 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003900 in_free_unref_items = TRUE;
3901
3902 /*
3903 * PASS 1: free the contents of the items. We don't free the items
3904 * themselves yet, so that it is possible to decrement refcount counters
3905 */
3906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003907 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003908 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003910 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003911 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003912
3913#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003914 // Go through the list of jobs and free items without the copyID. This
3915 // must happen before doing channels, because jobs refer to channels, but
3916 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003917 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3918
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003919 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003920 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3921#endif
3922
3923 /*
3924 * PASS 2: free the items themselves.
3925 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003926 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003927 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003928
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003929#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003930 // Go through the list of jobs and free items without the copyID. This
3931 // must happen before doing channels, because jobs refer to channels, but
3932 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003933 free_unused_jobs(copyID, COPYID_MASK);
3934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003935 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003936 free_unused_channels(copyID, COPYID_MASK);
3937#endif
3938
3939 in_free_unref_items = FALSE;
3940
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003941 return did_free;
3942}
3943
3944/*
3945 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003946 * "list_stack" is used to add lists to be marked. Can be NULL.
3947 *
3948 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003949 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003950 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003951set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003952{
3953 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003954 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003955 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003956 hashtab_T *cur_ht;
3957 ht_stack_T *ht_stack = NULL;
3958 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003959
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003960 cur_ht = ht;
3961 for (;;)
3962 {
3963 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003964 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003965 // Mark each item in the hashtab. If the item contains a hashtab
3966 // it is added to ht_stack, if it contains a list it is added to
3967 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003968 todo = (int)cur_ht->ht_used;
3969 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3970 if (!HASHITEM_EMPTY(hi))
3971 {
3972 --todo;
3973 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3974 &ht_stack, list_stack);
3975 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003976 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003977
3978 if (ht_stack == NULL)
3979 break;
3980
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003981 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003982 cur_ht = ht_stack->ht;
3983 tempitem = ht_stack;
3984 ht_stack = ht_stack->prev;
3985 free(tempitem);
3986 }
3987
3988 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003989}
3990
3991/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003992 * Mark a dict and its items with "copyID".
3993 * Returns TRUE if setting references failed somehow.
3994 */
3995 int
3996set_ref_in_dict(dict_T *d, int copyID)
3997{
3998 if (d != NULL && d->dv_copyID != copyID)
3999 {
4000 d->dv_copyID = copyID;
4001 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4002 }
4003 return FALSE;
4004}
4005
4006/*
4007 * Mark a list and its items with "copyID".
4008 * Returns TRUE if setting references failed somehow.
4009 */
4010 int
4011set_ref_in_list(list_T *ll, int copyID)
4012{
4013 if (ll != NULL && ll->lv_copyID != copyID)
4014 {
4015 ll->lv_copyID = copyID;
4016 return set_ref_in_list_items(ll, copyID, NULL);
4017 }
4018 return FALSE;
4019}
4020
4021/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004022 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004023 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4024 *
4025 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004026 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004027 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004028set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004029{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004030 listitem_T *li;
4031 int abort = FALSE;
4032 list_T *cur_l;
4033 list_stack_T *list_stack = NULL;
4034 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004035
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004036 cur_l = l;
4037 for (;;)
4038 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004039 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004040 // Mark each item in the list. If the item contains a hashtab
4041 // it is added to ht_stack, if it contains a list it is added to
4042 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004043 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4044 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4045 ht_stack, &list_stack);
4046 if (list_stack == NULL)
4047 break;
4048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004049 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004050 cur_l = list_stack->list;
4051 tempitem = list_stack;
4052 list_stack = list_stack->prev;
4053 free(tempitem);
4054 }
4055
4056 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004057}
4058
4059/*
4060 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004061 * "list_stack" is used to add lists to be marked. Can be NULL.
4062 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4063 *
4064 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004065 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004067set_ref_in_item(
4068 typval_T *tv,
4069 int copyID,
4070 ht_stack_T **ht_stack,
4071 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004072{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004073 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004074
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004075 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004076 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004077 dict_T *dd = tv->vval.v_dict;
4078
Bram Moolenaara03f2332016-02-06 18:09:59 +01004079 if (dd != NULL && dd->dv_copyID != copyID)
4080 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004081 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004082 dd->dv_copyID = copyID;
4083 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004084 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004085 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4086 }
4087 else
4088 {
4089 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4090 if (newitem == NULL)
4091 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004092 else
4093 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004094 newitem->ht = &dd->dv_hashtab;
4095 newitem->prev = *ht_stack;
4096 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004097 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004098 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004099 }
4100 }
4101 else if (tv->v_type == VAR_LIST)
4102 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004103 list_T *ll = tv->vval.v_list;
4104
Bram Moolenaara03f2332016-02-06 18:09:59 +01004105 if (ll != NULL && ll->lv_copyID != copyID)
4106 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004107 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004108 ll->lv_copyID = copyID;
4109 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004110 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004111 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004112 }
4113 else
4114 {
4115 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004116 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004117 if (newitem == NULL)
4118 abort = TRUE;
4119 else
4120 {
4121 newitem->list = ll;
4122 newitem->prev = *list_stack;
4123 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004124 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004125 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004126 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004127 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004128 else if (tv->v_type == VAR_FUNC)
4129 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004130 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004131 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004132 else if (tv->v_type == VAR_PARTIAL)
4133 {
4134 partial_T *pt = tv->vval.v_partial;
4135 int i;
4136
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004137 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004138 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004139 // Didn't see this partial yet.
4140 pt->pt_copyID = copyID;
4141
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004142 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004143
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004144 if (pt->pt_dict != NULL)
4145 {
4146 typval_T dtv;
4147
4148 dtv.v_type = VAR_DICT;
4149 dtv.vval.v_dict = pt->pt_dict;
4150 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4151 }
4152
4153 for (i = 0; i < pt->pt_argc; ++i)
4154 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4155 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004156 if (pt->pt_funcstack != NULL)
4157 {
4158 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4159
4160 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4161 abort = abort || set_ref_in_item(stack + i, copyID,
4162 ht_stack, list_stack);
4163 }
4164
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004165 }
4166 }
4167#ifdef FEAT_JOB_CHANNEL
4168 else if (tv->v_type == VAR_JOB)
4169 {
4170 job_T *job = tv->vval.v_job;
4171 typval_T dtv;
4172
4173 if (job != NULL && job->jv_copyID != copyID)
4174 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004175 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004176 if (job->jv_channel != NULL)
4177 {
4178 dtv.v_type = VAR_CHANNEL;
4179 dtv.vval.v_channel = job->jv_channel;
4180 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4181 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004182 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004183 {
4184 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004185 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004186 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4187 }
4188 }
4189 }
4190 else if (tv->v_type == VAR_CHANNEL)
4191 {
4192 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004193 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004194 typval_T dtv;
4195 jsonq_T *jq;
4196 cbq_T *cq;
4197
4198 if (ch != NULL && ch->ch_copyID != copyID)
4199 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004200 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004201 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004202 {
4203 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4204 jq = jq->jq_next)
4205 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4206 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4207 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004208 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004209 {
4210 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004211 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004212 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4213 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004214 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004215 {
4216 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004217 dtv.vval.v_partial =
4218 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004219 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4220 }
4221 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004222 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004223 {
4224 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004225 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004226 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4227 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004228 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004229 {
4230 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004231 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004232 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4233 }
4234 }
4235 }
4236#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004237 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004238}
4239
Bram Moolenaar8c711452005-01-14 21:53:12 +00004240/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004241 * Return a string with the string representation of a variable.
4242 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004243 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004244 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004245 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4246 * stings as "string()", otherwise does not put quotes around strings, as
4247 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004248 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4249 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004250 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004251 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004252 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004253echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004254 typval_T *tv,
4255 char_u **tofree,
4256 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004257 int copyID,
4258 int echo_style,
4259 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004260 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004261{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004262 static int recurse = 0;
4263 char_u *r = NULL;
4264
Bram Moolenaar33570922005-01-25 22:26:29 +00004265 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004266 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004267 if (!did_echo_string_emsg)
4268 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004269 // Only give this message once for a recursive call to avoid
4270 // flooding the user with errors. And stop iterating over lists
4271 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004272 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004273 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004274 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004275 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004276 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004277 }
4278 ++recurse;
4279
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004280 switch (tv->v_type)
4281 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004282 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004283 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004284 {
4285 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004286 r = tv->vval.v_string;
4287 if (r == NULL)
4288 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004289 }
4290 else
4291 {
4292 *tofree = string_quote(tv->vval.v_string, FALSE);
4293 r = *tofree;
4294 }
4295 break;
4296
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004297 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004298 if (echo_style)
4299 {
4300 *tofree = NULL;
4301 r = tv->vval.v_string;
4302 }
4303 else
4304 {
4305 *tofree = string_quote(tv->vval.v_string, TRUE);
4306 r = *tofree;
4307 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004308 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004309
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004310 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004311 {
4312 partial_T *pt = tv->vval.v_partial;
4313 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004314 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004315 garray_T ga;
4316 int i;
4317 char_u *tf;
4318
4319 ga_init2(&ga, 1, 100);
4320 ga_concat(&ga, (char_u *)"function(");
4321 if (fname != NULL)
4322 {
4323 ga_concat(&ga, fname);
4324 vim_free(fname);
4325 }
4326 if (pt != NULL && pt->pt_argc > 0)
4327 {
4328 ga_concat(&ga, (char_u *)", [");
4329 for (i = 0; i < pt->pt_argc; ++i)
4330 {
4331 if (i > 0)
4332 ga_concat(&ga, (char_u *)", ");
4333 ga_concat(&ga,
4334 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4335 vim_free(tf);
4336 }
4337 ga_concat(&ga, (char_u *)"]");
4338 }
4339 if (pt != NULL && pt->pt_dict != NULL)
4340 {
4341 typval_T dtv;
4342
4343 ga_concat(&ga, (char_u *)", ");
4344 dtv.v_type = VAR_DICT;
4345 dtv.vval.v_dict = pt->pt_dict;
4346 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4347 vim_free(tf);
4348 }
4349 ga_concat(&ga, (char_u *)")");
4350
4351 *tofree = ga.ga_data;
4352 r = *tofree;
4353 break;
4354 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004355
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004356 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004357 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004358 break;
4359
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004360 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004361 if (tv->vval.v_list == NULL)
4362 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004363 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004364 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004365 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004366 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004367 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4368 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004369 {
4370 *tofree = NULL;
4371 r = (char_u *)"[...]";
4372 }
4373 else
4374 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004375 int old_copyID = tv->vval.v_list->lv_copyID;
4376
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004377 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004378 *tofree = list2string(tv, copyID, restore_copyID);
4379 if (restore_copyID)
4380 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004381 r = *tofree;
4382 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004383 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004384
Bram Moolenaar8c711452005-01-14 21:53:12 +00004385 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004386 if (tv->vval.v_dict == NULL)
4387 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004388 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004389 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004390 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004391 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004392 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4393 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004394 {
4395 *tofree = NULL;
4396 r = (char_u *)"{...}";
4397 }
4398 else
4399 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004400 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004401
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004402 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004403 *tofree = dict2string(tv, copyID, restore_copyID);
4404 if (restore_copyID)
4405 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004406 r = *tofree;
4407 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004408 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004409
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004410 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004411 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004412 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004414 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004415 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004416 break;
4417
Bram Moolenaar835dc632016-02-07 14:27:38 +01004418 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004419 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004420 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004421 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004422 if (composite_val)
4423 {
4424 *tofree = string_quote(r, FALSE);
4425 r = *tofree;
4426 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004427 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004428
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004429 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004430#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004431 *tofree = NULL;
4432 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4433 r = numbuf;
4434 break;
4435#endif
4436
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004437 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004438 case VAR_SPECIAL:
4439 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004440 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004441 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004442 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004443
Bram Moolenaar8502c702014-06-17 12:51:16 +02004444 if (--recurse == 0)
4445 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004446 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004447}
4448
4449/*
4450 * Return a string with the string representation of a variable.
4451 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4452 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004453 * Does not put quotes around strings, as ":echo" displays values.
4454 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4455 * May return NULL.
4456 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004457 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004458echo_string(
4459 typval_T *tv,
4460 char_u **tofree,
4461 char_u *numbuf,
4462 int copyID)
4463{
4464 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4465}
4466
4467/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004468 * Return string "str" in ' quotes, doubling ' characters.
4469 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004470 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004471 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004472 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004473string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004474{
Bram Moolenaar33570922005-01-25 22:26:29 +00004475 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004476 char_u *p, *r, *s;
4477
Bram Moolenaar33570922005-01-25 22:26:29 +00004478 len = (function ? 13 : 3);
4479 if (str != NULL)
4480 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004481 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004482 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004483 if (*p == '\'')
4484 ++len;
4485 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004486 s = r = alloc(len);
4487 if (r != NULL)
4488 {
4489 if (function)
4490 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004491 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004492 r += 10;
4493 }
4494 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004495 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004496 if (str != NULL)
4497 for (p = str; *p != NUL; )
4498 {
4499 if (*p == '\'')
4500 *r++ = '\'';
4501 MB_COPY_CHAR(p, r);
4502 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004504 if (function)
4505 *r++ = ')';
4506 *r++ = NUL;
4507 }
4508 return s;
4509}
4510
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004511#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004512/*
4513 * Convert the string "text" to a floating point number.
4514 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4515 * this always uses a decimal point.
4516 * Returns the length of the text that was consumed.
4517 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004519string2float(
4520 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004521 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004522{
4523 char *s = (char *)text;
4524 float_T f;
4525
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004526 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004527 if (STRNICMP(text, "inf", 3) == 0)
4528 {
4529 *value = INFINITY;
4530 return 3;
4531 }
4532 if (STRNICMP(text, "-inf", 3) == 0)
4533 {
4534 *value = -INFINITY;
4535 return 4;
4536 }
4537 if (STRNICMP(text, "nan", 3) == 0)
4538 {
4539 *value = NAN;
4540 return 3;
4541 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004542 f = strtod(s, &s);
4543 *value = f;
4544 return (int)((char_u *)s - text);
4545}
4546#endif
4547
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004550 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004553var2fpos(
4554 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004555 int dollar_lnum, // TRUE when $ is last line
4556 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004558 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004560 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004562 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004563 if (varp->v_type == VAR_LIST)
4564 {
4565 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004566 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004567 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004568 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004569
4570 l = varp->vval.v_list;
4571 if (l == NULL)
4572 return NULL;
4573
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004574 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004575 pos.lnum = list_find_nr(l, 0L, &error);
4576 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004577 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004578
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004579 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004580 pos.col = list_find_nr(l, 1L, &error);
4581 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004582 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004583 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004585 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004586 li = list_find(l, 1L);
4587 if (li != NULL && li->li_tv.v_type == VAR_STRING
4588 && li->li_tv.vval.v_string != NULL
4589 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4590 pos.col = len + 1;
4591
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004592 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004593 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004594 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004595 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004597 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004598 pos.coladd = list_find_nr(l, 2L, &error);
4599 if (error)
4600 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004601
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004602 return &pos;
4603 }
4604
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004605 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004606 if (name == NULL)
4607 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004608 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004610 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004611 {
4612 if (VIsual_active)
4613 return &VIsual;
4614 return &curwin->w_cursor;
4615 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004616 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004618 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4620 return NULL;
4621 return pp;
4622 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004623
Bram Moolenaara5525202006-03-02 22:52:09 +00004624 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004625
Bram Moolenaar477933c2007-07-17 14:32:23 +00004626 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004627 {
4628 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004629 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004630 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004631 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004632 // In silent Ex mode topline is zero, but that's not a valid line
4633 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004634 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004635 return &pos;
4636 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004637 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004638 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004639 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004640 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004641 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004642 return &pos;
4643 }
4644 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004645 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004647 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 {
4649 pos.lnum = curbuf->b_ml.ml_line_count;
4650 pos.col = 0;
4651 }
4652 else
4653 {
4654 pos.lnum = curwin->w_cursor.lnum;
4655 pos.col = (colnr_T)STRLEN(ml_get_curline());
4656 }
4657 return &pos;
4658 }
4659 return NULL;
4660}
4661
4662/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004663 * Convert list in "arg" into a position and optional file number.
4664 * When "fnump" is NULL there is no file number, only 3 items.
4665 * Note that the column is passed on as-is, the caller may want to decrement
4666 * it to use 1 for the first column.
4667 * Return FAIL when conversion is not possible, doesn't check the position for
4668 * validity.
4669 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004671list2fpos(
4672 typval_T *arg,
4673 pos_T *posp,
4674 int *fnump,
4675 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004676{
4677 list_T *l = arg->vval.v_list;
4678 long i = 0;
4679 long n;
4680
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004681 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4682 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004683 if (arg->v_type != VAR_LIST
4684 || l == NULL
4685 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004686 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004687 return FAIL;
4688
4689 if (fnump != NULL)
4690 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004691 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004692 if (n < 0)
4693 return FAIL;
4694 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004695 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004696 *fnump = n;
4697 }
4698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004699 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004700 if (n < 0)
4701 return FAIL;
4702 posp->lnum = n;
4703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004704 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004705 if (n < 0)
4706 return FAIL;
4707 posp->col = n;
4708
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004709 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004710 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004711 posp->coladd = 0;
4712 else
4713 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004714
Bram Moolenaar493c1782014-05-28 14:34:46 +02004715 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004716 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004717
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004718 return OK;
4719}
4720
4721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 * Get the length of an environment variable name.
4723 * Advance "arg" to the first character after the name.
4724 * Return 0 for error.
4725 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004726 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004727get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728{
4729 char_u *p;
4730 int len;
4731
4732 for (p = *arg; vim_isIDc(*p); ++p)
4733 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004734 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 return 0;
4736
4737 len = (int)(p - *arg);
4738 *arg = p;
4739 return len;
4740}
4741
4742/*
4743 * Get the length of the name of a function or internal variable.
4744 * "arg" is advanced to the first non-white character after the name.
4745 * Return 0 if something is wrong.
4746 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004747 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004748get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 char_u *p;
4751 int len;
4752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004753 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004755 {
4756 if (*p == ':')
4757 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004758 // "s:" is start of "s:var", but "n:" is not and can be used in
4759 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004760 len = (int)(p - *arg);
4761 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4762 || len > 1)
4763 break;
4764 }
4765 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004766 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 return 0;
4768
4769 len = (int)(p - *arg);
4770 *arg = skipwhite(p);
4771
4772 return len;
4773}
4774
4775/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004776 * Get the length of the name of a variable or function.
4777 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004779 * Return -1 if curly braces expansion failed.
4780 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 * If the name contains 'magic' {}'s, expand them and return the
4782 * expanded name in an allocated string via 'alias' - caller must free.
4783 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004785get_name_len(
4786 char_u **arg,
4787 char_u **alias,
4788 int evaluate,
4789 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790{
4791 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 char_u *p;
4793 char_u *expr_start;
4794 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004796 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
4798 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4799 && (*arg)[2] == (int)KE_SNR)
4800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004801 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 *arg += 3;
4803 return get_id_len(arg) + 3;
4804 }
4805 len = eval_fname_script(*arg);
4806 if (len > 0)
4807 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004808 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 *arg += len;
4810 }
4811
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004813 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004815 p = find_name_end(*arg, &expr_start, &expr_end,
4816 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 if (expr_start != NULL)
4818 {
4819 char_u *temp_string;
4820
4821 if (!evaluate)
4822 {
4823 len += (int)(p - *arg);
4824 *arg = skipwhite(p);
4825 return len;
4826 }
4827
4828 /*
4829 * Include any <SID> etc in the expanded string:
4830 * Thus the -len here.
4831 */
4832 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4833 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004834 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 *alias = temp_string;
4836 *arg = skipwhite(p);
4837 return (int)STRLEN(temp_string);
4838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
4840 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004841 // Only give an error when there is something, otherwise it will be
4842 // reported at a higher level.
4843 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004844 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
4846 return len;
4847}
4848
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849/*
4850 * Find the end of a variable or function name, taking care of magic braces.
4851 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4852 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004853 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 * Return a pointer to just after the name. Equal to "arg" if there is no
4855 * valid name.
4856 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004857 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004858find_name_end(
4859 char_u *arg,
4860 char_u **expr_start,
4861 char_u **expr_end,
4862 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004864 int mb_nest = 0;
4865 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004867 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004868 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004872 *expr_start = NULL;
4873 *expr_end = NULL;
4874 }
4875
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004876 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004877 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4878 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004879 return arg;
4880
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 for (p = arg; *p != NUL
4882 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004883 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004884 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004886 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004888 if (*p == '\'')
4889 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004890 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004891 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004892 ;
4893 if (*p == NUL)
4894 break;
4895 }
4896 else if (*p == '"')
4897 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004898 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004899 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004900 if (*p == '\\' && p[1] != NUL)
4901 ++p;
4902 if (*p == NUL)
4903 break;
4904 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004905 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4906 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004907 // "s:" is start of "s:var", but "n:" is not and can be used in
4908 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004909 len = (int)(p - arg);
4910 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004911 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004912 break;
4913 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004914
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004917 if (*p == '[')
4918 ++br_nest;
4919 else if (*p == ']')
4920 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004922
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004923 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004925 if (*p == '{')
4926 {
4927 mb_nest++;
4928 if (expr_start != NULL && *expr_start == NULL)
4929 *expr_start = p;
4930 }
4931 else if (*p == '}')
4932 {
4933 mb_nest--;
4934 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4935 *expr_end = p;
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
4939
4940 return p;
4941}
4942
4943/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004944 * Expands out the 'magic' {}'s in a variable/function name.
4945 * Note that this can call itself recursively, to deal with
4946 * constructs like foo{bar}{baz}{bam}
4947 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4948 * "in_start" ^
4949 * "expr_start" ^
4950 * "expr_end" ^
4951 * "in_end" ^
4952 *
4953 * Returns a new allocated string, which the caller must free.
4954 * Returns NULL for failure.
4955 */
4956 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004957make_expanded_name(
4958 char_u *in_start,
4959 char_u *expr_start,
4960 char_u *expr_end,
4961 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004962{
4963 char_u c1;
4964 char_u *retval = NULL;
4965 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004966
4967 if (expr_end == NULL || in_end == NULL)
4968 return NULL;
4969 *expr_start = NUL;
4970 *expr_end = NUL;
4971 c1 = *in_end;
4972 *in_end = NUL;
4973
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004974 temp_result = eval_to_string(expr_start + 1, FALSE);
4975 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004976 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004977 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4978 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004979 if (retval != NULL)
4980 {
4981 STRCPY(retval, in_start);
4982 STRCAT(retval, temp_result);
4983 STRCAT(retval, expr_end + 1);
4984 }
4985 }
4986 vim_free(temp_result);
4987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004988 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004989 *expr_start = '{';
4990 *expr_end = '}';
4991
4992 if (retval != NULL)
4993 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004994 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004995 if (expr_start != NULL)
4996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004997 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004998 temp_result = make_expanded_name(retval, expr_start,
4999 expr_end, temp_result);
5000 vim_free(retval);
5001 retval = temp_result;
5002 }
5003 }
5004
5005 return retval;
5006}
5007
5008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005010 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005012 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005013eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005015 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5016}
5017
5018/*
5019 * Return TRUE if character "c" can be used as the first character in a
5020 * variable or function name (excluding '{' and '}').
5021 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005023eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005024{
5025 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026}
5027
5028/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005029 * Handle:
5030 * - expr[expr], expr[expr:expr] subscript
5031 * - ".name" lookup
5032 * - function call with Funcref variable: func(expr)
5033 * - method call: var->method()
5034 *
5035 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005036 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005037 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005038handle_subscript(
5039 char_u **arg,
5040 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005041 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005042 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005043{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005044 int evaluate = evalarg != NULL
5045 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005046 int ret = OK;
5047 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005048 int check_white = TRUE;
5049
5050 // When at the end of the line and ".name" follows in the next line then
5051 // consume the line break. Only when rettv is a dict.
5052 if (rettv->v_type == VAR_DICT)
5053 {
5054 int getnext;
5055 char_u *p = eval_next_non_blank(*arg, evalarg, &getnext);
5056
5057 if (getnext && *p == '.' && ASCII_ISALPHA(p[1]))
5058 {
5059 *arg = eval_next_line(evalarg);
5060 check_white = FALSE;
5061 }
5062 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005063
Bram Moolenaar61343f02019-07-20 21:11:13 +02005064 // "." is ".name" lookup when we found a dict or when evaluating and
5065 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005066 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005067 && (((**arg == '['
5068 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005069 || (!evaluate
5070 && (*arg)[1] != '.'
5071 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005072 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005073 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005074 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005075 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005076 {
5077 if (**arg == '(')
5078 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005079 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5080 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005081
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005082 // Stop the expression evaluation when immediately aborting on
5083 // error, or when an interrupt occurred or an exception was thrown
5084 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005085 if (aborting())
5086 {
5087 if (ret == OK)
5088 clear_tv(rettv);
5089 ret = FAIL;
5090 }
5091 dict_unref(selfdict);
5092 selfdict = NULL;
5093 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005094 else if (**arg == '-')
5095 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005096 if (ret == OK)
5097 {
5098 if ((*arg)[2] == '{')
5099 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005100 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005101 else
5102 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005103 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005104 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005105 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005106 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005107 {
5108 dict_unref(selfdict);
5109 if (rettv->v_type == VAR_DICT)
5110 {
5111 selfdict = rettv->vval.v_dict;
5112 if (selfdict != NULL)
5113 ++selfdict->dv_refcount;
5114 }
5115 else
5116 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005117 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005118 {
5119 clear_tv(rettv);
5120 ret = FAIL;
5121 }
5122 }
5123 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005124
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005125 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5126 // Don't do this when "Func" is already a partial that was bound
5127 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005128 if (selfdict != NULL
5129 && (rettv->v_type == VAR_FUNC
5130 || (rettv->v_type == VAR_PARTIAL
5131 && (rettv->vval.v_partial->pt_auto
5132 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005133 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005134
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005135 dict_unref(selfdict);
5136 return ret;
5137}
5138
5139/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005140 * Make a copy of an item.
5141 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005142 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5143 * reference to an already copied list/dict can be used.
5144 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005145 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005146 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005147item_copy(
5148 typval_T *from,
5149 typval_T *to,
5150 int deep,
5151 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005152{
5153 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005154 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005155
Bram Moolenaar33570922005-01-25 22:26:29 +00005156 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005158 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005159 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005160 }
5161 ++recurse;
5162
5163 switch (from->v_type)
5164 {
5165 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005166 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005167 case VAR_STRING:
5168 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005169 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005170 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005171 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005172 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005173 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 copy_tv(from, to);
5175 break;
5176 case VAR_LIST:
5177 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005178 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005179 if (from->vval.v_list == NULL)
5180 to->vval.v_list = NULL;
5181 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5182 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005183 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005184 to->vval.v_list = from->vval.v_list->lv_copylist;
5185 ++to->vval.v_list->lv_refcount;
5186 }
5187 else
5188 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5189 if (to->vval.v_list == NULL)
5190 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005191 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005192 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005194 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005195 case VAR_DICT:
5196 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005197 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005198 if (from->vval.v_dict == NULL)
5199 to->vval.v_dict = NULL;
5200 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5201 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005202 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005203 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5204 ++to->vval.v_dict->dv_refcount;
5205 }
5206 else
5207 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5208 if (to->vval.v_dict == NULL)
5209 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005210 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005211 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005212 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005214 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005215 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005216 }
5217 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005218 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005219}
5220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 void
5222echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5223{
5224 char_u *tofree;
5225 char_u numbuf[NUMBUFLEN];
5226 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5227
5228 if (*atstart)
5229 {
5230 *atstart = FALSE;
5231 // Call msg_start() after eval1(), evaluating the expression
5232 // may cause a message to appear.
5233 if (with_space)
5234 {
5235 // Mark the saved text as finishing the line, so that what
5236 // follows is displayed on a new line when scrolling back
5237 // at the more prompt.
5238 msg_sb_eol();
5239 msg_start();
5240 }
5241 }
5242 else if (with_space)
5243 msg_puts_attr(" ", echo_attr);
5244
5245 if (p != NULL)
5246 for ( ; *p != NUL && !got_int; ++p)
5247 {
5248 if (*p == '\n' || *p == '\r' || *p == TAB)
5249 {
5250 if (*p != TAB && *needclr)
5251 {
5252 // remove any text still there from the command
5253 msg_clr_eos();
5254 *needclr = FALSE;
5255 }
5256 msg_putchar_attr(*p, echo_attr);
5257 }
5258 else
5259 {
5260 if (has_mbyte)
5261 {
5262 int i = (*mb_ptr2len)(p);
5263
5264 (void)msg_outtrans_len_attr(p, i, echo_attr);
5265 p += i - 1;
5266 }
5267 else
5268 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5269 }
5270 }
5271 vim_free(tofree);
5272}
5273
Bram Moolenaare9a41262005-01-15 22:18:47 +00005274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 * ":echo expr1 ..." print each argument separated with a space, add a
5276 * newline at the end.
5277 * ":echon expr1 ..." print each argument plain.
5278 */
5279 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005280ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281{
5282 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005283 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 char_u *p;
5285 int needclr = TRUE;
5286 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005287 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005288 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005289 evalarg_T evalarg;
5290
Bram Moolenaare707c882020-07-01 13:07:37 +02005291 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292
5293 if (eap->skip)
5294 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005295 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005297 // If eval1() causes an error message the text from the command may
5298 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005299 need_clr_eos = needclr;
5300
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005302 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 {
5304 /*
5305 * Report the invalid expression unless the expression evaluation
5306 * has been cancelled due to an aborting error, an interrupt, or an
5307 * exception.
5308 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005309 if (!aborting() && did_emsg == did_emsg_before
5310 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005311 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 break;
5314 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005315 need_clr_eos = FALSE;
5316
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005319
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005320 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 arg = skipwhite(arg);
5322 }
5323 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005324 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
5326 if (eap->skip)
5327 --emsg_skip;
5328 else
5329 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005330 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 if (needclr)
5332 msg_clr_eos();
5333 if (eap->cmdidx == CMD_echo)
5334 msg_end();
5335 }
5336}
5337
5338/*
5339 * ":echohl {name}".
5340 */
5341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005342ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005344 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345}
5346
5347/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005348 * Returns the :echo attribute
5349 */
5350 int
5351get_echo_attr(void)
5352{
5353 return echo_attr;
5354}
5355
5356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 * ":execute expr1 ..." execute the result of an expression.
5358 * ":echomsg expr1 ..." Print a message
5359 * ":echoerr expr1 ..." Print an error
5360 * Each gets spaces around each argument and a newline at the end for
5361 * echo commands
5362 */
5363 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005364ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
5366 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005367 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 int ret = OK;
5369 char_u *p;
5370 garray_T ga;
5371 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
5373 ga_init2(&ga, 1, 80);
5374
5375 if (eap->skip)
5376 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005377 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005379 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005380 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383 if (!eap->skip)
5384 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005385 char_u buf[NUMBUFLEN];
5386
5387 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005388 {
5389 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5390 {
5391 emsg(_(e_inval_string));
5392 p = NULL;
5393 }
5394 else
5395 p = tv_get_string_buf(&rettv, buf);
5396 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005397 else
5398 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005399 if (p == NULL)
5400 {
5401 clear_tv(&rettv);
5402 ret = FAIL;
5403 break;
5404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 len = (int)STRLEN(p);
5406 if (ga_grow(&ga, len + 2) == FAIL)
5407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005408 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 ret = FAIL;
5410 break;
5411 }
5412 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 ga.ga_len += len;
5416 }
5417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 arg = skipwhite(arg);
5420 }
5421
5422 if (ret != FAIL && ga.ga_data != NULL)
5423 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005424 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5425 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005426 // Mark the already saved text as finishing the line, so that what
5427 // follows is displayed on a new line when scrolling back at the
5428 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005429 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005430 }
5431
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005433 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005434 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005435 out_flush();
5436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 else if (eap->cmdidx == CMD_echoerr)
5438 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005439 int save_did_emsg = did_emsg;
5440
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005441 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005442 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 if (!force_abort)
5444 did_emsg = save_did_emsg;
5445 }
5446 else if (eap->cmdidx == CMD_execute)
5447 do_cmdline((char_u *)ga.ga_data,
5448 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5449 }
5450
5451 ga_clear(&ga);
5452
5453 if (eap->skip)
5454 --emsg_skip;
5455
5456 eap->nextcmd = check_nextcmd(arg);
5457}
5458
5459/*
5460 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5461 * "arg" points to the "&" or '+' when called, to "option" when returning.
5462 * Returns NULL when no option name found. Otherwise pointer to the char
5463 * after the option name.
5464 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005465 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005466find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
5468 char_u *p = *arg;
5469
5470 ++p;
5471 if (*p == 'g' && p[1] == ':')
5472 {
5473 *opt_flags = OPT_GLOBAL;
5474 p += 2;
5475 }
5476 else if (*p == 'l' && p[1] == ':')
5477 {
5478 *opt_flags = OPT_LOCAL;
5479 p += 2;
5480 }
5481 else
5482 *opt_flags = 0;
5483
5484 if (!ASCII_ISALPHA(*p))
5485 return NULL;
5486 *arg = p;
5487
5488 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005489 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 else
5491 while (ASCII_ISALPHA(*p))
5492 ++p;
5493 return p;
5494}
5495
5496/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005497 * Display script name where an item was last set.
5498 * Should only be invoked when 'verbose' is non-zero.
5499 */
5500 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005501last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005502{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005503 char_u *p;
5504
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005505 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005506 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005507 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005508 if (p != NULL)
5509 {
5510 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005511 msg_puts(_("\n\tLast set from "));
5512 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005513 if (script_ctx.sc_lnum > 0)
5514 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005515 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005516 msg_outnum((long)script_ctx.sc_lnum);
5517 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005518 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005519 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005520 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005521 }
5522}
5523
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005524#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
5526/*
5527 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005528 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 * "flags" can be "g" to do a global substitute.
5530 * Returns an allocated string, NULL for error.
5531 */
5532 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005533do_string_sub(
5534 char_u *str,
5535 char_u *pat,
5536 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005537 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005538 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 int sublen;
5541 regmatch_T regmatch;
5542 int i;
5543 int do_all;
5544 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005545 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 garray_T ga;
5547 char_u *ret;
5548 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005549 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005551 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005553 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554
5555 ga_init2(&ga, 1, 200);
5556
5557 do_all = (flags[0] == 'g');
5558
5559 regmatch.rm_ic = p_ic;
5560 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5561 if (regmatch.regprog != NULL)
5562 {
5563 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005564 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5566 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005567 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005568 if (regmatch.startp[0] == regmatch.endp[0])
5569 {
5570 if (zero_width == regmatch.startp[0])
5571 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005572 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005573 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005574 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5575 (size_t)i);
5576 ga.ga_len += i;
5577 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005578 continue;
5579 }
5580 zero_width = regmatch.startp[0];
5581 }
5582
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 /*
5584 * Get some space for a temporary buffer to do the substitution
5585 * into. It will contain:
5586 * - The text up to where the match is.
5587 * - The substituted text.
5588 * - The text after the match.
5589 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005590 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005591 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5593 {
5594 ga_clear(&ga);
5595 break;
5596 }
5597
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005598 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 i = (int)(regmatch.startp[0] - tail);
5600 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005601 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005602 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 + ga.ga_len + i, TRUE, TRUE, FALSE);
5604 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005605 tail = regmatch.endp[0];
5606 if (*tail == NUL)
5607 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 if (!do_all)
5609 break;
5610 }
5611
5612 if (ga.ga_data != NULL)
5613 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5614
Bram Moolenaar473de612013-06-08 18:19:48 +02005615 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 }
5617
5618 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5619 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005620 if (p_cpo == empty_option)
5621 p_cpo = save_cpo;
5622 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005623 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005624 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625
5626 return ret;
5627}