blob: 0fe8fd34aa9cc04fe8c028f8131b82f75b62b3ff [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 Moolenaar071d4272004-06-13 20:20:40 +000039static int echo_attr = 0; /* attributes used for ":echo" */
40
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{
46 int fi_semicolon; /* TRUE if ending in '; var]' */
47 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000048 listwatch_T fi_lw; /* keep an eye on the item used. */
49 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010050 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,
177 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;
264 if (*s != NUL) /* check for trailing chars after expr */
265 {
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,
303 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 Moolenaard8e9bb22005-07-09 21:14:46 +0000470 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 {
577 /* 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 {
584 /* 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,
628 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 Moolenaar7480b5c2005-01-16 22:07:38 +0000644 /* 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 {
649 /* When skipping just find the end of the name. */
650 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
654 /* 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 {
658 /* 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 {
669 /* 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. */
672 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
684 /* Without [idx] or .key we are done. */
685 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
686 return p;
687
688 cc = *p;
689 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100690 /* Only pass &ht when we would write to the variable, it prevents autoload
691 * as well. */
692 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 Moolenaar8c711452005-01-14 21:53:12 +0000741 /* 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;
748 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 {
752 /* not a number or string */
753 clear_tv(&var1);
754 return NULL;
755 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000756 }
757
758 /* Optionally get the second index [ :expr]. */
759 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 Moolenaar8c711452005-01-14 21:53:12 +0000785 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
786 {
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 {
792 /* 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
812 /* Skip to past ']'. */
813 ++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 Moolenaar7480b5c2005-01-16 22:07:38 +0000820 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100821 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 Moolenaarbdb62052012-07-16 17:31:53 +0200832 /* 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. */
835 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
846 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 Moolenaar4228bec2011-03-27 16:03:15 +0200885 /* 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 Moolenaarf06e5a52017-02-23 14:25:17 +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 Moolenaar22fcfad2016-07-01 18:17:26 +0200976 /* 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 Moolenaar7480b5c2005-01-16 22:07:38 +0000990 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
991 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 Moolenaar7480b5c2005-01-16 22:07:38 +00001167 /* 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 Moolenaar7480b5c2005-01-16 22:07:38 +00001202 /* 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 Moolenaar520e1e42016-01-23 19:46:28 +01001247 /* Can't do anything with a Funcref, Dict, v:true on the right. */
1248 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
1389 *errp = TRUE; /* default: there is an error */
1390
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 Moolenaar3d60ec22005-01-05 22:19:46 +00001524 if (cmdidx == CMD_let)
1525 {
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 {
1529 /* ":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 {
1566 /* environment variable */
1567 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 {
1577 /* Autoload function/variable contains '#'. */
1578 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 Moolenaar8a349ff2014-11-12 20:09:06 +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 {
1589 if (c == '"') /* string */
1590 {
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 }
1596 else if (c == '\'') /* literal string */
1597 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001598 /* 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 Moolenaar3d60ec22005-01-05 22:19:46 +00001617 /* Doesn't look like something valid, expand as an expression
1618 * anyway. */
1619 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
1639 /* avoid 'l' flag in 'cpoptions' */
1640 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
1675 /* If "s" is the name of a variable of type VAR_FUNC
1676 * use its contents. */
1677 s = deref_func_name(s, &len, &partial, !evaluate);
1678
1679 /* Need to make a copy, in case evaluating the arguments makes
1680 * the name invalid. */
1681 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
1699 /* 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. */
1702 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1703 {
1704 rettv->vval.v_string = NULL;
1705 rettv->v_type = VAR_FUNC;
1706 }
1707
1708 /* Stop the expression evaluation when immediately
1709 * aborting on error, or when an interrupt occurred or
1710 * an exception was thrown but not caught. */
1711 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 Moolenaarc70646c2005-01-04 21:52:38 +00001809 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);
1827 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
1828 {
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;
2000 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002001 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
2005 /*
2006 * Get the first variable.
2007 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002008 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 return FAIL;
2010
2011 p = *arg;
2012 switch (p[0])
2013 {
2014 case '=': if (p[1] == '=')
2015 type = TYPE_EQUAL;
2016 else if (p[1] == '~')
2017 type = TYPE_MATCH;
2018 break;
2019 case '!': if (p[1] == '=')
2020 type = TYPE_NEQUAL;
2021 else if (p[1] == '~')
2022 type = TYPE_NOMATCH;
2023 break;
2024 case '>': if (p[1] != '=')
2025 {
2026 type = TYPE_GREATER;
2027 len = 1;
2028 }
2029 else
2030 type = TYPE_GEQUAL;
2031 break;
2032 case '<': if (p[1] != '=')
2033 {
2034 type = TYPE_SMALLER;
2035 len = 1;
2036 }
2037 else
2038 type = TYPE_SEQUAL;
2039 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002040 case 'i': if (p[1] == 's')
2041 {
2042 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2043 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002044 i = p[len];
2045 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002046 {
2047 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
2048 type_is = TRUE;
2049 }
2050 }
2051 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
2053
2054 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002055 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 */
2057 if (type != TYPE_UNKNOWN)
2058 {
2059 /* extra question mark appended: ignore case */
2060 if (p[len] == '?')
2061 {
2062 ic = TRUE;
2063 ++len;
2064 }
2065 /* extra '#' appended: match case */
2066 else if (p[len] == '#')
2067 {
2068 ic = FALSE;
2069 ++len;
2070 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002071 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 else
2073 ic = p_ic;
2074
2075 /*
2076 * Get the second variable.
2077 */
2078 *arg = skipwhite(p + len);
2079 if (eval5(arg, &var2, evaluate) == FAIL)
2080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 return FAIL;
2083 }
Bram Moolenaar31988702018-02-13 12:57:42 +01002084 if (evaluate)
2085 {
2086 int ret = typval_compare(rettv, &var2, type, type_is, ic);
2087
2088 clear_tv(&var2);
2089 return ret;
2090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
2092
2093 return OK;
2094}
2095
2096/*
2097 * Handle fourth level expression:
2098 * + number addition
2099 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002100 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002101 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 *
2103 * "arg" must point to the first non-white of the expression.
2104 * "arg" is advanced to the next non-white after the recognized expression.
2105 *
2106 * Return OK or FAIL.
2107 */
2108 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002109eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
Bram Moolenaar33570922005-01-25 22:26:29 +00002111 typval_T var2;
2112 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002114 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002115#ifdef FEAT_FLOAT
2116 float_T f1 = 0, f2 = 0;
2117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 char_u *s1, *s2;
2119 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2120 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002121 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123 /*
2124 * Get the first variable.
2125 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002126 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 return FAIL;
2128
2129 /*
2130 * Repeat computing, until no '+', '-' or '.' is following.
2131 */
2132 for (;;)
2133 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002134 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002136 concat = op == '.'
2137 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2138 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 break;
2140
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002141 if ((op != '+' || (rettv->v_type != VAR_LIST
2142 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002143#ifdef FEAT_FLOAT
2144 && (op == '.' || rettv->v_type != VAR_FLOAT)
2145#endif
2146 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002147 {
2148 /* For "list + ...", an illegal use of the first operand as
2149 * a number cannot be determined before evaluating the 2nd
2150 * operand: if this is also a list, all is ok.
2151 * For "something . ...", "something - ..." or "non-list + ...",
2152 * we know that the first operand needs to be a string or number
2153 * without evaluating the 2nd operand. So check before to avoid
2154 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002155 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002156 {
2157 clear_tv(rettv);
2158 return FAIL;
2159 }
2160 }
2161
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 /*
2163 * Get the second variable.
2164 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002165 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2166 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002168 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002170 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 return FAIL;
2172 }
2173
2174 if (evaluate)
2175 {
2176 /*
2177 * Compute the result.
2178 */
2179 if (op == '.')
2180 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002181 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
2182 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002183 if (s2 == NULL) /* type error ? */
2184 {
2185 clear_tv(rettv);
2186 clear_tv(&var2);
2187 return FAIL;
2188 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002189 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 clear_tv(rettv);
2191 rettv->v_type = VAR_STRING;
2192 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002194 else if (op == '+' && rettv->v_type == VAR_BLOB
2195 && var2.v_type == VAR_BLOB)
2196 {
2197 blob_T *b1 = rettv->vval.v_blob;
2198 blob_T *b2 = var2.vval.v_blob;
2199 blob_T *b = blob_alloc();
2200 int i;
2201
2202 if (b != NULL)
2203 {
2204 for (i = 0; i < blob_len(b1); i++)
2205 ga_append(&b->bv_ga, blob_get(b1, i));
2206 for (i = 0; i < blob_len(b2); i++)
2207 ga_append(&b->bv_ga, blob_get(b2, i));
2208
2209 clear_tv(rettv);
2210 rettv_blob_set(rettv, b);
2211 }
2212 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00002213 else if (op == '+' && rettv->v_type == VAR_LIST
2214 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002215 {
2216 /* concatenate Lists */
2217 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
2218 &var3) == FAIL)
2219 {
2220 clear_tv(rettv);
2221 clear_tv(&var2);
2222 return FAIL;
2223 }
2224 clear_tv(rettv);
2225 *rettv = var3;
2226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 else
2228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002229 int error = FALSE;
2230
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002231#ifdef FEAT_FLOAT
2232 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002233 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002234 f1 = rettv->vval.v_float;
2235 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002236 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002237 else
2238#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002239 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002240 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002241 if (error)
2242 {
2243 /* This can only happen for "list + non-list". For
2244 * "non-list + ..." or "something - ...", we returned
2245 * before evaluating the 2nd operand. */
2246 clear_tv(rettv);
2247 return FAIL;
2248 }
2249#ifdef FEAT_FLOAT
2250 if (var2.v_type == VAR_FLOAT)
2251 f1 = n1;
2252#endif
2253 }
2254#ifdef FEAT_FLOAT
2255 if (var2.v_type == VAR_FLOAT)
2256 {
2257 f2 = var2.vval.v_float;
2258 n2 = 0;
2259 }
2260 else
2261#endif
2262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002263 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002264 if (error)
2265 {
2266 clear_tv(rettv);
2267 clear_tv(&var2);
2268 return FAIL;
2269 }
2270#ifdef FEAT_FLOAT
2271 if (rettv->v_type == VAR_FLOAT)
2272 f2 = n2;
2273#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002274 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002276
2277#ifdef FEAT_FLOAT
2278 /* If there is a float on either side the result is a float. */
2279 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2280 {
2281 if (op == '+')
2282 f1 = f1 + f2;
2283 else
2284 f1 = f1 - f2;
2285 rettv->v_type = VAR_FLOAT;
2286 rettv->vval.v_float = f1;
2287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002289#endif
2290 {
2291 if (op == '+')
2292 n1 = n1 + n2;
2293 else
2294 n1 = n1 - n2;
2295 rettv->v_type = VAR_NUMBER;
2296 rettv->vval.v_number = n1;
2297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301 }
2302 return OK;
2303}
2304
2305/*
2306 * Handle fifth level expression:
2307 * * number multiplication
2308 * / number division
2309 * % number modulo
2310 *
2311 * "arg" must point to the first non-white of the expression.
2312 * "arg" is advanced to the next non-white after the recognized expression.
2313 *
2314 * Return OK or FAIL.
2315 */
2316 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002317eval6(
2318 char_u **arg,
2319 typval_T *rettv,
2320 int evaluate,
2321 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
Bram Moolenaar33570922005-01-25 22:26:29 +00002323 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002325 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002326#ifdef FEAT_FLOAT
2327 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002328 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002329#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002330 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
2332 /*
2333 * Get the first variable.
2334 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002335 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 return FAIL;
2337
2338 /*
2339 * Repeat computing, until no '*', '/' or '%' is following.
2340 */
2341 for (;;)
2342 {
2343 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002344 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 break;
2346
2347 if (evaluate)
2348 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002349#ifdef FEAT_FLOAT
2350 if (rettv->v_type == VAR_FLOAT)
2351 {
2352 f1 = rettv->vval.v_float;
2353 use_float = TRUE;
2354 n1 = 0;
2355 }
2356 else
2357#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002358 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 if (error)
2361 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 }
2363 else
2364 n1 = 0;
2365
2366 /*
2367 * Get the second variable.
2368 */
2369 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002370 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 return FAIL;
2372
2373 if (evaluate)
2374 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002375#ifdef FEAT_FLOAT
2376 if (var2.v_type == VAR_FLOAT)
2377 {
2378 if (!use_float)
2379 {
2380 f1 = n1;
2381 use_float = TRUE;
2382 }
2383 f2 = var2.vval.v_float;
2384 n2 = 0;
2385 }
2386 else
2387#endif
2388 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002389 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002390 clear_tv(&var2);
2391 if (error)
2392 return FAIL;
2393#ifdef FEAT_FLOAT
2394 if (use_float)
2395 f2 = n2;
2396#endif
2397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 /*
2400 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002401 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002403#ifdef FEAT_FLOAT
2404 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002406 if (op == '*')
2407 f1 = f1 * f2;
2408 else if (op == '/')
2409 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002410# ifdef VMS
2411 /* VMS crashes on divide by zero, work around it */
2412 if (f2 == 0.0)
2413 {
2414 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002415 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002416 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002417 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002418 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002419 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002420 }
2421 else
2422 f1 = f1 / f2;
2423# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002424 /* We rely on the floating point library to handle divide
2425 * by zero to result in "inf" and not a crash. */
2426 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002427# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002430 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002431 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002432 return FAIL;
2433 }
2434 rettv->v_type = VAR_FLOAT;
2435 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002440 if (op == '*')
2441 n1 = n1 * n2;
2442 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002443 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002445 n1 = num_modulus(n1, n2);
2446
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002447 rettv->v_type = VAR_NUMBER;
2448 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 }
2451 }
2452
2453 return OK;
2454}
2455
2456/*
2457 * Handle sixth level expression:
2458 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002459 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002460 * "string" string constant
2461 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 * &option-name option value
2463 * @r register contents
2464 * identifier variable value
2465 * function() function call
2466 * $VAR environment variable
2467 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002468 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002469 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002470 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 *
2472 * Also handle:
2473 * ! in front logical NOT
2474 * - in front unary minus
2475 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002476 * trailing [] subscript in String or List
2477 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002478 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 *
2480 * "arg" must point to the first non-white of the expression.
2481 * "arg" is advanced to the next non-white after the recognized expression.
2482 *
2483 * Return OK or FAIL.
2484 */
2485 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002486eval7(
2487 char_u **arg,
2488 typval_T *rettv,
2489 int evaluate,
2490 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002492 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 int len;
2494 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 char_u *start_leader, *end_leader;
2496 int ret = OK;
2497 char_u *alias;
2498
2499 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002501 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
2505 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002506 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 */
2508 start_leader = *arg;
2509 while (**arg == '!' || **arg == '-' || **arg == '+')
2510 *arg = skipwhite(*arg + 1);
2511 end_leader = *arg;
2512
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002513 if (**arg == '.' && (!isdigit(*(*arg + 1))
2514#ifdef FEAT_FLOAT
2515 || current_sctx.sc_version < 2
2516#endif
2517 ))
2518 {
2519 semsg(_(e_invexpr2), *arg);
2520 ++*arg;
2521 return FAIL;
2522 }
2523
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 switch (**arg)
2525 {
2526 /*
2527 * Number constant.
2528 */
2529 case '0':
2530 case '1':
2531 case '2':
2532 case '3':
2533 case '4':
2534 case '5':
2535 case '6':
2536 case '7':
2537 case '8':
2538 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002539 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002540 {
2541#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002542 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002543 int get_float = FALSE;
2544
2545 /* We accept a float when the format matches
2546 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002547 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002548 * With script version 2 and later the leading digit can be
2549 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002550 * Don't look for a float after the "." operator, so that
2551 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002552 if (**arg == '.')
2553 p = *arg;
2554 else
2555 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002556 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002558 get_float = TRUE;
2559 p = skipdigits(p + 2);
2560 if (*p == 'e' || *p == 'E')
2561 {
2562 ++p;
2563 if (*p == '-' || *p == '+')
2564 ++p;
2565 if (!vim_isdigit(*p))
2566 get_float = FALSE;
2567 else
2568 p = skipdigits(p + 1);
2569 }
2570 if (ASCII_ISALPHA(*p) || *p == '.')
2571 get_float = FALSE;
2572 }
2573 if (get_float)
2574 {
2575 float_T f;
2576
2577 *arg += string2float(*arg, &f);
2578 if (evaluate)
2579 {
2580 rettv->v_type = VAR_FLOAT;
2581 rettv->vval.v_float = f;
2582 }
2583 }
2584 else
2585#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002586 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002587 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002588 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01002589 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002590
2591 // Blob constant: 0z0123456789abcdef
2592 if (evaluate)
2593 blob = blob_alloc();
2594 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2595 {
2596 if (!vim_isxdigit(bp[1]))
2597 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002598 if (blob != NULL)
2599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002600 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002601 ga_clear(&blob->bv_ga);
2602 VIM_CLEAR(blob);
2603 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002604 ret = FAIL;
2605 break;
2606 }
2607 if (blob != NULL)
2608 ga_append(&blob->bv_ga,
2609 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01002610 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2611 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002612 }
2613 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002614 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002615 *arg = bp;
2616 }
2617 else
2618 {
2619 // decimal, hex or octal number
Bram Moolenaar60a8de22019-09-15 14:33:22 +02002620 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
2621 ? STR2NR_NO_OCT + STR2NR_QUOTE
2622 : STR2NR_ALL, &n, NULL, 0, TRUE);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002623 if (len == 0)
2624 {
2625 semsg(_(e_invexpr2), *arg);
2626 ret = FAIL;
2627 break;
2628 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002629 *arg += len;
2630 if (evaluate)
2631 {
2632 rettv->v_type = VAR_NUMBER;
2633 rettv->vval.v_number = n;
2634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
2636 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
2639 /*
2640 * String constant: "string".
2641 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 break;
2644
2645 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002649 break;
2650
2651 /*
2652 * List: [expr, expr]
2653 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 break;
2656
2657 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002658 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002659 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002660 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002661 {
2662 ++*arg;
2663 ret = dict_get_tv(arg, rettv, evaluate, TRUE);
2664 }
2665 else
2666 ret = NOTDONE;
2667 break;
2668
2669 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002670 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002671 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002673 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2674 if (ret == NOTDONE)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002675 ret = dict_get_tv(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 break;
2677
2678 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002679 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002681 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 break;
2683
2684 /*
2685 * Environment variable: $VAR.
2686 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002687 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 break;
2689
2690 /*
2691 * Register contents: @r.
2692 */
2693 case '@': ++*arg;
2694 if (evaluate)
2695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002696 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002697 rettv->vval.v_string = get_reg_contents(**arg,
2698 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
2700 if (**arg != NUL)
2701 ++*arg;
2702 break;
2703
2704 /*
2705 * nested expression: (expression).
2706 */
2707 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002708 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (**arg == ')')
2710 ++*arg;
2711 else if (ret == OK)
2712 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002713 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002714 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 ret = FAIL;
2716 }
2717 break;
2718
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 break;
2721 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722
2723 if (ret == NOTDONE)
2724 {
2725 /*
2726 * Must be a variable or function name.
2727 * Can also be a curly-braces kind of name: {expr}.
2728 */
2729 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002730 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (alias != NULL)
2732 s = alias;
2733
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002734 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 ret = FAIL;
2736 else
2737 {
2738 if (**arg == '(') /* recursive! */
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002739 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002741 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002742 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002743 {
2744 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002745 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002746 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002748 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 }
2750
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 *arg = skipwhite(*arg);
2752
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002753 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002754 * expr(expr), expr->name(expr) */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002755 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002756 ret = handle_subscript(arg, rettv, evaluate, TRUE,
2757 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759 /*
2760 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2761 */
2762 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002763 ret = eval7_leader(rettv, start_leader, &end_leader);
2764 return ret;
2765}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002766
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002767/*
2768 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2769 * Adjusts "end_leaderp" until it is at "start_leader".
2770 */
2771 static int
2772eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2773{
2774 char_u *end_leader = *end_leaderp;
2775 int ret = OK;
2776 int error = FALSE;
2777 varnumber_T val = 0;
2778#ifdef FEAT_FLOAT
2779 float_T f = 0.0;
2780
2781 if (rettv->v_type == VAR_FLOAT)
2782 f = rettv->vval.v_float;
2783 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002784#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002785 val = tv_get_number_chk(rettv, &error);
2786 if (error)
2787 {
2788 clear_tv(rettv);
2789 ret = FAIL;
2790 }
2791 else
2792 {
2793 while (end_leader > start_leader)
2794 {
2795 --end_leader;
2796 if (*end_leader == '!')
2797 {
2798#ifdef FEAT_FLOAT
2799 if (rettv->v_type == VAR_FLOAT)
2800 f = !f;
2801 else
2802#endif
2803 val = !val;
2804 }
2805 else if (*end_leader == '-')
2806 {
2807#ifdef FEAT_FLOAT
2808 if (rettv->v_type == VAR_FLOAT)
2809 f = -f;
2810 else
2811#endif
2812 val = -val;
2813 }
2814 }
2815#ifdef FEAT_FLOAT
2816 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002818 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002819 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002821 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002822#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002823 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002824 clear_tv(rettv);
2825 rettv->v_type = VAR_NUMBER;
2826 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002829 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 return ret;
2831}
2832
2833/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002834 * Call the function referred to in "rettv".
2835 */
2836 static int
2837call_func_rettv(
2838 char_u **arg,
2839 typval_T *rettv,
2840 int evaluate,
2841 dict_T *selfdict,
2842 typval_T *basetv)
2843{
2844 partial_T *pt = NULL;
2845 funcexe_T funcexe;
2846 typval_T functv;
2847 char_u *s;
2848 int ret;
2849
2850 // need to copy the funcref so that we can clear rettv
2851 if (evaluate)
2852 {
2853 functv = *rettv;
2854 rettv->v_type = VAR_UNKNOWN;
2855
2856 /* Invoke the function. Recursive! */
2857 if (functv.v_type == VAR_PARTIAL)
2858 {
2859 pt = functv.vval.v_partial;
2860 s = partial_name(pt);
2861 }
2862 else
2863 s = functv.vval.v_string;
2864 }
2865 else
2866 s = (char_u *)"";
2867
2868 vim_memset(&funcexe, 0, sizeof(funcexe));
2869 funcexe.firstline = curwin->w_cursor.lnum;
2870 funcexe.lastline = curwin->w_cursor.lnum;
2871 funcexe.evaluate = evaluate;
2872 funcexe.partial = pt;
2873 funcexe.selfdict = selfdict;
2874 funcexe.basetv = basetv;
2875 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2876
2877 /* Clear the funcref afterwards, so that deleting it while
2878 * evaluating the arguments is possible (see test55). */
2879 if (evaluate)
2880 clear_tv(&functv);
2881
2882 return ret;
2883}
2884
2885/*
2886 * Evaluate "->method()".
2887 * "*arg" points to the '-'.
2888 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2889 */
2890 static int
2891eval_lambda(
2892 char_u **arg,
2893 typval_T *rettv,
2894 int evaluate,
2895 int verbose) /* give error messages */
2896{
2897 typval_T base = *rettv;
2898 int ret;
2899
2900 // Skip over the ->.
2901 *arg += 2;
2902 rettv->v_type = VAR_UNKNOWN;
2903
2904 ret = get_lambda_tv(arg, rettv, evaluate);
2905 if (ret == NOTDONE)
2906 return FAIL;
2907 else if (**arg != '(')
2908 {
2909 if (verbose)
2910 {
2911 if (*skipwhite(*arg) == '(')
2912 semsg(_(e_nowhitespace));
2913 else
2914 semsg(_(e_missingparen), "lambda");
2915 }
2916 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002917 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002918 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002919 else
2920 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2921
2922 // Clear the funcref afterwards, so that deleting it while
2923 // evaluating the arguments is possible (see test55).
2924 if (evaluate)
2925 clear_tv(&base);
2926
2927 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002928}
2929
2930/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002931 * Evaluate "->method()".
2932 * "*arg" points to the '-'.
2933 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2934 */
2935 static int
2936eval_method(
2937 char_u **arg,
2938 typval_T *rettv,
2939 int evaluate,
2940 int verbose) /* give error messages */
2941{
2942 char_u *name;
2943 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002944 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002945 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002946 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002947
2948 // Skip over the ->.
2949 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002950 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002951
Bram Moolenaarac92e252019-08-03 21:58:38 +02002952 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002953 len = get_name_len(arg, &alias, evaluate, TRUE);
2954 if (alias != NULL)
2955 name = alias;
2956
2957 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002958 {
2959 if (verbose)
2960 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002961 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002962 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002963 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002964 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002965 if (**arg != '(')
2966 {
2967 if (verbose)
2968 semsg(_(e_missingparen), name);
2969 ret = FAIL;
2970 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002971 else if (VIM_ISWHITE((*arg)[-1]))
2972 {
2973 if (verbose)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002974 semsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002975 ret = FAIL;
2976 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002977 else
2978 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002979 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002980
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002981 // Clear the funcref afterwards, so that deleting it while
2982 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002983 if (evaluate)
2984 clear_tv(&base);
2985
Bram Moolenaarac92e252019-08-03 21:58:38 +02002986 return ret;
2987}
2988
2989/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002990 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2991 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002992 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2993 */
2994 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002995eval_index(
2996 char_u **arg,
2997 typval_T *rettv,
2998 int evaluate,
2999 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003000{
3001 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003002 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003003 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003004 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003005 long len = -1;
3006 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003007 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003008 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003009
Bram Moolenaara03f2332016-02-06 18:09:59 +01003010 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003011 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003012 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003013 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003014 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003015 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003016 return FAIL;
3017 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003018#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003019 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003020 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003021 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003022#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01003023 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003024 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003025 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003026 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003027 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003028 return FAIL;
3029 case VAR_UNKNOWN:
3030 if (evaluate)
3031 return FAIL;
3032 /* FALLTHROUGH */
3033
3034 case VAR_STRING:
3035 case VAR_NUMBER:
3036 case VAR_LIST:
3037 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003038 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003039 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003040 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003041
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003042 init_tv(&var1);
3043 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003044 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003045 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003046 /*
3047 * dict.name
3048 */
3049 key = *arg + 1;
3050 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3051 ;
3052 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003053 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003054 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003055 }
3056 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003057 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003058 /*
3059 * something[idx]
3060 *
3061 * Get the (first) variable from inside the [].
3062 */
3063 *arg = skipwhite(*arg + 1);
3064 if (**arg == ':')
3065 empty1 = TRUE;
3066 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3067 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003068 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003069 {
3070 /* not a number or string */
3071 clear_tv(&var1);
3072 return FAIL;
3073 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003074
3075 /*
3076 * Get the second variable from inside the [:].
3077 */
3078 if (**arg == ':')
3079 {
3080 range = TRUE;
3081 *arg = skipwhite(*arg + 1);
3082 if (**arg == ']')
3083 empty2 = TRUE;
3084 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003086 if (!empty1)
3087 clear_tv(&var1);
3088 return FAIL;
3089 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003090 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003091 {
3092 /* not a number or string */
3093 if (!empty1)
3094 clear_tv(&var1);
3095 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003096 return FAIL;
3097 }
3098 }
3099
3100 /* Check for the ']'. */
3101 if (**arg != ']')
3102 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003103 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003104 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003105 clear_tv(&var1);
3106 if (range)
3107 clear_tv(&var2);
3108 return FAIL;
3109 }
3110 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003111 }
3112
3113 if (evaluate)
3114 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003115 n1 = 0;
3116 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003117 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003118 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003119 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003120 }
3121 if (range)
3122 {
3123 if (empty2)
3124 n2 = -1;
3125 else
3126 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003127 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003128 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003129 }
3130 }
3131
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003132 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003133 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003134 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003135 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003136 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003137 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003138 case VAR_SPECIAL:
3139 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003140 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003141 break; /* not evaluating, skipping over subscript */
3142
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003143 case VAR_NUMBER:
3144 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003145 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003146 len = (long)STRLEN(s);
3147 if (range)
3148 {
3149 /* The resulting variable is a substring. If the indexes
3150 * are out of range the result is empty. */
3151 if (n1 < 0)
3152 {
3153 n1 = len + n1;
3154 if (n1 < 0)
3155 n1 = 0;
3156 }
3157 if (n2 < 0)
3158 n2 = len + n2;
3159 else if (n2 >= len)
3160 n2 = len;
3161 if (n1 >= len || n2 < 0 || n1 > n2)
3162 s = NULL;
3163 else
3164 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3165 }
3166 else
3167 {
3168 /* The resulting variable is a string of a single
3169 * character. If the index is too big or negative the
3170 * result is empty. */
3171 if (n1 >= len || n1 < 0)
3172 s = NULL;
3173 else
3174 s = vim_strnsave(s + n1, 1);
3175 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003176 clear_tv(rettv);
3177 rettv->v_type = VAR_STRING;
3178 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003179 break;
3180
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003181 case VAR_BLOB:
3182 len = blob_len(rettv->vval.v_blob);
3183 if (range)
3184 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003185 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003186 // are out of range the result is empty.
3187 if (n1 < 0)
3188 {
3189 n1 = len + n1;
3190 if (n1 < 0)
3191 n1 = 0;
3192 }
3193 if (n2 < 0)
3194 n2 = len + n2;
3195 else if (n2 >= len)
3196 n2 = len - 1;
3197 if (n1 >= len || n2 < 0 || n1 > n2)
3198 {
3199 clear_tv(rettv);
3200 rettv->v_type = VAR_BLOB;
3201 rettv->vval.v_blob = NULL;
3202 }
3203 else
3204 {
3205 blob_T *blob = blob_alloc();
3206
3207 if (blob != NULL)
3208 {
3209 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3210 {
3211 blob_free(blob);
3212 return FAIL;
3213 }
3214 blob->bv_ga.ga_len = n2 - n1 + 1;
3215 for (i = n1; i <= n2; i++)
3216 blob_set(blob, i - n1,
3217 blob_get(rettv->vval.v_blob, i));
3218
3219 clear_tv(rettv);
3220 rettv_blob_set(rettv, blob);
3221 }
3222 }
3223 }
3224 else
3225 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003226 // The resulting variable is a byte value.
3227 // If the index is too big or negative that is an error.
3228 if (n1 < 0)
3229 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003230 if (n1 < len && n1 >= 0)
3231 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003232 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003233
3234 clear_tv(rettv);
3235 rettv->v_type = VAR_NUMBER;
3236 rettv->vval.v_number = v;
3237 }
3238 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003239 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003240 }
3241 break;
3242
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003243 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003244 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003245 if (n1 < 0)
3246 n1 = len + n1;
3247 if (!empty1 && (n1 < 0 || n1 >= len))
3248 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003249 /* For a range we allow invalid values and return an empty
3250 * list. A list index out of range is an error. */
3251 if (!range)
3252 {
3253 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003254 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003255 return FAIL;
3256 }
3257 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003258 }
3259 if (range)
3260 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 list_T *l;
3262 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003263
3264 if (n2 < 0)
3265 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003266 else if (n2 >= len)
3267 n2 = len - 1;
3268 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003269 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003270 l = list_alloc();
3271 if (l == NULL)
3272 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003273 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003274 n1 <= n2; ++n1)
3275 {
3276 if (list_append_tv(l, &item->li_tv) == FAIL)
3277 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003278 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003279 return FAIL;
3280 }
3281 item = item->li_next;
3282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003283 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003284 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003285 }
3286 else
3287 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003288 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003289 clear_tv(rettv);
3290 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003291 }
3292 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003293
3294 case VAR_DICT:
3295 if (range)
3296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003297 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003298 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003299 if (len == -1)
3300 clear_tv(&var1);
3301 return FAIL;
3302 }
3303 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003304 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003305
3306 if (len == -1)
3307 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003308 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003309 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003310 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003311 clear_tv(&var1);
3312 return FAIL;
3313 }
3314 }
3315
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003316 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003317
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003318 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003319 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003320 if (len == -1)
3321 clear_tv(&var1);
3322 if (item == NULL)
3323 return FAIL;
3324
3325 copy_tv(&item->di_tv, &var1);
3326 clear_tv(rettv);
3327 *rettv = var1;
3328 }
3329 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003330 }
3331 }
3332
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003333 return OK;
3334}
3335
3336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 * Get an option value.
3338 * "arg" points to the '&' or '+' before the option name.
3339 * "arg" is advanced to character after the option name.
3340 * Return OK or FAIL.
3341 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003343get_option_tv(
3344 char_u **arg,
3345 typval_T *rettv, /* when NULL, only check if option exists */
3346 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 char_u *option_end;
3349 long numval;
3350 char_u *stringval;
3351 int opt_type;
3352 int c;
3353 int working = (**arg == '+'); /* has("+option") */
3354 int ret = OK;
3355 int opt_flags;
3356
3357 /*
3358 * Isolate the option name and find its value.
3359 */
3360 option_end = find_option_end(arg, &opt_flags);
3361 if (option_end == NULL)
3362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003363 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003364 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 return FAIL;
3366 }
3367
3368 if (!evaluate)
3369 {
3370 *arg = option_end;
3371 return OK;
3372 }
3373
3374 c = *option_end;
3375 *option_end = NUL;
3376 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003377 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
3379 if (opt_type == -3) /* invalid name */
3380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003381 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003382 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 ret = FAIL;
3384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003385 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
3387 if (opt_type == -2) /* hidden string option */
3388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003389 rettv->v_type = VAR_STRING;
3390 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
3392 else if (opt_type == -1) /* hidden number option */
3393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003394 rettv->v_type = VAR_NUMBER;
3395 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
3397 else if (opt_type == 1) /* number option */
3398 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003399 rettv->v_type = VAR_NUMBER;
3400 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402 else /* string option */
3403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003404 rettv->v_type = VAR_STRING;
3405 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
3407 }
3408 else if (working && (opt_type == -2 || opt_type == -1))
3409 ret = FAIL;
3410
3411 *option_end = c; /* put back for error messages */
3412 *arg = option_end;
3413
3414 return ret;
3415}
3416
3417/*
3418 * Allocate a variable for a string constant.
3419 * Return OK or FAIL.
3420 */
3421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003422get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
3424 char_u *p;
3425 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 int extra = 0;
3427
3428 /*
3429 * Find the end of the string, skipping backslashed characters.
3430 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003431 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 {
3433 if (*p == '\\' && p[1] != NUL)
3434 {
3435 ++p;
3436 /* A "\<x>" form occupies at least 4 characters, and produces up
3437 * to 6 characters: reserve space for 2 extra */
3438 if (*p == '<')
3439 extra += 2;
3440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
3442
3443 if (*p != '"')
3444 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003445 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 return FAIL;
3447 }
3448
3449 /* If only parsing, set *arg and return here */
3450 if (!evaluate)
3451 {
3452 *arg = p + 1;
3453 return OK;
3454 }
3455
3456 /*
3457 * Copy the string into allocated memory, handling backslashed
3458 * characters.
3459 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003460 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (name == NULL)
3462 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003463 rettv->v_type = VAR_STRING;
3464 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
Bram Moolenaar8c711452005-01-14 21:53:12 +00003466 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
3468 if (*p == '\\')
3469 {
3470 switch (*++p)
3471 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003472 case 'b': *name++ = BS; ++p; break;
3473 case 'e': *name++ = ESC; ++p; break;
3474 case 'f': *name++ = FF; ++p; break;
3475 case 'n': *name++ = NL; ++p; break;
3476 case 'r': *name++ = CAR; ++p; break;
3477 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
3479 case 'X': /* hex: "\x1", "\x12" */
3480 case 'x':
3481 case 'u': /* Unicode: "\u0023" */
3482 case 'U':
3483 if (vim_isxdigit(p[1]))
3484 {
3485 int n, nr;
3486 int c = toupper(*p);
3487
3488 if (c == 'X')
3489 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003490 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003492 else
3493 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 nr = 0;
3495 while (--n >= 0 && vim_isxdigit(p[1]))
3496 {
3497 ++p;
3498 nr = (nr << 4) + hex2nr(*p);
3499 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003500 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 /* For "\u" store the number according to
3502 * 'encoding'. */
3503 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003504 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003506 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 break;
3509
3510 /* octal: "\1", "\12", "\123" */
3511 case '0':
3512 case '1':
3513 case '2':
3514 case '3':
3515 case '4':
3516 case '5':
3517 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003518 case '7': *name = *p++ - '0';
3519 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003521 *name = (*name << 3) + *p++ - '0';
3522 if (*p >= '0' && *p <= '7')
3523 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003525 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 break;
3527
3528 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02003529 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 if (extra != 0)
3531 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003532 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 break;
3534 }
3535 /* FALLTHROUGH */
3536
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 break;
3539 }
3540 }
3541 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003542 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003545 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02003546 if (*p != NUL) /* just in case */
3547 ++p;
3548 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 return OK;
3551}
3552
3553/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003554 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 * Return OK or FAIL.
3556 */
3557 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003558get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559{
3560 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003561 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003562 int reduce = 0;
3563
3564 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003565 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003566 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003567 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003568 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003569 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003570 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003571 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003572 break;
3573 ++reduce;
3574 ++p;
3575 }
3576 }
3577
Bram Moolenaar8c711452005-01-14 21:53:12 +00003578 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003580 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003581 return FAIL;
3582 }
3583
Bram Moolenaar8c711452005-01-14 21:53:12 +00003584 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003585 if (!evaluate)
3586 {
3587 *arg = p + 1;
3588 return OK;
3589 }
3590
3591 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003592 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003593 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003594 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003595 if (str == NULL)
3596 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003597 rettv->v_type = VAR_STRING;
3598 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003599
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003601 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003603 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003605 break;
3606 ++p;
3607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003608 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003609 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003611 *arg = p + 1;
3612
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003613 return OK;
3614}
3615
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003616/*
3617 * Return the function name of the partial.
3618 */
3619 char_u *
3620partial_name(partial_T *pt)
3621{
3622 if (pt->pt_name != NULL)
3623 return pt->pt_name;
3624 return pt->pt_func->uf_name;
3625}
3626
Bram Moolenaarddecc252016-04-06 22:59:37 +02003627 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003628partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003629{
3630 int i;
3631
3632 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003633 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003634 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003635 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003636 if (pt->pt_name != NULL)
3637 {
3638 func_unref(pt->pt_name);
3639 vim_free(pt->pt_name);
3640 }
3641 else
3642 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003643 vim_free(pt);
3644}
3645
3646/*
3647 * Unreference a closure: decrement the reference count and free it when it
3648 * becomes zero.
3649 */
3650 void
3651partial_unref(partial_T *pt)
3652{
3653 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003654 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003655}
3656
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003657static int tv_equal_recurse_limit;
3658
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003659 static int
3660func_equal(
3661 typval_T *tv1,
3662 typval_T *tv2,
3663 int ic) /* ignore case */
3664{
3665 char_u *s1, *s2;
3666 dict_T *d1, *d2;
3667 int a1, a2;
3668 int i;
3669
3670 /* empty and NULL function name considered the same */
3671 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003672 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003673 if (s1 != NULL && *s1 == NUL)
3674 s1 = NULL;
3675 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003676 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003677 if (s2 != NULL && *s2 == NUL)
3678 s2 = NULL;
3679 if (s1 == NULL || s2 == NULL)
3680 {
3681 if (s1 != s2)
3682 return FALSE;
3683 }
3684 else if (STRCMP(s1, s2) != 0)
3685 return FALSE;
3686
3687 /* empty dict and NULL dict is different */
3688 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3689 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3690 if (d1 == NULL || d2 == NULL)
3691 {
3692 if (d1 != d2)
3693 return FALSE;
3694 }
3695 else if (!dict_equal(d1, d2, ic, TRUE))
3696 return FALSE;
3697
3698 /* empty list and no list considered the same */
3699 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3700 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3701 if (a1 != a2)
3702 return FALSE;
3703 for (i = 0; i < a1; ++i)
3704 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3705 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3706 return FALSE;
3707
3708 return TRUE;
3709}
3710
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003711/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003712 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003713 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003714 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003715 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003716 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003717tv_equal(
3718 typval_T *tv1,
3719 typval_T *tv2,
3720 int ic, /* ignore case */
3721 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003722{
3723 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003724 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003725 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003726 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003727
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003728 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003729 * recursiveness to a limit. We guess they are equal then.
3730 * A fixed limit has the problem of still taking an awful long time.
3731 * Reduce the limit every time running into it. That should work fine for
3732 * deeply linked structures that are not recursively linked and catch
3733 * recursiveness quickly. */
3734 if (!recursive)
3735 tv_equal_recurse_limit = 1000;
3736 if (recursive_cnt >= tv_equal_recurse_limit)
3737 {
3738 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003739 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003740 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003741
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02003742 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3743 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003744 if ((tv1->v_type == VAR_FUNC
3745 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3746 && (tv2->v_type == VAR_FUNC
3747 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3748 {
3749 ++recursive_cnt;
3750 r = func_equal(tv1, tv2, ic);
3751 --recursive_cnt;
3752 return r;
3753 }
3754
3755 if (tv1->v_type != tv2->v_type)
3756 return FALSE;
3757
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003758 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003759 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003760 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003761 ++recursive_cnt;
3762 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3763 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003764 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003765
3766 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003767 ++recursive_cnt;
3768 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3769 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003770 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003771
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003772 case VAR_BLOB:
3773 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3774
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003775 case VAR_NUMBER:
3776 return tv1->vval.v_number == tv2->vval.v_number;
3777
3778 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003779 s1 = tv_get_string_buf(tv1, buf1);
3780 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003781 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003782
3783 case VAR_SPECIAL:
3784 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01003785
3786 case VAR_FLOAT:
3787#ifdef FEAT_FLOAT
3788 return tv1->vval.v_float == tv2->vval.v_float;
3789#endif
3790 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003791#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003792 return tv1->vval.v_job == tv2->vval.v_job;
3793#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003794 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003795#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003796 return tv1->vval.v_channel == tv2->vval.v_channel;
3797#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003798 case VAR_FUNC:
3799 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003800 case VAR_UNKNOWN:
3801 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003802 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003803
Bram Moolenaara03f2332016-02-06 18:09:59 +01003804 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
3805 * does not equal anything, not even itself. */
3806 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003807}
3808
3809/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003810 * Return the next (unique) copy ID.
3811 * Used for serializing nested structures.
3812 */
3813 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003814get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003815{
3816 current_copyID += COPYID_INC;
3817 return current_copyID;
3818}
3819
3820/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003821 * Garbage collection for lists and dictionaries.
3822 *
3823 * We use reference counts to be able to free most items right away when they
3824 * are no longer used. But for composite items it's possible that it becomes
3825 * unused while the reference count is > 0: When there is a recursive
3826 * reference. Example:
3827 * :let l = [1, 2, 3]
3828 * :let d = {9: l}
3829 * :let l[1] = d
3830 *
3831 * Since this is quite unusual we handle this with garbage collection: every
3832 * once in a while find out which lists and dicts are not referenced from any
3833 * variable.
3834 *
3835 * Here is a good reference text about garbage collection (refers to Python
3836 * but it applies to all reference-counting mechanisms):
3837 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003838 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003839
3840/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003841 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003842 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003843 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003844 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003845 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003846garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003847{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003848 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003849 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003850 buf_T *buf;
3851 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003852 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003853 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003854
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003855 if (!testing)
3856 {
3857 /* Only do this once. */
3858 want_garbage_collect = FALSE;
3859 may_garbage_collect = FALSE;
3860 garbage_collect_at_exit = FALSE;
3861 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003862
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003863 /* We advance by two because we add one for items referenced through
3864 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003865 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003866
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003867 /*
3868 * 1. Go through all accessible variables and mark all lists and dicts
3869 * with copyID.
3870 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003871
3872 /* Don't free variables in the previous_funccal list unless they are only
3873 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00003874 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003875 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003876
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003877 /* script-local variables */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003878 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003879
3880 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02003881 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003882 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3883 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003884
3885 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003886 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003887 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3888 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003889 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003890 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3891 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003892#ifdef FEAT_TEXT_PROP
3893 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
3894 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3895 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003896 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02003897 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003898 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3899 NULL, NULL);
3900#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003901
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003902 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02003903 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003904 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3905 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003906 /* global variables */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003907 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003908
3909 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003910 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003911
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003912 /* named functions (matters for closures) */
3913 abort = abort || set_ref_in_functions(copyID);
3914
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003915 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003916 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003917
Bram Moolenaard812df62008-11-09 12:46:09 +00003918 /* v: vars */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003919 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003920
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003921 // callbacks in buffers
3922 abort = abort || set_ref_in_buffers(copyID);
3923
Bram Moolenaar1dced572012-04-05 16:54:08 +02003924#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003925 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003926#endif
3927
Bram Moolenaardb913952012-06-29 12:54:53 +02003928#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003929 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003930#endif
3931
3932#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003933 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003934#endif
3935
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003936#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003937 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003938 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003939#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003940#ifdef FEAT_NETBEANS_INTG
3941 abort = abort || set_ref_in_nb_channel(copyID);
3942#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003943
Bram Moolenaare3188e22016-05-31 21:13:04 +02003944#ifdef FEAT_TIMERS
3945 abort = abort || set_ref_in_timer(copyID);
3946#endif
3947
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003948#ifdef FEAT_QUICKFIX
3949 abort = abort || set_ref_in_quickfix(copyID);
3950#endif
3951
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003952#ifdef FEAT_TERMINAL
3953 abort = abort || set_ref_in_term(copyID);
3954#endif
3955
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003956#ifdef FEAT_TEXT_PROP
3957 abort = abort || set_ref_in_popups(copyID);
3958#endif
3959
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003960 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003961 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003962 /*
3963 * 2. Free lists and dictionaries that are not referenced.
3964 */
3965 did_free = free_unref_items(copyID);
3966
3967 /*
3968 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003969 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003970 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003971 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003972 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003973 else if (p_verbose > 0)
3974 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003975 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003976 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003977
3978 return did_free;
3979}
3980
3981/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003982 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003983 */
3984 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003985free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003986{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003987 int did_free = FALSE;
3988
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003989 /* Let all "free" functions know that we are here. This means no
3990 * dictionaries, lists, channels or jobs are to be freed, because we will
3991 * do that here. */
3992 in_free_unref_items = TRUE;
3993
3994 /*
3995 * PASS 1: free the contents of the items. We don't free the items
3996 * themselves yet, so that it is possible to decrement refcount counters
3997 */
3998
Bram Moolenaarcd524592016-07-17 14:57:05 +02003999 /* Go through the list of dicts and free items without the copyID. */
4000 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004001
Bram Moolenaarda861d62016-07-17 15:46:27 +02004002 /* Go through the list of lists and free items without the copyID. */
4003 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004004
4005#ifdef FEAT_JOB_CHANNEL
4006 /* Go through the list of jobs and free items without the copyID. This
4007 * must happen before doing channels, because jobs refer to channels, but
4008 * the reference from the channel to the job isn't tracked. */
4009 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4010
4011 /* Go through the list of channels and free items without the copyID. */
4012 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4013#endif
4014
4015 /*
4016 * PASS 2: free the items themselves.
4017 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004018 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004019 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004020
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004021#ifdef FEAT_JOB_CHANNEL
4022 /* Go through the list of jobs and free items without the copyID. This
4023 * must happen before doing channels, because jobs refer to channels, but
4024 * the reference from the channel to the job isn't tracked. */
4025 free_unused_jobs(copyID, COPYID_MASK);
4026
4027 /* Go through the list of channels and free items without the copyID. */
4028 free_unused_channels(copyID, COPYID_MASK);
4029#endif
4030
4031 in_free_unref_items = FALSE;
4032
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004033 return did_free;
4034}
4035
4036/*
4037 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004038 * "list_stack" is used to add lists to be marked. Can be NULL.
4039 *
4040 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004041 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004042 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004043set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004044{
4045 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004046 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004047 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004048 hashtab_T *cur_ht;
4049 ht_stack_T *ht_stack = NULL;
4050 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004051
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004052 cur_ht = ht;
4053 for (;;)
4054 {
4055 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004056 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004057 /* Mark each item in the hashtab. If the item contains a hashtab
4058 * it is added to ht_stack, if it contains a list it is added to
4059 * list_stack. */
4060 todo = (int)cur_ht->ht_used;
4061 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4062 if (!HASHITEM_EMPTY(hi))
4063 {
4064 --todo;
4065 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4066 &ht_stack, list_stack);
4067 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004068 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004069
4070 if (ht_stack == NULL)
4071 break;
4072
4073 /* take an item from the stack */
4074 cur_ht = ht_stack->ht;
4075 tempitem = ht_stack;
4076 ht_stack = ht_stack->prev;
4077 free(tempitem);
4078 }
4079
4080 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004081}
4082
4083/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004084 * Mark a dict and its items with "copyID".
4085 * Returns TRUE if setting references failed somehow.
4086 */
4087 int
4088set_ref_in_dict(dict_T *d, int copyID)
4089{
4090 if (d != NULL && d->dv_copyID != copyID)
4091 {
4092 d->dv_copyID = copyID;
4093 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4094 }
4095 return FALSE;
4096}
4097
4098/*
4099 * Mark a list and its items with "copyID".
4100 * Returns TRUE if setting references failed somehow.
4101 */
4102 int
4103set_ref_in_list(list_T *ll, int copyID)
4104{
4105 if (ll != NULL && ll->lv_copyID != copyID)
4106 {
4107 ll->lv_copyID = copyID;
4108 return set_ref_in_list_items(ll, copyID, NULL);
4109 }
4110 return FALSE;
4111}
4112
4113/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004114 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004115 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4116 *
4117 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004118 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004119 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004120set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004121{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004122 listitem_T *li;
4123 int abort = FALSE;
4124 list_T *cur_l;
4125 list_stack_T *list_stack = NULL;
4126 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004127
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004128 cur_l = l;
4129 for (;;)
4130 {
4131 if (!abort)
4132 /* Mark each item in the list. If the item contains a hashtab
4133 * it is added to ht_stack, if it contains a list it is added to
4134 * list_stack. */
4135 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4136 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4137 ht_stack, &list_stack);
4138 if (list_stack == NULL)
4139 break;
4140
4141 /* take an item from the stack */
4142 cur_l = list_stack->list;
4143 tempitem = list_stack;
4144 list_stack = list_stack->prev;
4145 free(tempitem);
4146 }
4147
4148 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004149}
4150
4151/*
4152 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004153 * "list_stack" is used to add lists to be marked. Can be NULL.
4154 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4155 *
4156 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004157 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004158 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004159set_ref_in_item(
4160 typval_T *tv,
4161 int copyID,
4162 ht_stack_T **ht_stack,
4163 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004164{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004165 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004166
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004167 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004168 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004169 dict_T *dd = tv->vval.v_dict;
4170
Bram Moolenaara03f2332016-02-06 18:09:59 +01004171 if (dd != NULL && dd->dv_copyID != copyID)
4172 {
4173 /* Didn't see this dict yet. */
4174 dd->dv_copyID = copyID;
4175 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004176 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004177 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4178 }
4179 else
4180 {
4181 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4182 if (newitem == NULL)
4183 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004184 else
4185 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004186 newitem->ht = &dd->dv_hashtab;
4187 newitem->prev = *ht_stack;
4188 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004189 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004190 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004191 }
4192 }
4193 else if (tv->v_type == VAR_LIST)
4194 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004195 list_T *ll = tv->vval.v_list;
4196
Bram Moolenaara03f2332016-02-06 18:09:59 +01004197 if (ll != NULL && ll->lv_copyID != copyID)
4198 {
4199 /* Didn't see this list yet. */
4200 ll->lv_copyID = copyID;
4201 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004202 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004203 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004204 }
4205 else
4206 {
4207 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004208 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004209 if (newitem == NULL)
4210 abort = TRUE;
4211 else
4212 {
4213 newitem->list = ll;
4214 newitem->prev = *list_stack;
4215 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004216 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004217 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004218 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004219 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004220 else if (tv->v_type == VAR_FUNC)
4221 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004222 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004223 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004224 else if (tv->v_type == VAR_PARTIAL)
4225 {
4226 partial_T *pt = tv->vval.v_partial;
4227 int i;
4228
4229 /* A partial does not have a copyID, because it cannot contain itself.
4230 */
4231 if (pt != NULL)
4232 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004233 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004234
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004235 if (pt->pt_dict != NULL)
4236 {
4237 typval_T dtv;
4238
4239 dtv.v_type = VAR_DICT;
4240 dtv.vval.v_dict = pt->pt_dict;
4241 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4242 }
4243
4244 for (i = 0; i < pt->pt_argc; ++i)
4245 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4246 ht_stack, list_stack);
4247 }
4248 }
4249#ifdef FEAT_JOB_CHANNEL
4250 else if (tv->v_type == VAR_JOB)
4251 {
4252 job_T *job = tv->vval.v_job;
4253 typval_T dtv;
4254
4255 if (job != NULL && job->jv_copyID != copyID)
4256 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004257 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004258 if (job->jv_channel != NULL)
4259 {
4260 dtv.v_type = VAR_CHANNEL;
4261 dtv.vval.v_channel = job->jv_channel;
4262 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4263 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004264 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004265 {
4266 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004267 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004268 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4269 }
4270 }
4271 }
4272 else if (tv->v_type == VAR_CHANNEL)
4273 {
4274 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004275 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004276 typval_T dtv;
4277 jsonq_T *jq;
4278 cbq_T *cq;
4279
4280 if (ch != NULL && ch->ch_copyID != copyID)
4281 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004282 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004283 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004284 {
4285 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4286 jq = jq->jq_next)
4287 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4288 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4289 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004290 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004291 {
4292 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004293 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004294 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4295 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004296 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004297 {
4298 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004299 dtv.vval.v_partial =
4300 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004301 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4302 }
4303 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004304 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004305 {
4306 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004307 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004308 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4309 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004310 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004311 {
4312 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004313 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004314 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4315 }
4316 }
4317 }
4318#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004319 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004320}
4321
Bram Moolenaar8c711452005-01-14 21:53:12 +00004322/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004323 * Return a string with the string representation of a variable.
4324 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004325 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004326 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004327 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4328 * stings as "string()", otherwise does not put quotes around strings, as
4329 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004330 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4331 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004332 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004333 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004334 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004335echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004336 typval_T *tv,
4337 char_u **tofree,
4338 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004339 int copyID,
4340 int echo_style,
4341 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004342 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004343{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004344 static int recurse = 0;
4345 char_u *r = NULL;
4346
Bram Moolenaar33570922005-01-25 22:26:29 +00004347 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004348 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004349 if (!did_echo_string_emsg)
4350 {
4351 /* Only give this message once for a recursive call to avoid
4352 * flooding the user with errors. And stop iterating over lists
4353 * and dicts. */
4354 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004355 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004356 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004357 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004358 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004359 }
4360 ++recurse;
4361
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004362 switch (tv->v_type)
4363 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004364 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004365 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004366 {
4367 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004368 r = tv->vval.v_string;
4369 if (r == NULL)
4370 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004371 }
4372 else
4373 {
4374 *tofree = string_quote(tv->vval.v_string, FALSE);
4375 r = *tofree;
4376 }
4377 break;
4378
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004379 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004380 if (echo_style)
4381 {
4382 *tofree = NULL;
4383 r = tv->vval.v_string;
4384 }
4385 else
4386 {
4387 *tofree = string_quote(tv->vval.v_string, TRUE);
4388 r = *tofree;
4389 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004390 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004391
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004392 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004393 {
4394 partial_T *pt = tv->vval.v_partial;
4395 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004396 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004397 garray_T ga;
4398 int i;
4399 char_u *tf;
4400
4401 ga_init2(&ga, 1, 100);
4402 ga_concat(&ga, (char_u *)"function(");
4403 if (fname != NULL)
4404 {
4405 ga_concat(&ga, fname);
4406 vim_free(fname);
4407 }
4408 if (pt != NULL && pt->pt_argc > 0)
4409 {
4410 ga_concat(&ga, (char_u *)", [");
4411 for (i = 0; i < pt->pt_argc; ++i)
4412 {
4413 if (i > 0)
4414 ga_concat(&ga, (char_u *)", ");
4415 ga_concat(&ga,
4416 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4417 vim_free(tf);
4418 }
4419 ga_concat(&ga, (char_u *)"]");
4420 }
4421 if (pt != NULL && pt->pt_dict != NULL)
4422 {
4423 typval_T dtv;
4424
4425 ga_concat(&ga, (char_u *)", ");
4426 dtv.v_type = VAR_DICT;
4427 dtv.vval.v_dict = pt->pt_dict;
4428 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4429 vim_free(tf);
4430 }
4431 ga_concat(&ga, (char_u *)")");
4432
4433 *tofree = ga.ga_data;
4434 r = *tofree;
4435 break;
4436 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004437
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004438 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004439 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004440 break;
4441
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004442 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004443 if (tv->vval.v_list == NULL)
4444 {
4445 *tofree = NULL;
4446 r = NULL;
4447 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004448 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4449 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004450 {
4451 *tofree = NULL;
4452 r = (char_u *)"[...]";
4453 }
4454 else
4455 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004456 int old_copyID = tv->vval.v_list->lv_copyID;
4457
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004458 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004459 *tofree = list2string(tv, copyID, restore_copyID);
4460 if (restore_copyID)
4461 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004462 r = *tofree;
4463 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004464 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004465
Bram Moolenaar8c711452005-01-14 21:53:12 +00004466 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004467 if (tv->vval.v_dict == NULL)
4468 {
4469 *tofree = NULL;
4470 r = NULL;
4471 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004472 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4473 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004474 {
4475 *tofree = NULL;
4476 r = (char_u *)"{...}";
4477 }
4478 else
4479 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004480 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004481 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004482 *tofree = dict2string(tv, copyID, restore_copyID);
4483 if (restore_copyID)
4484 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004485 r = *tofree;
4486 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004487 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004488
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004489 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004490 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004491 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004492 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004493 break;
4494
Bram Moolenaar835dc632016-02-07 14:27:38 +01004495 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004496 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004497 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004498 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004499 if (composite_val)
4500 {
4501 *tofree = string_quote(r, FALSE);
4502 r = *tofree;
4503 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004504 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004505
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004506 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004507#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004508 *tofree = NULL;
4509 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4510 r = numbuf;
4511 break;
4512#endif
4513
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004514 case VAR_SPECIAL:
4515 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004516 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004517 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004518 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004519
Bram Moolenaar8502c702014-06-17 12:51:16 +02004520 if (--recurse == 0)
4521 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004522 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004523}
4524
4525/*
4526 * Return a string with the string representation of a variable.
4527 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4528 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004529 * Does not put quotes around strings, as ":echo" displays values.
4530 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4531 * May return NULL.
4532 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004533 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004534echo_string(
4535 typval_T *tv,
4536 char_u **tofree,
4537 char_u *numbuf,
4538 int copyID)
4539{
4540 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4541}
4542
4543/*
4544 * Return a string with the string representation of a variable.
4545 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4546 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004547 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004548 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004549 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004550 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004551tv2string(
4552 typval_T *tv,
4553 char_u **tofree,
4554 char_u *numbuf,
4555 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004556{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004557 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004558}
4559
4560/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004561 * Return string "str" in ' quotes, doubling ' characters.
4562 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004563 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004564 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004565 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004566string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004567{
Bram Moolenaar33570922005-01-25 22:26:29 +00004568 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004569 char_u *p, *r, *s;
4570
Bram Moolenaar33570922005-01-25 22:26:29 +00004571 len = (function ? 13 : 3);
4572 if (str != NULL)
4573 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004574 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004575 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004576 if (*p == '\'')
4577 ++len;
4578 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004579 s = r = alloc(len);
4580 if (r != NULL)
4581 {
4582 if (function)
4583 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004584 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004585 r += 10;
4586 }
4587 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004588 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004589 if (str != NULL)
4590 for (p = str; *p != NUL; )
4591 {
4592 if (*p == '\'')
4593 *r++ = '\'';
4594 MB_COPY_CHAR(p, r);
4595 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004596 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004597 if (function)
4598 *r++ = ')';
4599 *r++ = NUL;
4600 }
4601 return s;
4602}
4603
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004604#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004605/*
4606 * Convert the string "text" to a floating point number.
4607 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4608 * this always uses a decimal point.
4609 * Returns the length of the text that was consumed.
4610 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004611 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004612string2float(
4613 char_u *text,
4614 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004615{
4616 char *s = (char *)text;
4617 float_T f;
4618
Bram Moolenaar62473612017-01-08 19:25:40 +01004619 /* MS-Windows does not deal with "inf" and "nan" properly. */
4620 if (STRNICMP(text, "inf", 3) == 0)
4621 {
4622 *value = INFINITY;
4623 return 3;
4624 }
4625 if (STRNICMP(text, "-inf", 3) == 0)
4626 {
4627 *value = -INFINITY;
4628 return 4;
4629 }
4630 if (STRNICMP(text, "nan", 3) == 0)
4631 {
4632 *value = NAN;
4633 return 3;
4634 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004635 f = strtod(s, &s);
4636 *value = f;
4637 return (int)((char_u *)s - text);
4638}
4639#endif
4640
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 * Get the value of an environment variable.
4643 * "arg" is pointing to the '$'. It is advanced to after the name.
4644 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004645 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 */
4647 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004648get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649{
4650 char_u *string = NULL;
4651 int len;
4652 int cc;
4653 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004654 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
4656 ++*arg;
4657 name = *arg;
4658 len = get_env_len(arg);
4659 if (evaluate)
4660 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004661 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01004662 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004663
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004664 cc = name[len];
4665 name[len] = NUL;
4666 /* first try vim_getenv(), fast for normal environment vars */
4667 string = vim_getenv(name, &mustfree);
4668 if (string != NULL && *string != NUL)
4669 {
4670 if (!mustfree)
4671 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004673 else
4674 {
4675 if (mustfree)
4676 vim_free(string);
4677
4678 /* next try expanding things like $VIM and ${HOME} */
4679 string = expand_env_save(name - 1);
4680 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004681 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004682 }
4683 name[len] = cc;
4684
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004685 rettv->v_type = VAR_STRING;
4686 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
4688
4689 return OK;
4690}
4691
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004694 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004696 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004697var2fpos(
4698 typval_T *varp,
4699 int dollar_lnum, /* TRUE when $ is last line */
4700 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004702 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004704 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
Bram Moolenaara5525202006-03-02 22:52:09 +00004706 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004707 if (varp->v_type == VAR_LIST)
4708 {
4709 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004710 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004711 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004712 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004713
4714 l = varp->vval.v_list;
4715 if (l == NULL)
4716 return NULL;
4717
4718 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00004719 pos.lnum = list_find_nr(l, 0L, &error);
4720 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004721 return NULL; /* invalid line number */
4722
4723 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00004724 pos.col = list_find_nr(l, 1L, &error);
4725 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004726 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004727 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004728
4729 /* We accept "$" for the column number: last column. */
4730 li = list_find(l, 1L);
4731 if (li != NULL && li->li_tv.v_type == VAR_STRING
4732 && li->li_tv.vval.v_string != NULL
4733 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4734 pos.col = len + 1;
4735
Bram Moolenaara5525202006-03-02 22:52:09 +00004736 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004737 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004738 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00004739 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004740
Bram Moolenaara5525202006-03-02 22:52:09 +00004741 /* Get the virtual offset. Defaults to zero. */
4742 pos.coladd = list_find_nr(l, 2L, &error);
4743 if (error)
4744 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004745
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004746 return &pos;
4747 }
4748
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004749 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004750 if (name == NULL)
4751 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004752 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004754 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
4755 {
4756 if (VIsual_active)
4757 return &VIsual;
4758 return &curwin->w_cursor;
4759 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004760 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004762 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4764 return NULL;
4765 return pp;
4766 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004767
Bram Moolenaara5525202006-03-02 22:52:09 +00004768 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004769
Bram Moolenaar477933c2007-07-17 14:32:23 +00004770 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004771 {
4772 pos.col = 0;
4773 if (name[1] == '0') /* "w0": first visible line */
4774 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004775 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004776 /* In silent Ex mode topline is zero, but that's not a valid line
4777 * number; use one instead. */
4778 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004779 return &pos;
4780 }
4781 else if (name[1] == '$') /* "w$": last visible line */
4782 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004783 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004784 /* In silent Ex mode botline is zero, return zero then. */
4785 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004786 return &pos;
4787 }
4788 }
4789 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004791 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
4793 pos.lnum = curbuf->b_ml.ml_line_count;
4794 pos.col = 0;
4795 }
4796 else
4797 {
4798 pos.lnum = curwin->w_cursor.lnum;
4799 pos.col = (colnr_T)STRLEN(ml_get_curline());
4800 }
4801 return &pos;
4802 }
4803 return NULL;
4804}
4805
4806/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004807 * Convert list in "arg" into a position and optional file number.
4808 * When "fnump" is NULL there is no file number, only 3 items.
4809 * Note that the column is passed on as-is, the caller may want to decrement
4810 * it to use 1 for the first column.
4811 * Return FAIL when conversion is not possible, doesn't check the position for
4812 * validity.
4813 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004814 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004815list2fpos(
4816 typval_T *arg,
4817 pos_T *posp,
4818 int *fnump,
4819 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004820{
4821 list_T *l = arg->vval.v_list;
4822 long i = 0;
4823 long n;
4824
Bram Moolenaar493c1782014-05-28 14:34:46 +02004825 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4826 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00004827 if (arg->v_type != VAR_LIST
4828 || l == NULL
4829 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004830 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004831 return FAIL;
4832
4833 if (fnump != NULL)
4834 {
4835 n = list_find_nr(l, i++, NULL); /* fnum */
4836 if (n < 0)
4837 return FAIL;
4838 if (n == 0)
4839 n = curbuf->b_fnum; /* current buffer */
4840 *fnump = n;
4841 }
4842
4843 n = list_find_nr(l, i++, NULL); /* lnum */
4844 if (n < 0)
4845 return FAIL;
4846 posp->lnum = n;
4847
4848 n = list_find_nr(l, i++, NULL); /* col */
4849 if (n < 0)
4850 return FAIL;
4851 posp->col = n;
4852
Bram Moolenaar493c1782014-05-28 14:34:46 +02004853 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004854 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004855 posp->coladd = 0;
4856 else
4857 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004858
Bram Moolenaar493c1782014-05-28 14:34:46 +02004859 if (curswantp != NULL)
4860 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
4861
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004862 return OK;
4863}
4864
4865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 * Get the length of an environment variable name.
4867 * Advance "arg" to the first character after the name.
4868 * Return 0 for error.
4869 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004870 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004871get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872{
4873 char_u *p;
4874 int len;
4875
4876 for (p = *arg; vim_isIDc(*p); ++p)
4877 ;
4878 if (p == *arg) /* no name found */
4879 return 0;
4880
4881 len = (int)(p - *arg);
4882 *arg = p;
4883 return len;
4884}
4885
4886/*
4887 * Get the length of the name of a function or internal variable.
4888 * "arg" is advanced to the first non-white character after the name.
4889 * Return 0 if something is wrong.
4890 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004891 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004892get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893{
4894 char_u *p;
4895 int len;
4896
4897 /* Find the end of the name. */
4898 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004899 {
4900 if (*p == ':')
4901 {
4902 /* "s:" is start of "s:var", but "n:" is not and can be used in
4903 * slice "[n:]". Also "xx:" is not a namespace. */
4904 len = (int)(p - *arg);
4905 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4906 || len > 1)
4907 break;
4908 }
4909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 if (p == *arg) /* no name found */
4911 return 0;
4912
4913 len = (int)(p - *arg);
4914 *arg = skipwhite(p);
4915
4916 return len;
4917}
4918
4919/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004920 * Get the length of the name of a variable or function.
4921 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004923 * Return -1 if curly braces expansion failed.
4924 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 * If the name contains 'magic' {}'s, expand them and return the
4926 * expanded name in an allocated string via 'alias' - caller must free.
4927 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004928 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004929get_name_len(
4930 char_u **arg,
4931 char_u **alias,
4932 int evaluate,
4933 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934{
4935 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 char_u *p;
4937 char_u *expr_start;
4938 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 *alias = NULL; /* default to no alias */
4941
4942 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4943 && (*arg)[2] == (int)KE_SNR)
4944 {
4945 /* hard coded <SNR>, already translated */
4946 *arg += 3;
4947 return get_id_len(arg) + 3;
4948 }
4949 len = eval_fname_script(*arg);
4950 if (len > 0)
4951 {
4952 /* literal "<SID>", "s:" or "<SNR>" */
4953 *arg += len;
4954 }
4955
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004957 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004959 p = find_name_end(*arg, &expr_start, &expr_end,
4960 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 if (expr_start != NULL)
4962 {
4963 char_u *temp_string;
4964
4965 if (!evaluate)
4966 {
4967 len += (int)(p - *arg);
4968 *arg = skipwhite(p);
4969 return len;
4970 }
4971
4972 /*
4973 * Include any <SID> etc in the expanded string:
4974 * Thus the -len here.
4975 */
4976 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4977 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004978 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 *alias = temp_string;
4980 *arg = skipwhite(p);
4981 return (int)STRLEN(temp_string);
4982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
4984 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004985 // Only give an error when there is something, otherwise it will be
4986 // reported at a higher level.
4987 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004988 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989
4990 return len;
4991}
4992
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004993/*
4994 * Find the end of a variable or function name, taking care of magic braces.
4995 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4996 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004997 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 * Return a pointer to just after the name. Equal to "arg" if there is no
4999 * valid name.
5000 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005001 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005002find_name_end(
5003 char_u *arg,
5004 char_u **expr_start,
5005 char_u **expr_end,
5006 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005008 int mb_nest = 0;
5009 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005011 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005013 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005015 *expr_start = NULL;
5016 *expr_end = NULL;
5017 }
5018
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005019 /* Quick check for valid starting character. */
5020 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
5021 return arg;
5022
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005023 for (p = arg; *p != NUL
5024 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005025 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005026 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005027 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005028 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005029 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005030 if (*p == '\'')
5031 {
5032 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005033 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005034 ;
5035 if (*p == NUL)
5036 break;
5037 }
5038 else if (*p == '"')
5039 {
5040 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005041 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005042 if (*p == '\\' && p[1] != NUL)
5043 ++p;
5044 if (*p == NUL)
5045 break;
5046 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005047 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5048 {
5049 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005050 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005051 len = (int)(p - arg);
5052 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005053 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005054 break;
5055 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005056
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005057 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 if (*p == '[')
5060 ++br_nest;
5061 else if (*p == ']')
5062 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005064
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 if (*p == '{')
5068 {
5069 mb_nest++;
5070 if (expr_start != NULL && *expr_start == NULL)
5071 *expr_start = p;
5072 }
5073 else if (*p == '}')
5074 {
5075 mb_nest--;
5076 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5077 *expr_end = p;
5078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081
5082 return p;
5083}
5084
5085/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005086 * Expands out the 'magic' {}'s in a variable/function name.
5087 * Note that this can call itself recursively, to deal with
5088 * constructs like foo{bar}{baz}{bam}
5089 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5090 * "in_start" ^
5091 * "expr_start" ^
5092 * "expr_end" ^
5093 * "in_end" ^
5094 *
5095 * Returns a new allocated string, which the caller must free.
5096 * Returns NULL for failure.
5097 */
5098 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005099make_expanded_name(
5100 char_u *in_start,
5101 char_u *expr_start,
5102 char_u *expr_end,
5103 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005104{
5105 char_u c1;
5106 char_u *retval = NULL;
5107 char_u *temp_result;
5108 char_u *nextcmd = NULL;
5109
5110 if (expr_end == NULL || in_end == NULL)
5111 return NULL;
5112 *expr_start = NUL;
5113 *expr_end = NUL;
5114 c1 = *in_end;
5115 *in_end = NUL;
5116
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005117 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005118 if (temp_result != NULL && nextcmd == NULL)
5119 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005120 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5121 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005122 if (retval != NULL)
5123 {
5124 STRCPY(retval, in_start);
5125 STRCAT(retval, temp_result);
5126 STRCAT(retval, expr_end + 1);
5127 }
5128 }
5129 vim_free(temp_result);
5130
5131 *in_end = c1; /* put char back for error messages */
5132 *expr_start = '{';
5133 *expr_end = '}';
5134
5135 if (retval != NULL)
5136 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005137 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005138 if (expr_start != NULL)
5139 {
5140 /* Further expansion! */
5141 temp_result = make_expanded_name(retval, expr_start,
5142 expr_end, temp_result);
5143 vim_free(retval);
5144 retval = temp_result;
5145 }
5146 }
5147
5148 return retval;
5149}
5150
5151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005153 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005155 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005156eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005158 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5159}
5160
5161/*
5162 * Return TRUE if character "c" can be used as the first character in a
5163 * variable or function name (excluding '{' and '}').
5164 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005165 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005166eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005167{
5168 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169}
5170
5171/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005172 * Handle:
5173 * - expr[expr], expr[expr:expr] subscript
5174 * - ".name" lookup
5175 * - function call with Funcref variable: func(expr)
5176 * - method call: var->method()
5177 *
5178 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005179 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005180 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005181handle_subscript(
5182 char_u **arg,
5183 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005184 int evaluate, // do more than finding the end
5185 int verbose, // give error messages
5186 char_u *start_leader, // start of '!' and '-' prefixes
5187 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005188{
5189 int ret = OK;
5190 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005191
Bram Moolenaar61343f02019-07-20 21:11:13 +02005192 // "." is ".name" lookup when we found a dict or when evaluating and
5193 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005194 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005195 && (((**arg == '['
5196 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005197 || (!evaluate
5198 && (*arg)[1] != '.'
5199 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005200 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005201 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005202 && !VIM_ISWHITE(*(*arg - 1)))
5203 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005204 {
5205 if (**arg == '(')
5206 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005207 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005208
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005209 // Stop the expression evaluation when immediately aborting on
5210 // error, or when an interrupt occurred or an exception was thrown
5211 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005212 if (aborting())
5213 {
5214 if (ret == OK)
5215 clear_tv(rettv);
5216 ret = FAIL;
5217 }
5218 dict_unref(selfdict);
5219 selfdict = NULL;
5220 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005221 else if (**arg == '-')
5222 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005223 // Expression "-1.0->method()" applies the leader "-" before
5224 // applying ->.
5225 if (evaluate && *end_leaderp > start_leader)
5226 ret = eval7_leader(rettv, start_leader, end_leaderp);
5227 if (ret == OK)
5228 {
5229 if ((*arg)[2] == '{')
5230 // expr->{lambda}()
5231 ret = eval_lambda(arg, rettv, evaluate, verbose);
5232 else
5233 // expr->name()
5234 ret = eval_method(arg, rettv, evaluate, verbose);
5235 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005236 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005237 else /* **arg == '[' || **arg == '.' */
5238 {
5239 dict_unref(selfdict);
5240 if (rettv->v_type == VAR_DICT)
5241 {
5242 selfdict = rettv->vval.v_dict;
5243 if (selfdict != NULL)
5244 ++selfdict->dv_refcount;
5245 }
5246 else
5247 selfdict = NULL;
5248 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
5249 {
5250 clear_tv(rettv);
5251 ret = FAIL;
5252 }
5253 }
5254 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005255
Bram Moolenaar1d429612016-05-24 15:44:17 +02005256 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
5257 * Don't do this when "Func" is already a partial that was bound
5258 * explicitly (pt_auto is FALSE). */
5259 if (selfdict != NULL
5260 && (rettv->v_type == VAR_FUNC
5261 || (rettv->v_type == VAR_PARTIAL
5262 && (rettv->vval.v_partial->pt_auto
5263 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005264 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005265
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005266 dict_unref(selfdict);
5267 return ret;
5268}
5269
5270/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005271 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 * value).
5273 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005274 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005275alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005277 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278}
5279
5280/*
5281 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 * The string "s" must have been allocated, it is consumed.
5283 * Return NULL for out of memory, the variable otherwise.
5284 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005285 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005286alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
Bram Moolenaar33570922005-01-25 22:26:29 +00005288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005290 rettv = alloc_tv();
5291 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005293 rettv->v_type = VAR_STRING;
5294 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296 else
5297 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005298 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299}
5300
5301/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005305free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306{
5307 if (varp != NULL)
5308 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 switch (varp->v_type)
5310 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005312 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005313 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005314 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005315 vim_free(varp->vval.v_string);
5316 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005317 case VAR_PARTIAL:
5318 partial_unref(varp->vval.v_partial);
5319 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005320 case VAR_BLOB:
5321 blob_unref(varp->vval.v_blob);
5322 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005323 case VAR_LIST:
5324 list_unref(varp->vval.v_list);
5325 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005326 case VAR_DICT:
5327 dict_unref(varp->vval.v_dict);
5328 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005329 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005330#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005331 job_unref(varp->vval.v_job);
5332 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005333#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005334 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005335#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005336 channel_unref(varp->vval.v_channel);
5337 break;
5338#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005339 case VAR_NUMBER:
5340 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005341 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005342 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005343 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 vim_free(varp);
5346 }
5347}
5348
5349/*
5350 * Free the memory for a variable value and set the value to NULL or 0.
5351 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005352 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005353clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
5355 if (varp != NULL)
5356 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005360 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005361 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005362 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005363 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005365 case VAR_PARTIAL:
5366 partial_unref(varp->vval.v_partial);
5367 varp->vval.v_partial = NULL;
5368 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005369 case VAR_BLOB:
5370 blob_unref(varp->vval.v_blob);
5371 varp->vval.v_blob = NULL;
5372 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 case VAR_LIST:
5374 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005375 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005377 case VAR_DICT:
5378 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005379 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005380 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005381 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005382 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 varp->vval.v_number = 0;
5384 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005385 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005386#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005387 varp->vval.v_float = 0.0;
5388 break;
5389#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005390 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005391#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005392 job_unref(varp->vval.v_job);
5393 varp->vval.v_job = NULL;
5394#endif
5395 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005396 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005397#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005398 channel_unref(varp->vval.v_channel);
5399 varp->vval.v_channel = NULL;
5400#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005401 case VAR_UNKNOWN:
5402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005404 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 }
5406}
5407
5408/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005409 * Set the value of a variable to NULL without freeing items.
5410 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005411 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005412init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005413{
5414 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00005415 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005416}
5417
5418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 * Get the number value of a variable.
5420 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005421 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005422 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005423 * caller of incompatible types: it sets *denote to TRUE if "denote"
5424 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005426 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005427tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005429 int error = FALSE;
5430
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005431 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005432}
5433
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005434 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005435tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005436{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005437 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005442 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005443 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005444#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005445 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005446 break;
5447#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005449 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005450 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 break;
5452 case VAR_STRING:
5453 if (varp->vval.v_string != NULL)
5454 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005455 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005456 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005457 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005458 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005459 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005460 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005461 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005462 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005463 case VAR_SPECIAL:
5464 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
5465 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005466 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005467#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005468 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005469 break;
5470#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005471 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005472#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005473 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005474 break;
5475#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005476 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005477 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005478 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005479 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005480 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005483 if (denote == NULL) /* useful for values that must be unsigned */
5484 n = -1;
5485 else
5486 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488}
5489
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005490#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005491 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005492tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005493{
5494 switch (varp->v_type)
5495 {
5496 case VAR_NUMBER:
5497 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005498 case VAR_FLOAT:
5499 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005500 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005501 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005502 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005503 break;
5504 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005505 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005506 break;
5507 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005508 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005509 break;
5510 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005511 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005512 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005513 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005514 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005515 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005516 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005517# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005518 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005519 break;
5520# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005521 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005522# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005523 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005524 break;
5525# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005526 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005527 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005528 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005529 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005530 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005531 break;
5532 }
5533 return 0;
5534}
5535#endif
5536
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 * Get the string value of a variable.
5539 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005540 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5541 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 * If the String variable has never been set, return an empty string.
5543 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005544 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005545 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005547 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005548tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549{
5550 static char_u mybuf[NUMBUFLEN];
5551
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005552 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553}
5554
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005555 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005556tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005558 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005559
5560 return res != NULL ? res : (char_u *)"";
5561}
5562
Bram Moolenaar7d647822014-04-05 21:28:56 +02005563/*
5564 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5565 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005566 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005567tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005568{
5569 static char_u mybuf[NUMBUFLEN];
5570
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005571 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005572}
5573
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005574 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005575tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005576{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005580 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005581 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582 return buf;
5583 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005584 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005585 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005586 break;
5587 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005588 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589 break;
5590 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005591 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005592 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005593 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005594#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005595 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005596 break;
5597#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 case VAR_STRING:
5599 if (varp->vval.v_string != NULL)
5600 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005601 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005602 case VAR_SPECIAL:
5603 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5604 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005605 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005606 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005607 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005608 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005609#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005610 {
5611 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005612 char *status;
5613
5614 if (job == NULL)
5615 return (char_u *)"no process";
5616 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005617 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005618 : "run";
5619# ifdef UNIX
5620 vim_snprintf((char *)buf, NUMBUFLEN,
5621 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005622# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005623 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005624 "process %ld %s",
5625 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005626 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005627# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005628 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005629 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5630# endif
5631 return buf;
5632 }
5633#endif
5634 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005635 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005636#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005637 {
5638 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005639 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005640
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005641 if (channel == NULL)
5642 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5643 else
5644 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005645 "channel %d %s", channel->ch_id, status);
5646 return buf;
5647 }
5648#endif
5649 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005650 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005651 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005652 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005654 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655}
5656
5657/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005658 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5659 * string() on Dict, List, etc.
5660 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005661 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005662tv_stringify(typval_T *varp, char_u *buf)
5663{
5664 if (varp->v_type == VAR_LIST
5665 || varp->v_type == VAR_DICT
5666 || varp->v_type == VAR_FUNC
5667 || varp->v_type == VAR_PARTIAL
5668 || varp->v_type == VAR_FLOAT)
5669 {
5670 typval_T tmp;
5671
5672 f_string(varp, &tmp);
5673 tv_get_string_buf(&tmp, buf);
5674 clear_tv(varp);
5675 *varp = tmp;
5676 return tmp.vval.v_string;
5677 }
5678 return tv_get_string_buf(varp, buf);
5679}
5680
5681/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005682 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5683 * Also give an error message, using "name" or _("name") when use_gettext is
5684 * TRUE.
5685 */
5686 static int
5687tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5688{
5689 int lock = 0;
5690
5691 switch (tv->v_type)
5692 {
5693 case VAR_BLOB:
5694 if (tv->vval.v_blob != NULL)
5695 lock = tv->vval.v_blob->bv_lock;
5696 break;
5697 case VAR_LIST:
5698 if (tv->vval.v_list != NULL)
5699 lock = tv->vval.v_list->lv_lock;
5700 break;
5701 case VAR_DICT:
5702 if (tv->vval.v_dict != NULL)
5703 lock = tv->vval.v_dict->dv_lock;
5704 break;
5705 default:
5706 break;
5707 }
5708 return var_check_lock(tv->v_lock, name, use_gettext)
5709 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5710}
5711
5712/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005713 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005715 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005716 * It is OK for "from" and "to" to point to the same item. This is used to
5717 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005718 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005719 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005720copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005723 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724 switch (from->v_type)
5725 {
5726 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005727 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728 to->vval.v_number = from->vval.v_number;
5729 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005730 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005731#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005732 to->vval.v_float = from->vval.v_float;
5733 break;
5734#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005735 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005736#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005737 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005738 if (to->vval.v_job != NULL)
5739 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005740 break;
5741#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005742 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005743#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005744 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005745 if (to->vval.v_channel != NULL)
5746 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005747 break;
5748#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 case VAR_STRING:
5750 case VAR_FUNC:
5751 if (from->vval.v_string == NULL)
5752 to->vval.v_string = NULL;
5753 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005754 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005756 if (from->v_type == VAR_FUNC)
5757 func_ref(to->vval.v_string);
5758 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005760 case VAR_PARTIAL:
5761 if (from->vval.v_partial == NULL)
5762 to->vval.v_partial = NULL;
5763 else
5764 {
5765 to->vval.v_partial = from->vval.v_partial;
5766 ++to->vval.v_partial->pt_refcount;
5767 }
5768 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005769 case VAR_BLOB:
5770 if (from->vval.v_blob == NULL)
5771 to->vval.v_blob = NULL;
5772 else
5773 {
5774 to->vval.v_blob = from->vval.v_blob;
5775 ++to->vval.v_blob->bv_refcount;
5776 }
5777 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778 case VAR_LIST:
5779 if (from->vval.v_list == NULL)
5780 to->vval.v_list = NULL;
5781 else
5782 {
5783 to->vval.v_list = from->vval.v_list;
5784 ++to->vval.v_list->lv_refcount;
5785 }
5786 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 case VAR_DICT:
5788 if (from->vval.v_dict == NULL)
5789 to->vval.v_dict = NULL;
5790 else
5791 {
5792 to->vval.v_dict = from->vval.v_dict;
5793 ++to->vval.v_dict->dv_refcount;
5794 }
5795 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005796 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005797 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005798 break;
5799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800}
5801
5802/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005803 * Make a copy of an item.
5804 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005805 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5806 * reference to an already copied list/dict can be used.
5807 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005808 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005809 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005810item_copy(
5811 typval_T *from,
5812 typval_T *to,
5813 int deep,
5814 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005815{
5816 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005817 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005818
Bram Moolenaar33570922005-01-25 22:26:29 +00005819 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005820 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005821 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005822 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005823 }
5824 ++recurse;
5825
5826 switch (from->v_type)
5827 {
5828 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005829 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005830 case VAR_STRING:
5831 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005832 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005833 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005834 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005835 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005836 copy_tv(from, to);
5837 break;
5838 case VAR_LIST:
5839 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005840 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005841 if (from->vval.v_list == NULL)
5842 to->vval.v_list = NULL;
5843 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5844 {
5845 /* use the copy made earlier */
5846 to->vval.v_list = from->vval.v_list->lv_copylist;
5847 ++to->vval.v_list->lv_refcount;
5848 }
5849 else
5850 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5851 if (to->vval.v_list == NULL)
5852 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005853 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005854 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005855 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005856 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005857 case VAR_DICT:
5858 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005859 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005860 if (from->vval.v_dict == NULL)
5861 to->vval.v_dict = NULL;
5862 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5863 {
5864 /* use the copy made earlier */
5865 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5866 ++to->vval.v_dict->dv_refcount;
5867 }
5868 else
5869 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5870 if (to->vval.v_dict == NULL)
5871 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005872 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005873 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01005874 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005875 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005876 }
5877 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005878 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005879}
5880
5881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 * ":echo expr1 ..." print each argument separated with a space, add a
5883 * newline at the end.
5884 * ":echon expr1 ..." print each argument plain.
5885 */
5886 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005887ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888{
5889 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005890 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 char_u *p;
5893 int needclr = TRUE;
5894 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005895 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01005896 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005897 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
5899 if (eap->skip)
5900 ++emsg_skip;
5901 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
5902 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005903 /* If eval1() causes an error message the text from the command may
5904 * still need to be cleared. E.g., "echo 22,44". */
5905 need_clr_eos = needclr;
5906
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005908 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 {
5910 /*
5911 * Report the invalid expression unless the expression evaluation
5912 * has been cancelled due to an aborting error, an interrupt, or an
5913 * exception.
5914 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005915 if (!aborting() && did_emsg == did_emsg_before
5916 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005917 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005918 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 break;
5920 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005921 need_clr_eos = FALSE;
5922
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 if (!eap->skip)
5924 {
5925 if (atstart)
5926 {
5927 atstart = FALSE;
5928 /* Call msg_start() after eval1(), evaluating the expression
5929 * may cause a message to appear. */
5930 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01005931 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02005932 /* Mark the saved text as finishing the line, so that what
5933 * follows is displayed on a new line when scrolling back
5934 * at the more prompt. */
5935 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01005937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 }
5939 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005940 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005941 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005942 if (p != NULL)
5943 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005945 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005947 if (*p != TAB && needclr)
5948 {
5949 /* remove any text still there from the command */
5950 msg_clr_eos();
5951 needclr = FALSE;
5952 }
5953 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 }
5955 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005956 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005957 if (has_mbyte)
5958 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005959 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005960
5961 (void)msg_outtrans_len_attr(p, i, echo_attr);
5962 p += i - 1;
5963 }
5964 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005965 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005970 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 arg = skipwhite(arg);
5972 }
5973 eap->nextcmd = check_nextcmd(arg);
5974
5975 if (eap->skip)
5976 --emsg_skip;
5977 else
5978 {
5979 /* remove text that may still be there from the command */
5980 if (needclr)
5981 msg_clr_eos();
5982 if (eap->cmdidx == CMD_echo)
5983 msg_end();
5984 }
5985}
5986
5987/*
5988 * ":echohl {name}".
5989 */
5990 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005991ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005993 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994}
5995
5996/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005997 * Returns the :echo attribute
5998 */
5999 int
6000get_echo_attr(void)
6001{
6002 return echo_attr;
6003}
6004
6005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 * ":execute expr1 ..." execute the result of an expression.
6007 * ":echomsg expr1 ..." Print a message
6008 * ":echoerr expr1 ..." Print an error
6009 * Each gets spaces around each argument and a newline at the end for
6010 * echo commands
6011 */
6012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006013ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014{
6015 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006016 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 int ret = OK;
6018 char_u *p;
6019 garray_T ga;
6020 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006021 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022
6023 ga_init2(&ga, 1, 80);
6024
6025 if (eap->skip)
6026 ++emsg_skip;
6027 while (*arg != NUL && *arg != '|' && *arg != '\n')
6028 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006029 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6030 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032
6033 if (!eap->skip)
6034 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006035 char_u buf[NUMBUFLEN];
6036
6037 if (eap->cmdidx == CMD_execute)
6038 p = tv_get_string_buf(&rettv, buf);
6039 else
6040 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 len = (int)STRLEN(p);
6042 if (ga_grow(&ga, len + 2) == FAIL)
6043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006044 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 ret = FAIL;
6046 break;
6047 }
6048 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 ga.ga_len += len;
6052 }
6053
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006054 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 arg = skipwhite(arg);
6056 }
6057
6058 if (ret != FAIL && ga.ga_data != NULL)
6059 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006060 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6061 {
6062 /* Mark the already saved text as finishing the line, so that what
6063 * follows is displayed on a new line when scrolling back at the
6064 * more prompt. */
6065 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006066 }
6067
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006069 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006070 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006071 out_flush();
6072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 else if (eap->cmdidx == CMD_echoerr)
6074 {
6075 /* We don't want to abort following commands, restore did_emsg. */
6076 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006077 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 if (!force_abort)
6079 did_emsg = save_did_emsg;
6080 }
6081 else if (eap->cmdidx == CMD_execute)
6082 do_cmdline((char_u *)ga.ga_data,
6083 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6084 }
6085
6086 ga_clear(&ga);
6087
6088 if (eap->skip)
6089 --emsg_skip;
6090
6091 eap->nextcmd = check_nextcmd(arg);
6092}
6093
6094/*
6095 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6096 * "arg" points to the "&" or '+' when called, to "option" when returning.
6097 * Returns NULL when no option name found. Otherwise pointer to the char
6098 * after the option name.
6099 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006100 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006101find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
6103 char_u *p = *arg;
6104
6105 ++p;
6106 if (*p == 'g' && p[1] == ':')
6107 {
6108 *opt_flags = OPT_GLOBAL;
6109 p += 2;
6110 }
6111 else if (*p == 'l' && p[1] == ':')
6112 {
6113 *opt_flags = OPT_LOCAL;
6114 p += 2;
6115 }
6116 else
6117 *opt_flags = 0;
6118
6119 if (!ASCII_ISALPHA(*p))
6120 return NULL;
6121 *arg = p;
6122
6123 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
6124 p += 4; /* termcap option */
6125 else
6126 while (ASCII_ISALPHA(*p))
6127 ++p;
6128 return p;
6129}
6130
6131/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006132 * Display script name where an item was last set.
6133 * Should only be invoked when 'verbose' is non-zero.
6134 */
6135 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006136last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006137{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006138 char_u *p;
6139
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006140 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006141 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006142 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006143 if (p != NULL)
6144 {
6145 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006146 msg_puts(_("\n\tLast set from "));
6147 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006148 if (script_ctx.sc_lnum > 0)
6149 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006150 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006151 msg_outnum((long)script_ctx.sc_lnum);
6152 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006153 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006154 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006155 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006156 }
6157}
6158
Bram Moolenaar61be3762019-03-19 23:04:17 +01006159/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006160 * Compare "typ1" and "typ2". Put the result in "typ1".
6161 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006162 int
6163typval_compare(
6164 typval_T *typ1, /* first operand */
6165 typval_T *typ2, /* second operand */
6166 exptype_T type, /* operator */
6167 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01006168 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006169{
6170 int i;
6171 varnumber_T n1, n2;
6172 char_u *s1, *s2;
6173 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
6174
Bram Moolenaar31988702018-02-13 12:57:42 +01006175 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006176 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006177 /* For "is" a different type always means FALSE, for "notis"
6178 * it means TRUE. */
6179 n1 = (type == TYPE_NEQUAL);
6180 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006181 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6182 {
6183 if (type_is)
6184 {
6185 n1 = (typ1->v_type == typ2->v_type
6186 && typ1->vval.v_blob == typ2->vval.v_blob);
6187 if (type == TYPE_NEQUAL)
6188 n1 = !n1;
6189 }
6190 else if (typ1->v_type != typ2->v_type
6191 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
6192 {
6193 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006194 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006195 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006196 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006197 clear_tv(typ1);
6198 return FAIL;
6199 }
6200 else
6201 {
6202 // Compare two Blobs for being equal or unequal.
6203 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
6204 if (type == TYPE_NEQUAL)
6205 n1 = !n1;
6206 }
6207 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006208 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6209 {
6210 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006211 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006212 n1 = (typ1->v_type == typ2->v_type
6213 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006214 if (type == TYPE_NEQUAL)
6215 n1 = !n1;
6216 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006217 else if (typ1->v_type != typ2->v_type
6218 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006219 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006220 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006221 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006222 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006223 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006224 clear_tv(typ1);
6225 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006226 }
6227 else
6228 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006229 /* Compare two Lists for being equal or unequal. */
6230 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6231 ic, FALSE);
6232 if (type == TYPE_NEQUAL)
6233 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006234 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006235 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006236
6237 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6238 {
6239 if (type_is)
6240 {
6241 n1 = (typ1->v_type == typ2->v_type
6242 && typ1->vval.v_dict == typ2->vval.v_dict);
6243 if (type == TYPE_NEQUAL)
6244 n1 = !n1;
6245 }
6246 else if (typ1->v_type != typ2->v_type
6247 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
6248 {
6249 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006250 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006251 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006252 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006253 clear_tv(typ1);
6254 return FAIL;
6255 }
6256 else
6257 {
6258 /* Compare two Dictionaries for being equal or unequal. */
6259 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6260 ic, FALSE);
6261 if (type == TYPE_NEQUAL)
6262 n1 = !n1;
6263 }
6264 }
6265
6266 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6267 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6268 {
6269 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
6270 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006271 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006272 clear_tv(typ1);
6273 return FAIL;
6274 }
6275 if ((typ1->v_type == VAR_PARTIAL
6276 && typ1->vval.v_partial == NULL)
6277 || (typ2->v_type == VAR_PARTIAL
6278 && typ2->vval.v_partial == NULL))
6279 /* when a partial is NULL assume not equal */
6280 n1 = FALSE;
6281 else if (type_is)
6282 {
6283 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
6284 /* strings are considered the same if their value is
6285 * the same */
6286 n1 = tv_equal(typ1, typ2, ic, FALSE);
6287 else if (typ1->v_type == VAR_PARTIAL
6288 && typ2->v_type == VAR_PARTIAL)
6289 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6290 else
6291 n1 = FALSE;
6292 }
6293 else
6294 n1 = tv_equal(typ1, typ2, ic, FALSE);
6295 if (type == TYPE_NEQUAL)
6296 n1 = !n1;
6297 }
6298
6299#ifdef FEAT_FLOAT
6300 /*
6301 * If one of the two variables is a float, compare as a float.
6302 * When using "=~" or "!~", always compare as string.
6303 */
6304 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
6305 && type != TYPE_MATCH && type != TYPE_NOMATCH)
6306 {
6307 float_T f1, f2;
6308
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006309 f1 = tv_get_float(typ1);
6310 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006311 n1 = FALSE;
6312 switch (type)
6313 {
6314 case TYPE_EQUAL: n1 = (f1 == f2); break;
6315 case TYPE_NEQUAL: n1 = (f1 != f2); break;
6316 case TYPE_GREATER: n1 = (f1 > f2); break;
6317 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
6318 case TYPE_SMALLER: n1 = (f1 < f2); break;
6319 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
6320 case TYPE_UNKNOWN:
6321 case TYPE_MATCH:
6322 case TYPE_NOMATCH: break; /* avoid gcc warning */
6323 }
6324 }
6325#endif
6326
6327 /*
6328 * If one of the two variables is a number, compare as a number.
6329 * When using "=~" or "!~", always compare as string.
6330 */
6331 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
6332 && type != TYPE_MATCH && type != TYPE_NOMATCH)
6333 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006334 n1 = tv_get_number(typ1);
6335 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006336 switch (type)
6337 {
6338 case TYPE_EQUAL: n1 = (n1 == n2); break;
6339 case TYPE_NEQUAL: n1 = (n1 != n2); break;
6340 case TYPE_GREATER: n1 = (n1 > n2); break;
6341 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
6342 case TYPE_SMALLER: n1 = (n1 < n2); break;
6343 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
6344 case TYPE_UNKNOWN:
6345 case TYPE_MATCH:
6346 case TYPE_NOMATCH: break; /* avoid gcc warning */
6347 }
6348 }
6349 else
6350 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006351 s1 = tv_get_string_buf(typ1, buf1);
6352 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006353 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
6354 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6355 else
6356 i = 0;
6357 n1 = FALSE;
6358 switch (type)
6359 {
6360 case TYPE_EQUAL: n1 = (i == 0); break;
6361 case TYPE_NEQUAL: n1 = (i != 0); break;
6362 case TYPE_GREATER: n1 = (i > 0); break;
6363 case TYPE_GEQUAL: n1 = (i >= 0); break;
6364 case TYPE_SMALLER: n1 = (i < 0); break;
6365 case TYPE_SEQUAL: n1 = (i <= 0); break;
6366
6367 case TYPE_MATCH:
6368 case TYPE_NOMATCH:
6369 n1 = pattern_match(s2, s1, ic);
6370 if (type == TYPE_NOMATCH)
6371 n1 = !n1;
6372 break;
6373
6374 case TYPE_UNKNOWN: break; /* avoid gcc warning */
6375 }
6376 }
6377 clear_tv(typ1);
6378 typ1->v_type = VAR_NUMBER;
6379 typ1->vval.v_number = n1;
6380
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006381 return OK;
6382}
6383
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006384 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006385typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006386{
6387 char_u *tofree;
6388 char_u numbuf[NUMBUFLEN];
6389 char_u *ret = NULL;
6390
6391 if (arg == NULL)
6392 return vim_strsave((char_u *)"(does not exist)");
6393 ret = tv2string(arg, &tofree, numbuf, 0);
6394 /* Make a copy if we have a value but it's not in allocated memory. */
6395 if (ret != NULL && tofree == NULL)
6396 ret = vim_strsave(ret);
6397 return ret;
6398}
6399
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006400#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402/*
6403 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006404 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 * "flags" can be "g" to do a global substitute.
6406 * Returns an allocated string, NULL for error.
6407 */
6408 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006409do_string_sub(
6410 char_u *str,
6411 char_u *pat,
6412 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006413 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006414 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
6416 int sublen;
6417 regmatch_T regmatch;
6418 int i;
6419 int do_all;
6420 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006421 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 garray_T ga;
6423 char_u *ret;
6424 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006425 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426
6427 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
6428 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006429 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430
6431 ga_init2(&ga, 1, 200);
6432
6433 do_all = (flags[0] == 'g');
6434
6435 regmatch.rm_ic = p_ic;
6436 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6437 if (regmatch.regprog != NULL)
6438 {
6439 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006440 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6442 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01006443 /* Skip empty match except for first match. */
6444 if (regmatch.startp[0] == regmatch.endp[0])
6445 {
6446 if (zero_width == regmatch.startp[0])
6447 {
6448 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar1614a142019-10-06 22:00:13 +02006449 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006450 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6451 (size_t)i);
6452 ga.ga_len += i;
6453 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006454 continue;
6455 }
6456 zero_width = regmatch.startp[0];
6457 }
6458
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 /*
6460 * Get some space for a temporary buffer to do the substitution
6461 * into. It will contain:
6462 * - The text up to where the match is.
6463 * - The substituted text.
6464 * - The text after the match.
6465 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006466 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006467 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6469 {
6470 ga_clear(&ga);
6471 break;
6472 }
6473
6474 /* copy the text up to where the match is */
6475 i = (int)(regmatch.startp[0] - tail);
6476 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
6477 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006478 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 + ga.ga_len + i, TRUE, TRUE, FALSE);
6480 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006481 tail = regmatch.endp[0];
6482 if (*tail == NUL)
6483 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (!do_all)
6485 break;
6486 }
6487
6488 if (ga.ga_data != NULL)
6489 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6490
Bram Moolenaar473de612013-06-08 18:19:48 +02006491 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 }
6493
6494 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6495 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006496 if (p_cpo == empty_option)
6497 p_cpo = save_cpo;
6498 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006499 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006500 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501
6502 return ret;
6503}