blob: 41ebeb6f5b73561d0e035106aa0ea2643c2fc65d [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 Moolenaarc70646c2005-01-04 21:52:38 +000023static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020025#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020026static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020027#endif
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +020028static char *e_nowhitespace = N_("E274: No white space allowed before parenthesis");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010030#define NAMESPACE_CHAR (char_u *)"abglstvw"
31
Bram Moolenaar532c7802005-01-27 14:44:31 +000032/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000033 * When recursively copying lists and dicts we need to remember which ones we
34 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000035 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000036 */
37static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010039static int echo_attr = 0; // attributes used for ":echo"
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
Bram Moolenaar071d4272004-06-13 20:20:40 +000041/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000042 * Info used by a ":for" loop.
43 */
Bram Moolenaar33570922005-01-25 22:26:29 +000044typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010046 int fi_semicolon; // TRUE if ending in '; var]'
47 int fi_varcount; // nr of variables in the list
48 listwatch_T fi_lw; // keep an eye on the item used.
49 list_T *fi_list; // list being used
50 int fi_bi; // index of blob
51 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000052} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000053
Bram Moolenaar48e697e2016-01-23 22:17:30 +010054static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int eval2(char_u **arg, typval_T *rettv, int evaluate);
56static int eval3(char_u **arg, typval_T *rettv, int evaluate);
57static int eval4(char_u **arg, typval_T *rettv, int evaluate);
58static int eval5(char_u **arg, typval_T *rettv, int evaluate);
59static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
60static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +020061static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000062
Bram Moolenaar48e697e2016-01-23 22:17:30 +010063static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
64static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +010065static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +010066static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020067static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010068static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020069
Bram Moolenaare21c1582019-03-02 11:57:09 +010070/*
71 * Return "n1" divided by "n2", taking care of dividing by zero.
72 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020073 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010074num_divide(varnumber_T n1, varnumber_T n2)
75{
76 varnumber_T result;
77
78 if (n2 == 0) // give an error message?
79 {
80 if (n1 == 0)
81 result = VARNUM_MIN; // similar to NaN
82 else if (n1 < 0)
83 result = -VARNUM_MAX;
84 else
85 result = VARNUM_MAX;
86 }
87 else
88 result = n1 / n2;
89
90 return result;
91}
92
93/*
94 * Return "n1" modulus "n2", taking care of dividing by zero.
95 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020096 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010097num_modulus(varnumber_T n1, varnumber_T n2)
98{
99 // Give an error when n2 is 0?
100 return (n2 == 0) ? 0 : (n1 % n2);
101}
102
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100103#if defined(EBCDIC) || defined(PROTO)
104/*
105 * Compare struct fst by function name.
106 */
107 static int
108compare_func_name(const void *s1, const void *s2)
109{
110 struct fst *p1 = (struct fst *)s1;
111 struct fst *p2 = (struct fst *)s2;
112
113 return STRCMP(p1->f_name, p2->f_name);
114}
115
116/*
117 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100118 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100119 * On machines using EBCDIC we have to sort it.
120 */
121 static void
122sortFunctions(void)
123{
124 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
125
126 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
127}
128#endif
129
Bram Moolenaar33570922005-01-25 22:26:29 +0000130/*
131 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000132 */
133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100134eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000135{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200136 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200137 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000138
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200139#ifdef EBCDIC
140 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100141 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200142 */
143 sortFunctions();
144#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000145}
146
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000147#if defined(EXITFREE) || defined(PROTO)
148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100149eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000150{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200151 evalvars_clear();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000152
Bram Moolenaard9fba312005-06-26 22:34:35 +0000153 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200154 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000155
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200156 // autoloaded script names
157 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000158
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200159 // unreferenced lists and dicts
160 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200161
162 // functions not garbage collected
163 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000164}
165#endif
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167/*
168 * Top level evaluation function, returning a boolean.
169 * Sets "error" to TRUE if there was an error.
170 * Return TRUE or FALSE.
171 */
172 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100173eval_to_bool(
174 char_u *arg,
175 int *error,
176 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100177 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178{
Bram Moolenaar33570922005-01-25 22:26:29 +0000179 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200180 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
182 if (skip)
183 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000184 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 else
187 {
188 *error = FALSE;
189 if (!skip)
190 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100191 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000192 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 }
194 }
195 if (skip)
196 --emsg_skip;
197
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200198 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199}
200
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201/*
202 * Call eval1() and give an error message if not done at a lower level.
203 */
204 static int
205eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
206{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100207 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208 int ret;
209 int did_emsg_before = did_emsg;
210 int called_emsg_before = called_emsg;
211
212 ret = eval1(arg, rettv, evaluate);
213 if (ret == FAIL)
214 {
215 // Report the invalid expression unless the expression evaluation has
216 // been cancelled due to an aborting error, an interrupt, or an
217 // exception, or we already gave a more specific error.
218 // Also check called_emsg for when using assert_fails().
219 if (!aborting() && did_emsg == did_emsg_before
220 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100221 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222 }
223 return ret;
224}
225
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200226 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100227eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
228{
229 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100230 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200231 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100232
233 if (expr->v_type == VAR_FUNC)
234 {
235 s = expr->vval.v_string;
236 if (s == NULL || *s == NUL)
237 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200238 vim_memset(&funcexe, 0, sizeof(funcexe));
239 funcexe.evaluate = TRUE;
240 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100241 return FAIL;
242 }
243 else if (expr->v_type == VAR_PARTIAL)
244 {
245 partial_T *partial = expr->vval.v_partial;
246
247 s = partial_name(partial);
248 if (s == NULL || *s == NUL)
249 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200250 vim_memset(&funcexe, 0, sizeof(funcexe));
251 funcexe.evaluate = TRUE;
252 funcexe.partial = partial;
253 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100254 return FAIL;
255 }
256 else
257 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100258 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100259 if (s == NULL)
260 return FAIL;
261 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100262 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100263 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100264 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100265 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200266 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100267 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100268 return FAIL;
269 }
270 }
271 return OK;
272}
273
274/*
275 * Like eval_to_bool() but using a typval_T instead of a string.
276 * Works for string, funcref and partial.
277 */
278 int
279eval_expr_to_bool(typval_T *expr, int *error)
280{
281 typval_T rettv;
282 int res;
283
284 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
285 {
286 *error = TRUE;
287 return FALSE;
288 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100289 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100290 clear_tv(&rettv);
291 return res;
292}
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294/*
295 * Top level evaluation function, returning a string. If "skip" is TRUE,
296 * only parsing to "nextcmd" is done, without reporting errors. Return
297 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
298 */
299 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100300eval_to_string_skip(
301 char_u *arg,
302 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100303 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304{
Bram Moolenaar33570922005-01-25 22:26:29 +0000305 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 char_u *retval;
307
308 if (skip)
309 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000310 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 retval = NULL;
312 else
313 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100314 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000315 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 }
317 if (skip)
318 --emsg_skip;
319
320 return retval;
321}
322
323/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000324 * Skip over an expression at "*pp".
325 * Return FAIL for an error, OK otherwise.
326 */
327 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100328skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000329{
Bram Moolenaar33570922005-01-25 22:26:29 +0000330 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000331
332 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000333 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000334}
335
336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000338 * When "convert" is TRUE convert a List into a sequence of lines and convert
339 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 * Return pointer to allocated memory, or NULL for failure.
341 */
342 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100343eval_to_string(
344 char_u *arg,
345 char_u **nextcmd,
346 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347{
Bram Moolenaar33570922005-01-25 22:26:29 +0000348 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000350 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000351#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000352 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000355 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 retval = NULL;
357 else
358 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000359 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000360 {
361 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000362 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200363 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200364 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200365 if (tv.vval.v_list->lv_len > 0)
366 ga_append(&ga, NL);
367 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000368 ga_append(&ga, NUL);
369 retval = (char_u *)ga.ga_data;
370 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000371#ifdef FEAT_FLOAT
372 else if (convert && tv.v_type == VAR_FLOAT)
373 {
374 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
375 retval = vim_strsave(numbuf);
376 }
377#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000378 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100379 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000380 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 }
382
383 return retval;
384}
385
386/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000387 * Call eval_to_string() without using current local variables and using
388 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 */
390 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100391eval_to_string_safe(
392 char_u *arg,
393 char_u **nextcmd,
394 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395{
396 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200397 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200399 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000400 if (use_sandbox)
401 ++sandbox;
402 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000403 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000404 if (use_sandbox)
405 --sandbox;
406 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200407 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 return retval;
409}
410
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411/*
412 * Top level evaluation function, returning a number.
413 * Evaluates "expr" silently.
414 * Returns -1 for an error.
415 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200416 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100417eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418{
Bram Moolenaar33570922005-01-25 22:26:29 +0000419 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200420 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000421 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
423 ++emsg_off;
424
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000425 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 retval = -1;
427 else
428 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100429 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000430 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 }
432 --emsg_off;
433
434 return retval;
435}
436
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000437/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000438 * Top level evaluation function.
439 * Returns an allocated typval_T with the result.
440 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000441 */
442 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100443eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000444{
445 typval_T *tv;
446
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200447 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000448 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100449 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000450
451 return tv;
452}
453
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100455 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200456 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
457 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000458 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100461call_vim_function(
462 char_u *func,
463 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200464 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200465 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000467 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200468 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100470 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200471 vim_memset(&funcexe, 0, sizeof(funcexe));
472 funcexe.firstline = curwin->w_cursor.lnum;
473 funcexe.lastline = curwin->w_cursor.lnum;
474 funcexe.evaluate = TRUE;
475 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000476 if (ret == FAIL)
477 clear_tv(rettv);
478
479 return ret;
480}
481
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100482/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100483 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100484 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200485 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
486 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100487 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200488 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100489call_func_retnr(
490 char_u *func,
491 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200492 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100493{
494 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200495 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100496
Bram Moolenaarded27a12018-08-01 19:06:03 +0200497 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100498 return -1;
499
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100500 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100501 clear_tv(&rettv);
502 return retval;
503}
504
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000505/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100506 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000507 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200508 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
509 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000510 */
511 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100512call_func_retstr(
513 char_u *func,
514 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200515 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000516{
517 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000518 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000519
Bram Moolenaarded27a12018-08-01 19:06:03 +0200520 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000521 return NULL;
522
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100523 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000524 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 return retval;
526}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000527
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000528/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100529 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200530 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
531 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000532 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000533 */
534 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100535call_func_retlist(
536 char_u *func,
537 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200538 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000539{
540 typval_T rettv;
541
Bram Moolenaarded27a12018-08-01 19:06:03 +0200542 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000543 return NULL;
544
545 if (rettv.v_type != VAR_LIST)
546 {
547 clear_tv(&rettv);
548 return NULL;
549 }
550
551 return rettv.vval.v_list;
552}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#ifdef FEAT_FOLDING
555/*
556 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
557 * it in "*cp". Doesn't give error messages.
558 */
559 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100560eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561{
Bram Moolenaar33570922005-01-25 22:26:29 +0000562 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200563 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000565 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
566 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
568 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000569 if (use_sandbox)
570 ++sandbox;
571 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000573 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 retval = 0;
575 else
576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100577 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000578 if (tv.v_type == VAR_NUMBER)
579 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000580 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 retval = 0;
582 else
583 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100584 // If the result is a string, check if there is a non-digit before
585 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (!VIM_ISDIGIT(*s) && *s != '-')
588 *cp = *s++;
589 retval = atol((char *)s);
590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000591 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
593 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000594 if (use_sandbox)
595 --sandbox;
596 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200598 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599}
600#endif
601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000603 * Get an lval: variable, Dict item or List item that can be assigned a value
604 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
605 * "name.key", "name.key[expr]" etc.
606 * Indexing only works if "name" is an existing List or Dictionary.
607 * "name" points to the start of the name.
608 * If "rettv" is not NULL it points to the value to be assigned.
609 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
610 * wrong; must end in space or cmd separator.
611 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100612 * flags:
613 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100614 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100615 * GLV_NO_AUTOLOAD: do not use script autoloading
616 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000617 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000618 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000619 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000620 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200621 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100622get_lval(
623 char_u *name,
624 typval_T *rettv,
625 lval_T *lp,
626 int unlet,
627 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100628 int flags, // GLV_ values
629 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000630{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000631 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000632 char_u *expr_start, *expr_end;
633 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000634 dictitem_T *v;
635 typval_T var1;
636 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000637 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000638 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000639 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000640 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000641 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100642 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100644 // Clear everything in "lp".
Bram Moolenaar33570922005-01-25 22:26:29 +0000645 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000646
647 if (skip)
648 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100649 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000650 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000651 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000652 }
653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100654 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000655 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000656 if (expr_start != NULL)
657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100658 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100659 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000660 && *p != '[' && *p != '.')
661 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100662 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000663 return NULL;
664 }
665
666 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
667 if (lp->ll_exp_name == NULL)
668 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100669 // Report an invalid expression in braces, unless the
670 // expression evaluation has been cancelled due to an
671 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000672 if (!aborting() && !quiet)
673 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000674 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100675 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000676 return NULL;
677 }
678 }
679 lp->ll_name = lp->ll_exp_name;
680 }
681 else
682 lp->ll_name = name;
683
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100684 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000685 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
686 return p;
687
688 cc = *p;
689 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100690 // Only pass &ht when we would write to the variable, it prevents autoload
691 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100692 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
693 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000694 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100695 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000696 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000697 if (v == NULL)
698 return NULL;
699
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000700 /*
701 * Loop until no more [idx] or .key is following.
702 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000703 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100704 var1.v_type = VAR_UNKNOWN;
705 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000706 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000708 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
709 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100710 && lp->ll_tv->vval.v_dict != NULL)
711 && !(lp->ll_tv->v_type == VAR_BLOB
712 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000714 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100715 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000716 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000718 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000720 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100721 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000722 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000723 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000724
Bram Moolenaar8c711452005-01-14 21:53:12 +0000725 len = -1;
726 if (*p == '.')
727 {
728 key = p + 1;
729 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
730 ;
731 if (len == 0)
732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000733 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100734 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000735 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000736 }
737 p = key + len;
738 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000739 else
740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100741 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000742 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000743 if (*p == ':')
744 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000745 else
746 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000747 empty1 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100748 if (eval1(&p, &var1, TRUE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000749 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100750 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000751 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100752 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000753 clear_tv(&var1);
754 return NULL;
755 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000756 }
757
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100758 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000759 if (*p == ':')
760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000761 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000763 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100764 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100765 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000766 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000767 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100768 if (rettv != NULL
769 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100770 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100771 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100772 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000773 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000774 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100775 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100776 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000777 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000778 }
779 p = skipwhite(p + 1);
780 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000781 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000782 else
783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000784 lp->ll_empty2 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100785 if (eval1(&p, &var2, TRUE) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +0000786 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100787 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000789 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100790 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000791 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100792 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100793 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000794 clear_tv(&var2);
795 return NULL;
796 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000797 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000798 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000799 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000800 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000802
Bram Moolenaar8c711452005-01-14 21:53:12 +0000803 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100806 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100807 clear_tv(&var1);
808 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000809 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000810 }
811
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100812 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000813 ++p;
814 }
815
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000817 {
818 if (len == -1)
819 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100820 // "[key]": get key from "var1"
821 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200822 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000823 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000824 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000826 }
827 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000828 lp->ll_list = NULL;
829 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000830 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100832 // When assigning to a scope dictionary check that a function and
833 // variable name is valid (only variable name unless it is l: or
834 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200835 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200836 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200837 int prevval;
838 int wrong;
839
840 if (len != -1)
841 {
842 prevval = key[len];
843 key[len] = NUL;
844 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200845 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100846 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200847 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
848 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200849 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200850 || !valid_varname(key);
851 if (len != -1)
852 key[len] = prevval;
853 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200854 return NULL;
855 }
856
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000858 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100859 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200860 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100861 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100863 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100864 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200865 return NULL;
866 }
867
Bram Moolenaar31b81602019-02-10 22:14:27 +0100868 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000869 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000870 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000871 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100872 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100873 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000874 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000875 }
876 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000878 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100880 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000882 p = NULL;
883 break;
884 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100885 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100886 else if ((flags & GLV_READ_ONLY) == 0
887 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100888 {
889 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200890 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100891 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200892
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100893 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000895 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100896 else if (lp->ll_tv->v_type == VAR_BLOB)
897 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100898 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
899
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100900 /*
901 * Get the number and item for the only or first index of the List.
902 */
903 if (empty1)
904 lp->ll_n1 = 0;
905 else
906 // is number or string
907 lp->ll_n1 = (long)tv_get_number(&var1);
908 clear_tv(&var1);
909
910 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100911 || lp->ll_n1 > bloblen
912 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100913 {
914 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100915 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100916 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100917 return NULL;
918 }
919 if (lp->ll_range && !lp->ll_empty2)
920 {
921 lp->ll_n2 = (long)tv_get_number(&var2);
922 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100923 if (lp->ll_n2 < 0
924 || lp->ll_n2 >= bloblen
925 || lp->ll_n2 < lp->ll_n1)
926 {
927 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100928 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100929 return NULL;
930 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100931 }
932 lp->ll_blob = lp->ll_tv->vval.v_blob;
933 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100934 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100935 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000936 else
937 {
938 /*
939 * Get the number and item for the only or first index of the List.
940 */
941 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000943 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100944 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100945 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100946 clear_tv(&var1);
947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000948 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 lp->ll_list = lp->ll_tv->vval.v_list;
950 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
951 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000952 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000953 if (lp->ll_n1 < 0)
954 {
955 lp->ll_n1 = 0;
956 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
957 }
958 }
959 if (lp->ll_li == NULL)
960 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100961 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200962 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100963 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000964 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000965 }
966
967 /*
968 * May need to find the item or absolute index for the second
969 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000970 * When no index given: "lp->ll_empty2" is TRUE.
971 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000972 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000974 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100975 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100976 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +0000977 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +0200982 {
983 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100984 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000985 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +0200986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 }
989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100990 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000991 if (lp->ll_n1 < 0)
992 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
993 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +0200994 {
995 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100996 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +0200998 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000999 }
1000
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001001 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001003 }
1004
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001005 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 return p;
1007}
1008
1009/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001010 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001013clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014{
1015 vim_free(lp->ll_exp_name);
1016 vim_free(lp->ll_newkey);
1017}
1018
1019/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001020 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001022 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1023 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001024 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001025 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001026set_var_lval(
1027 lval_T *lp,
1028 char_u *endp,
1029 typval_T *rettv,
1030 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001031 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01001032 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033{
1034 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 listitem_T *ri;
1036 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037
1038 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001040 cc = *endp;
1041 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001042 if (lp->ll_blob != NULL)
1043 {
1044 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001045
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001046 if (op != NULL && *op != '=')
1047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001048 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001049 return;
1050 }
1051
1052 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1053 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001054 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001055
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001056 if (lp->ll_empty2)
1057 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1058
1059 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001060 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001061 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001062 return;
1063 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001064 if (lp->ll_empty2)
1065 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001066
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001067 ir = 0;
1068 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1069 blob_set(lp->ll_blob, il,
1070 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001071 }
1072 else
1073 {
1074 val = (int)tv_get_number_chk(rettv, &error);
1075 if (!error)
1076 {
1077 garray_T *gap = &lp->ll_blob->bv_ga;
1078
1079 // Allow for appending a byte. Setting a byte beyond
1080 // the end is an error otherwise.
1081 if (lp->ll_n1 < gap->ga_len
1082 || (lp->ll_n1 == gap->ga_len
1083 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1084 {
1085 blob_set(lp->ll_blob, lp->ll_n1, val);
1086 if (lp->ll_n1 == gap->ga_len)
1087 ++gap->ga_len;
1088 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001089 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001090 }
1091 }
1092 }
1093 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001094 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001095 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001096
Bram Moolenaar9937a052019-06-15 15:45:06 +02001097 if (is_const)
1098 {
1099 emsg(_(e_cannot_mod));
1100 *endp = cc;
1101 return;
1102 }
1103
Bram Moolenaarff697e62019-02-12 22:28:33 +01001104 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001105 di = NULL;
1106 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1107 &tv, &di, TRUE, FALSE) == OK)
1108 {
1109 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001110 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1111 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001112 && tv_op(&tv, rettv, op) == OK)
1113 set_var(lp->ll_name, &tv, FALSE);
1114 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001115 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001117 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02001118 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001119 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001121 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001122 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001123 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001124 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 else if (lp->ll_range)
1126 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001127 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001128 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001129
Bram Moolenaar9937a052019-06-15 15:45:06 +02001130 if (is_const)
1131 {
1132 emsg(_("E996: Cannot lock a range"));
1133 return;
1134 }
1135
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001136 /*
1137 * Check whether any of the list items is locked
1138 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001139 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001140 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001141 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001142 return;
1143 ri = ri->li_next;
1144 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1145 break;
1146 ll_li = ll_li->li_next;
1147 ++ll_n1;
1148 }
1149
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 /*
1151 * Assign the List values to the list items.
1152 */
1153 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001154 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001155 if (op != NULL && *op != '=')
1156 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1157 else
1158 {
1159 clear_tv(&lp->ll_li->li_tv);
1160 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1161 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 ri = ri->li_next;
1163 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1164 break;
1165 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001166 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001167 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001168 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001169 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170 ri = NULL;
1171 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001172 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001173 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001174 lp->ll_li = lp->ll_li->li_next;
1175 ++lp->ll_n1;
1176 }
1177 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001178 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001179 else if (lp->ll_empty2
1180 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001182 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001183 }
1184 else
1185 {
1186 /*
1187 * Assign to a List or Dictionary item.
1188 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02001189 if (is_const)
1190 {
1191 emsg(_("E996: Cannot lock a list or dict"));
1192 return;
1193 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 if (lp->ll_newkey != NULL)
1195 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001196 if (op != NULL && *op != '=')
1197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001199 return;
1200 }
1201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001202 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001203 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001204 if (di == NULL)
1205 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001206 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1207 {
1208 vim_free(di);
1209 return;
1210 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001211 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001212 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001213 else if (op != NULL && *op != '=')
1214 {
1215 tv_op(lp->ll_tv, rettv, op);
1216 return;
1217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001218 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001220
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 /*
1222 * Assign the value to the variable or list item.
1223 */
1224 if (copy)
1225 copy_tv(rettv, lp->ll_tv);
1226 else
1227 {
1228 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001229 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001231 }
1232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001233}
1234
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001235/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001236 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1237 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001238 * Returns OK or FAIL.
1239 */
1240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001241tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001242{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001243 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001244 char_u numbuf[NUMBUFLEN];
1245 char_u *s;
1246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001247 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001248 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
1249 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001250 {
1251 switch (tv1->v_type)
1252 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001253 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001254 case VAR_DICT:
1255 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001256 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001257 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001258 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001259 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001260 break;
1261
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001262 case VAR_BLOB:
1263 if (*op != '+' || tv2->v_type != VAR_BLOB)
1264 break;
1265 // BLOB += BLOB
1266 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1267 {
1268 blob_T *b1 = tv1->vval.v_blob;
1269 blob_T *b2 = tv2->vval.v_blob;
1270 int i, len = blob_len(b2);
1271 for (i = 0; i < len; i++)
1272 ga_append(&b1->bv_ga, blob_get(b2, i));
1273 }
1274 return OK;
1275
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001276 case VAR_LIST:
1277 if (*op != '+' || tv2->v_type != VAR_LIST)
1278 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001279 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001280 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1281 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1282 return OK;
1283
1284 case VAR_NUMBER:
1285 case VAR_STRING:
1286 if (tv2->v_type == VAR_LIST)
1287 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001288 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001289 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001290 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001291 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001292#ifdef FEAT_FLOAT
1293 if (tv2->v_type == VAR_FLOAT)
1294 {
1295 float_T f = n;
1296
Bram Moolenaarff697e62019-02-12 22:28:33 +01001297 if (*op == '%')
1298 break;
1299 switch (*op)
1300 {
1301 case '+': f += tv2->vval.v_float; break;
1302 case '-': f -= tv2->vval.v_float; break;
1303 case '*': f *= tv2->vval.v_float; break;
1304 case '/': f /= tv2->vval.v_float; break;
1305 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001306 clear_tv(tv1);
1307 tv1->v_type = VAR_FLOAT;
1308 tv1->vval.v_float = f;
1309 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001310 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001311#endif
1312 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001313 switch (*op)
1314 {
1315 case '+': n += tv_get_number(tv2); break;
1316 case '-': n -= tv_get_number(tv2); break;
1317 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001318 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1319 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001320 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001321 clear_tv(tv1);
1322 tv1->v_type = VAR_NUMBER;
1323 tv1->vval.v_number = n;
1324 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001325 }
1326 else
1327 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001328 if (tv2->v_type == VAR_FLOAT)
1329 break;
1330
Bram Moolenaarff697e62019-02-12 22:28:33 +01001331 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001332 s = tv_get_string(tv1);
1333 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334 clear_tv(tv1);
1335 tv1->v_type = VAR_STRING;
1336 tv1->vval.v_string = s;
1337 }
1338 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001339
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001340 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001341#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001342 {
1343 float_T f;
1344
Bram Moolenaarff697e62019-02-12 22:28:33 +01001345 if (*op == '%' || *op == '.'
1346 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001347 && tv2->v_type != VAR_NUMBER
1348 && tv2->v_type != VAR_STRING))
1349 break;
1350 if (tv2->v_type == VAR_FLOAT)
1351 f = tv2->vval.v_float;
1352 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001353 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001354 switch (*op)
1355 {
1356 case '+': tv1->vval.v_float += f; break;
1357 case '-': tv1->vval.v_float -= f; break;
1358 case '*': tv1->vval.v_float *= f; break;
1359 case '/': tv1->vval.v_float /= f; break;
1360 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001361 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001362#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001363 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364 }
1365 }
1366
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001367 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001368 return FAIL;
1369}
1370
1371/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001372 * Evaluate the expression used in a ":for var in expr" command.
1373 * "arg" points to "var".
1374 * Set "*errp" to TRUE for an error, FALSE otherwise;
1375 * Return a pointer that holds the info. Null when there is an error.
1376 */
1377 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001378eval_for_line(
1379 char_u *arg,
1380 int *errp,
1381 char_u **nextcmdp,
1382 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001383{
Bram Moolenaar33570922005-01-25 22:26:29 +00001384 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001385 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001386 typval_T tv;
1387 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001388
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001389 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001390
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001391 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001392 if (fi == NULL)
1393 return NULL;
1394
1395 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
1396 if (expr == NULL)
1397 return fi;
1398
1399 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001400 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001401 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001402 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001403 return fi;
1404 }
1405
1406 if (skip)
1407 ++emsg_skip;
1408 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1409 {
1410 *errp = FALSE;
1411 if (!skip)
1412 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001413 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001414 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001415 l = tv.vval.v_list;
1416 if (l == NULL)
1417 {
1418 // a null list is like an empty list: do nothing
1419 clear_tv(&tv);
1420 }
1421 else
1422 {
1423 // No need to increment the refcount, it's already set for
1424 // the list being used in "tv".
1425 fi->fi_list = l;
1426 list_add_watch(l, &fi->fi_lw);
1427 fi->fi_lw.lw_item = l->lv_first;
1428 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001429 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001430 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001431 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001432 fi->fi_bi = 0;
1433 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001434 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001435 typval_T btv;
1436
1437 // Make a copy, so that the iteration still works when the
1438 // blob is changed.
1439 blob_copy(&tv, &btv);
1440 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001441 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001442 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001443 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001444 else
1445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001446 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001447 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001448 }
1449 }
1450 }
1451 if (skip)
1452 --emsg_skip;
1453
1454 return fi;
1455}
1456
1457/*
1458 * Use the first item in a ":for" list. Advance to the next.
1459 * Assign the values to the variable (list). "arg" points to the first one.
1460 * Return TRUE when a valid item was found, FALSE when at end of list or
1461 * something wrong.
1462 */
1463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001464next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001465{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001466 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001467 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001468 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001469
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001470 if (fi->fi_blob != NULL)
1471 {
1472 typval_T tv;
1473
1474 if (fi->fi_bi >= blob_len(fi->fi_blob))
1475 return FALSE;
1476 tv.v_type = VAR_NUMBER;
1477 tv.v_lock = VAR_FIXED;
1478 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1479 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001480 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
1481 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001482 }
1483
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001484 item = fi->fi_lw.lw_item;
1485 if (item == NULL)
1486 result = FALSE;
1487 else
1488 {
1489 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001490 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
1491 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001492 }
1493 return result;
1494}
1495
1496/*
1497 * Free the structure used to store info used by ":for".
1498 */
1499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001500free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001501{
Bram Moolenaar33570922005-01-25 22:26:29 +00001502 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001503
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001504 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001505 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001506 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001507 list_unref(fi->fi_list);
1508 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001509 if (fi != NULL && fi->fi_blob != NULL)
1510 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001511 vim_free(fi);
1512}
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001515set_context_for_expression(
1516 expand_T *xp,
1517 char_u *arg,
1518 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
1520 int got_eq = FALSE;
1521 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001522 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001524 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001525 {
1526 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001527 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001528 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001529 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001530 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001531 {
1532 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001533 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001534 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001535 break;
1536 }
1537 return;
1538 }
1539 }
1540 else
1541 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1542 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 while ((xp->xp_pattern = vim_strpbrk(arg,
1544 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1545 {
1546 c = *xp->xp_pattern;
1547 if (c == '&')
1548 {
1549 c = xp->xp_pattern[1];
1550 if (c == '&')
1551 {
1552 ++xp->xp_pattern;
1553 xp->xp_context = cmdidx != CMD_let || got_eq
1554 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1555 }
1556 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001559 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1560 xp->xp_pattern += 2;
1561
1562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 }
1564 else if (c == '$')
1565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001566 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 xp->xp_context = EXPAND_ENV_VARS;
1568 }
1569 else if (c == '=')
1570 {
1571 got_eq = TRUE;
1572 xp->xp_context = EXPAND_EXPRESSION;
1573 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001574 else if (c == '#'
1575 && xp->xp_context == EXPAND_EXPRESSION)
1576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001577 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001578 break;
1579 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001580 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 && xp->xp_context == EXPAND_FUNCTIONS
1582 && vim_strchr(xp->xp_pattern, '(') == NULL)
1583 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001584 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 break;
1586 }
1587 else if (cmdidx != CMD_let || got_eq)
1588 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001589 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
1591 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1592 if (c == '\\' && xp->xp_pattern[1] != NUL)
1593 ++xp->xp_pattern;
1594 xp->xp_context = EXPAND_NOTHING;
1595 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001596 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001598 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1600 /* skip */ ;
1601 xp->xp_context = EXPAND_NOTHING;
1602 }
1603 else if (c == '|')
1604 {
1605 if (xp->xp_pattern[1] == '|')
1606 {
1607 ++xp->xp_pattern;
1608 xp->xp_context = EXPAND_EXPRESSION;
1609 }
1610 else
1611 xp->xp_context = EXPAND_COMMANDS;
1612 }
1613 else
1614 xp->xp_context = EXPAND_EXPRESSION;
1615 }
1616 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001617 // Doesn't look like something valid, expand as an expression
1618 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001619 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 arg = xp->xp_pattern;
1621 if (*arg != NUL)
1622 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1623 /* skip */ ;
1624 }
1625 xp->xp_pattern = arg;
1626}
1627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001629 * Return TRUE if "pat" matches "text".
1630 * Does not use 'cpo' and always uses 'magic'.
1631 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001632 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001633pattern_match(char_u *pat, char_u *text, int ic)
1634{
1635 int matches = FALSE;
1636 char_u *save_cpo;
1637 regmatch_T regmatch;
1638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001639 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001640 save_cpo = p_cpo;
1641 p_cpo = (char_u *)"";
1642 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1643 if (regmatch.regprog != NULL)
1644 {
1645 regmatch.rm_ic = ic;
1646 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1647 vim_regfree(regmatch.regprog);
1648 }
1649 p_cpo = save_cpo;
1650 return matches;
1651}
1652
1653/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001654 * Handle a name followed by "(". Both for just "name(arg)" and for
1655 * "expr->name(arg)".
1656 * Returns OK or FAIL.
1657 */
1658 static int
1659eval_func(
1660 char_u **arg, // points to "(", will be advanced
1661 char_u *name,
1662 int name_len,
1663 typval_T *rettv,
1664 int evaluate,
1665 typval_T *basetv) // "expr" for "expr->name(arg)"
1666{
1667 char_u *s = name;
1668 int len = name_len;
1669 partial_T *partial;
1670 int ret = OK;
1671
1672 if (!evaluate)
1673 check_vars(s, len);
1674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001675 // If "s" is the name of a variable of type VAR_FUNC
1676 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001677 s = deref_func_name(s, &len, &partial, !evaluate);
1678
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001679 // Need to make a copy, in case evaluating the arguments makes
1680 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001681 s = vim_strsave(s);
1682 if (s == NULL)
1683 ret = FAIL;
1684 else
1685 {
1686 funcexe_T funcexe;
1687
1688 // Invoke the function.
1689 vim_memset(&funcexe, 0, sizeof(funcexe));
1690 funcexe.firstline = curwin->w_cursor.lnum;
1691 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001692 funcexe.evaluate = evaluate;
1693 funcexe.partial = partial;
1694 funcexe.basetv = basetv;
1695 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1696 }
1697 vim_free(s);
1698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001699 // If evaluate is FALSE rettv->v_type was not set in
1700 // get_func_tv, but it's needed in handle_subscript() to parse
1701 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001702 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1703 {
1704 rettv->vval.v_string = NULL;
1705 rettv->v_type = VAR_FUNC;
1706 }
1707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001708 // Stop the expression evaluation when immediately
1709 // aborting on error, or when an interrupt occurred or
1710 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001711 if (evaluate && aborting())
1712 {
1713 if (ret == OK)
1714 clear_tv(rettv);
1715 ret = FAIL;
1716 }
1717 return ret;
1718}
1719
1720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001722 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1724 */
1725
1726/*
1727 * Handle zero level expression.
1728 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001729 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001730 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 * Return OK or FAIL.
1732 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001734eval0(
1735 char_u *arg,
1736 typval_T *rettv,
1737 char_u **nextcmd,
1738 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739{
1740 int ret;
1741 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001742 int did_emsg_before = did_emsg;
1743 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
1745 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (ret == FAIL || !ends_excmd(*p))
1748 {
1749 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001750 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 /*
1752 * Report the invalid expression unless the expression evaluation has
1753 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001754 * exception, or we already gave a more specific error.
1755 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001757 if (!aborting() && did_emsg == did_emsg_before
1758 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001759 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 ret = FAIL;
1761 }
1762 if (nextcmd != NULL)
1763 *nextcmd = check_nextcmd(p);
1764
1765 return ret;
1766}
1767
1768/*
1769 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001770 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 *
1772 * "arg" must point to the first non-white of the expression.
1773 * "arg" is advanced to the next non-white after the recognized expression.
1774 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001775 * Note: "rettv.v_lock" is not set.
1776 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 * Return OK or FAIL.
1778 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001780eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
1785 /*
1786 * Get the first variable.
1787 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 return FAIL;
1790
1791 if ((*arg)[0] == '?')
1792 {
1793 result = FALSE;
1794 if (evaluate)
1795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001796 int error = FALSE;
1797
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001798 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001801 if (error)
1802 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804
1805 /*
1806 * Get the second variable.
1807 */
1808 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001809 if (eval1(arg, rettv, evaluate && result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 return FAIL;
1811
1812 /*
1813 * Check for the ":".
1814 */
1815 if ((*arg)[0] != ':')
1816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001817 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 return FAIL;
1821 }
1822
1823 /*
1824 * Get the third variable.
1825 */
1826 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001827 if (eval1(arg, &var2, evaluate && !result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
1829 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 return FAIL;
1832 }
1833 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
1836
1837 return OK;
1838}
1839
1840/*
1841 * Handle first level expression:
1842 * expr2 || expr2 || expr2 logical OR
1843 *
1844 * "arg" must point to the first non-white of the expression.
1845 * "arg" is advanced to the next non-white after the recognized expression.
1846 *
1847 * Return OK or FAIL.
1848 */
1849 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001850eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851{
Bram Moolenaar33570922005-01-25 22:26:29 +00001852 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 long result;
1854 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001855 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
1857 /*
1858 * Get the first variable.
1859 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 return FAIL;
1862
1863 /*
1864 * Repeat until there is no following "||".
1865 */
1866 first = TRUE;
1867 result = FALSE;
1868 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1869 {
1870 if (evaluate && first)
1871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001872 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001875 if (error)
1876 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 first = FALSE;
1878 }
1879
1880 /*
1881 * Get the second variable.
1882 */
1883 *arg = skipwhite(*arg + 2);
1884 if (eval3(arg, &var2, evaluate && !result) == FAIL)
1885 return FAIL;
1886
1887 /*
1888 * Compute the result.
1889 */
1890 if (evaluate && !result)
1891 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001892 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001895 if (error)
1896 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 if (evaluate)
1899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 rettv->v_type = VAR_NUMBER;
1901 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 }
1904
1905 return OK;
1906}
1907
1908/*
1909 * Handle second level expression:
1910 * expr3 && expr3 && expr3 logical AND
1911 *
1912 * "arg" must point to the first non-white of the expression.
1913 * "arg" is advanced to the next non-white after the recognized expression.
1914 *
1915 * Return OK or FAIL.
1916 */
1917 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001918eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 long result;
1922 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001923 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
1925 /*
1926 * Get the first variable.
1927 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 return FAIL;
1930
1931 /*
1932 * Repeat until there is no following "&&".
1933 */
1934 first = TRUE;
1935 result = TRUE;
1936 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1937 {
1938 if (evaluate && first)
1939 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001940 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001943 if (error)
1944 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 first = FALSE;
1946 }
1947
1948 /*
1949 * Get the second variable.
1950 */
1951 *arg = skipwhite(*arg + 2);
1952 if (eval4(arg, &var2, evaluate && result) == FAIL)
1953 return FAIL;
1954
1955 /*
1956 * Compute the result.
1957 */
1958 if (evaluate && result)
1959 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001960 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001963 if (error)
1964 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
1966 if (evaluate)
1967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001968 rettv->v_type = VAR_NUMBER;
1969 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971 }
1972
1973 return OK;
1974}
1975
1976/*
1977 * Handle third level expression:
1978 * var1 == var2
1979 * var1 =~ var2
1980 * var1 != var2
1981 * var1 !~ var2
1982 * var1 > var2
1983 * var1 >= var2
1984 * var1 < var2
1985 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00001986 * var1 is var2
1987 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 *
1989 * "arg" must point to the first non-white of the expression.
1990 * "arg" is advanced to the next non-white after the recognized expression.
1991 *
1992 * Return OK or FAIL.
1993 */
1994 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001995eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
Bram Moolenaar33570922005-01-25 22:26:29 +00001997 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 char_u *p;
1999 int i;
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002000 exptype_T type = ETYPE_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
2004 /*
2005 * Get the first variable.
2006 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002007 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 return FAIL;
2009
2010 p = *arg;
2011 switch (p[0])
2012 {
2013 case '=': if (p[1] == '=')
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002014 type = ETYPE_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 else if (p[1] == '~')
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002016 type = ETYPE_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 break;
2018 case '!': if (p[1] == '=')
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002019 type = ETYPE_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 else if (p[1] == '~')
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002021 type = ETYPE_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 break;
2023 case '>': if (p[1] != '=')
2024 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002025 type = ETYPE_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 len = 1;
2027 }
2028 else
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002029 type = ETYPE_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 break;
2031 case '<': if (p[1] != '=')
2032 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002033 type = ETYPE_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 len = 1;
2035 }
2036 else
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002037 type = ETYPE_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002039 case 'i': if (p[1] == 's')
2040 {
2041 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2042 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002043 i = p[len];
2044 if (!isalnum(i) && i != '_')
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002045 type = len == 2 ? ETYPE_IS : ETYPE_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002046 }
2047 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
2049
2050 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002051 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 */
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002053 if (type != ETYPE_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002055 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (p[len] == '?')
2057 {
2058 ic = TRUE;
2059 ++len;
2060 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002061 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 else if (p[len] == '#')
2063 {
2064 ic = FALSE;
2065 ++len;
2066 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002067 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 else
2069 ic = p_ic;
2070
2071 /*
2072 * Get the second variable.
2073 */
2074 *arg = skipwhite(p + len);
2075 if (eval5(arg, &var2, evaluate) == FAIL)
2076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002077 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 return FAIL;
2079 }
Bram Moolenaar31988702018-02-13 12:57:42 +01002080 if (evaluate)
2081 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002082 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002083
2084 clear_tv(&var2);
2085 return ret;
2086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
2088
2089 return OK;
2090}
2091
2092/*
2093 * Handle fourth level expression:
2094 * + number addition
2095 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002096 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002097 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 *
2099 * "arg" must point to the first non-white of the expression.
2100 * "arg" is advanced to the next non-white after the recognized expression.
2101 *
2102 * Return OK or FAIL.
2103 */
2104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002105eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106{
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 typval_T var2;
2108 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002110 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002111#ifdef FEAT_FLOAT
2112 float_T f1 = 0, f2 = 0;
2113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 char_u *s1, *s2;
2115 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2116 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002117 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
2119 /*
2120 * Get the first variable.
2121 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002122 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 return FAIL;
2124
2125 /*
2126 * Repeat computing, until no '+', '-' or '.' is following.
2127 */
2128 for (;;)
2129 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002130 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002132 concat = op == '.'
2133 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2134 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 break;
2136
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002137 if ((op != '+' || (rettv->v_type != VAR_LIST
2138 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002139#ifdef FEAT_FLOAT
2140 && (op == '.' || rettv->v_type != VAR_FLOAT)
2141#endif
2142 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002144 // For "list + ...", an illegal use of the first operand as
2145 // a number cannot be determined before evaluating the 2nd
2146 // operand: if this is also a list, all is ok.
2147 // For "something . ...", "something - ..." or "non-list + ...",
2148 // we know that the first operand needs to be a string or number
2149 // without evaluating the 2nd operand. So check before to avoid
2150 // side effects after an error.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002151 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002152 {
2153 clear_tv(rettv);
2154 return FAIL;
2155 }
2156 }
2157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 /*
2159 * Get the second variable.
2160 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002161 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2162 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002164 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 return FAIL;
2168 }
2169
2170 if (evaluate)
2171 {
2172 /*
2173 * Compute the result.
2174 */
2175 if (op == '.')
2176 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002177 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002178 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002179 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002180 {
2181 clear_tv(rettv);
2182 clear_tv(&var2);
2183 return FAIL;
2184 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002185 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 clear_tv(rettv);
2187 rettv->v_type = VAR_STRING;
2188 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002190 else if (op == '+' && rettv->v_type == VAR_BLOB
2191 && var2.v_type == VAR_BLOB)
2192 {
2193 blob_T *b1 = rettv->vval.v_blob;
2194 blob_T *b2 = var2.vval.v_blob;
2195 blob_T *b = blob_alloc();
2196 int i;
2197
2198 if (b != NULL)
2199 {
2200 for (i = 0; i < blob_len(b1); i++)
2201 ga_append(&b->bv_ga, blob_get(b1, i));
2202 for (i = 0; i < blob_len(b2); i++)
2203 ga_append(&b->bv_ga, blob_get(b2, i));
2204
2205 clear_tv(rettv);
2206 rettv_blob_set(rettv, b);
2207 }
2208 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00002209 else if (op == '+' && rettv->v_type == VAR_LIST
2210 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002211 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002212 // concatenate Lists
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002213 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
2214 &var3) == FAIL)
2215 {
2216 clear_tv(rettv);
2217 clear_tv(&var2);
2218 return FAIL;
2219 }
2220 clear_tv(rettv);
2221 *rettv = var3;
2222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 else
2224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002225 int error = FALSE;
2226
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002227#ifdef FEAT_FLOAT
2228 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002229 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002230 f1 = rettv->vval.v_float;
2231 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002232 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002233 else
2234#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002235 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002236 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002237 if (error)
2238 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002239 // This can only happen for "list + non-list". For
2240 // "non-list + ..." or "something - ...", we returned
2241 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002242 clear_tv(rettv);
2243 return FAIL;
2244 }
2245#ifdef FEAT_FLOAT
2246 if (var2.v_type == VAR_FLOAT)
2247 f1 = n1;
2248#endif
2249 }
2250#ifdef FEAT_FLOAT
2251 if (var2.v_type == VAR_FLOAT)
2252 {
2253 f2 = var2.vval.v_float;
2254 n2 = 0;
2255 }
2256 else
2257#endif
2258 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002259 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002260 if (error)
2261 {
2262 clear_tv(rettv);
2263 clear_tv(&var2);
2264 return FAIL;
2265 }
2266#ifdef FEAT_FLOAT
2267 if (rettv->v_type == VAR_FLOAT)
2268 f2 = n2;
2269#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002272
2273#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002274 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002275 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2276 {
2277 if (op == '+')
2278 f1 = f1 + f2;
2279 else
2280 f1 = f1 - f2;
2281 rettv->v_type = VAR_FLOAT;
2282 rettv->vval.v_float = f1;
2283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002285#endif
2286 {
2287 if (op == '+')
2288 n1 = n1 + n2;
2289 else
2290 n1 = n1 - n2;
2291 rettv->v_type = VAR_NUMBER;
2292 rettv->vval.v_number = n1;
2293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297 }
2298 return OK;
2299}
2300
2301/*
2302 * Handle fifth level expression:
2303 * * number multiplication
2304 * / number division
2305 * % number modulo
2306 *
2307 * "arg" must point to the first non-white of the expression.
2308 * "arg" is advanced to the next non-white after the recognized expression.
2309 *
2310 * Return OK or FAIL.
2311 */
2312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002313eval6(
2314 char_u **arg,
2315 typval_T *rettv,
2316 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002317 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
Bram Moolenaar33570922005-01-25 22:26:29 +00002319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002321 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002322#ifdef FEAT_FLOAT
2323 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002324 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002325#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002326 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328 /*
2329 * Get the first variable.
2330 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002331 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 return FAIL;
2333
2334 /*
2335 * Repeat computing, until no '*', '/' or '%' is following.
2336 */
2337 for (;;)
2338 {
2339 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002340 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 break;
2342
2343 if (evaluate)
2344 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002345#ifdef FEAT_FLOAT
2346 if (rettv->v_type == VAR_FLOAT)
2347 {
2348 f1 = rettv->vval.v_float;
2349 use_float = TRUE;
2350 n1 = 0;
2351 }
2352 else
2353#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002354 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356 if (error)
2357 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359 else
2360 n1 = 0;
2361
2362 /*
2363 * Get the second variable.
2364 */
2365 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002366 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 return FAIL;
2368
2369 if (evaluate)
2370 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002371#ifdef FEAT_FLOAT
2372 if (var2.v_type == VAR_FLOAT)
2373 {
2374 if (!use_float)
2375 {
2376 f1 = n1;
2377 use_float = TRUE;
2378 }
2379 f2 = var2.vval.v_float;
2380 n2 = 0;
2381 }
2382 else
2383#endif
2384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002385 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002386 clear_tv(&var2);
2387 if (error)
2388 return FAIL;
2389#ifdef FEAT_FLOAT
2390 if (use_float)
2391 f2 = n2;
2392#endif
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395 /*
2396 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002397 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002399#ifdef FEAT_FLOAT
2400 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002402 if (op == '*')
2403 f1 = f1 * f2;
2404 else if (op == '/')
2405 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002406# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002407 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002408 if (f2 == 0.0)
2409 {
2410 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002411 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002412 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002413 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002414 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002415 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002416 }
2417 else
2418 f1 = f1 / f2;
2419# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002420 // We rely on the floating point library to handle divide
2421 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002422 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002423# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002426 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002427 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002428 return FAIL;
2429 }
2430 rettv->v_type = VAR_FLOAT;
2431 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002436 if (op == '*')
2437 n1 = n1 * n2;
2438 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002439 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002441 n1 = num_modulus(n1, n2);
2442
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002443 rettv->v_type = VAR_NUMBER;
2444 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
2447 }
2448
2449 return OK;
2450}
2451
2452/*
2453 * Handle sixth level expression:
2454 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002455 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002456 * "string" string constant
2457 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 * &option-name option value
2459 * @r register contents
2460 * identifier variable value
2461 * function() function call
2462 * $VAR environment variable
2463 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002464 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002465 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002466 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 *
2468 * Also handle:
2469 * ! in front logical NOT
2470 * - in front unary minus
2471 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002472 * trailing [] subscript in String or List
2473 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002474 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 *
2476 * "arg" must point to the first non-white of the expression.
2477 * "arg" is advanced to the next non-white after the recognized expression.
2478 *
2479 * Return OK or FAIL.
2480 */
2481 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002482eval7(
2483 char_u **arg,
2484 typval_T *rettv,
2485 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002486 int want_string UNUSED) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002488 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 int len;
2490 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 char_u *start_leader, *end_leader;
2492 int ret = OK;
2493 char_u *alias;
2494
2495 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002497 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002499 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
2501 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002502 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 */
2504 start_leader = *arg;
2505 while (**arg == '!' || **arg == '-' || **arg == '+')
2506 *arg = skipwhite(*arg + 1);
2507 end_leader = *arg;
2508
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002509 if (**arg == '.' && (!isdigit(*(*arg + 1))
2510#ifdef FEAT_FLOAT
2511 || current_sctx.sc_version < 2
2512#endif
2513 ))
2514 {
2515 semsg(_(e_invexpr2), *arg);
2516 ++*arg;
2517 return FAIL;
2518 }
2519
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 switch (**arg)
2521 {
2522 /*
2523 * Number constant.
2524 */
2525 case '0':
2526 case '1':
2527 case '2':
2528 case '3':
2529 case '4':
2530 case '5':
2531 case '6':
2532 case '7':
2533 case '8':
2534 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002535 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002536 {
2537#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002538 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002539 int get_float = FALSE;
2540
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002541 // We accept a float when the format matches
2542 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2543 // strict to avoid backwards compatibility problems.
2544 // With script version 2 and later the leading digit can be
2545 // omitted.
2546 // Don't look for a float after the "." operator, so that
2547 // ":let vers = 1.2.3" doesn't fail.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002548 if (**arg == '.')
2549 p = *arg;
2550 else
2551 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002552 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002554 get_float = TRUE;
2555 p = skipdigits(p + 2);
2556 if (*p == 'e' || *p == 'E')
2557 {
2558 ++p;
2559 if (*p == '-' || *p == '+')
2560 ++p;
2561 if (!vim_isdigit(*p))
2562 get_float = FALSE;
2563 else
2564 p = skipdigits(p + 1);
2565 }
2566 if (ASCII_ISALPHA(*p) || *p == '.')
2567 get_float = FALSE;
2568 }
2569 if (get_float)
2570 {
2571 float_T f;
2572
2573 *arg += string2float(*arg, &f);
2574 if (evaluate)
2575 {
2576 rettv->v_type = VAR_FLOAT;
2577 rettv->vval.v_float = f;
2578 }
2579 }
2580 else
2581#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002582 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002583 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002584 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01002585 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002586
2587 // Blob constant: 0z0123456789abcdef
2588 if (evaluate)
2589 blob = blob_alloc();
2590 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2591 {
2592 if (!vim_isxdigit(bp[1]))
2593 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002594 if (blob != NULL)
2595 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002596 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002597 ga_clear(&blob->bv_ga);
2598 VIM_CLEAR(blob);
2599 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002600 ret = FAIL;
2601 break;
2602 }
2603 if (blob != NULL)
2604 ga_append(&blob->bv_ga,
2605 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01002606 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2607 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002608 }
2609 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002610 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002611 *arg = bp;
2612 }
2613 else
2614 {
2615 // decimal, hex or octal number
Bram Moolenaar60a8de22019-09-15 14:33:22 +02002616 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
2617 ? STR2NR_NO_OCT + STR2NR_QUOTE
2618 : STR2NR_ALL, &n, NULL, 0, TRUE);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002619 if (len == 0)
2620 {
2621 semsg(_(e_invexpr2), *arg);
2622 ret = FAIL;
2623 break;
2624 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002625 *arg += len;
2626 if (evaluate)
2627 {
2628 rettv->v_type = VAR_NUMBER;
2629 rettv->vval.v_number = n;
2630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
2632 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635 /*
2636 * String constant: "string".
2637 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002638 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 break;
2640
2641 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002644 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002645 break;
2646
2647 /*
2648 * List: [expr, expr]
2649 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 break;
2652
2653 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002654 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002655 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002656 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002657 {
2658 ++*arg;
2659 ret = dict_get_tv(arg, rettv, evaluate, TRUE);
2660 }
2661 else
2662 ret = NOTDONE;
2663 break;
2664
2665 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002666 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002667 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002669 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2670 if (ret == NOTDONE)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002671 ret = dict_get_tv(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 break;
2673
2674 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002675 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002677 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 break;
2679
2680 /*
2681 * Environment variable: $VAR.
2682 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002683 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 break;
2685
2686 /*
2687 * Register contents: @r.
2688 */
2689 case '@': ++*arg;
2690 if (evaluate)
2691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002692 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002693 rettv->vval.v_string = get_reg_contents(**arg,
2694 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
2696 if (**arg != NUL)
2697 ++*arg;
2698 break;
2699
2700 /*
2701 * nested expression: (expression).
2702 */
2703 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002704 ret = eval1(arg, rettv, evaluate); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 if (**arg == ')')
2706 ++*arg;
2707 else if (ret == OK)
2708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002709 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002710 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 ret = FAIL;
2712 }
2713 break;
2714
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 break;
2717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718
2719 if (ret == NOTDONE)
2720 {
2721 /*
2722 * Must be a variable or function name.
2723 * Can also be a curly-braces kind of name: {expr}.
2724 */
2725 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002726 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (alias != NULL)
2728 s = alias;
2729
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002730 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 ret = FAIL;
2732 else
2733 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002734 if (**arg == '(') // recursive!
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002735 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002737 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002738 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002739 {
2740 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002741 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002742 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002744 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 *arg = skipwhite(*arg);
2748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002749 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2750 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002751 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002752 ret = handle_subscript(arg, rettv, evaluate, TRUE,
2753 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
2755 /*
2756 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2757 */
2758 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002759 ret = eval7_leader(rettv, start_leader, &end_leader);
2760 return ret;
2761}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002762
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002763/*
2764 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2765 * Adjusts "end_leaderp" until it is at "start_leader".
2766 */
2767 static int
2768eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2769{
2770 char_u *end_leader = *end_leaderp;
2771 int ret = OK;
2772 int error = FALSE;
2773 varnumber_T val = 0;
2774#ifdef FEAT_FLOAT
2775 float_T f = 0.0;
2776
2777 if (rettv->v_type == VAR_FLOAT)
2778 f = rettv->vval.v_float;
2779 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002780#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002781 val = tv_get_number_chk(rettv, &error);
2782 if (error)
2783 {
2784 clear_tv(rettv);
2785 ret = FAIL;
2786 }
2787 else
2788 {
2789 while (end_leader > start_leader)
2790 {
2791 --end_leader;
2792 if (*end_leader == '!')
2793 {
2794#ifdef FEAT_FLOAT
2795 if (rettv->v_type == VAR_FLOAT)
2796 f = !f;
2797 else
2798#endif
2799 val = !val;
2800 }
2801 else if (*end_leader == '-')
2802 {
2803#ifdef FEAT_FLOAT
2804 if (rettv->v_type == VAR_FLOAT)
2805 f = -f;
2806 else
2807#endif
2808 val = -val;
2809 }
2810 }
2811#ifdef FEAT_FLOAT
2812 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002814 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002815 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002817 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002818#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002819 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002820 clear_tv(rettv);
2821 rettv->v_type = VAR_NUMBER;
2822 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002825 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 return ret;
2827}
2828
2829/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002830 * Call the function referred to in "rettv".
2831 */
2832 static int
2833call_func_rettv(
2834 char_u **arg,
2835 typval_T *rettv,
2836 int evaluate,
2837 dict_T *selfdict,
2838 typval_T *basetv)
2839{
2840 partial_T *pt = NULL;
2841 funcexe_T funcexe;
2842 typval_T functv;
2843 char_u *s;
2844 int ret;
2845
2846 // need to copy the funcref so that we can clear rettv
2847 if (evaluate)
2848 {
2849 functv = *rettv;
2850 rettv->v_type = VAR_UNKNOWN;
2851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002852 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002853 if (functv.v_type == VAR_PARTIAL)
2854 {
2855 pt = functv.vval.v_partial;
2856 s = partial_name(pt);
2857 }
2858 else
2859 s = functv.vval.v_string;
2860 }
2861 else
2862 s = (char_u *)"";
2863
2864 vim_memset(&funcexe, 0, sizeof(funcexe));
2865 funcexe.firstline = curwin->w_cursor.lnum;
2866 funcexe.lastline = curwin->w_cursor.lnum;
2867 funcexe.evaluate = evaluate;
2868 funcexe.partial = pt;
2869 funcexe.selfdict = selfdict;
2870 funcexe.basetv = basetv;
2871 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2872
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002873 // Clear the funcref afterwards, so that deleting it while
2874 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002875 if (evaluate)
2876 clear_tv(&functv);
2877
2878 return ret;
2879}
2880
2881/*
2882 * Evaluate "->method()".
2883 * "*arg" points to the '-'.
2884 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2885 */
2886 static int
2887eval_lambda(
2888 char_u **arg,
2889 typval_T *rettv,
2890 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002891 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002892{
2893 typval_T base = *rettv;
2894 int ret;
2895
2896 // Skip over the ->.
2897 *arg += 2;
2898 rettv->v_type = VAR_UNKNOWN;
2899
2900 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002901 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002902 return FAIL;
2903 else if (**arg != '(')
2904 {
2905 if (verbose)
2906 {
2907 if (*skipwhite(*arg) == '(')
2908 semsg(_(e_nowhitespace));
2909 else
2910 semsg(_(e_missingparen), "lambda");
2911 }
2912 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002913 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002914 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002915 else
2916 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2917
2918 // Clear the funcref afterwards, so that deleting it while
2919 // evaluating the arguments is possible (see test55).
2920 if (evaluate)
2921 clear_tv(&base);
2922
2923 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002924}
2925
2926/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002927 * Evaluate "->method()".
2928 * "*arg" points to the '-'.
2929 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2930 */
2931 static int
2932eval_method(
2933 char_u **arg,
2934 typval_T *rettv,
2935 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002936 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002937{
2938 char_u *name;
2939 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002940 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002941 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002942 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002943
2944 // Skip over the ->.
2945 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002946 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002947
Bram Moolenaarac92e252019-08-03 21:58:38 +02002948 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002949 len = get_name_len(arg, &alias, evaluate, TRUE);
2950 if (alias != NULL)
2951 name = alias;
2952
2953 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002954 {
2955 if (verbose)
2956 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002957 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002958 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002959 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002960 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002961 if (**arg != '(')
2962 {
2963 if (verbose)
2964 semsg(_(e_missingparen), name);
2965 ret = FAIL;
2966 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002967 else if (VIM_ISWHITE((*arg)[-1]))
2968 {
2969 if (verbose)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002970 semsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002971 ret = FAIL;
2972 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002973 else
2974 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002975 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002976
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002977 // Clear the funcref afterwards, so that deleting it while
2978 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002979 if (evaluate)
2980 clear_tv(&base);
2981
Bram Moolenaarac92e252019-08-03 21:58:38 +02002982 return ret;
2983}
2984
2985/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002986 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2987 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002988 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2989 */
2990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002991eval_index(
2992 char_u **arg,
2993 typval_T *rettv,
2994 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002995 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002996{
2997 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002998 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002999 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003000 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003001 long len = -1;
3002 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003003 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003004 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003005
Bram Moolenaara03f2332016-02-06 18:09:59 +01003006 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003007 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003008 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003009 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003010 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003011 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003012 return FAIL;
3013 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003014#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003015 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003016 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003017 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003018#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01003019 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003020 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003021 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003022 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003023 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003024 return FAIL;
3025 case VAR_UNKNOWN:
3026 if (evaluate)
3027 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003028 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003029
3030 case VAR_STRING:
3031 case VAR_NUMBER:
3032 case VAR_LIST:
3033 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003034 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003035 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003036 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003037
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003038 init_tv(&var1);
3039 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003040 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003041 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003042 /*
3043 * dict.name
3044 */
3045 key = *arg + 1;
3046 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3047 ;
3048 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003049 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003050 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003051 }
3052 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003053 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003054 /*
3055 * something[idx]
3056 *
3057 * Get the (first) variable from inside the [].
3058 */
3059 *arg = skipwhite(*arg + 1);
3060 if (**arg == ':')
3061 empty1 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003062 else if (eval1(arg, &var1, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003063 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003064 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003065 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003066 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003067 clear_tv(&var1);
3068 return FAIL;
3069 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003070
3071 /*
3072 * Get the second variable from inside the [:].
3073 */
3074 if (**arg == ':')
3075 {
3076 range = TRUE;
3077 *arg = skipwhite(*arg + 1);
3078 if (**arg == ']')
3079 empty2 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003080 else if (eval1(arg, &var2, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003081 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003082 if (!empty1)
3083 clear_tv(&var1);
3084 return FAIL;
3085 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003086 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003087 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003088 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003089 if (!empty1)
3090 clear_tv(&var1);
3091 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003092 return FAIL;
3093 }
3094 }
3095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003096 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003097 if (**arg != ']')
3098 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003099 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003100 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003101 clear_tv(&var1);
3102 if (range)
3103 clear_tv(&var2);
3104 return FAIL;
3105 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003106 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003107 }
3108
3109 if (evaluate)
3110 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003111 n1 = 0;
3112 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003113 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003114 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003115 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003116 }
3117 if (range)
3118 {
3119 if (empty2)
3120 n2 = -1;
3121 else
3122 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003123 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003124 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003125 }
3126 }
3127
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003128 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003129 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003130 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003131 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003132 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003133 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003134 case VAR_SPECIAL:
3135 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003136 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003137 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003138
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003139 case VAR_NUMBER:
3140 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003141 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003142 len = (long)STRLEN(s);
3143 if (range)
3144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003145 // The resulting variable is a substring. If the indexes
3146 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003147 if (n1 < 0)
3148 {
3149 n1 = len + n1;
3150 if (n1 < 0)
3151 n1 = 0;
3152 }
3153 if (n2 < 0)
3154 n2 = len + n2;
3155 else if (n2 >= len)
3156 n2 = len;
3157 if (n1 >= len || n2 < 0 || n1 > n2)
3158 s = NULL;
3159 else
3160 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3161 }
3162 else
3163 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003164 // The resulting variable is a string of a single
3165 // character. If the index is too big or negative the
3166 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003167 if (n1 >= len || n1 < 0)
3168 s = NULL;
3169 else
3170 s = vim_strnsave(s + n1, 1);
3171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003172 clear_tv(rettv);
3173 rettv->v_type = VAR_STRING;
3174 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003175 break;
3176
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003177 case VAR_BLOB:
3178 len = blob_len(rettv->vval.v_blob);
3179 if (range)
3180 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003181 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003182 // are out of range the result is empty.
3183 if (n1 < 0)
3184 {
3185 n1 = len + n1;
3186 if (n1 < 0)
3187 n1 = 0;
3188 }
3189 if (n2 < 0)
3190 n2 = len + n2;
3191 else if (n2 >= len)
3192 n2 = len - 1;
3193 if (n1 >= len || n2 < 0 || n1 > n2)
3194 {
3195 clear_tv(rettv);
3196 rettv->v_type = VAR_BLOB;
3197 rettv->vval.v_blob = NULL;
3198 }
3199 else
3200 {
3201 blob_T *blob = blob_alloc();
3202
3203 if (blob != NULL)
3204 {
3205 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3206 {
3207 blob_free(blob);
3208 return FAIL;
3209 }
3210 blob->bv_ga.ga_len = n2 - n1 + 1;
3211 for (i = n1; i <= n2; i++)
3212 blob_set(blob, i - n1,
3213 blob_get(rettv->vval.v_blob, i));
3214
3215 clear_tv(rettv);
3216 rettv_blob_set(rettv, blob);
3217 }
3218 }
3219 }
3220 else
3221 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003222 // The resulting variable is a byte value.
3223 // If the index is too big or negative that is an error.
3224 if (n1 < 0)
3225 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003226 if (n1 < len && n1 >= 0)
3227 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003228 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003229
3230 clear_tv(rettv);
3231 rettv->v_type = VAR_NUMBER;
3232 rettv->vval.v_number = v;
3233 }
3234 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003235 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003236 }
3237 break;
3238
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003239 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003240 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003241 if (n1 < 0)
3242 n1 = len + n1;
3243 if (!empty1 && (n1 < 0 || n1 >= len))
3244 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003245 // For a range we allow invalid values and return an empty
3246 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003247 if (!range)
3248 {
3249 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003250 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003251 return FAIL;
3252 }
3253 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003254 }
3255 if (range)
3256 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003257 list_T *l;
3258 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003259
3260 if (n2 < 0)
3261 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003262 else if (n2 >= len)
3263 n2 = len - 1;
3264 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003265 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003266 l = list_alloc();
3267 if (l == NULL)
3268 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003269 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003270 n1 <= n2; ++n1)
3271 {
3272 if (list_append_tv(l, &item->li_tv) == FAIL)
3273 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003274 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003275 return FAIL;
3276 }
3277 item = item->li_next;
3278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003279 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003280 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003281 }
3282 else
3283 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003284 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003285 clear_tv(rettv);
3286 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003287 }
3288 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003289
3290 case VAR_DICT:
3291 if (range)
3292 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003293 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003294 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003295 if (len == -1)
3296 clear_tv(&var1);
3297 return FAIL;
3298 }
3299 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003300 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003301
3302 if (len == -1)
3303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003304 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003305 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003306 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003307 clear_tv(&var1);
3308 return FAIL;
3309 }
3310 }
3311
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003312 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003313
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003314 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003315 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003316 if (len == -1)
3317 clear_tv(&var1);
3318 if (item == NULL)
3319 return FAIL;
3320
3321 copy_tv(&item->di_tv, &var1);
3322 clear_tv(rettv);
3323 *rettv = var1;
3324 }
3325 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003326 }
3327 }
3328
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003329 return OK;
3330}
3331
3332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 * Get an option value.
3334 * "arg" points to the '&' or '+' before the option name.
3335 * "arg" is advanced to character after the option name.
3336 * Return OK or FAIL.
3337 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003338 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003339get_option_tv(
3340 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003341 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003342 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 char_u *option_end;
3345 long numval;
3346 char_u *stringval;
3347 int opt_type;
3348 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003349 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 int ret = OK;
3351 int opt_flags;
3352
3353 /*
3354 * Isolate the option name and find its value.
3355 */
3356 option_end = find_option_end(arg, &opt_flags);
3357 if (option_end == NULL)
3358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003359 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003360 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 return FAIL;
3362 }
3363
3364 if (!evaluate)
3365 {
3366 *arg = option_end;
3367 return OK;
3368 }
3369
3370 c = *option_end;
3371 *option_end = NUL;
3372 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003373 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003375 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003377 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003378 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 ret = FAIL;
3380 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003381 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003383 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003385 rettv->v_type = VAR_STRING;
3386 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003388 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003390 rettv->v_type = VAR_NUMBER;
3391 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003393 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003395 rettv->v_type = VAR_NUMBER;
3396 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003398 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003400 rettv->v_type = VAR_STRING;
3401 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
3403 }
3404 else if (working && (opt_type == -2 || opt_type == -1))
3405 ret = FAIL;
3406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003407 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 *arg = option_end;
3409
3410 return ret;
3411}
3412
3413/*
3414 * Allocate a variable for a string constant.
3415 * Return OK or FAIL.
3416 */
3417 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003418get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 char_u *p;
3421 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 int extra = 0;
3423
3424 /*
3425 * Find the end of the string, skipping backslashed characters.
3426 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003427 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 {
3429 if (*p == '\\' && p[1] != NUL)
3430 {
3431 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003432 // A "\<x>" form occupies at least 4 characters, and produces up
3433 // to 6 characters: reserve space for 2 extra
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 if (*p == '<')
3435 extra += 2;
3436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438
3439 if (*p != '"')
3440 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003441 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 return FAIL;
3443 }
3444
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003445 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (!evaluate)
3447 {
3448 *arg = p + 1;
3449 return OK;
3450 }
3451
3452 /*
3453 * Copy the string into allocated memory, handling backslashed
3454 * characters.
3455 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003456 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (name == NULL)
3458 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003459 rettv->v_type = VAR_STRING;
3460 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaar8c711452005-01-14 21:53:12 +00003462 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
3464 if (*p == '\\')
3465 {
3466 switch (*++p)
3467 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003468 case 'b': *name++ = BS; ++p; break;
3469 case 'e': *name++ = ESC; ++p; break;
3470 case 'f': *name++ = FF; ++p; break;
3471 case 'n': *name++ = NL; ++p; break;
3472 case 'r': *name++ = CAR; ++p; break;
3473 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003475 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003477 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 case 'U':
3479 if (vim_isxdigit(p[1]))
3480 {
3481 int n, nr;
3482 int c = toupper(*p);
3483
3484 if (c == 'X')
3485 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003486 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003488 else
3489 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 nr = 0;
3491 while (--n >= 0 && vim_isxdigit(p[1]))
3492 {
3493 ++p;
3494 nr = (nr << 4) + hex2nr(*p);
3495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003496 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003497 // For "\u" store the number according to
3498 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003500 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003502 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 break;
3505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003506 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 case '0':
3508 case '1':
3509 case '2':
3510 case '3':
3511 case '4':
3512 case '5':
3513 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003514 case '7': *name = *p++ - '0';
3515 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003517 *name = (*name << 3) + *p++ - '0';
3518 if (*p >= '0' && *p <= '7')
3519 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003521 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 break;
3523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003524 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003525 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3526 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 if (extra != 0)
3528 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003529 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 break;
3531 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003532 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
Bram Moolenaar8c711452005-01-14 21:53:12 +00003534 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 break;
3536 }
3537 }
3538 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003539 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003542 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003543 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003544 ++p;
3545 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 return OK;
3548}
3549
3550/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003551 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 * Return OK or FAIL.
3553 */
3554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003555get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556{
3557 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003558 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003559 int reduce = 0;
3560
3561 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003562 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003563 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003564 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003565 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003566 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003567 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003568 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003569 break;
3570 ++reduce;
3571 ++p;
3572 }
3573 }
3574
Bram Moolenaar8c711452005-01-14 21:53:12 +00003575 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003577 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003578 return FAIL;
3579 }
3580
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003581 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003582 if (!evaluate)
3583 {
3584 *arg = p + 1;
3585 return OK;
3586 }
3587
3588 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003590 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003591 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003592 if (str == NULL)
3593 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003594 rettv->v_type = VAR_STRING;
3595 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003596
Bram Moolenaar8c711452005-01-14 21:53:12 +00003597 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003598 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003600 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003602 break;
3603 ++p;
3604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003605 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003607 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003608 *arg = p + 1;
3609
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003610 return OK;
3611}
3612
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003613/*
3614 * Return the function name of the partial.
3615 */
3616 char_u *
3617partial_name(partial_T *pt)
3618{
3619 if (pt->pt_name != NULL)
3620 return pt->pt_name;
3621 return pt->pt_func->uf_name;
3622}
3623
Bram Moolenaarddecc252016-04-06 22:59:37 +02003624 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003625partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003626{
3627 int i;
3628
3629 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003630 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003631 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003632 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003633 if (pt->pt_name != NULL)
3634 {
3635 func_unref(pt->pt_name);
3636 vim_free(pt->pt_name);
3637 }
3638 else
3639 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003640 vim_free(pt);
3641}
3642
3643/*
3644 * Unreference a closure: decrement the reference count and free it when it
3645 * becomes zero.
3646 */
3647 void
3648partial_unref(partial_T *pt)
3649{
3650 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003651 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003652}
3653
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003654static int tv_equal_recurse_limit;
3655
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003656 static int
3657func_equal(
3658 typval_T *tv1,
3659 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003660 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003661{
3662 char_u *s1, *s2;
3663 dict_T *d1, *d2;
3664 int a1, a2;
3665 int i;
3666
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003667 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003668 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003669 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003670 if (s1 != NULL && *s1 == NUL)
3671 s1 = NULL;
3672 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003673 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003674 if (s2 != NULL && *s2 == NUL)
3675 s2 = NULL;
3676 if (s1 == NULL || s2 == NULL)
3677 {
3678 if (s1 != s2)
3679 return FALSE;
3680 }
3681 else if (STRCMP(s1, s2) != 0)
3682 return FALSE;
3683
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003684 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003685 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3686 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3687 if (d1 == NULL || d2 == NULL)
3688 {
3689 if (d1 != d2)
3690 return FALSE;
3691 }
3692 else if (!dict_equal(d1, d2, ic, TRUE))
3693 return FALSE;
3694
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003695 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003696 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3697 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3698 if (a1 != a2)
3699 return FALSE;
3700 for (i = 0; i < a1; ++i)
3701 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3702 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3703 return FALSE;
3704
3705 return TRUE;
3706}
3707
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003708/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003709 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003710 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003711 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003712 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003714tv_equal(
3715 typval_T *tv1,
3716 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003717 int ic, // ignore case
3718 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003719{
3720 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003721 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003722 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003723 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003725 // Catch lists and dicts that have an endless loop by limiting
3726 // recursiveness to a limit. We guess they are equal then.
3727 // A fixed limit has the problem of still taking an awful long time.
3728 // Reduce the limit every time running into it. That should work fine for
3729 // deeply linked structures that are not recursively linked and catch
3730 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003731 if (!recursive)
3732 tv_equal_recurse_limit = 1000;
3733 if (recursive_cnt >= tv_equal_recurse_limit)
3734 {
3735 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003736 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003737 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003738
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003739 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3740 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003741 if ((tv1->v_type == VAR_FUNC
3742 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3743 && (tv2->v_type == VAR_FUNC
3744 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3745 {
3746 ++recursive_cnt;
3747 r = func_equal(tv1, tv2, ic);
3748 --recursive_cnt;
3749 return r;
3750 }
3751
3752 if (tv1->v_type != tv2->v_type)
3753 return FALSE;
3754
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003755 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003756 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003757 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003758 ++recursive_cnt;
3759 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3760 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003761 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003762
3763 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003764 ++recursive_cnt;
3765 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3766 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003767 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003768
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003769 case VAR_BLOB:
3770 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3771
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003772 case VAR_NUMBER:
3773 return tv1->vval.v_number == tv2->vval.v_number;
3774
3775 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003776 s1 = tv_get_string_buf(tv1, buf1);
3777 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003778 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003779
3780 case VAR_SPECIAL:
3781 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01003782
3783 case VAR_FLOAT:
3784#ifdef FEAT_FLOAT
3785 return tv1->vval.v_float == tv2->vval.v_float;
3786#endif
3787 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003788#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003789 return tv1->vval.v_job == tv2->vval.v_job;
3790#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003791 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003792#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003793 return tv1->vval.v_channel == tv2->vval.v_channel;
3794#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003795 case VAR_FUNC:
3796 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003797 case VAR_UNKNOWN:
3798 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003799 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003800
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003801 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3802 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003803 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003804}
3805
3806/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003807 * Return the next (unique) copy ID.
3808 * Used for serializing nested structures.
3809 */
3810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003811get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003812{
3813 current_copyID += COPYID_INC;
3814 return current_copyID;
3815}
3816
3817/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003818 * Garbage collection for lists and dictionaries.
3819 *
3820 * We use reference counts to be able to free most items right away when they
3821 * are no longer used. But for composite items it's possible that it becomes
3822 * unused while the reference count is > 0: When there is a recursive
3823 * reference. Example:
3824 * :let l = [1, 2, 3]
3825 * :let d = {9: l}
3826 * :let l[1] = d
3827 *
3828 * Since this is quite unusual we handle this with garbage collection: every
3829 * once in a while find out which lists and dicts are not referenced from any
3830 * variable.
3831 *
3832 * Here is a good reference text about garbage collection (refers to Python
3833 * but it applies to all reference-counting mechanisms):
3834 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003835 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003836
3837/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003838 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003839 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003840 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003841 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003842 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003843garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003844{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003845 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003846 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003847 buf_T *buf;
3848 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003849 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003850 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003851
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003852 if (!testing)
3853 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003854 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003855 want_garbage_collect = FALSE;
3856 may_garbage_collect = FALSE;
3857 garbage_collect_at_exit = FALSE;
3858 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003859
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003860 // We advance by two because we add one for items referenced through
3861 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003862 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003863
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003864 /*
3865 * 1. Go through all accessible variables and mark all lists and dicts
3866 * with copyID.
3867 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003868
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003869 // Don't free variables in the previous_funccal list unless they are only
3870 // referenced through previous_funccal. This must be first, because if
3871 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003872 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003874 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003875 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003876
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003877 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003878 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003879 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3880 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003881
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003882 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003883 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003884 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3885 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003886 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003887 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3888 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003889#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003890 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3891 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3892 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003893 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02003894 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003895 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3896 NULL, NULL);
3897#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003898
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003899 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003900 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003901 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3902 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003903 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003904 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003906 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003907 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003909 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003910 abort = abort || set_ref_in_functions(copyID);
3911
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003912 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003913 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003915 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003916 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003917
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003918 // callbacks in buffers
3919 abort = abort || set_ref_in_buffers(copyID);
3920
Bram Moolenaar1dced572012-04-05 16:54:08 +02003921#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003922 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003923#endif
3924
Bram Moolenaardb913952012-06-29 12:54:53 +02003925#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003926 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003927#endif
3928
3929#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003930 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003931#endif
3932
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003933#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003934 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003935 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003936#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003937#ifdef FEAT_NETBEANS_INTG
3938 abort = abort || set_ref_in_nb_channel(copyID);
3939#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003940
Bram Moolenaare3188e22016-05-31 21:13:04 +02003941#ifdef FEAT_TIMERS
3942 abort = abort || set_ref_in_timer(copyID);
3943#endif
3944
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003945#ifdef FEAT_QUICKFIX
3946 abort = abort || set_ref_in_quickfix(copyID);
3947#endif
3948
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003949#ifdef FEAT_TERMINAL
3950 abort = abort || set_ref_in_term(copyID);
3951#endif
3952
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003953#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003954 abort = abort || set_ref_in_popups(copyID);
3955#endif
3956
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003957 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003958 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003959 /*
3960 * 2. Free lists and dictionaries that are not referenced.
3961 */
3962 did_free = free_unref_items(copyID);
3963
3964 /*
3965 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003966 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003967 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003968 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003969 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003970 else if (p_verbose > 0)
3971 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003972 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003973 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003974
3975 return did_free;
3976}
3977
3978/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003979 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003980 */
3981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003982free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003983{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003984 int did_free = FALSE;
3985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003986 // Let all "free" functions know that we are here. This means no
3987 // dictionaries, lists, channels or jobs are to be freed, because we will
3988 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003989 in_free_unref_items = TRUE;
3990
3991 /*
3992 * PASS 1: free the contents of the items. We don't free the items
3993 * themselves yet, so that it is possible to decrement refcount counters
3994 */
3995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003996 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003997 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003999 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004000 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004001
4002#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004003 // Go through the list of jobs and free items without the copyID. This
4004 // must happen before doing channels, because jobs refer to channels, but
4005 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004006 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004008 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004009 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4010#endif
4011
4012 /*
4013 * PASS 2: free the items themselves.
4014 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004015 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004016 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004017
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004018#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004019 // Go through the list of jobs and free items without the copyID. This
4020 // must happen before doing channels, because jobs refer to channels, but
4021 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004022 free_unused_jobs(copyID, COPYID_MASK);
4023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004024 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004025 free_unused_channels(copyID, COPYID_MASK);
4026#endif
4027
4028 in_free_unref_items = FALSE;
4029
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004030 return did_free;
4031}
4032
4033/*
4034 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004035 * "list_stack" is used to add lists to be marked. Can be NULL.
4036 *
4037 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004038 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004039 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004040set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004041{
4042 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004043 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004044 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004045 hashtab_T *cur_ht;
4046 ht_stack_T *ht_stack = NULL;
4047 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004048
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004049 cur_ht = ht;
4050 for (;;)
4051 {
4052 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004053 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004054 // Mark each item in the hashtab. If the item contains a hashtab
4055 // it is added to ht_stack, if it contains a list it is added to
4056 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004057 todo = (int)cur_ht->ht_used;
4058 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4059 if (!HASHITEM_EMPTY(hi))
4060 {
4061 --todo;
4062 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4063 &ht_stack, list_stack);
4064 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004065 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004066
4067 if (ht_stack == NULL)
4068 break;
4069
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004070 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004071 cur_ht = ht_stack->ht;
4072 tempitem = ht_stack;
4073 ht_stack = ht_stack->prev;
4074 free(tempitem);
4075 }
4076
4077 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004078}
4079
4080/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004081 * Mark a dict and its items with "copyID".
4082 * Returns TRUE if setting references failed somehow.
4083 */
4084 int
4085set_ref_in_dict(dict_T *d, int copyID)
4086{
4087 if (d != NULL && d->dv_copyID != copyID)
4088 {
4089 d->dv_copyID = copyID;
4090 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4091 }
4092 return FALSE;
4093}
4094
4095/*
4096 * Mark a list and its items with "copyID".
4097 * Returns TRUE if setting references failed somehow.
4098 */
4099 int
4100set_ref_in_list(list_T *ll, int copyID)
4101{
4102 if (ll != NULL && ll->lv_copyID != copyID)
4103 {
4104 ll->lv_copyID = copyID;
4105 return set_ref_in_list_items(ll, copyID, NULL);
4106 }
4107 return FALSE;
4108}
4109
4110/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004111 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004112 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4113 *
4114 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004115 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004116 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004117set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004118{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004119 listitem_T *li;
4120 int abort = FALSE;
4121 list_T *cur_l;
4122 list_stack_T *list_stack = NULL;
4123 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004124
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004125 cur_l = l;
4126 for (;;)
4127 {
4128 if (!abort)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004129 // Mark each item in the list. If the item contains a hashtab
4130 // it is added to ht_stack, if it contains a list it is added to
4131 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004132 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4133 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4134 ht_stack, &list_stack);
4135 if (list_stack == NULL)
4136 break;
4137
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004138 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004139 cur_l = list_stack->list;
4140 tempitem = list_stack;
4141 list_stack = list_stack->prev;
4142 free(tempitem);
4143 }
4144
4145 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004146}
4147
4148/*
4149 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004150 * "list_stack" is used to add lists to be marked. Can be NULL.
4151 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4152 *
4153 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004154 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004155 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004156set_ref_in_item(
4157 typval_T *tv,
4158 int copyID,
4159 ht_stack_T **ht_stack,
4160 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004161{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004162 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004163
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004164 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004165 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004166 dict_T *dd = tv->vval.v_dict;
4167
Bram Moolenaara03f2332016-02-06 18:09:59 +01004168 if (dd != NULL && dd->dv_copyID != copyID)
4169 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004170 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004171 dd->dv_copyID = copyID;
4172 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004173 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004174 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4175 }
4176 else
4177 {
4178 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4179 if (newitem == NULL)
4180 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004181 else
4182 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004183 newitem->ht = &dd->dv_hashtab;
4184 newitem->prev = *ht_stack;
4185 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004186 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004187 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004188 }
4189 }
4190 else if (tv->v_type == VAR_LIST)
4191 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004192 list_T *ll = tv->vval.v_list;
4193
Bram Moolenaara03f2332016-02-06 18:09:59 +01004194 if (ll != NULL && ll->lv_copyID != copyID)
4195 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004196 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004197 ll->lv_copyID = copyID;
4198 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004199 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004200 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004201 }
4202 else
4203 {
4204 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004205 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004206 if (newitem == NULL)
4207 abort = TRUE;
4208 else
4209 {
4210 newitem->list = ll;
4211 newitem->prev = *list_stack;
4212 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004213 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004214 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004215 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004216 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004217 else if (tv->v_type == VAR_FUNC)
4218 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004219 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004220 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004221 else if (tv->v_type == VAR_PARTIAL)
4222 {
4223 partial_T *pt = tv->vval.v_partial;
4224 int i;
4225
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004226 // A partial does not have a copyID, because it cannot contain itself.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004227 if (pt != NULL)
4228 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004229 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004230
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004231 if (pt->pt_dict != NULL)
4232 {
4233 typval_T dtv;
4234
4235 dtv.v_type = VAR_DICT;
4236 dtv.vval.v_dict = pt->pt_dict;
4237 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4238 }
4239
4240 for (i = 0; i < pt->pt_argc; ++i)
4241 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4242 ht_stack, list_stack);
4243 }
4244 }
4245#ifdef FEAT_JOB_CHANNEL
4246 else if (tv->v_type == VAR_JOB)
4247 {
4248 job_T *job = tv->vval.v_job;
4249 typval_T dtv;
4250
4251 if (job != NULL && job->jv_copyID != copyID)
4252 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004253 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004254 if (job->jv_channel != NULL)
4255 {
4256 dtv.v_type = VAR_CHANNEL;
4257 dtv.vval.v_channel = job->jv_channel;
4258 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4259 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004260 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004261 {
4262 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004263 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004264 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4265 }
4266 }
4267 }
4268 else if (tv->v_type == VAR_CHANNEL)
4269 {
4270 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004271 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004272 typval_T dtv;
4273 jsonq_T *jq;
4274 cbq_T *cq;
4275
4276 if (ch != NULL && ch->ch_copyID != copyID)
4277 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004278 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004279 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004280 {
4281 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4282 jq = jq->jq_next)
4283 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4284 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4285 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004286 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004287 {
4288 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004289 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004290 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4291 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004292 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004293 {
4294 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004295 dtv.vval.v_partial =
4296 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004297 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4298 }
4299 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004300 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004301 {
4302 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004303 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004304 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4305 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004306 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004307 {
4308 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004309 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004310 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4311 }
4312 }
4313 }
4314#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004315 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004316}
4317
Bram Moolenaar8c711452005-01-14 21:53:12 +00004318/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004319 * Return a string with the string representation of a variable.
4320 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004321 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004322 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004323 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4324 * stings as "string()", otherwise does not put quotes around strings, as
4325 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004326 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4327 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004328 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004329 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004330 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004331echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004332 typval_T *tv,
4333 char_u **tofree,
4334 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004335 int copyID,
4336 int echo_style,
4337 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004338 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004339{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004340 static int recurse = 0;
4341 char_u *r = NULL;
4342
Bram Moolenaar33570922005-01-25 22:26:29 +00004343 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004344 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004345 if (!did_echo_string_emsg)
4346 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004347 // Only give this message once for a recursive call to avoid
4348 // flooding the user with errors. And stop iterating over lists
4349 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004350 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004351 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004352 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004353 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004354 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004355 }
4356 ++recurse;
4357
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004358 switch (tv->v_type)
4359 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004360 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004361 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004362 {
4363 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004364 r = tv->vval.v_string;
4365 if (r == NULL)
4366 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004367 }
4368 else
4369 {
4370 *tofree = string_quote(tv->vval.v_string, FALSE);
4371 r = *tofree;
4372 }
4373 break;
4374
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004375 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004376 if (echo_style)
4377 {
4378 *tofree = NULL;
4379 r = tv->vval.v_string;
4380 }
4381 else
4382 {
4383 *tofree = string_quote(tv->vval.v_string, TRUE);
4384 r = *tofree;
4385 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004386 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004387
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004388 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004389 {
4390 partial_T *pt = tv->vval.v_partial;
4391 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004392 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004393 garray_T ga;
4394 int i;
4395 char_u *tf;
4396
4397 ga_init2(&ga, 1, 100);
4398 ga_concat(&ga, (char_u *)"function(");
4399 if (fname != NULL)
4400 {
4401 ga_concat(&ga, fname);
4402 vim_free(fname);
4403 }
4404 if (pt != NULL && pt->pt_argc > 0)
4405 {
4406 ga_concat(&ga, (char_u *)", [");
4407 for (i = 0; i < pt->pt_argc; ++i)
4408 {
4409 if (i > 0)
4410 ga_concat(&ga, (char_u *)", ");
4411 ga_concat(&ga,
4412 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4413 vim_free(tf);
4414 }
4415 ga_concat(&ga, (char_u *)"]");
4416 }
4417 if (pt != NULL && pt->pt_dict != NULL)
4418 {
4419 typval_T dtv;
4420
4421 ga_concat(&ga, (char_u *)", ");
4422 dtv.v_type = VAR_DICT;
4423 dtv.vval.v_dict = pt->pt_dict;
4424 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4425 vim_free(tf);
4426 }
4427 ga_concat(&ga, (char_u *)")");
4428
4429 *tofree = ga.ga_data;
4430 r = *tofree;
4431 break;
4432 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004433
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004434 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004435 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004436 break;
4437
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004438 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004439 if (tv->vval.v_list == NULL)
4440 {
4441 *tofree = NULL;
4442 r = NULL;
4443 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004444 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4445 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004446 {
4447 *tofree = NULL;
4448 r = (char_u *)"[...]";
4449 }
4450 else
4451 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004452 int old_copyID = tv->vval.v_list->lv_copyID;
4453
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004454 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004455 *tofree = list2string(tv, copyID, restore_copyID);
4456 if (restore_copyID)
4457 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004458 r = *tofree;
4459 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004460 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004461
Bram Moolenaar8c711452005-01-14 21:53:12 +00004462 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004463 if (tv->vval.v_dict == NULL)
4464 {
4465 *tofree = NULL;
4466 r = NULL;
4467 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004468 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4469 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004470 {
4471 *tofree = NULL;
4472 r = (char_u *)"{...}";
4473 }
4474 else
4475 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004476 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004477 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004478 *tofree = dict2string(tv, copyID, restore_copyID);
4479 if (restore_copyID)
4480 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004481 r = *tofree;
4482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004483 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004484
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004485 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004486 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004487 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004488 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004489 break;
4490
Bram Moolenaar835dc632016-02-07 14:27:38 +01004491 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004492 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004493 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004494 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004495 if (composite_val)
4496 {
4497 *tofree = string_quote(r, FALSE);
4498 r = *tofree;
4499 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004500 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004501
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004502 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004503#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004504 *tofree = NULL;
4505 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4506 r = numbuf;
4507 break;
4508#endif
4509
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004510 case VAR_SPECIAL:
4511 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004512 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004513 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004514 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004515
Bram Moolenaar8502c702014-06-17 12:51:16 +02004516 if (--recurse == 0)
4517 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004518 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004519}
4520
4521/*
4522 * Return a string with the string representation of a variable.
4523 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4524 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004525 * Does not put quotes around strings, as ":echo" displays values.
4526 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4527 * May return NULL.
4528 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004529 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004530echo_string(
4531 typval_T *tv,
4532 char_u **tofree,
4533 char_u *numbuf,
4534 int copyID)
4535{
4536 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4537}
4538
4539/*
4540 * Return a string with the string representation of a variable.
4541 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4542 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004543 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004544 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004545 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004547tv2string(
4548 typval_T *tv,
4549 char_u **tofree,
4550 char_u *numbuf,
4551 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004552{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004553 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004554}
4555
4556/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004557 * Return string "str" in ' quotes, doubling ' characters.
4558 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004559 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004560 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004561 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004562string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004563{
Bram Moolenaar33570922005-01-25 22:26:29 +00004564 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004565 char_u *p, *r, *s;
4566
Bram Moolenaar33570922005-01-25 22:26:29 +00004567 len = (function ? 13 : 3);
4568 if (str != NULL)
4569 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004570 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004571 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004572 if (*p == '\'')
4573 ++len;
4574 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004575 s = r = alloc(len);
4576 if (r != NULL)
4577 {
4578 if (function)
4579 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004580 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004581 r += 10;
4582 }
4583 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004584 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004585 if (str != NULL)
4586 for (p = str; *p != NUL; )
4587 {
4588 if (*p == '\'')
4589 *r++ = '\'';
4590 MB_COPY_CHAR(p, r);
4591 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004592 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004593 if (function)
4594 *r++ = ')';
4595 *r++ = NUL;
4596 }
4597 return s;
4598}
4599
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004600#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004601/*
4602 * Convert the string "text" to a floating point number.
4603 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4604 * this always uses a decimal point.
4605 * Returns the length of the text that was consumed.
4606 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004608string2float(
4609 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004610 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004611{
4612 char *s = (char *)text;
4613 float_T f;
4614
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004615 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004616 if (STRNICMP(text, "inf", 3) == 0)
4617 {
4618 *value = INFINITY;
4619 return 3;
4620 }
4621 if (STRNICMP(text, "-inf", 3) == 0)
4622 {
4623 *value = -INFINITY;
4624 return 4;
4625 }
4626 if (STRNICMP(text, "nan", 3) == 0)
4627 {
4628 *value = NAN;
4629 return 3;
4630 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004631 f = strtod(s, &s);
4632 *value = f;
4633 return (int)((char_u *)s - text);
4634}
4635#endif
4636
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 * Get the value of an environment variable.
4639 * "arg" is pointing to the '$'. It is advanced to after the name.
4640 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004641 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 */
4643 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004644get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645{
4646 char_u *string = NULL;
4647 int len;
4648 int cc;
4649 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004650 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
4652 ++*arg;
4653 name = *arg;
4654 len = get_env_len(arg);
4655 if (evaluate)
4656 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004657 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004658 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004659
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004660 cc = name[len];
4661 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004662 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004663 string = vim_getenv(name, &mustfree);
4664 if (string != NULL && *string != NUL)
4665 {
4666 if (!mustfree)
4667 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004669 else
4670 {
4671 if (mustfree)
4672 vim_free(string);
4673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004674 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004675 string = expand_env_save(name - 1);
4676 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004677 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004678 }
4679 name[len] = cc;
4680
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004681 rettv->v_type = VAR_STRING;
4682 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
4684
4685 return OK;
4686}
4687
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004690 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004692 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004693var2fpos(
4694 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004695 int dollar_lnum, // TRUE when $ is last line
4696 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004698 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004700 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004702 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004703 if (varp->v_type == VAR_LIST)
4704 {
4705 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004706 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004707 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004708 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004709
4710 l = varp->vval.v_list;
4711 if (l == NULL)
4712 return NULL;
4713
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004714 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004715 pos.lnum = list_find_nr(l, 0L, &error);
4716 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004717 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004718
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004719 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004720 pos.col = list_find_nr(l, 1L, &error);
4721 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004722 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004723 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004725 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004726 li = list_find(l, 1L);
4727 if (li != NULL && li->li_tv.v_type == VAR_STRING
4728 && li->li_tv.vval.v_string != NULL
4729 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4730 pos.col = len + 1;
4731
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004732 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004733 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004734 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004735 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004736
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004737 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004738 pos.coladd = list_find_nr(l, 2L, &error);
4739 if (error)
4740 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004741
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004742 return &pos;
4743 }
4744
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004745 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004746 if (name == NULL)
4747 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004748 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004750 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004751 {
4752 if (VIsual_active)
4753 return &VIsual;
4754 return &curwin->w_cursor;
4755 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004756 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004758 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4760 return NULL;
4761 return pp;
4762 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004763
Bram Moolenaara5525202006-03-02 22:52:09 +00004764 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004765
Bram Moolenaar477933c2007-07-17 14:32:23 +00004766 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004767 {
4768 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004769 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004770 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004771 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004772 // In silent Ex mode topline is zero, but that's not a valid line
4773 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004774 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004775 return &pos;
4776 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004777 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004778 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004779 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004780 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004781 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004782 return &pos;
4783 }
4784 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004785 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004787 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 {
4789 pos.lnum = curbuf->b_ml.ml_line_count;
4790 pos.col = 0;
4791 }
4792 else
4793 {
4794 pos.lnum = curwin->w_cursor.lnum;
4795 pos.col = (colnr_T)STRLEN(ml_get_curline());
4796 }
4797 return &pos;
4798 }
4799 return NULL;
4800}
4801
4802/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004803 * Convert list in "arg" into a position and optional file number.
4804 * When "fnump" is NULL there is no file number, only 3 items.
4805 * Note that the column is passed on as-is, the caller may want to decrement
4806 * it to use 1 for the first column.
4807 * Return FAIL when conversion is not possible, doesn't check the position for
4808 * validity.
4809 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004811list2fpos(
4812 typval_T *arg,
4813 pos_T *posp,
4814 int *fnump,
4815 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004816{
4817 list_T *l = arg->vval.v_list;
4818 long i = 0;
4819 long n;
4820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004821 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4822 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004823 if (arg->v_type != VAR_LIST
4824 || l == NULL
4825 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004826 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004827 return FAIL;
4828
4829 if (fnump != NULL)
4830 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004831 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004832 if (n < 0)
4833 return FAIL;
4834 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004835 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004836 *fnump = n;
4837 }
4838
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004839 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004840 if (n < 0)
4841 return FAIL;
4842 posp->lnum = n;
4843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004844 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004845 if (n < 0)
4846 return FAIL;
4847 posp->col = n;
4848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004849 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004850 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004851 posp->coladd = 0;
4852 else
4853 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004854
Bram Moolenaar493c1782014-05-28 14:34:46 +02004855 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004856 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004857
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004858 return OK;
4859}
4860
4861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 * Get the length of an environment variable name.
4863 * Advance "arg" to the first character after the name.
4864 * Return 0 for error.
4865 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004866 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004867get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
4869 char_u *p;
4870 int len;
4871
4872 for (p = *arg; vim_isIDc(*p); ++p)
4873 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004874 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 return 0;
4876
4877 len = (int)(p - *arg);
4878 *arg = p;
4879 return len;
4880}
4881
4882/*
4883 * Get the length of the name of a function or internal variable.
4884 * "arg" is advanced to the first non-white character after the name.
4885 * Return 0 if something is wrong.
4886 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004887 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004888get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889{
4890 char_u *p;
4891 int len;
4892
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004893 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004895 {
4896 if (*p == ':')
4897 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004898 // "s:" is start of "s:var", but "n:" is not and can be used in
4899 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004900 len = (int)(p - *arg);
4901 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4902 || len > 1)
4903 break;
4904 }
4905 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004906 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 return 0;
4908
4909 len = (int)(p - *arg);
4910 *arg = skipwhite(p);
4911
4912 return len;
4913}
4914
4915/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004916 * Get the length of the name of a variable or function.
4917 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004919 * Return -1 if curly braces expansion failed.
4920 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 * If the name contains 'magic' {}'s, expand them and return the
4922 * expanded name in an allocated string via 'alias' - caller must free.
4923 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004925get_name_len(
4926 char_u **arg,
4927 char_u **alias,
4928 int evaluate,
4929 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930{
4931 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 char_u *p;
4933 char_u *expr_start;
4934 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004936 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
4938 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4939 && (*arg)[2] == (int)KE_SNR)
4940 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004941 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 *arg += 3;
4943 return get_id_len(arg) + 3;
4944 }
4945 len = eval_fname_script(*arg);
4946 if (len > 0)
4947 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004948 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 *arg += len;
4950 }
4951
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004953 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004955 p = find_name_end(*arg, &expr_start, &expr_end,
4956 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 if (expr_start != NULL)
4958 {
4959 char_u *temp_string;
4960
4961 if (!evaluate)
4962 {
4963 len += (int)(p - *arg);
4964 *arg = skipwhite(p);
4965 return len;
4966 }
4967
4968 /*
4969 * Include any <SID> etc in the expanded string:
4970 * Thus the -len here.
4971 */
4972 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4973 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004974 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 *alias = temp_string;
4976 *arg = skipwhite(p);
4977 return (int)STRLEN(temp_string);
4978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979
4980 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004981 // Only give an error when there is something, otherwise it will be
4982 // reported at a higher level.
4983 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004984 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985
4986 return len;
4987}
4988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004989/*
4990 * Find the end of a variable or function name, taking care of magic braces.
4991 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4992 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004993 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004994 * Return a pointer to just after the name. Equal to "arg" if there is no
4995 * valid name.
4996 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004997 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004998find_name_end(
4999 char_u *arg,
5000 char_u **expr_start,
5001 char_u **expr_end,
5002 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 int mb_nest = 0;
5005 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005007 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005009 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005011 *expr_start = NULL;
5012 *expr_end = NULL;
5013 }
5014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005015 // Quick check for valid starting character.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005016 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
5017 return arg;
5018
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005019 for (p = arg; *p != NUL
5020 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005021 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005022 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005023 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005024 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005025 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005026 if (*p == '\'')
5027 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005028 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005029 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005030 ;
5031 if (*p == NUL)
5032 break;
5033 }
5034 else if (*p == '"')
5035 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005036 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005037 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005038 if (*p == '\\' && p[1] != NUL)
5039 ++p;
5040 if (*p == NUL)
5041 break;
5042 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005043 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5044 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005045 // "s:" is start of "s:var", but "n:" is not and can be used in
5046 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005047 len = (int)(p - arg);
5048 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005049 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005050 break;
5051 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005052
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005053 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005055 if (*p == '[')
5056 ++br_nest;
5057 else if (*p == ']')
5058 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005060
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005061 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005063 if (*p == '{')
5064 {
5065 mb_nest++;
5066 if (expr_start != NULL && *expr_start == NULL)
5067 *expr_start = p;
5068 }
5069 else if (*p == '}')
5070 {
5071 mb_nest--;
5072 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5073 *expr_end = p;
5074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077
5078 return p;
5079}
5080
5081/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005082 * Expands out the 'magic' {}'s in a variable/function name.
5083 * Note that this can call itself recursively, to deal with
5084 * constructs like foo{bar}{baz}{bam}
5085 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5086 * "in_start" ^
5087 * "expr_start" ^
5088 * "expr_end" ^
5089 * "in_end" ^
5090 *
5091 * Returns a new allocated string, which the caller must free.
5092 * Returns NULL for failure.
5093 */
5094 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005095make_expanded_name(
5096 char_u *in_start,
5097 char_u *expr_start,
5098 char_u *expr_end,
5099 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005100{
5101 char_u c1;
5102 char_u *retval = NULL;
5103 char_u *temp_result;
5104 char_u *nextcmd = NULL;
5105
5106 if (expr_end == NULL || in_end == NULL)
5107 return NULL;
5108 *expr_start = NUL;
5109 *expr_end = NUL;
5110 c1 = *in_end;
5111 *in_end = NUL;
5112
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005113 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005114 if (temp_result != NULL && nextcmd == NULL)
5115 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005116 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5117 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005118 if (retval != NULL)
5119 {
5120 STRCPY(retval, in_start);
5121 STRCAT(retval, temp_result);
5122 STRCAT(retval, expr_end + 1);
5123 }
5124 }
5125 vim_free(temp_result);
5126
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005127 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005128 *expr_start = '{';
5129 *expr_end = '}';
5130
5131 if (retval != NULL)
5132 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005133 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005134 if (expr_start != NULL)
5135 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005136 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005137 temp_result = make_expanded_name(retval, expr_start,
5138 expr_end, temp_result);
5139 vim_free(retval);
5140 retval = temp_result;
5141 }
5142 }
5143
5144 return retval;
5145}
5146
5147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005149 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005151 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005152eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005154 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5155}
5156
5157/*
5158 * Return TRUE if character "c" can be used as the first character in a
5159 * variable or function name (excluding '{' and '}').
5160 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005162eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005163{
5164 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165}
5166
5167/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005168 * Handle:
5169 * - expr[expr], expr[expr:expr] subscript
5170 * - ".name" lookup
5171 * - function call with Funcref variable: func(expr)
5172 * - method call: var->method()
5173 *
5174 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005175 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005176 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005177handle_subscript(
5178 char_u **arg,
5179 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005180 int evaluate, // do more than finding the end
5181 int verbose, // give error messages
5182 char_u *start_leader, // start of '!' and '-' prefixes
5183 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005184{
5185 int ret = OK;
5186 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005187
Bram Moolenaar61343f02019-07-20 21:11:13 +02005188 // "." is ".name" lookup when we found a dict or when evaluating and
5189 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005190 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005191 && (((**arg == '['
5192 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005193 || (!evaluate
5194 && (*arg)[1] != '.'
5195 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005196 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005197 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005198 && !VIM_ISWHITE(*(*arg - 1)))
5199 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005200 {
5201 if (**arg == '(')
5202 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005203 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005204
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005205 // Stop the expression evaluation when immediately aborting on
5206 // error, or when an interrupt occurred or an exception was thrown
5207 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005208 if (aborting())
5209 {
5210 if (ret == OK)
5211 clear_tv(rettv);
5212 ret = FAIL;
5213 }
5214 dict_unref(selfdict);
5215 selfdict = NULL;
5216 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005217 else if (**arg == '-')
5218 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005219 // Expression "-1.0->method()" applies the leader "-" before
5220 // applying ->.
5221 if (evaluate && *end_leaderp > start_leader)
5222 ret = eval7_leader(rettv, start_leader, end_leaderp);
5223 if (ret == OK)
5224 {
5225 if ((*arg)[2] == '{')
5226 // expr->{lambda}()
5227 ret = eval_lambda(arg, rettv, evaluate, verbose);
5228 else
5229 // expr->name()
5230 ret = eval_method(arg, rettv, evaluate, verbose);
5231 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005232 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005233 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005234 {
5235 dict_unref(selfdict);
5236 if (rettv->v_type == VAR_DICT)
5237 {
5238 selfdict = rettv->vval.v_dict;
5239 if (selfdict != NULL)
5240 ++selfdict->dv_refcount;
5241 }
5242 else
5243 selfdict = NULL;
5244 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
5245 {
5246 clear_tv(rettv);
5247 ret = FAIL;
5248 }
5249 }
5250 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005252 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5253 // Don't do this when "Func" is already a partial that was bound
5254 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005255 if (selfdict != NULL
5256 && (rettv->v_type == VAR_FUNC
5257 || (rettv->v_type == VAR_PARTIAL
5258 && (rettv->vval.v_partial->pt_auto
5259 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005260 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005261
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005262 dict_unref(selfdict);
5263 return ret;
5264}
5265
5266/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005267 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 * value).
5269 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005270 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005271alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005273 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274}
5275
5276/*
5277 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 * The string "s" must have been allocated, it is consumed.
5279 * Return NULL for out of memory, the variable otherwise.
5280 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005281 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005282alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283{
Bram Moolenaar33570922005-01-25 22:26:29 +00005284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005286 rettv = alloc_tv();
5287 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005289 rettv->v_type = VAR_STRING;
5290 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292 else
5293 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005294 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295}
5296
5297/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005300 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005301free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
5303 if (varp != NULL)
5304 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 switch (varp->v_type)
5306 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005308 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005309 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005310 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311 vim_free(varp->vval.v_string);
5312 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005313 case VAR_PARTIAL:
5314 partial_unref(varp->vval.v_partial);
5315 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005316 case VAR_BLOB:
5317 blob_unref(varp->vval.v_blob);
5318 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319 case VAR_LIST:
5320 list_unref(varp->vval.v_list);
5321 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005322 case VAR_DICT:
5323 dict_unref(varp->vval.v_dict);
5324 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005325 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005326#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005327 job_unref(varp->vval.v_job);
5328 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005329#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005330 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005331#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005332 channel_unref(varp->vval.v_channel);
5333 break;
5334#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005335 case VAR_NUMBER:
5336 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005337 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005338 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005339 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 vim_free(varp);
5342 }
5343}
5344
5345/*
5346 * Free the memory for a variable value and set the value to NULL or 0.
5347 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005349clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350{
5351 if (varp != NULL)
5352 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005356 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005357 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005358 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005359 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005361 case VAR_PARTIAL:
5362 partial_unref(varp->vval.v_partial);
5363 varp->vval.v_partial = NULL;
5364 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005365 case VAR_BLOB:
5366 blob_unref(varp->vval.v_blob);
5367 varp->vval.v_blob = NULL;
5368 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 case VAR_LIST:
5370 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005371 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005373 case VAR_DICT:
5374 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005375 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005377 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005378 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 varp->vval.v_number = 0;
5380 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005381 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005382#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005383 varp->vval.v_float = 0.0;
5384 break;
5385#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005386 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005387#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005388 job_unref(varp->vval.v_job);
5389 varp->vval.v_job = NULL;
5390#endif
5391 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005392 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005393#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005394 channel_unref(varp->vval.v_channel);
5395 varp->vval.v_channel = NULL;
5396#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005397 case VAR_UNKNOWN:
5398 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005400 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
5402}
5403
5404/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005405 * Set the value of a variable to NULL without freeing items.
5406 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005407 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005408init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005409{
5410 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00005411 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005412}
5413
5414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 * Get the number value of a variable.
5416 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005417 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005418 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005419 * caller of incompatible types: it sets *denote to TRUE if "denote"
5420 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005422 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005423tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005425 int error = FALSE;
5426
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005427 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005428}
5429
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005430 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005431tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005432{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005433 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005438 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005439 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005440#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005441 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005442 break;
5443#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005445 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005446 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 break;
5448 case VAR_STRING:
5449 if (varp->vval.v_string != NULL)
5450 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005451 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005452 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005453 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005454 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005455 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005456 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005457 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005458 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005459 case VAR_SPECIAL:
5460 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005461 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005462#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005463 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005464 break;
5465#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005466 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005467#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005468 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005469 break;
5470#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005471 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005472 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005473 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005474 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005475 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005478 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005479 n = -1;
5480 else
5481 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483}
5484
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005485#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005487tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005488{
5489 switch (varp->v_type)
5490 {
5491 case VAR_NUMBER:
5492 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005493 case VAR_FLOAT:
5494 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005495 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005496 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005497 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005498 break;
5499 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005500 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005501 break;
5502 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005503 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005504 break;
5505 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005506 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005507 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005508 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005509 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005510 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005511 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005512# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005513 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005514 break;
5515# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005516 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005517# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005518 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005519 break;
5520# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005521 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005522 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005523 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005524 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005525 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005526 break;
5527 }
5528 return 0;
5529}
5530#endif
5531
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 * Get the string value of a variable.
5534 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005535 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5536 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 * If the String variable has never been set, return an empty string.
5538 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005539 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005540 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005542 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005543tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 static char_u mybuf[NUMBUFLEN];
5546
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005547 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548}
5549
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005550 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005551tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005553 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005554
5555 return res != NULL ? res : (char_u *)"";
5556}
5557
Bram Moolenaar7d647822014-04-05 21:28:56 +02005558/*
5559 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5560 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005561 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005562tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005563{
5564 static char_u mybuf[NUMBUFLEN];
5565
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005566 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005567}
5568
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005569 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005570tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005571{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005575 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005576 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 return buf;
5578 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005579 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005580 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 break;
5582 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005583 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584 break;
5585 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005586 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005587 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005588 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005589#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005590 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005591 break;
5592#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 case VAR_STRING:
5594 if (varp->vval.v_string != NULL)
5595 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005596 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005597 case VAR_SPECIAL:
5598 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5599 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005600 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005601 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005602 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005603 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005604#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005605 {
5606 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005607 char *status;
5608
5609 if (job == NULL)
5610 return (char_u *)"no process";
5611 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005612 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005613 : "run";
5614# ifdef UNIX
5615 vim_snprintf((char *)buf, NUMBUFLEN,
5616 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005617# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005618 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005619 "process %ld %s",
5620 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005621 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005622# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005623 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005624 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5625# endif
5626 return buf;
5627 }
5628#endif
5629 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005630 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005631#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005632 {
5633 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005634 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005635
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005636 if (channel == NULL)
5637 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5638 else
5639 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005640 "channel %d %s", channel->ch_id, status);
5641 return buf;
5642 }
5643#endif
5644 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005645 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005646 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005647 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005649 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650}
5651
5652/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005653 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5654 * string() on Dict, List, etc.
5655 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005656 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005657tv_stringify(typval_T *varp, char_u *buf)
5658{
5659 if (varp->v_type == VAR_LIST
5660 || varp->v_type == VAR_DICT
5661 || varp->v_type == VAR_FUNC
5662 || varp->v_type == VAR_PARTIAL
5663 || varp->v_type == VAR_FLOAT)
5664 {
5665 typval_T tmp;
5666
5667 f_string(varp, &tmp);
5668 tv_get_string_buf(&tmp, buf);
5669 clear_tv(varp);
5670 *varp = tmp;
5671 return tmp.vval.v_string;
5672 }
5673 return tv_get_string_buf(varp, buf);
5674}
5675
5676/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005677 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5678 * Also give an error message, using "name" or _("name") when use_gettext is
5679 * TRUE.
5680 */
5681 static int
5682tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5683{
5684 int lock = 0;
5685
5686 switch (tv->v_type)
5687 {
5688 case VAR_BLOB:
5689 if (tv->vval.v_blob != NULL)
5690 lock = tv->vval.v_blob->bv_lock;
5691 break;
5692 case VAR_LIST:
5693 if (tv->vval.v_list != NULL)
5694 lock = tv->vval.v_list->lv_lock;
5695 break;
5696 case VAR_DICT:
5697 if (tv->vval.v_dict != NULL)
5698 lock = tv->vval.v_dict->dv_lock;
5699 break;
5700 default:
5701 break;
5702 }
5703 return var_check_lock(tv->v_lock, name, use_gettext)
5704 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5705}
5706
5707/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005708 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005710 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005711 * It is OK for "from" and "to" to point to the same item. This is used to
5712 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005713 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005714 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005715copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005718 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719 switch (from->v_type)
5720 {
5721 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005722 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723 to->vval.v_number = from->vval.v_number;
5724 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005725 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005726#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005727 to->vval.v_float = from->vval.v_float;
5728 break;
5729#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005730 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005731#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005732 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005733 if (to->vval.v_job != NULL)
5734 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005735 break;
5736#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005737 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005738#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005739 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005740 if (to->vval.v_channel != NULL)
5741 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005742 break;
5743#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005744 case VAR_STRING:
5745 case VAR_FUNC:
5746 if (from->vval.v_string == NULL)
5747 to->vval.v_string = NULL;
5748 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005749 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005750 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005751 if (from->v_type == VAR_FUNC)
5752 func_ref(to->vval.v_string);
5753 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005755 case VAR_PARTIAL:
5756 if (from->vval.v_partial == NULL)
5757 to->vval.v_partial = NULL;
5758 else
5759 {
5760 to->vval.v_partial = from->vval.v_partial;
5761 ++to->vval.v_partial->pt_refcount;
5762 }
5763 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005764 case VAR_BLOB:
5765 if (from->vval.v_blob == NULL)
5766 to->vval.v_blob = NULL;
5767 else
5768 {
5769 to->vval.v_blob = from->vval.v_blob;
5770 ++to->vval.v_blob->bv_refcount;
5771 }
5772 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005773 case VAR_LIST:
5774 if (from->vval.v_list == NULL)
5775 to->vval.v_list = NULL;
5776 else
5777 {
5778 to->vval.v_list = from->vval.v_list;
5779 ++to->vval.v_list->lv_refcount;
5780 }
5781 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 case VAR_DICT:
5783 if (from->vval.v_dict == NULL)
5784 to->vval.v_dict = NULL;
5785 else
5786 {
5787 to->vval.v_dict = from->vval.v_dict;
5788 ++to->vval.v_dict->dv_refcount;
5789 }
5790 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005791 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005792 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005793 break;
5794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795}
5796
5797/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005798 * Make a copy of an item.
5799 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005800 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5801 * reference to an already copied list/dict can be used.
5802 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005803 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005804 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005805item_copy(
5806 typval_T *from,
5807 typval_T *to,
5808 int deep,
5809 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005810{
5811 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005812 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005813
Bram Moolenaar33570922005-01-25 22:26:29 +00005814 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005815 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005816 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005817 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005818 }
5819 ++recurse;
5820
5821 switch (from->v_type)
5822 {
5823 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005824 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005825 case VAR_STRING:
5826 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005827 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005828 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005829 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005830 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005831 copy_tv(from, to);
5832 break;
5833 case VAR_LIST:
5834 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005835 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005836 if (from->vval.v_list == NULL)
5837 to->vval.v_list = NULL;
5838 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5839 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005840 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005841 to->vval.v_list = from->vval.v_list->lv_copylist;
5842 ++to->vval.v_list->lv_refcount;
5843 }
5844 else
5845 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5846 if (to->vval.v_list == NULL)
5847 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005848 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005849 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005850 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005851 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005852 case VAR_DICT:
5853 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005854 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005855 if (from->vval.v_dict == NULL)
5856 to->vval.v_dict = NULL;
5857 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5858 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005859 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005860 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5861 ++to->vval.v_dict->dv_refcount;
5862 }
5863 else
5864 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5865 if (to->vval.v_dict == NULL)
5866 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005867 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005868 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005869 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005870 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005871 }
5872 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005873 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005874}
5875
5876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 * ":echo expr1 ..." print each argument separated with a space, add a
5878 * newline at the end.
5879 * ":echon expr1 ..." print each argument plain.
5880 */
5881 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005882ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 char_u *p;
5888 int needclr = TRUE;
5889 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005890 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01005891 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005892 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
5894 if (eap->skip)
5895 ++emsg_skip;
5896 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
5897 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005898 // If eval1() causes an error message the text from the command may
5899 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005900 need_clr_eos = needclr;
5901
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005903 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 {
5905 /*
5906 * Report the invalid expression unless the expression evaluation
5907 * has been cancelled due to an aborting error, an interrupt, or an
5908 * exception.
5909 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005910 if (!aborting() && did_emsg == did_emsg_before
5911 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005912 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005913 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 break;
5915 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005916 need_clr_eos = FALSE;
5917
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 if (!eap->skip)
5919 {
5920 if (atstart)
5921 {
5922 atstart = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005923 // Call msg_start() after eval1(), evaluating the expression
5924 // may cause a message to appear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01005926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005927 // Mark the saved text as finishing the line, so that what
5928 // follows is displayed on a new line when scrolling back
5929 // at the more prompt.
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02005930 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01005932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 }
5934 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005935 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005936 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005937 if (p != NULL)
5938 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005940 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005942 if (*p != TAB && needclr)
5943 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005944 // remove any text still there from the command
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005945 msg_clr_eos();
5946 needclr = FALSE;
5947 }
5948 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 }
5950 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005951 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005952 if (has_mbyte)
5953 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005954 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005955
5956 (void)msg_outtrans_len_attr(p, i, echo_attr);
5957 p += i - 1;
5958 }
5959 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005960 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005965 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 arg = skipwhite(arg);
5967 }
5968 eap->nextcmd = check_nextcmd(arg);
5969
5970 if (eap->skip)
5971 --emsg_skip;
5972 else
5973 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005974 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 if (needclr)
5976 msg_clr_eos();
5977 if (eap->cmdidx == CMD_echo)
5978 msg_end();
5979 }
5980}
5981
5982/*
5983 * ":echohl {name}".
5984 */
5985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005986ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005988 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989}
5990
5991/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005992 * Returns the :echo attribute
5993 */
5994 int
5995get_echo_attr(void)
5996{
5997 return echo_attr;
5998}
5999
6000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 * ":execute expr1 ..." execute the result of an expression.
6002 * ":echomsg expr1 ..." Print a message
6003 * ":echoerr expr1 ..." Print an error
6004 * Each gets spaces around each argument and a newline at the end for
6005 * echo commands
6006 */
6007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006008ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 int ret = OK;
6013 char_u *p;
6014 garray_T ga;
6015 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006016 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017
6018 ga_init2(&ga, 1, 80);
6019
6020 if (eap->skip)
6021 ++emsg_skip;
6022 while (*arg != NUL && *arg != '|' && *arg != '\n')
6023 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006024 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6025 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027
6028 if (!eap->skip)
6029 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006030 char_u buf[NUMBUFLEN];
6031
6032 if (eap->cmdidx == CMD_execute)
6033 p = tv_get_string_buf(&rettv, buf);
6034 else
6035 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 len = (int)STRLEN(p);
6037 if (ga_grow(&ga, len + 2) == FAIL)
6038 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006039 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 ret = FAIL;
6041 break;
6042 }
6043 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 ga.ga_len += len;
6047 }
6048
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006049 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 arg = skipwhite(arg);
6051 }
6052
6053 if (ret != FAIL && ga.ga_data != NULL)
6054 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006055 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6056 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006057 // Mark the already saved text as finishing the line, so that what
6058 // follows is displayed on a new line when scrolling back at the
6059 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006060 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006061 }
6062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006064 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006065 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006066 out_flush();
6067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 else if (eap->cmdidx == CMD_echoerr)
6069 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006070 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006072 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 if (!force_abort)
6074 did_emsg = save_did_emsg;
6075 }
6076 else if (eap->cmdidx == CMD_execute)
6077 do_cmdline((char_u *)ga.ga_data,
6078 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6079 }
6080
6081 ga_clear(&ga);
6082
6083 if (eap->skip)
6084 --emsg_skip;
6085
6086 eap->nextcmd = check_nextcmd(arg);
6087}
6088
6089/*
6090 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6091 * "arg" points to the "&" or '+' when called, to "option" when returning.
6092 * Returns NULL when no option name found. Otherwise pointer to the char
6093 * after the option name.
6094 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006095 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
6098 char_u *p = *arg;
6099
6100 ++p;
6101 if (*p == 'g' && p[1] == ':')
6102 {
6103 *opt_flags = OPT_GLOBAL;
6104 p += 2;
6105 }
6106 else if (*p == 'l' && p[1] == ':')
6107 {
6108 *opt_flags = OPT_LOCAL;
6109 p += 2;
6110 }
6111 else
6112 *opt_flags = 0;
6113
6114 if (!ASCII_ISALPHA(*p))
6115 return NULL;
6116 *arg = p;
6117
6118 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006119 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 else
6121 while (ASCII_ISALPHA(*p))
6122 ++p;
6123 return p;
6124}
6125
6126/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006127 * Display script name where an item was last set.
6128 * Should only be invoked when 'verbose' is non-zero.
6129 */
6130 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006131last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006132{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006133 char_u *p;
6134
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006135 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006136 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006137 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006138 if (p != NULL)
6139 {
6140 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006141 msg_puts(_("\n\tLast set from "));
6142 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006143 if (script_ctx.sc_lnum > 0)
6144 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006145 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006146 msg_outnum((long)script_ctx.sc_lnum);
6147 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006148 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006149 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006150 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006151 }
6152}
6153
Bram Moolenaar61be3762019-03-19 23:04:17 +01006154/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006155 * Compare "typ1" and "typ2". Put the result in "typ1".
6156 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006157 int
6158typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006159 typval_T *typ1, // first operand
6160 typval_T *typ2, // second operand
6161 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006162 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006163{
6164 int i;
6165 varnumber_T n1, n2;
6166 char_u *s1, *s2;
6167 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006168 int type_is = type == ETYPE_IS || type == ETYPE_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006169
Bram Moolenaar31988702018-02-13 12:57:42 +01006170 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006171 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006172 // For "is" a different type always means FALSE, for "notis"
6173 // it means TRUE.
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006174 n1 = (type == ETYPE_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006175 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006176 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6177 {
6178 if (type_is)
6179 {
6180 n1 = (typ1->v_type == typ2->v_type
6181 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006182 if (type == ETYPE_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006183 n1 = !n1;
6184 }
6185 else if (typ1->v_type != typ2->v_type
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006186 || (type != ETYPE_EQUAL && type != ETYPE_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006187 {
6188 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006189 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006190 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006191 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006192 clear_tv(typ1);
6193 return FAIL;
6194 }
6195 else
6196 {
6197 // Compare two Blobs for being equal or unequal.
6198 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006199 if (type == ETYPE_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006200 n1 = !n1;
6201 }
6202 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006203 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6204 {
6205 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006206 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006207 n1 = (typ1->v_type == typ2->v_type
6208 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006209 if (type == ETYPE_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006210 n1 = !n1;
6211 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006212 else if (typ1->v_type != typ2->v_type
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006213 || (type != ETYPE_EQUAL && type != ETYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006214 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006215 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006216 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006217 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006218 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006219 clear_tv(typ1);
6220 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006221 }
6222 else
6223 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006224 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006225 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6226 ic, FALSE);
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006227 if (type == ETYPE_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006228 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006229 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006230 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006231
6232 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6233 {
6234 if (type_is)
6235 {
6236 n1 = (typ1->v_type == typ2->v_type
6237 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006238 if (type == ETYPE_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006239 n1 = !n1;
6240 }
6241 else if (typ1->v_type != typ2->v_type
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006242 || (type != ETYPE_EQUAL && type != ETYPE_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006243 {
6244 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006245 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006246 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006247 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006248 clear_tv(typ1);
6249 return FAIL;
6250 }
6251 else
6252 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006253 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006254 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6255 ic, FALSE);
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006256 if (type == ETYPE_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006257 n1 = !n1;
6258 }
6259 }
6260
6261 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6262 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6263 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006264 if (type != ETYPE_EQUAL && type != ETYPE_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006265 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006266 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006267 clear_tv(typ1);
6268 return FAIL;
6269 }
6270 if ((typ1->v_type == VAR_PARTIAL
6271 && typ1->vval.v_partial == NULL)
6272 || (typ2->v_type == VAR_PARTIAL
6273 && typ2->vval.v_partial == NULL))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006274 // when a partial is NULL assume not equal
Bram Moolenaar31988702018-02-13 12:57:42 +01006275 n1 = FALSE;
6276 else if (type_is)
6277 {
6278 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006279 // strings are considered the same if their value is
6280 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006281 n1 = tv_equal(typ1, typ2, ic, FALSE);
6282 else if (typ1->v_type == VAR_PARTIAL
6283 && typ2->v_type == VAR_PARTIAL)
6284 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6285 else
6286 n1 = FALSE;
6287 }
6288 else
6289 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006290 if (type == ETYPE_NEQUAL || type == ETYPE_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006291 n1 = !n1;
6292 }
6293
6294#ifdef FEAT_FLOAT
6295 /*
6296 * If one of the two variables is a float, compare as a float.
6297 * When using "=~" or "!~", always compare as string.
6298 */
6299 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006300 && type != ETYPE_MATCH && type != ETYPE_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006301 {
6302 float_T f1, f2;
6303
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006304 f1 = tv_get_float(typ1);
6305 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006306 n1 = FALSE;
6307 switch (type)
6308 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006309 case ETYPE_EQUAL: n1 = (f1 == f2); break;
6310 case ETYPE_NEQUAL: n1 = (f1 != f2); break;
6311 case ETYPE_GREATER: n1 = (f1 > f2); break;
6312 case ETYPE_GEQUAL: n1 = (f1 >= f2); break;
6313 case ETYPE_SMALLER: n1 = (f1 < f2); break;
6314 case ETYPE_SEQUAL: n1 = (f1 <= f2); break;
6315 case ETYPE_UNKNOWN:
6316 case ETYPE_IS:
6317 case ETYPE_ISNOT:
6318 case ETYPE_MATCH:
6319 case ETYPE_NOMATCH: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006320 }
6321 }
6322#endif
6323
6324 /*
6325 * If one of the two variables is a number, compare as a number.
6326 * When using "=~" or "!~", always compare as string.
6327 */
6328 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006329 && type != ETYPE_MATCH && type != ETYPE_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006330 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006331 n1 = tv_get_number(typ1);
6332 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006333 switch (type)
6334 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006335 case ETYPE_EQUAL: n1 = (n1 == n2); break;
6336 case ETYPE_NEQUAL: n1 = (n1 != n2); break;
6337 case ETYPE_GREATER: n1 = (n1 > n2); break;
6338 case ETYPE_GEQUAL: n1 = (n1 >= n2); break;
6339 case ETYPE_SMALLER: n1 = (n1 < n2); break;
6340 case ETYPE_SEQUAL: n1 = (n1 <= n2); break;
6341 case ETYPE_UNKNOWN:
6342 case ETYPE_IS:
6343 case ETYPE_ISNOT:
6344 case ETYPE_MATCH:
6345 case ETYPE_NOMATCH: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006346 }
6347 }
6348 else
6349 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006350 s1 = tv_get_string_buf(typ1, buf1);
6351 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006352 if (type != ETYPE_MATCH && type != ETYPE_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006353 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6354 else
6355 i = 0;
6356 n1 = FALSE;
6357 switch (type)
6358 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006359 case ETYPE_EQUAL: n1 = (i == 0); break;
6360 case ETYPE_NEQUAL: n1 = (i != 0); break;
6361 case ETYPE_GREATER: n1 = (i > 0); break;
6362 case ETYPE_GEQUAL: n1 = (i >= 0); break;
6363 case ETYPE_SMALLER: n1 = (i < 0); break;
6364 case ETYPE_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006365
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006366 case ETYPE_MATCH:
6367 case ETYPE_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006368 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006369 if (type == ETYPE_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006370 n1 = !n1;
6371 break;
6372
Bram Moolenaar07a3db82019-12-25 18:14:14 +01006373 case ETYPE_IS:
6374 case ETYPE_ISNOT:
6375 case ETYPE_UNKNOWN: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006376 }
6377 }
6378 clear_tv(typ1);
6379 typ1->v_type = VAR_NUMBER;
6380 typ1->vval.v_number = n1;
6381
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006382 return OK;
6383}
6384
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006385 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006386typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006387{
6388 char_u *tofree;
6389 char_u numbuf[NUMBUFLEN];
6390 char_u *ret = NULL;
6391
6392 if (arg == NULL)
6393 return vim_strsave((char_u *)"(does not exist)");
6394 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006395 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006396 if (ret != NULL && tofree == NULL)
6397 ret = vim_strsave(ret);
6398 return ret;
6399}
6400
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006401#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402
6403/*
6404 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006405 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 * "flags" can be "g" to do a global substitute.
6407 * Returns an allocated string, NULL for error.
6408 */
6409 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006410do_string_sub(
6411 char_u *str,
6412 char_u *pat,
6413 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006414 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006415 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416{
6417 int sublen;
6418 regmatch_T regmatch;
6419 int i;
6420 int do_all;
6421 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006422 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 garray_T ga;
6424 char_u *ret;
6425 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006426 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006428 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006430 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431
6432 ga_init2(&ga, 1, 200);
6433
6434 do_all = (flags[0] == 'g');
6435
6436 regmatch.rm_ic = p_ic;
6437 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6438 if (regmatch.regprog != NULL)
6439 {
6440 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006441 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6443 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006444 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006445 if (regmatch.startp[0] == regmatch.endp[0])
6446 {
6447 if (zero_width == regmatch.startp[0])
6448 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006449 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006450 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006451 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6452 (size_t)i);
6453 ga.ga_len += i;
6454 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006455 continue;
6456 }
6457 zero_width = regmatch.startp[0];
6458 }
6459
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 /*
6461 * Get some space for a temporary buffer to do the substitution
6462 * into. It will contain:
6463 * - The text up to where the match is.
6464 * - The substituted text.
6465 * - The text after the match.
6466 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006467 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006468 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6470 {
6471 ga_clear(&ga);
6472 break;
6473 }
6474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006475 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 i = (int)(regmatch.startp[0] - tail);
6477 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006478 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006479 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 + ga.ga_len + i, TRUE, TRUE, FALSE);
6481 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006482 tail = regmatch.endp[0];
6483 if (*tail == NUL)
6484 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 if (!do_all)
6486 break;
6487 }
6488
6489 if (ga.ga_data != NULL)
6490 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6491
Bram Moolenaar473de612013-06-08 18:19:48 +02006492 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 }
6494
6495 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6496 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006497 if (p_cpo == empty_option)
6498 p_cpo = save_cpo;
6499 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006500 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006501 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
6503 return ret;
6504}